#Step 1: Find path of data file and copy path (if using windows)
#Step 2: In the console below, type readClipboard()
#Step 3: Copy and paste R's path to the line below in quotes

#CHANGE BETWEEN QUOTES IN THIS LINE TO REFLECT DIRECTORY OF DATA:
myDataLocation = "C:\\Dropbox\\!PRE 906\\Lectures\\07 Scale Building with CFA\\R"

#SET WORKING DIRECTORY TO LOCATION OF DATA FILE
setwd(myDataLocation)

#AUTOMATING PACKAGES NEEDED FOR ANALYSES--------------------------------------------------------------------
needed_packages = c("lavaan","semPlot")

for (i in 1:length(needed_packages)){
  haspackage = require(needed_packages[i], character.only = TRUE)
  if (haspackage==FALSE){
    install.packages(needed_packages[i])
    library(needed_packages[i], character.only = TRUE)
  }
}
## Loading required package: lavaan
## This is lavaan 0.5-17
## lavaan is BETA software! Please report any bugs.
## Loading required package: semPlot
#Model Examples----------------------------------------------------------------------------------------------

#READ IN JOB PERFORMANCE DATA SET:
gri_data = read.csv(file = "alldata_gri.csv", na.strings="99") #note: data files with missing noted by 99 needs option na.strings

#Model 01 ===================================================================================================

model01.syntax = "

#factor specification statement (only statement needed)
  gambling =~ GRI1 + GRI2 + GRI3 + GRI4 + GRI5 + GRI6 + GRI7 + GRI8 + GRI9 + GRI10 + GRI11 + GRI12 +
              GRI13 + GRI14 + GRI15 + GRI16 + GRI17 + GRI18 + GRI19 + GRI20 + GRI21 + GRI22 + GRI23 + GRI24
"

#model estimation
model01.fit = sem(model01.syntax, data=gri_data, mimic="Mplus", estimator = "MLR")

#display model output
summary(model01.fit, fit.measures = TRUE, standardized = TRUE)
## lavaan (0.5-17) converged normally after  51 iterations
##
##   Number of observations                          1304
##
##   Number of missing patterns                         5
##
##   Estimator                                         ML      Robust
##   Minimum Function Test Statistic             3729.276    2697.490
##   Degrees of freedom                               252         252
##   P-value (Chi-square)                           0.000       0.000
##   Scaling correction factor                                  1.382
##     for the Yuan-Bentler correction (Mplus variant)
##
## Model test baseline model:
##
##   Minimum Function Test Statistic            11790.193    8007.561
##   Degrees of freedom                               276         276
##   P-value                                        0.000       0.000
##
## User model versus baseline model:
##
##   Comparative Fit Index (CFI)                    0.698       0.684
##   Tucker-Lewis Index (TLI)                       0.669       0.654
##
## Loglikelihood and Information Criteria:
##
##   Loglikelihood user model (H0)             -43696.259  -43696.259
##   Scaling correction factor                                  2.023
##     for the MLR correction
##   Loglikelihood unrestricted model (H1)     -41831.621  -41831.621
##   Scaling correction factor                                  1.525
##     for the MLR correction
##
##   Number of free parameters                         72          72
##   Akaike (AIC)                               87536.519   87536.519
##   Bayesian (BIC)                             87908.988   87908.988
##   Sample-size adjusted Bayesian (BIC)        87680.279   87680.279
##
## Root Mean Square Error of Approximation:
##
##   RMSEA                                          0.103       0.086
##   90 Percent Confidence Interval          0.100  0.106       0.084  0.089
##   P-value RMSEA <= 0.05                          0.000       0.000
##
## Standardized Root Mean Square Residual:
##
##   SRMR                                           0.087       0.087
##
## Parameter estimates:
##
##   Information                                 Observed
##   Standard Errors                   Robust.huber.white
##
##                    Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
## Latent variables:
##   gambling =~
##     GRI1              1.000                               0.602    0.593
##     GRI2              0.880    0.078   11.305    0.000    0.530    0.328
##     GRI3              0.751    0.065   11.568    0.000    0.452    0.522
##     GRI4              0.956    0.090   10.627    0.000    0.575    0.422
##     GRI5              1.060    0.083   12.723    0.000    0.638    0.667
##     GRI6              0.761    0.063   12.124    0.000    0.458    0.630
##     GRI7              1.045    0.079   13.263    0.000    0.629    0.578
##     GRI8              1.119    0.055   20.363    0.000    0.674    0.573
##     GRI9              0.877    0.053   16.690    0.000    0.528    0.732
##     GRI10             0.996    0.071   14.039    0.000    0.600    0.695
##     GRI11             0.965    0.068   14.141    0.000    0.580    0.531
##     GRI12             0.427    0.086    4.968    0.000    0.257    0.133
##     GRI13             1.107    0.065   17.114    0.000    0.666    0.696
##     GRI14             1.001    0.087   11.519    0.000    0.602    0.407
##     GRI15             0.900    0.070   12.933    0.000    0.541    0.448
##     GRI16             0.673    0.065   10.372    0.000    0.405    0.585
##     GRI17             0.941    0.082   11.469    0.000    0.566    0.542
##     GRI18             0.972    0.075   12.901    0.000    0.585    0.456
##     GRI19             0.857    0.056   15.373    0.000    0.515    0.468
##     GRI20             0.852    0.109    7.841    0.000    0.513    0.264
##     GRI21             0.944    0.068   13.787    0.000    0.568    0.685
##     GRI22             0.657    0.082    7.979    0.000    0.395    0.221
##     GRI23             1.081    0.073   14.746    0.000    0.651    0.684
##     GRI24             0.942    0.069   13.734    0.000    0.567    0.473
##
## Intercepts:
##     GRI1              1.804    0.028   64.252    0.000    1.804    1.779
##     GRI2              1.932    0.045   43.195    0.000    1.932    1.196
##     GRI3              1.551    0.024   64.701    0.000    1.551    1.792
##     GRI4              1.926    0.038   51.046    0.000    1.926    1.414
##     GRI5              1.577    0.026   59.592    0.000    1.577    1.650
##     GRI6              1.444    0.020   71.689    0.000    1.444    1.985
##     GRI7              1.916    0.030   63.640    0.000    1.916    1.762
##     GRI8              1.663    0.033   51.135    0.000    1.663    1.416
##     GRI9              1.423    0.020   71.223    0.000    1.423    1.973
##     GRI10             1.558    0.024   65.094    0.000    1.558    1.806
##     GRI11             1.696    0.030   56.081    0.000    1.696    1.553
##     GRI12             2.893    0.054   54.035    0.000    2.893    1.496
##     GRI13             1.547    0.027   58.344    0.000    1.547    1.616
##     GRI14             2.319    0.041   56.656    0.000    2.319    1.569
##     GRI15             1.902    0.034   56.782    0.000    1.902    1.573
##     GRI16             1.334    0.019   69.622    0.000    1.334    1.928
##     GRI17             1.677    0.029   58.015    0.000    1.677    1.607
##     GRI18             1.801    0.036   50.660    0.000    1.801    1.403
##     GRI19             1.697    0.030   55.650    0.000    1.697    1.541
##     GRI20             2.875    0.054   53.494    0.000    2.875    1.482
##     GRI21             1.432    0.023   62.375    0.000    1.432    1.727
##     GRI22             2.510    0.049   50.710    0.000    2.510    1.404
##     GRI23             1.547    0.026   58.738    0.000    1.547    1.627
##     GRI24             1.808    0.033   54.408    0.000    1.808    1.507
##     gambling          0.000                               0.000    0.000
##
## Variances:
##     GRI1              0.666    0.061   10.923    0.000    0.666    0.648
##     GRI2              2.328    0.139   16.761    0.000    2.328    0.892
##     GRI3              0.545    0.043   12.764    0.000    0.545    0.727
##     GRI4              1.525    0.085   17.870    0.000    1.525    0.822
##     GRI5              0.506    0.044   11.507    0.000    0.506    0.555
##     GRI6              0.319    0.024   13.360    0.000    0.319    0.604
##     GRI7              0.786    0.054   14.670    0.000    0.786    0.665
##     GRI8              0.926    0.087   10.586    0.000    0.926    0.671
##     GRI9              0.241    0.017   14.492    0.000    0.241    0.464
##     GRI10             0.385    0.037   10.311    0.000    0.385    0.517
##     GRI11             0.856    0.078   10.948    0.000    0.856    0.718
##     GRI12             3.671    0.087   42.000    0.000    3.671    0.982
##     GRI13             0.472    0.045   10.470    0.000    0.472    0.515
##     GRI14             1.821    0.078   23.458    0.000    1.821    0.834
##     GRI15             1.169    0.077   15.217    0.000    1.169    0.799
##     GRI16             0.315    0.033    9.645    0.000    0.315    0.658
##     GRI17             0.769    0.054   14.261    0.000    0.769    0.706
##     GRI18             1.306    0.095   13.773    0.000    1.306    0.792
##     GRI19             0.947    0.082   11.575    0.000    0.947    0.781
##     GRI20             3.502    0.096   36.457    0.000    3.502    0.930
##     GRI21             0.364    0.034   10.664    0.000    0.364    0.530
##     GRI22             3.039    0.104   29.241    0.000    3.039    0.951
##     GRI23             0.481    0.040   12.016    0.000    0.481    0.532
##     GRI24             1.118    0.074   15.177    0.000    1.118    0.777
##     gambling          0.362    0.047    7.625    0.000    1.000    1.000
#display normalized residual covariances
residuals(model01.fit, type="normalized")
## $cov
##       GRI1    GRI2    GRI3    GRI4    GRI5    GRI6    GRI7    GRI8
## GRI1    0.000
## GRI2    2.718   0.000
## GRI3    0.661   1.690   0.000
## GRI4    1.165   6.206   1.943   0.000
## GRI5    0.133   1.457   0.192   3.027   0.000
## GRI6   -0.556  -1.995   0.683  -2.536   0.302   0.000
## GRI7   -0.597  -0.525   0.430   4.256   2.408   0.843   0.000
## GRI8    2.310   3.673   0.500   4.075   1.042  -0.183   0.867   0.000
## GRI9    0.134   0.256   0.098  -0.187   1.379   0.836   0.292   0.575
## GRI10  -0.351  -1.106   0.486   0.359   0.399   1.346   1.021   0.745
## GRI11  -1.792  -4.137  -0.740  -3.404  -0.947   0.718  -0.446  -2.131
## GRI12   0.100   3.384  -3.627   7.637  -0.541  -4.348   0.139   4.783
## GRI13  -0.214  -1.429  -0.030  -1.626  -0.493   0.109  -0.143  -0.816
## GRI14   0.120  -2.912   0.117   1.132   0.311  -1.933   1.936  -1.987
## GRI15   1.511   2.008  -1.443   0.857  -1.774  -1.619   0.212   1.975
## GRI16  -0.998  -1.722   0.025  -5.197  -2.128   0.943  -3.172  -1.842
## GRI17  -2.464  -3.780  -0.419  -3.498  -1.548  -0.566  -1.355  -4.163
## GRI18   0.157  -1.431  -0.893  -6.003  -0.950  -0.552  -2.854  -0.716
## GRI19   3.729   3.141  -0.736  -0.406  -2.957  -2.075  -4.808   2.086
## GRI20   2.601   6.605  -0.967  14.083   1.934  -6.870   1.724   5.783
## GRI21  -0.742  -0.519  -0.406  -2.749  -0.690  -0.461  -1.997  -2.392
## GRI22  -1.251  -9.327  -2.011 -12.660  -1.845   4.389  -1.552  -3.866
## GRI23  -1.489   0.355  -0.831   2.207  -0.301  -1.228   0.947  -1.000
## GRI24  -0.540  -5.130  -1.032  -7.446  -2.023   1.378  -3.646  -2.037
##       GRI9    GRI10   GRI11   GRI12   GRI13   GRI14   GRI15   GRI16
## GRI1
## GRI2
## GRI3
## GRI4
## GRI5
## GRI6
## GRI7
## GRI8
## GRI9    0.000
## GRI10   1.277   0.083
## GRI11  -0.240   0.319   0.000
## GRI12  -2.354  -1.312  -0.921   0.000
## GRI13   0.281  -0.356  -0.054  -1.727   0.000
## GRI14  -0.783  -0.393   3.523   1.760  -0.894  -0.004
## GRI15  -1.736  -1.039  -1.784   6.281   0.098   1.410  -0.002
## GRI16  -1.781  -0.720   1.311  -0.360   0.331  -0.915   1.244   0.000
## GRI17  -1.750  -0.900   2.168  -2.894   0.304   3.558   1.114   3.046
## GRI18  -1.419  -2.417   2.498   1.251  -0.067  -1.329  -0.018   3.348
## GRI19  -1.364  -1.554  -1.761   5.772  -0.772  -3.592   4.617   2.814
## GRI20  -3.529  -1.037  -4.721  14.643  -3.416   0.993   8.642  -2.564
## GRI21   0.058  -0.750   0.144  -3.072   1.299  -0.926  -0.514   1.930
## GRI22  -0.104   0.847   5.822  -5.207   0.492   4.022  -5.065   1.593
## GRI23  -0.802  -0.759   1.793  -0.470   0.524   0.306   0.659   0.968
## GRI24   0.116   0.258   1.114  -2.248   1.461   2.001  -4.120   1.839
##       GRI17   GRI18   GRI19   GRI20   GRI21   GRI22   GRI23   GRI24
## GRI1
## GRI2
## GRI3
## GRI4
## GRI5
## GRI6
## GRI7
## GRI8
## GRI9
## GRI10
## GRI11
## GRI12
## GRI13
## GRI14
## GRI15
## GRI16
## GRI17   0.000
## GRI18   1.244   0.000
## GRI19  -0.919   4.002   0.000
## GRI20  -4.003  -1.539   7.795   0.034
## GRI21   1.395   1.484   1.630  -1.301   0.000
## GRI22   3.524   5.217  -4.726 -15.947   1.969   0.000
## GRI23   2.554   0.122  -0.173   3.193   0.344  -2.496   0.000
## GRI24   4.263   2.592  -1.465  -8.673   3.073  10.542  -1.833   0.000
##
## $mean
##   GRI1   GRI2   GRI3   GRI4   GRI5   GRI6   GRI7   GRI8   GRI9  GRI10
##  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000 -0.001  0.032
##  GRI11  GRI12  GRI13  GRI14  GRI15  GRI16  GRI17  GRI18  GRI19  GRI20
##  0.000  0.000  0.000 -0.003  0.004  0.000  0.000  0.000  0.000  0.024
##  GRI21  GRI22  GRI23  GRI24
##  0.000  0.000  0.000  0.000
#reorder normalized residuals from largest in absolute value to smallest in absolute value
residual_matrix = residuals(model01.fit, type="normalized")$cov
norm_resid = NULL

for (i in 1:dim(residual_matrix)[1]){
  for (j in i:dim(residual_matrix)[2]){
    norm_resid = rbind(norm_resid,cbind(residual_matrix[i,j], abs(residual_matrix[i,j]),colnames(residual_matrix)[i],rownames(residual_matrix)[j]))
  }
}

norm_resid = data.frame(normresid = norm_resid[,1], abs_normresid = norm_resid[,2], var1 = norm_resid[,3], var2 = norm_resid[,4])
norm_resid = norm_resid[order(norm_resid$abs_normresid, decreasing=TRUE),]
norm_resid = norm_resid[c("normresid", "var1", "var2")]
norm_resid[1:10,]
##             normresid  var1  var2
## 45  -9.32683833600825  GRI2 GRI22
## 290 -8.67310050087066 GRI20 GRI24
## 251  8.64240818484154 GRI15 GRI20
## 281  7.79516530810538 GRI19 GRI20
## 78    7.6371195942697  GRI4 GRI12
## 90  -7.44644923002808  GRI4 GRI24
## 125 -6.87026145595052  GRI6 GRI20
## 43   6.60514767353204  GRI2 GRI20
## 213  6.28078559602154 GRI12 GRI15
## 27   6.20571540924132  GRI2  GRI4
#get modification indices
model01.mi = modindices(model01.fit)
model01.mi = model01.mi[order(model01.mi$mi.scaled, decreasing=TRUE),]
model01.mi[1:10,]
##      lhs op   rhs      mi mi.scaled    epc sepc.lv sepc.all sepc.nox
## 1   GRI4 ~~ GRI20 269.642   195.040  1.065   1.065    0.403    0.403
## 2  GRI20 ~~ GRI22 238.767   172.707 -1.404  -1.404   -0.405   -0.405
## 3  GRI12 ~~ GRI20 225.551   163.147  1.498   1.498    0.399    0.399
## 4  GRI22 ~~ GRI24 180.710   130.713  0.697   0.697    0.325    0.325
## 5   GRI4 ~~ GRI22 150.710   109.013 -0.741  -0.741   -0.304   -0.304
## 6   GRI1 ~~ GRI19 119.367    86.342  0.250   0.250    0.224    0.224
## 7  GRI15 ~~ GRI20 104.120    75.313  0.581   0.581    0.248    0.248
## 8  GRI19 ~~ GRI20  99.265    71.801  0.511   0.511    0.239    0.239
## 9  GRI20 ~~ GRI24  92.818    67.138 -0.537  -0.537   -0.231   -0.231
## 10  GRI2 ~~ GRI22  80.579    58.285 -0.666  -0.666   -0.231   -0.231
#Model 02 =========================================================================================================

model02.syntax = "

#factor specification statement (only statement needed)
  gambling =~ GRI1 + GRI3  + GRI5 + GRI6 + GRI7 + GRI9 + GRI10 + GRI11 +
              GRI13 + GRI14 + GRI15 + GRI16 + GRI18 + GRI21 + GRI23
"

#model estimation
model02.fit = sem(model02.syntax, data=gri_data, mimic="Mplus", estimator = "MLR")

#display model output
summary(model02.fit, fit.measures = TRUE, standardized = TRUE)
## lavaan (0.5-17) converged normally after  48 iterations
##
##   Number of observations                          1304
##
##   Number of missing patterns                         5
##
##   Estimator                                         ML      Robust
##   Minimum Function Test Statistic              691.824     420.480
##   Degrees of freedom                                90          90
##   P-value (Chi-square)                           0.000       0.000
##   Scaling correction factor                                  1.645
##     for the Yuan-Bentler correction (Mplus variant)
##
## Model test baseline model:
##
##   Minimum Function Test Statistic             6791.150    3726.133
##   Degrees of freedom                               105         105
##   P-value                                        0.000       0.000
##
## User model versus baseline model:
##
##   Comparative Fit Index (CFI)                    0.910       0.909
##   Tucker-Lewis Index (TLI)                       0.895       0.894
##
## Loglikelihood and Information Criteria:
##
##   Loglikelihood user model (H0)             -23888.184  -23888.184
##   Scaling correction factor                                  2.404
##     for the MLR correction
##   Loglikelihood unrestricted model (H1)     -23542.272  -23542.272
##   Scaling correction factor                                  1.898
##     for the MLR correction
##
##   Number of free parameters                         45          45
##   Akaike (AIC)                               47866.367   47866.367
##   Bayesian (BIC)                             48099.161   48099.161
##   Sample-size adjusted Bayesian (BIC)        47956.218   47956.218
##
## Root Mean Square Error of Approximation:
##
##   RMSEA                                          0.072       0.053
##   90 Percent Confidence Interval          0.067  0.077       0.049  0.057
##   P-value RMSEA <= 0.05                          0.000       0.100
##
## Standardized Root Mean Square Residual:
##
##   SRMR                                           0.042       0.042
##
## Parameter estimates:
##
##   Information                                 Observed
##   Standard Errors                   Robust.huber.white
##
##                    Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
## Latent variables:
##   gambling =~
##     GRI1              1.000                               0.572    0.564
##     GRI3              0.790    0.071   11.111    0.000    0.452    0.522
##     GRI5              1.122    0.093   12.124    0.000    0.642    0.672
##     GRI6              0.826    0.069   12.047    0.000    0.473    0.650
##     GRI7              1.118    0.086   12.930    0.000    0.640    0.588
##     GRI9              0.941    0.059   16.009    0.000    0.538    0.746
##     GRI10             1.060    0.079   13.438    0.000    0.606    0.703
##     GRI11             1.039    0.075   13.887    0.000    0.594    0.544
##     GRI13             1.180    0.072   16.320    0.000    0.675    0.705
##     GRI14             1.040    0.094   11.061    0.000    0.595    0.403
##     GRI15             0.887    0.073   12.134    0.000    0.508    0.420
##     GRI16             0.698    0.070   10.009    0.000    0.399    0.577
##     GRI18             0.990    0.081   12.267    0.000    0.566    0.441
##     GRI21             0.986    0.075   13.167    0.000    0.564    0.681
##     GRI23             1.120    0.080   13.942    0.000    0.641    0.674
##
## Intercepts:
##     GRI1              1.804    0.028   64.252    0.000    1.804    1.779
##     GRI3              1.551    0.024   64.701    0.000    1.551    1.792
##     GRI5              1.577    0.026   59.592    0.000    1.577    1.650
##     GRI6              1.444    0.020   71.689    0.000    1.444    1.985
##     GRI7              1.916    0.030   63.640    0.000    1.916    1.762
##     GRI9              1.423    0.020   71.230    0.000    1.423    1.973
##     GRI10             1.558    0.024   65.101    0.000    1.558    1.806
##     GRI11             1.696    0.030   56.081    0.000    1.696    1.553
##     GRI13             1.547    0.027   58.344    0.000    1.547    1.616
##     GRI14             2.319    0.041   56.656    0.000    2.319    1.569
##     GRI15             1.902    0.034   56.778    0.000    1.902    1.573
##     GRI16             1.334    0.019   69.622    0.000    1.334    1.928
##     GRI18             1.801    0.036   50.660    0.000    1.801    1.403
##     GRI21             1.432    0.023   62.375    0.000    1.432    1.727
##     GRI23             1.547    0.026   58.738    0.000    1.547    1.627
##     gambling          0.000                               0.000    0.000
##
## Variances:
##     GRI1              0.701    0.065   10.800    0.000    0.701    0.682
##     GRI3              0.545    0.044   12.480    0.000    0.545    0.728
##     GRI5              0.500    0.044   11.262    0.000    0.500    0.548
##     GRI6              0.306    0.023   13.094    0.000    0.306    0.578
##     GRI7              0.772    0.055   14.094    0.000    0.772    0.654
##     GRI9              0.231    0.017   13.189    0.000    0.231    0.443
##     GRI10             0.377    0.038    9.803    0.000    0.377    0.506
##     GRI11             0.840    0.077   10.892    0.000    0.840    0.704
##     GRI13             0.461    0.046   10.078    0.000    0.461    0.503
##     GRI14             1.830    0.078   23.487    0.000    1.830    0.838
##     GRI15             1.204    0.078   15.537    0.000    1.204    0.824
##     GRI16             0.319    0.034    9.473    0.000    0.319    0.667
##     GRI18             1.327    0.097   13.680    0.000    1.327    0.805
##     GRI21             0.369    0.035   10.515    0.000    0.369    0.537
##     GRI23             0.494    0.042   11.651    0.000    0.494    0.546
##     gambling          0.327    0.046    7.113    0.000    1.000    1.000
#reorder normalized residuals from largest in absolute value to smallest in absolute value
residual_matrix = residuals(model02.fit, type="normalized")$cov
norm_resid = NULL

for (i in 1:dim(residual_matrix)[1]){
  for (j in i:dim(residual_matrix)[2]){
    norm_resid = rbind(norm_resid,cbind(residual_matrix[i,j], abs(residual_matrix[i,j]),colnames(residual_matrix)[i],rownames(residual_matrix)[j]))
  }
}

norm_resid02 = data.frame(normresid = norm_resid[,1], abs_normresid = norm_resid[,2], var1 = norm_resid[,3], var2 = norm_resid[,4])
norm_resid02 = norm_resid02[order(norm_resid02$abs_normresid, decreasing=TRUE),]
norm_resid02 = norm_resid02[c("normresid", "var1", "var2")]
norm_resid02[1:10,]
##             normresid  var1  var2
## 112  3.63280079733077 GRI16 GRI18
## 87   3.45162018057343 GRI11 GRI14
## 62  -3.19966129603671  GRI7 GRI16
## 63  -2.72751220000772  GRI7 GRI18
## 90   2.55636245374021 GRI11 GRI18
## 11   2.28942168896894  GRI1 GRI15
## 82  -2.22785586027936 GRI10 GRI18
## 32   2.22666179805302  GRI5  GRI7
## 64  -2.10020478575298  GRI7 GRI21
## 49  -2.09940063863164  GRI6 GRI14
#get modification indices
model02.mi = modindices(model02.fit)
model02.mi = model02.mi[order(model02.mi$mi.scaled, decreasing=TRUE),]
model02.mi[1:10,]
##      lhs op   rhs     mi mi.scaled    epc sepc.lv sepc.all sepc.nox
## 1  GRI16 ~~ GRI18 61.069    37.117  0.147   0.147    0.166    0.166
## 2   GRI5 ~~  GRI7 57.732    35.089  0.142   0.142    0.137    0.137
## 3   GRI7 ~~ GRI16 53.505    32.519 -0.107  -0.107   -0.142   -0.142
## 4  GRI16 ~~ GRI21 48.782    29.649  0.072   0.072    0.126    0.126
## 5   GRI9 ~~ GRI16 39.502    24.008 -0.053  -0.053   -0.106   -0.106
## 6   GRI5 ~~ GRI16 37.254    22.642 -0.073  -0.073   -0.111   -0.111
## 7  GRI11 ~~ GRI14 31.499    19.145  0.200   0.200    0.124    0.124
## 8   GRI7 ~~ GRI21 30.148    18.324 -0.089  -0.089   -0.098   -0.098
## 9   GRI5 ~~  GRI9 29.234    17.768  0.059   0.059    0.085    0.085
## 10  GRI9 ~~ GRI10 27.565    16.754  0.050   0.050    0.080    0.080
#Model 03 =========================================================================================================

model03.syntax = "

#factor specification statement (only statement needed)
  gambling =~ GRI1 + GRI3  + GRI5 + GRI6 + GRI9 + GRI10 + GRI11 +
              GRI13 + GRI14 + GRI15 + GRI21 + GRI23
"

#model estimation
model03.fit = sem(model03.syntax, data=gri_data, mimic="Mplus", estimator = "MLR")

#display model output
summary(model03.fit, fit.measures = TRUE, standardized = TRUE)
## lavaan (0.5-17) converged normally after  42 iterations
##
##   Number of observations                          1304
##
##   Number of missing patterns                         5
##
##   Estimator                                         ML      Robust
##   Minimum Function Test Statistic              296.920     185.178
##   Degrees of freedom                                54          54
##   P-value (Chi-square)                           0.000       0.000
##   Scaling correction factor                                  1.603
##     for the Yuan-Bentler correction (Mplus variant)
##
## Model test baseline model:
##
##   Minimum Function Test Statistic             5186.873    2802.860
##   Degrees of freedom                                66          66
##   P-value                                        0.000       0.000
##
## User model versus baseline model:
##
##   Comparative Fit Index (CFI)                    0.953       0.952
##   Tucker-Lewis Index (TLI)                       0.942       0.941
##
## Loglikelihood and Information Criteria:
##
##   Loglikelihood user model (H0)             -18988.425  -18988.425
##   Scaling correction factor                                  2.431
##     for the MLR correction
##   Loglikelihood unrestricted model (H1)     -18839.965  -18839.965
##   Scaling correction factor                                  1.934
##     for the MLR correction
##
##   Number of free parameters                         36          36
##   Akaike (AIC)                               38048.850   38048.850
##   Bayesian (BIC)                             38235.085   38235.085
##   Sample-size adjusted Bayesian (BIC)        38120.730   38120.730
##
## Root Mean Square Error of Approximation:
##
##   RMSEA                                          0.059       0.043
##   90 Percent Confidence Interval          0.052  0.065       0.038  0.049
##   P-value RMSEA <= 0.05                          0.013       0.981
##
## Standardized Root Mean Square Residual:
##
##   SRMR                                           0.032       0.032
##
## Parameter estimates:
##
##   Information                                 Observed
##   Standard Errors                   Robust.huber.white
##
##                    Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
## Latent variables:
##   gambling =~
##     GRI1              1.000                               0.575    0.567
##     GRI3              0.785    0.073   10.791    0.000    0.452    0.522
##     GRI5              1.118    0.096   11.661    0.000    0.643    0.673
##     GRI6              0.815    0.070   11.589    0.000    0.469    0.645
##     GRI9              0.960    0.059   16.173    0.000    0.553    0.766
##     GRI10             1.068    0.081   13.181    0.000    0.614    0.711
##     GRI11             1.012    0.075   13.584    0.000    0.583    0.533
##     GRI13             1.172    0.073   16.131    0.000    0.674    0.704
##     GRI14             1.023    0.095   10.790    0.000    0.589    0.398
##     GRI15             0.857    0.071   11.999    0.000    0.493    0.408
##     GRI21             0.967    0.071   13.590    0.000    0.557    0.672
##     GRI23             1.086    0.080   13.634    0.000    0.625    0.657
##
## Intercepts:
##     GRI1              1.804    0.028   64.252    0.000    1.804    1.779
##     GRI3              1.551    0.024   64.701    0.000    1.551    1.792
##     GRI5              1.577    0.026   59.592    0.000    1.577    1.650
##     GRI6              1.444    0.020   71.689    0.000    1.444    1.985
##     GRI9              1.423    0.020   71.231    0.000    1.423    1.973
##     GRI10             1.559    0.024   65.057    0.000    1.559    1.805
##     GRI11             1.696    0.030   56.081    0.000    1.696    1.553
##     GRI13             1.547    0.027   58.344    0.000    1.547    1.616
##     GRI14             2.319    0.041   56.656    0.000    2.319    1.569
##     GRI15             1.902    0.034   56.776    0.000    1.902    1.573
##     GRI21             1.432    0.023   62.375    0.000    1.432    1.727
##     GRI23             1.547    0.026   58.738    0.000    1.547    1.627
##     gambling          0.000                               0.000    0.000
##
## Variances:
##     GRI1              0.697    0.066   10.638    0.000    0.697    0.678
##     GRI3              0.545    0.044   12.360    0.000    0.545    0.728
##     GRI5              0.499    0.047   10.559    0.000    0.499    0.547
##     GRI6              0.309    0.024   12.902    0.000    0.309    0.584
##     GRI9              0.215    0.017   12.732    0.000    0.215    0.413
##     GRI10             0.369    0.040    9.315    0.000    0.369    0.494
##     GRI11             0.854    0.078   10.927    0.000    0.854    0.716
##     GRI13             0.462    0.047    9.912    0.000    0.462    0.504
##     GRI14             1.837    0.078   23.406    0.000    1.837    0.841
##     GRI15             1.219    0.078   15.615    0.000    1.219    0.834
##     GRI21             0.377    0.036   10.560    0.000    0.377    0.549
##     GRI23             0.514    0.044   11.618    0.000    0.514    0.568
##     gambling          0.331    0.047    7.110    0.000    1.000    1.000
#reorder normalized residuals from largest in absolute value to smallest in absolute value
residual_matrix = residuals(model03.fit, type="normalized")$cov
norm_resid = NULL

for (i in 1:dim(residual_matrix)[1]){
  for (j in i:dim(residual_matrix)[2]){
    norm_resid = rbind(norm_resid,cbind(residual_matrix[i,j], abs(residual_matrix[i,j]),colnames(residual_matrix)[i],rownames(residual_matrix)[j]))
  }
}

norm_resid03 = data.frame(normresid = norm_resid[,1], abs_normresid = norm_resid[,2], var1 = norm_resid[,3], var2 = norm_resid[,4])
norm_resid03 = norm_resid03[order(norm_resid03$abs_normresid, decreasing=TRUE),]
norm_resid03 = norm_resid03[c("normresid", "var1", "var2")]
norm_resid03[1:10,]
##            normresid  var1  var2
## 60  3.64318632366229 GRI11 GRI14
## 10   2.4339516031951  GRI1 GRI15
## 63  2.09232108679075 GRI11 GRI23
## 70  2.07617371754213 GRI14 GRI15
## 39 -1.94469007924095  GRI6 GRI14
## 75  1.76200459640771 GRI15 GRI23
## 7  -1.46719258001517  GRI1 GRI11
## 67  1.37345776791315 GRI13 GRI21
## 48 -1.31765083432754  GRI9 GRI15
## 61  -1.1225378246283 GRI11 GRI15
#get modification indices
model03.mi = modindices(model03.fit)
model03.mi = model03.mi[order(model03.mi$mi.scaled, decreasing=TRUE),]
model03.mi[1:10,]
##      lhs op   rhs     mi mi.scaled    epc sepc.lv sepc.all sepc.nox
## 1  GRI11 ~~ GRI14 34.671    21.623  0.212   0.212    0.132    0.132
## 2  GRI13 ~~ GRI21 33.510    20.899  0.077   0.077    0.097    0.097
## 3  GRI11 ~~ GRI23 31.042    19.360  0.111   0.111    0.107    0.107
## 4   GRI6 ~~ GRI10 20.740    12.935  0.049   0.049    0.078    0.078
## 5   GRI5 ~~  GRI9 20.677    12.895  0.050   0.050    0.072    0.072
## 6   GRI1 ~~ GRI15 20.459    12.759  0.121   0.121    0.099    0.099
## 7   GRI9 ~~ GRI23 15.808     9.859 -0.044  -0.044   -0.064   -0.064
## 8   GRI9 ~~ GRI10 15.127     9.434  0.037   0.037    0.060    0.060
## 9   GRI6 ~~ GRI23 13.182     8.221 -0.045  -0.045   -0.065   -0.065
## 10 GRI15 ~~ GRI23 11.654     7.268  0.080   0.080    0.069    0.069
#plot path diagram with standardized coefficients
semPaths(model03.fit,intercepts = TRUE, residuals = TRUE, style="mx", layout="tree", rotation=1, optimizeLatRes=TRUE, whatLabels = "std")

#Model 03 w/information =========================================================================================================

model03a.syntax = "

#factor specification statement (only statement needed)
  gambling =~ GRI1 + L3*GRI3 + L5*GRI5 + L6*GRI6 + L9*GRI9 + L10*GRI10 + L11*GRI11 +
              L13*GRI13 + L14*GRI14 + L15*GRI15 + L21*GRI21 + L23*GRI23

#labeling unique variances of indicator variables
  GRI1 ~~ U1*GRI1
  GRI3 ~~ U3*GRI3
  GRI5 ~~ U5*GRI5
  GRI6 ~~ U6*GRI6
  GRI9 ~~ U9*GRI9
  GRI10 ~~ U10*GRI10
  GRI11 ~~ U11*GRI11
  GRI13 ~~ U13*GRI13
  GRI14 ~~ U14*GRI14
  GRI15 ~~ U15*GRI15
  GRI21 ~~ U21*GRI21
  GRI23 ~~ U23*GRI23

#calculating item information with parameter labels:
  info1 := 1 / U1
  info3 := (L3*L3) / U3
  info5 := (L5*L5) / U5
  info6 := (L6*L6) / U6
  info9 := (L9*L9) / U9
  info10 := (L10*L10) / U10
  info11 := (L11*L11) / U11
  info13 := (L13*L13) / U13
  info14 := (L14*L14) / U14
  info15 := (L15*L15) / U15
  info21 := (L21*L21) / U21
  info23 := (L23*L23) / U23
"
#model estimation
model03a.fit = sem(model03a.syntax, data=gri_data, mimic="Mplus", estimator = "MLR")

#display model output
summary(model03a.fit, fit.measures = TRUE, standardized = TRUE)
## lavaan (0.5-17) converged normally after  42 iterations
##
##   Number of observations                          1304
##
##   Number of missing patterns                         5
##
##   Estimator                                         ML      Robust
##   Minimum Function Test Statistic              296.920     185.178
##   Degrees of freedom                                54          54
##   P-value (Chi-square)                           0.000       0.000
##   Scaling correction factor                                  1.603
##     for the Yuan-Bentler correction (Mplus variant)
##
## Model test baseline model:
##
##   Minimum Function Test Statistic             5186.873    2802.860
##   Degrees of freedom                                66          66
##   P-value                                        0.000       0.000
##
## User model versus baseline model:
##
##   Comparative Fit Index (CFI)                    0.953       0.952
##   Tucker-Lewis Index (TLI)                       0.942       0.941
##
## Loglikelihood and Information Criteria:
##
##   Loglikelihood user model (H0)             -18988.425  -18988.425
##   Scaling correction factor                                  2.431
##     for the MLR correction
##   Loglikelihood unrestricted model (H1)     -18839.965  -18839.965
##   Scaling correction factor                                  1.934
##     for the MLR correction
##
##   Number of free parameters                         36          36
##   Akaike (AIC)                               38048.850   38048.850
##   Bayesian (BIC)                             38235.085   38235.085
##   Sample-size adjusted Bayesian (BIC)        38120.730   38120.730
##
## Root Mean Square Error of Approximation:
##
##   RMSEA                                          0.059       0.043
##   90 Percent Confidence Interval          0.052  0.065       0.038  0.049
##   P-value RMSEA <= 0.05                          0.013       0.981
##
## Standardized Root Mean Square Residual:
##
##   SRMR                                           0.032       0.032
##
## Parameter estimates:
##
##   Information                                 Observed
##   Standard Errors                   Robust.huber.white
##
##                    Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
## Latent variables:
##   gambling =~
##     GRI1              1.000                               0.575    0.567
##     GRI3     (L3)     0.785    0.073   10.791    0.000    0.452    0.522
##     GRI5     (L5)     1.118    0.096   11.661    0.000    0.643    0.673
##     GRI6     (L6)     0.815    0.070   11.589    0.000    0.469    0.645
##     GRI9     (L9)     0.960    0.059   16.173    0.000    0.553    0.766
##     GRI10   (L10)     1.068    0.081   13.181    0.000    0.614    0.711
##     GRI11   (L11)     1.012    0.075   13.584    0.000    0.583    0.533
##     GRI13   (L13)     1.172    0.073   16.131    0.000    0.674    0.704
##     GRI14   (L14)     1.023    0.095   10.790    0.000    0.589    0.398
##     GRI15   (L15)     0.857    0.071   11.999    0.000    0.493    0.408
##     GRI21   (L21)     0.967    0.071   13.590    0.000    0.557    0.672
##     GRI23   (L23)     1.086    0.080   13.634    0.000    0.625    0.657
##
## Intercepts:
##     GRI1              1.804    0.028   64.252    0.000    1.804    1.779
##     GRI3              1.551    0.024   64.701    0.000    1.551    1.792
##     GRI5              1.577    0.026   59.592    0.000    1.577    1.650
##     GRI6              1.444    0.020   71.689    0.000    1.444    1.985
##     GRI9              1.423    0.020   71.231    0.000    1.423    1.973
##     GRI10             1.559    0.024   65.057    0.000    1.559    1.805
##     GRI11             1.696    0.030   56.081    0.000    1.696    1.553
##     GRI13             1.547    0.027   58.344    0.000    1.547    1.616
##     GRI14             2.319    0.041   56.656    0.000    2.319    1.569
##     GRI15             1.902    0.034   56.776    0.000    1.902    1.573
##     GRI21             1.432    0.023   62.375    0.000    1.432    1.727
##     GRI23             1.547    0.026   58.738    0.000    1.547    1.627
##     gambling          0.000                               0.000    0.000
##
## Variances:
##     GRI1     (U1)     0.697    0.066   10.638    0.000    0.697    0.678
##     GRI3     (U3)     0.545    0.044   12.360    0.000    0.545    0.728
##     GRI5     (U5)     0.499    0.047   10.559    0.000    0.499    0.547
##     GRI6     (U6)     0.309    0.024   12.902    0.000    0.309    0.584
##     GRI9     (U9)     0.215    0.017   12.732    0.000    0.215    0.413
##     GRI10   (U10)     0.369    0.040    9.315    0.000    0.369    0.494
##     GRI11   (U11)     0.854    0.078   10.927    0.000    0.854    0.716
##     GRI13   (U13)     0.462    0.047    9.912    0.000    0.462    0.504
##     GRI14   (U14)     1.837    0.078   23.406    0.000    1.837    0.841
##     GRI15   (U15)     1.219    0.078   15.615    0.000    1.219    0.834
##     GRI21   (U21)     0.377    0.036   10.560    0.000    0.377    0.549
##     GRI23   (U23)     0.514    0.044   11.618    0.000    0.514    0.568
##     gamblng           0.331    0.047    7.110    0.000    1.000    1.000
##
## Defined parameters:
##     info1             1.434    0.135   10.638    0.000    1.434    1.475
##     info3             1.130    0.241    4.687    0.000    0.374    0.374
##     info5             2.504    0.543    4.613    0.000    0.829    0.829
##     info6             2.148    0.419    5.131    0.000    0.711    0.711
##     info9             4.296    0.635    6.763    0.000    1.423    1.423
##     info10            3.092    0.610    5.070    0.000    1.024    1.024
##     info11            1.201    0.233    5.143    0.000    0.398    0.398
##     info13            2.971    0.507    5.858    0.000    0.984    0.984
##     info14            0.570    0.116    4.909    0.000    0.189    0.189
##     info15            0.602    0.116    5.203    0.000    0.200    0.200
##     info21            2.482    0.435    5.708    0.000    0.822    0.822
##     info23            2.297    0.411    5.582    0.000    0.761    0.761