if (!require(lavaan)) install.packages("lavaan")
## Loading required package: lavaan
## This is lavaan 0.6-19
## lavaan is FREE software! Please report any bugs.
library(lavaan)
#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

#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: semPlot
#FUNCTIONS FOR ANALYSES BELOW -------------------------------------------------------------------------------
data01 = read.csv(file = "gamblingdata.csv", na.strings="99")

#MODEL 01: Gambling GRI Single Factor Model ----------------------------------------------------------------
model01.syntax = "
  GAMBLING =~ GRI1 + GRI3 + GRI5
"

model01.fit = sem(
  model01.syntax, 
  data = data01, 
  estimator = "MLR", 
  mimic="Mplus"
)
summary(model01.fit, fit.measures=TRUE, rsquare=TRUE, standardized=TRUE)
## lavaan 0.6-19 ended normally after 22 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         9
## 
##   Number of observations                          1336
##   Number of missing patterns                         1
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                                 0.000       0.000
##   Degrees of freedom                                 0           0
## 
## Model Test Baseline Model:
## 
##   Test statistic                               480.988     199.641
##   Degrees of freedom                                 3           3
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  2.409
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000       1.000
##   Tucker-Lewis Index (TLI)                       1.000       1.000
##                                                                   
##   Robust Comparative Fit Index (CFI)                         1.000
##   Robust Tucker-Lewis Index (TLI)                            1.000
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -5254.609   -5254.609
##   Loglikelihood unrestricted model (H1)      -5254.609   -5254.609
##                                                                   
##   Akaike (AIC)                               10527.219   10527.219
##   Bayesian (BIC)                             10573.996   10573.996
##   Sample-size adjusted Bayesian (SABIC)      10545.407   10545.407
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000          NA
##   90 Percent confidence interval - lower         0.000          NA
##   90 Percent confidence interval - upper         0.000          NA
##   P-value H_0: RMSEA <= 0.050                       NA          NA
##   P-value H_0: RMSEA >= 0.080                       NA          NA
##                                                                   
##   Robust RMSEA                                               0.000
##   90 Percent confidence interval - lower                     0.000
##   90 Percent confidence interval - upper                     0.000
##   P-value H_0: Robust RMSEA <= 0.050                            NA
##   P-value H_0: Robust RMSEA >= 0.080                            NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.000       0.000
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1              1.000                               0.638    0.621
##     GRI3              0.726    0.093    7.820    0.000    0.463    0.535
##     GRI5              0.995    0.121    8.244    0.000    0.635    0.652
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1              1.823    0.028   64.871    0.000    1.823    1.775
##    .GRI3              1.548    0.024   65.365    0.000    1.548    1.788
##    .GRI5              1.593    0.027   59.749    0.000    1.593    1.635
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1              0.647    0.076    8.481    0.000    0.647    0.614
##    .GRI3              0.535    0.047   11.449    0.000    0.535    0.714
##    .GRI5              0.546    0.061    8.953    0.000    0.546    0.575
##     GAMBLING          0.407    0.066    6.124    0.000    1.000    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.386
##     GRI3              0.286
##     GRI5              0.425
#display normalized residual covariances
residuals(model01.fit, type="normalized")
## $type
## [1] "normalized"
## 
## $cov
##      GRI1 GRI3 GRI5
## GRI1    0          
## GRI3    0    0     
## GRI5    0    0    0
## 
## $mean
## GRI1 GRI3 GRI5 
##    0    0    0
#plot path diagram with standardized coefficients
semPaths(model01.fit,intercepts = FALSE, residuals = TRUE, style="mx", layout="tree", rotation=1, what = "std",
         optimizeLatRes=TRUE, whatLabels = "std", sizeLat = 5, sizeLat2=5, sizeMan=5, sizeMan2=5)

#model fitted values:
fitted.values(model01.fit)
## $cov
##       GRI1  GRI3  GRI5
## GRI1 1.055            
## GRI3 0.296 0.749      
## GRI5 0.405 0.294 0.949
## 
## $mean
##  GRI1  GRI3  GRI5 
## 1.823 1.548 1.593
#MODEL 02: Full Structural Equation Model ------------------------------------------------------------------
model02.syntax = "
  GAMBLING =~ GRI1 + GRI3 + GRI5
  GAMBLING ~ Student
"

model02.fit = sem(
  model02.syntax, 
  data=data01, 
  estimator = "MLR", 
  mimic="Mplus"
)
summary(model02.fit, fit.measures=TRUE, rsquare=TRUE, standardized=TRUE)
## lavaan 0.6-19 ended normally after 28 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        10
## 
##   Number of observations                          1336
##   Number of missing patterns                         1
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                               103.882      79.938
##   Degrees of freedom                                 2           2
##   P-value (Chi-square)                           0.000       0.000
##   Scaling correction factor                                  1.300
##     Yuan-Bentler correction (Mplus variant)                       
## 
## Model Test Baseline Model:
## 
##   Test statistic                               853.812     391.369
##   Degrees of freedom                                 6           6
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  2.182
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.880       0.798
##   Tucker-Lewis Index (TLI)                       0.639       0.393
##                                                                   
##   Robust Comparative Fit Index (CFI)                         0.880
##   Robust Tucker-Lewis Index (TLI)                            0.641
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -5120.139   -5120.139
##   Scaling correction factor                                  2.338
##       for the MLR correction                                      
##   Loglikelihood unrestricted model (H1)      -5068.197   -5068.197
##   Scaling correction factor                                  2.165
##       for the MLR correction                                      
##                                                                   
##   Akaike (AIC)                               10260.277   10260.277
##   Bayesian (BIC)                             10312.251   10312.251
##   Sample-size adjusted Bayesian (SABIC)      10280.486   10280.486
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.195       0.171
##   90 Percent confidence interval - lower         0.164       0.144
##   90 Percent confidence interval - upper         0.228       0.200
##   P-value H_0: RMSEA <= 0.050                    0.000       0.000
##   P-value H_0: RMSEA >= 0.080                    1.000       1.000
##                                                                   
##   Robust RMSEA                                               0.194
##   90 Percent confidence interval - lower                     0.156
##   90 Percent confidence interval - upper                     0.236
##   P-value H_0: Robust RMSEA <= 0.050                         0.000
##   P-value H_0: Robust RMSEA >= 0.080                         1.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.050       0.050
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1              1.000                               0.666    0.648
##     GRI3              0.563    0.076    7.380    0.000    0.375    0.433
##     GRI5              1.014    0.140    7.265    0.000    0.675    0.693
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING ~                                                            
##     Student          -1.162    0.140   -8.327    0.000   -1.745   -0.541
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1              2.859    0.132   21.684    0.000    2.859    2.784
##    .GRI3              2.131    0.078   27.395    0.000    2.131    2.462
##    .GRI5              2.644    0.139   18.972    0.000    2.644    2.714
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1              0.611    0.082    7.503    0.000    0.611    0.580
##    .GRI3              0.609    0.047   13.048    0.000    0.609    0.813
##    .GRI5              0.494    0.063    7.872    0.000    0.494    0.520
##    .GAMBLING          0.313    0.051    6.138    0.000    0.707    0.707
## 
## R-Square:
##                    Estimate
##     GRI1              0.420
##     GRI3              0.187
##     GRI5              0.480
##     GAMBLING          0.293
#display normalized residual covariances
residuals(model02.fit, type="normalized")
## $type
## [1] "normalized"
## 
## $cov
##           GRI1   GRI3   GRI5 Studnt
## GRI1     0.000                     
## GRI3     1.224  0.000              
## GRI5    -1.052  1.172  0.000       
## Student -1.145  4.653 -0.719  0.000
## 
## $mean
##    GRI1    GRI3    GRI5 Student 
##       0       0       0       0
#display modification indices
modindices(model02.fit)
##     lhs op  rhs     mi    epc sepc.lv sepc.all sepc.nox
## 15 GRI1 ~~ GRI3 17.178  0.104   0.104    0.170    0.170
## 16 GRI1 ~~ GRI5 93.140 -0.477  -0.477   -0.868   -0.868
## 17 GRI3 ~~ GRI5 21.072  0.115   0.115    0.209    0.209
#MODEL Configural: Setting up invariance testing of GRI by student status ==========================

configural.syntax = "
#===================================================================================================
#Factor loadings all freely estimated in both groups with label for each group
  GAMBLING =~ c(SL1, NSL1)*GRI1 + c(SL3, NSL3)*GRI3 + c(SL5, NSL5)*GRI5
#===================================================================================================
#Item intercepts all freely estimated in both groups with label for each group
  GRI1 ~ c(SI1, NSI1)*1
  GRI3 ~ c(SI3, NSI3)*1
  GRI5 ~ c(SI5, NSI5)*1
#===================================================================================================
#Redidual variances all freely estimated with label for each group
  GRI1 ~~ c(SR1, NSR1)*GRI1
  GRI3 ~~ c(SR3, NSR3)*GRI3
  GRI5 ~~ c(SR5, NSR5)*GRI5
#===================================================================================================
#Factor variances all freely estimated in both groups with label for each group
  GAMBLING ~~ c(1, 1)*GAMBLING
#===================================================================================================
#Factor means all freely estimated in both groups with label for each group
  GAMBLING ~ c(0, 0)*1
#===================================================================================================  
"
modelConfigural.fit = lavaan(
  configural.syntax, 
  data=data01, 
  estimator = "MLR", 
  mimic="Mplus", 
  group = "Student"
)

summary(modelConfigural.fit, fit.measures=TRUE, rsquare=TRUE, standardized=TRUE)
## lavaan 0.6-19 ended normally after 46 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        18
## 
##   Number of observations per group:                   
##     1                                             1192
##     0                                              144
##   Number of missing patterns per group:               
##     1                                                1
##     0                                                1
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                                 0.000       0.000
##   Degrees of freedom                                 0           0
##   Test statistic for each group:
##     1                                            0.000       0.000
##     0                                            0.000       0.000
## 
## Model Test Baseline Model:
## 
##   Test statistic                               401.955     217.534
##   Degrees of freedom                                 6           6
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.848
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000       1.000
##   Tucker-Lewis Index (TLI)                       1.000       1.000
##                                                                   
##   Robust Comparative Fit Index (CFI)                         1.000
##   Robust Tucker-Lewis Index (TLI)                            1.000
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -4919.375   -4919.375
##   Loglikelihood unrestricted model (H1)      -4919.375   -4919.375
##                                                                   
##   Akaike (AIC)                                9874.750    9874.750
##   Bayesian (BIC)                              9968.304    9968.304
##   Sample-size adjusted Bayesian (SABIC)       9911.126    9911.126
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000          NA
##   90 Percent confidence interval - lower         0.000          NA
##   90 Percent confidence interval - upper         0.000          NA
##   P-value H_0: RMSEA <= 0.050                       NA          NA
##   P-value H_0: RMSEA >= 0.080                       NA          NA
##                                                                   
##   Robust RMSEA                                               0.000
##   90 Percent confidence interval - lower                     0.000
##   90 Percent confidence interval - upper                     0.000
##   P-value H_0: Robust RMSEA <= 0.050                            NA
##   P-value H_0: Robust RMSEA >= 0.080                            NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.000       0.000
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## 
## Group 1 [1]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1     (SL1)    0.409    0.043    9.492    0.000    0.409    0.466
##     GRI3     (SL3)    0.573    0.051   11.310    0.000    0.573    0.694
##     GRI5     (SL5)    0.431    0.040   10.694    0.000    0.431    0.560
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1     (SI1)    1.683    0.025   66.131    0.000    1.683    1.915
##    .GRI3     (SI3)    1.526    0.024   63.788    0.000    1.526    1.848
##    .GRI5     (SI5)    1.456    0.022   65.314    0.000    1.456    1.892
##     GAMBLING          0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1     (SR1)    0.604    0.070    8.680    0.000    0.604    0.783
##    .GRI3     (SR3)    0.353    0.055    6.444    0.000    0.353    0.518
##    .GRI5     (SR5)    0.406    0.046    8.841    0.000    0.406    0.687
##     GAMBLING          1.000                               1.000    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.217
##     GRI3              0.482
##     GRI5              0.313
## 
## 
## Group 2 [0]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1    (NSL1)    1.013    0.215    4.710    0.000    1.013    0.736
##     GRI3    (NSL3)    0.556    0.131    4.242    0.000    0.556    0.494
##     GRI5    (NSL5)    0.817    0.203    4.033    0.000    0.817    0.521
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1    (NSI1)    2.979    0.115   25.967    0.000    2.979    2.164
##    .GRI3    (NSI3)    1.729    0.094   18.435    0.000    1.729    1.536
##    .GRI5    (NSI5)    2.729    0.131   20.875    0.000    2.729    1.740
##     GAMBLIN           0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1    (NSR1)    0.868    0.406    2.138    0.033    0.868    0.458
##    .GRI3    (NSR3)    0.957    0.201    4.757    0.000    0.957    0.756
##    .GRI5    (NSR5)    1.794    0.308    5.834    0.000    1.794    0.729
##     GAMBLIN           1.000                               1.000    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.542
##     GRI3              0.244
##     GRI5              0.271
#MODEL Configural: Testing equality of loadings of GRI by student status ===========================

metric.syntax = "
#===================================================================================================
#Factor loadings all freely estimated in both groups with label for each group
  GAMBLING =~ c(L1, L1)*GRI1 + c(L3, L3)*GRI3 + c(L5, L5)*GRI5
#===================================================================================================
#Item intercepts all freely estimated in both groups with label for each group
  GRI1 ~ c(SI1, NSI1)*1
  GRI3 ~ c(SI3, NSI3)*1
  GRI5 ~ c(SI5, NSI5)*1
#===================================================================================================
#Redidual variances all freely estimated with label for each group
  GRI1 ~~ c(SR1, NSR1)*GRI1
  GRI3 ~~ c(SR3, NSR3)*GRI3
  GRI5 ~~ c(SR5, NSR5)*GRI5
#===================================================================================================
#Factor variances all freely estimated in one group but not other for identification
  GAMBLING ~~ c(1, NA)*GAMBLING
#===================================================================================================
#Factor means all freely estimated in both groups with label for each group
  GAMBLING ~ c(0, 0)*1
#===================================================================================================  
"
modelMetric.fit = lavaan(
  metric.syntax, 
  data=data01, 
  estimator = "MLR", 
  mimic="Mplus", 
  group = "Student"
)

summary(modelMetric.fit, fit.measures=TRUE, rsquare=TRUE, standardized=TRUE)
## lavaan 0.6-19 ended normally after 41 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        19
##   Number of equality constraints                     3
## 
##   Number of observations per group:                   
##     1                                             1192
##     0                                              144
##   Number of missing patterns per group:               
##     1                                                1
##     0                                                1
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                                 8.412       5.904
##   Degrees of freedom                                 2           2
##   P-value (Chi-square)                           0.015       0.052
##   Scaling correction factor                                  1.425
##     Yuan-Bentler correction (Mplus variant)                       
##   Test statistic for each group:
##     1                                            0.379       0.379
##     0                                            5.525       5.525
## 
## Model Test Baseline Model:
## 
##   Test statistic                               401.955     217.534
##   Degrees of freedom                                 6           6
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.848
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.984       0.982
##   Tucker-Lewis Index (TLI)                       0.951       0.945
##                                                                   
##   Robust Comparative Fit Index (CFI)                         0.986
##   Robust Tucker-Lewis Index (TLI)                            0.957
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -4923.581   -4923.581
##   Scaling correction factor                                  1.563
##       for the MLR correction                                      
##   Loglikelihood unrestricted model (H1)      -4919.375   -4919.375
##   Scaling correction factor                                  1.808
##       for the MLR correction                                      
##                                                                   
##   Akaike (AIC)                                9879.162    9879.162
##   Bayesian (BIC)                              9962.321    9962.321
##   Sample-size adjusted Bayesian (SABIC)       9911.496    9911.496
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.069       0.054
##   90 Percent confidence interval - lower         0.026       0.013
##   90 Percent confidence interval - upper         0.120       0.098
##   P-value H_0: RMSEA <= 0.050                    0.198       0.366
##   P-value H_0: RMSEA >= 0.080                    0.421       0.188
##                                                                   
##   Robust RMSEA                                               0.065
##   90 Percent confidence interval - lower                     0.000
##   90 Percent confidence interval - upper                     0.128
##   P-value H_0: Robust RMSEA <= 0.050                         0.264
##   P-value H_0: Robust RMSEA >= 0.080                         0.409
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.014       0.014
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## 
## Group 1 [1]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1      (L1)    0.427    0.041   10.492    0.000    0.427    0.484
##     GRI3      (L3)    0.552    0.052   10.613    0.000    0.552    0.671
##     GRI5      (L5)    0.440    0.039   11.332    0.000    0.440    0.571
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1     (SI1)    1.683    0.025   66.131    0.000    1.683    1.908
##    .GRI3     (SI3)    1.526    0.024   63.788    0.000    1.526    1.854
##    .GRI5     (SI5)    1.456    0.022   65.314    0.000    1.456    1.890
##     GAMBLING          0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1     (SR1)    0.596    0.069    8.633    0.000    0.596    0.766
##    .GRI3     (SR3)    0.373    0.054    6.961    0.000    0.373    0.550
##    .GRI5     (SR5)    0.400    0.045    8.810    0.000    0.400    0.674
##     GAMBLING          1.000                               1.000    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.234
##     GRI3              0.450
##     GRI5              0.326
## 
## 
## Group 2 [0]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1      (L1)    0.427    0.041   10.492    0.000    0.642    0.481
##     GRI3      (L3)    0.552    0.052   10.613    0.000    0.831    0.720
##     GRI5      (L5)    0.440    0.039   11.332    0.000    0.662    0.425
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1    (NSI1)    2.979    0.115   25.967    0.000    2.979    2.231
##    .GRI3    (NSI3)    1.729    0.094   18.435    0.000    1.729    1.498
##    .GRI5    (NSI5)    2.729    0.131   20.875    0.000    2.729    1.754
##     GAMBLIN           0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1    (NSR1)    1.371    0.177    7.738    0.000    1.371    0.769
##    .GRI3    (NSR3)    0.642    0.186    3.448    0.001    0.642    0.482
##    .GRI5    (NSR5)    1.983    0.216    9.178    0.000    1.983    0.819
##     GAMBLIN           2.264    0.558    4.057    0.000    1.000    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.231
##     GRI3              0.518
##     GRI5              0.181
# likelihood ratio test: all loadings tested at once:
anova(modelConfigural.fit, modelMetric.fit)
## 
## Scaled Chi-Squared Difference Test (method = "satorra.bentler.2001")
## 
## lavaan->lavTestLRT():  
##    lavaan NOTE: The "Chisq" column contains standard test statistics, not the 
##    robust test that should be reported per model. A robust difference test is 
##    a function of two standard (not robust) statistics.
##                     Df    AIC    BIC  Chisq Chisq diff Df diff Pr(>Chisq)  
## modelConfigural.fit  0 9874.8 9968.3 0.0000                                
## modelMetric.fit      2 9879.2 9962.3 8.4121     5.9037       2    0.05224 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# looks like invariance--move to scalar

#MODEL Scalar: Testing equality of loadings of GRI by student status ===============================

scalar.syntax = "
#===================================================================================================
#Factor loadings all freely estimated in both groups with label for each group
  GAMBLING =~ c(L1, L1)*GRI1 + c(L3, L3)*GRI3 + c(L5, L5)*GRI5
#===================================================================================================
#Item intercepts all freely estimated in both groups with label for each group
  GRI1 ~ c(I1, I1)*1
  GRI3 ~ c(I3, I3)*1
  GRI5 ~ c(I5, I5)*1
#===================================================================================================
#Redidual variances all freely estimated with label for each group
  GRI1 ~~ c(SR1, NSR1)*GRI1
  GRI3 ~~ c(SR3, NSR3)*GRI3
  GRI5 ~~ c(SR5, NSR5)*GRI5
#===================================================================================================
#Factor variances all freely estimated in one group but not other for identification
  GAMBLING ~~ c(1, NA)*GAMBLING
#===================================================================================================
#Factor means all freely estimated in one group but not other for identification
  GAMBLING ~ c(0, NA)*1
#===================================================================================================  
"
modelScalar.fit = lavaan(
  scalar.syntax, 
  data=data01, 
  estimator = "MLR", 
  mimic="Mplus", 
  group = "Student"
)

summary(modelScalar.fit, fit.measures=TRUE, rsquare=TRUE, standardized=TRUE)
## lavaan 0.6-19 ended normally after 52 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        20
##   Number of equality constraints                     6
## 
##   Number of observations per group:                   
##     1                                             1192
##     0                                              144
##   Number of missing patterns per group:               
##     1                                                1
##     0                                                1
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                                95.838      82.821
##   Degrees of freedom                                 4           4
##   P-value (Chi-square)                           0.000       0.000
##   Scaling correction factor                                  1.157
##     Yuan-Bentler correction (Mplus variant)                       
##   Test statistic for each group:
##     1                                           30.327      30.327
##     0                                           52.494      52.494
## 
## Model Test Baseline Model:
## 
##   Test statistic                               401.955     217.534
##   Degrees of freedom                                 6           6
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.848
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.768       0.627
##   Tucker-Lewis Index (TLI)                       0.652       0.441
##                                                                   
##   Robust Comparative Fit Index (CFI)                         0.769
##   Robust Tucker-Lewis Index (TLI)                            0.654
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -4967.294   -4967.294
##   Scaling correction factor                                  1.396
##       for the MLR correction                                      
##   Loglikelihood unrestricted model (H1)      -4919.375   -4919.375
##   Scaling correction factor                                  1.808
##       for the MLR correction                                      
##                                                                   
##   Akaike (AIC)                                9962.588    9962.588
##   Bayesian (BIC)                             10035.352   10035.352
##   Sample-size adjusted Bayesian (SABIC)       9990.881    9990.881
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.185       0.172
##   90 Percent confidence interval - lower         0.154       0.143
##   90 Percent confidence interval - upper         0.218       0.203
##   P-value H_0: RMSEA <= 0.050                    0.000       0.000
##   P-value H_0: RMSEA >= 0.080                    1.000       1.000
##                                                                   
##   Robust RMSEA                                               0.184
##   90 Percent confidence interval - lower                     0.148
##   90 Percent confidence interval - upper                     0.222
##   P-value H_0: Robust RMSEA <= 0.050                         0.000
##   P-value H_0: Robust RMSEA >= 0.080                         1.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.066       0.066
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## 
## Group 1 [1]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1      (L1)    0.488    0.043   11.233    0.000    0.488    0.543
##     GRI3      (L3)    0.394    0.060    6.595    0.000    0.394    0.494
##     GRI5      (L5)    0.496    0.043   11.593    0.000    0.496    0.638
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1      (I1)    1.698    0.027   63.246    0.000    1.698    1.890
##    .GRI3      (I3)    1.500    0.023   66.196    0.000    1.500    1.882
##    .GRI5      (I5)    1.461    0.023   64.354    0.000    1.461    1.878
##     GAMBLING          0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1     (SR1)    0.569    0.076    7.513    0.000    0.569    0.705
##    .GRI3     (SR3)    0.480    0.053    9.150    0.000    0.480    0.756
##    .GRI5     (SR5)    0.359    0.051    7.000    0.000    0.359    0.593
##     GAMBLING          1.000                               1.000    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.295
##     GRI3              0.244
##     GRI5              0.407
## 
## 
## Group 2 [0]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1      (L1)    0.488    0.043   11.233    0.000    0.797    0.580
##     GRI3      (L3)    0.394    0.060    6.595    0.000    0.643    0.487
##     GRI5      (L5)    0.496    0.043   11.593    0.000    0.810    0.517
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1      (I1)    1.698    0.027   63.246    0.000    1.698    1.235
##    .GRI3      (I3)    1.500    0.023   66.196    0.000    1.500    1.137
##    .GRI5      (I5)    1.461    0.023   64.354    0.000    1.461    0.932
##     GAMBLING          2.066    0.316    6.539    0.000    1.266    1.266
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1    (NSR1)    1.254    0.275    4.559    0.000    1.254    0.664
##    .GRI3    (NSR3)    1.329    0.205    6.493    0.000    1.329    0.763
##    .GRI5    (NSR5)    1.800    0.284    6.338    0.000    1.800    0.733
##     GAMBLIN           2.664    0.838    3.178    0.001    1.000    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.336
##     GRI3              0.237
##     GRI5              0.267
# likelihood ratio test: all loadings tested at once:
anova(modelMetric.fit, modelScalar.fit)
## 
## Scaled Chi-Squared Difference Test (method = "satorra.bentler.2001")
## 
## lavaan->lavTestLRT():  
##    lavaan NOTE: The "Chisq" column contains standard test statistics, not the 
##    robust test that should be reported per model. A robust difference test is 
##    a function of two standard (not robust) statistics.
##                 Df    AIC     BIC   Chisq Chisq diff Df diff Pr(>Chisq)    
## modelMetric.fit  2 9879.2  9962.3  8.4121                                  
## modelScalar.fit  4 9962.6 10035.4 95.8381     98.293       2  < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# modification indices inspection:
scalarMI1 = lavTestScore(modelScalar.fit)
## Warning: lavaan->lavTestScore():  
##    se is not `standard'; not implemented yet; falling back to ordinary score 
##    test
#change label values
labelMap = data.frame(
  lhs = modelScalar.fit@ParTable$plabel,
  parameter = modelScalar.fit@ParTable$label
)

scalarMI1$uni = merge(x = scalarMI1$uni, y = labelMap, by = "lhs", all.x = TRUE)

# reorder by decreasing values of X2:
scalarMI1$uni = scalarMI1$uni[order(scalarMI1$uni$X2, decreasing = TRUE),]

#restrict to only means shown
scalarMI1$uni[grep(x = scalarMI1$uni$parameter, pattern = "I"),]
##    lhs op   rhs       X2 df      p.value parameter
## 5 .p5. == .p16. 73.73821  1 0.000000e+00        I3
## 4 .p4. == .p15. 24.03290  1 9.470376e-07        I1
## 6 .p6. == .p17. 10.75211  1 1.041604e-03        I5
# free parameter for I3 first:

scalar.syntax1 = "
#===================================================================================================
#Factor loadings all freely estimated in both groups with label for each group
  GAMBLING =~ c(L1, L1)*GRI1 + c(L3, L3)*GRI3 + c(L5, L5)*GRI5
#===================================================================================================
#Item intercepts all freely estimated in both groups with label for each group
  GRI1 ~ c(I1, I1)*1
  GRI3 ~ c(SI3, NSI3)*1
  GRI5 ~ c(I5, I5)*1
#===================================================================================================
#Redidual variances all freely estimated with label for each group
  GRI1 ~~ c(SR1, NSR1)*GRI1
  GRI3 ~~ c(SR3, NSR3)*GRI3
  GRI5 ~~ c(SR5, NSR5)*GRI5
#===================================================================================================
#Factor variances all freely estimated in one group but not other for identification
  GAMBLING ~~ c(1, NA)*GAMBLING
#===================================================================================================
#Factor means all freely estimated in one group but not other for identification
  GAMBLING ~ c(0, NA)*1
#===================================================================================================  
"
modelScalar.fit1 = lavaan(
  scalar.syntax1, 
  data=data01, 
  estimator = "MLR", 
  mimic="Mplus", 
  group = "Student"
)

summary(modelScalar.fit1, fit.measures=TRUE, rsquare=TRUE, standardized=TRUE)
## lavaan 0.6-19 ended normally after 51 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        20
##   Number of equality constraints                     5
## 
##   Number of observations per group:                   
##     1                                             1192
##     0                                              144
##   Number of missing patterns per group:               
##     1                                                1
##     0                                                1
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                                 8.507       6.357
##   Degrees of freedom                                 3           3
##   P-value (Chi-square)                           0.037       0.095
##   Scaling correction factor                                  1.338
##     Yuan-Bentler correction (Mplus variant)                       
##   Test statistic for each group:
##     1                                            0.485       0.485
##     0                                            5.872       5.872
## 
## Model Test Baseline Model:
## 
##   Test statistic                               401.955     217.534
##   Degrees of freedom                                 6           6
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.848
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.986       0.984
##   Tucker-Lewis Index (TLI)                       0.972       0.968
##                                                                   
##   Robust Comparative Fit Index (CFI)                         0.989
##   Robust Tucker-Lewis Index (TLI)                            0.978
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -4923.629   -4923.629
##   Scaling correction factor                                  1.427
##       for the MLR correction                                      
##   Loglikelihood unrestricted model (H1)      -4919.375   -4919.375
##   Scaling correction factor                                  1.808
##       for the MLR correction                                      
##                                                                   
##   Akaike (AIC)                                9877.257    9877.257
##   Bayesian (BIC)                              9955.219    9955.219
##   Sample-size adjusted Bayesian (SABIC)       9907.570    9907.570
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.052       0.041
##   90 Percent confidence interval - lower         0.012       0.000
##   90 Percent confidence interval - upper         0.096       0.080
##   P-value H_0: RMSEA <= 0.050                    0.390       0.592
##   P-value H_0: RMSEA >= 0.080                    0.164       0.048
##                                                                   
##   Robust RMSEA                                               0.047
##   90 Percent confidence interval - lower                     0.000
##   90 Percent confidence interval - upper                     0.100
##   P-value H_0: Robust RMSEA <= 0.050                         0.458
##   P-value H_0: Robust RMSEA >= 0.080                         0.174
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.015       0.015
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## 
## Group 1 [1]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1      (L1)    0.431    0.035   12.217    0.000    0.431    0.488
##     GRI3      (L3)    0.553    0.052   10.672    0.000    0.553    0.671
##     GRI5      (L5)    0.436    0.034   12.784    0.000    0.436    0.567
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1      (I1)    1.684    0.025   66.460    0.000    1.684    1.907
##    .GRI3     (SI3)    1.526    0.024   63.788    0.000    1.526    1.854
##    .GRI5      (I5)    1.455    0.022   66.384    0.000    1.455    1.892
##     GAMBLING          0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1     (SR1)    0.594    0.068    8.670    0.000    0.594    0.762
##    .GRI3     (SR3)    0.372    0.053    6.958    0.000    0.372    0.549
##    .GRI5     (SR5)    0.402    0.045    8.914    0.000    0.402    0.679
##     GAMBLING          1.000                               1.000    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.238
##     GRI3              0.451
##     GRI5              0.321
## 
## 
## Group 2 [0]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1      (L1)    0.431    0.035   12.217    0.000    0.648    0.485
##     GRI3      (L3)    0.553    0.052   10.672    0.000    0.831    0.720
##     GRI5      (L5)    0.436    0.034   12.784    0.000    0.655    0.422
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1      (I1)    1.684    0.025   66.460    0.000    1.684    1.259
##    .GRI3    (NSI3)    0.087    0.223    0.390    0.697    0.087    0.075
##    .GRI5      (I5)    1.455    0.022   66.384    0.000    1.455    0.936
##     GAMBLIN           2.971    0.319    9.311    0.000    1.977    1.977
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1    (NSR1)    1.368    0.177    7.740    0.000    1.368    0.765
##    .GRI3    (NSR3)    0.642    0.186    3.453    0.001    0.642    0.482
##    .GRI5    (NSR5)    1.987    0.211    9.411    0.000    1.987    0.822
##     GAMBLIN           2.260    0.556    4.066    0.000    1.000    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.235
##     GRI3              0.518
##     GRI5              0.178
# likelihood ratio test: all loadings tested at once:
anova(modelMetric.fit, modelScalar.fit1)
## 
## Scaled Chi-Squared Difference Test (method = "satorra.bentler.2001")
## 
## lavaan->lavTestLRT():  
##    lavaan NOTE: The "Chisq" column contains standard test statistics, not the 
##    robust test that should be reported per model. A robust difference test is 
##    a function of two standard (not robust) statistics.
##                  Df    AIC    BIC  Chisq Chisq diff Df diff Pr(>Chisq)
## modelMetric.fit   2 9879.2 9962.3 8.4121                              
## modelScalar.fit1  3 9877.3 9955.2 8.5070   0.081488       1     0.7753
# proceed to residual with GRI3 non-invariant for intercept and residual ============================


#MODEL Residual: Testing equality of residual variances of GRI by student status ====================

residual.syntax = "
#===================================================================================================
#Factor loadings all freely estimated in both groups with label for each group
  GAMBLING =~ c(L1, L1)*GRI1 + c(L3, L3)*GRI3 + c(L5, L5)*GRI5
#===================================================================================================
#Item intercepts all freely estimated in both groups with label for each group
  GRI1 ~ c(I1, I1)*1
  GRI3 ~ c(SI3, NSI3)*1
  GRI5 ~ c(I5, I5)*1
#===================================================================================================
#Redidual variances all freely estimated with label for each group
  GRI1 ~~ c(R1, R1)*GRI1
  GRI3 ~~ c(R3, R3)*GRI3
  GRI5 ~~ c(R5, R5)*GRI5
#===================================================================================================
#Factor variances all freely estimated in one group but not other for identification
  GAMBLING ~~ c(1, NA)*GAMBLING
#===================================================================================================
#Factor means all freely estimated in one group but not other for identification
  GAMBLING ~ c(0, NA)*1
#===================================================================================================  
"

modelResidual.fit = lavaan(
  residual.syntax, 
  data=data01, 
  estimator = "MLR", 
  mimic="Mplus", 
  group = "Student"
)

summary(modelResidual.fit, fit.measures=TRUE, rsquare=TRUE, standardized=TRUE)
## lavaan 0.6-19 ended normally after 35 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        20
##   Number of equality constraints                     8
## 
##   Number of observations per group:                   
##     1                                             1192
##     0                                              144
##   Number of missing patterns per group:               
##     1                                                1
##     0                                                1
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                               209.808     321.542
##   Degrees of freedom                                 6           6
##   P-value (Chi-square)                           0.000       0.000
##   Scaling correction factor                                  0.653
##     Yuan-Bentler correction (Mplus variant)                       
##   Test statistic for each group:
##     1                                           96.675      96.675
##     0                                          224.867     224.867
## 
## Model Test Baseline Model:
## 
##   Test statistic                               401.955     217.534
##   Degrees of freedom                                 6           6
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.848
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.485       0.000
##   Tucker-Lewis Index (TLI)                       0.485      -0.492
##                                                                   
##   Robust Comparative Fit Index (CFI)                         0.486
##   Robust Tucker-Lewis Index (TLI)                            0.486
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -5024.279   -5024.279
##   Scaling correction factor                                  1.432
##       for the MLR correction                                      
##   Loglikelihood unrestricted model (H1)      -4919.375   -4919.375
##   Scaling correction factor                                  1.808
##       for the MLR correction                                      
##                                                                   
##   Akaike (AIC)                               10072.558   10072.558
##   Bayesian (BIC)                             10134.927   10134.927
##   Sample-size adjusted Bayesian (SABIC)      10096.808   10096.808
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.225       0.281
##   90 Percent confidence interval - lower         0.200       0.249
##   90 Percent confidence interval - upper         0.252       0.314
##   P-value H_0: RMSEA <= 0.050                    0.000       0.000
##   P-value H_0: RMSEA >= 0.080                    1.000       1.000
##                                                                   
##   Robust RMSEA                                               0.224
##   90 Percent confidence interval - lower                     0.195
##   90 Percent confidence interval - upper                     0.255
##   P-value H_0: Robust RMSEA <= 0.050                         0.000
##   P-value H_0: Robust RMSEA >= 0.080                         1.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.115       0.115
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## 
## Group 1 [1]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1      (L1)    0.434    0.044    9.833    0.000    0.434    0.476
##     GRI3      (L3)    0.378    0.066    5.703    0.000    0.378    0.457
##     GRI5      (L5)    0.471    0.043   11.044    0.000    0.471    0.569
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1      (I1)    1.691    0.027   61.569    0.000    1.691    1.855
##    .GRI3     (SI3)    1.526    0.024   63.788    0.000    1.526    1.844
##    .GRI5      (I5)    1.450    0.023   62.655    0.000    1.450    1.750
##     GAMBLING          0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1      (R1)    0.643    0.078    8.223    0.000    0.643    0.774
##    .GRI3      (R3)    0.542    0.057    9.557    0.000    0.542    0.791
##    .GRI5      (R5)    0.465    0.080    5.840    0.000    0.465    0.677
##     GAMBLING          1.000                               1.000    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.226
##     GRI3              0.209
##     GRI5              0.323
## 
## 
## Group 2 [0]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1      (L1)    0.434    0.044    9.833    0.000    0.964    0.769
##     GRI3      (L3)    0.378    0.066    5.703    0.000    0.840    0.752
##     GRI5      (L5)    0.471    0.043   11.044    0.000    1.047    0.838
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1      (I1)    1.691    0.027   61.569    0.000    1.691    1.348
##    .GRI3    (NSI3)    0.667    0.156    4.269    0.000    0.667    0.597
##    .GRI5      (I5)    1.450    0.023   62.655    0.000    1.450    1.161
##     GAMBLIN           2.811    0.301    9.329    0.000    1.265    1.265
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1      (R1)    0.643    0.078    8.223    0.000    0.643    0.409
##    .GRI3      (R3)    0.542    0.057    9.557    0.000    0.542    0.434
##    .GRI5      (R5)    0.465    0.080    5.840    0.000    0.465    0.298
##     GAMBLING          4.939    1.218    4.054    0.000    1.000    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.591
##     GRI3              0.566
##     GRI5              0.702
# likelihood ratio test: all loadings tested at once:
anova(modelScalar.fit1, modelResidual.fit)
## Warning: lavaan->lav_test_diff_SatorraBentler2001():  
##    scaling factor is negative
## 
## Scaled Chi-Squared Difference Test (method = "satorra.bentler.2001")
## 
## lavaan->lavTestLRT():  
##    lavaan NOTE: The "Chisq" column contains standard test statistics, not the 
##    robust test that should be reported per model. A robust difference test is 
##    a function of two standard (not robust) statistics.
##                   Df     AIC     BIC   Chisq Chisq diff Df diff Pr(>Chisq)
## modelScalar.fit1   3  9877.3  9955.2   8.507                              
## modelResidual.fit  6 10072.6 10134.9 209.808                  3
# looks like a problem...investigate MIs
residualMI1 = lavTestScore(modelResidual.fit)
## Warning: lavaan->lavTestScore():  
##    se is not `standard'; not implemented yet; falling back to ordinary score 
##    test
#change label values
labelMap = data.frame(
  lhs = modelResidual.fit@ParTable$plabel,
  parameter = modelResidual.fit@ParTable$label
)

residualMI1$uni = merge(x = residualMI1$uni, y = labelMap, by = "lhs", all.x = TRUE)

# reorder by decreasing values of X2:
residualMI1$uni = residualMI1$uni[order(residualMI1$uni$X2, decreasing = TRUE),]

#restrict to only means shown
residualMI1$uni[grep(x = residualMI1$uni$parameter, pattern = "R"),]
##    lhs op   rhs        X2 df      p.value parameter
## 8 .p9. == .p20. 283.19426  1 0.000000e+00        R5
## 7 .p8. == .p19.  63.41500  1 1.665335e-15        R3
## 6 .p7. == .p18.  60.25915  1 8.326673e-15        R1
# free parameter for R5 first:

residual.syntax1 = "
#===================================================================================================
#Factor loadings all freely estimated in both groups with label for each group
  GAMBLING =~ c(L1, L1)*GRI1 + c(L3, L3)*GRI3 + c(L5, L5)*GRI5
#===================================================================================================
#Item intercepts all freely estimated in both groups with label for each group
  GRI1 ~ c(I1, I1)*1
  GRI3 ~ c(SI3, NSI3)*1
  GRI5 ~ c(I5, I5)*1
#===================================================================================================
#Redidual variances all freely estimated with label for each group
  GRI1 ~~ c(R1, R1)*GRI1
  GRI3 ~~ c(R3, R3)*GRI3
  GRI5 ~~ c(SR5, NSR5)*GRI5
#===================================================================================================
#Factor variances all freely estimated in one group but not other for identification
  GAMBLING ~~ c(1, NA)*GAMBLING
#===================================================================================================
#Factor means all freely estimated in one group but not other for identification
  GAMBLING ~ c(0, NA)*1
#===================================================================================================  
"

modelResidual.fit1 = lavaan(
  residual.syntax1, 
  data=data01, 
  estimator = "MLR", 
  mimic="Mplus", 
  group = "Student"
)

summary(modelResidual.fit1, fit.measures=TRUE, rsquare=TRUE, standardized=TRUE)
## lavaan 0.6-19 ended normally after 42 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        20
##   Number of equality constraints                     7
## 
##   Number of observations per group:                   
##     1                                             1192
##     0                                              144
##   Number of missing patterns per group:               
##     1                                                1
##     0                                                1
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                                55.848      47.409
##   Degrees of freedom                                 5           5
##   P-value (Chi-square)                           0.000       0.000
##   Scaling correction factor                                  1.178
##     Yuan-Bentler correction (Mplus variant)                       
##   Test statistic for each group:
##     1                                            9.700       9.700
##     0                                           37.709      37.709
## 
## Model Test Baseline Model:
## 
##   Test statistic                               401.955     217.534
##   Degrees of freedom                                 6           6
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.848
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.872       0.800
##   Tucker-Lewis Index (TLI)                       0.846       0.759
##                                                                   
##   Robust Comparative Fit Index (CFI)                         0.875
##   Robust Tucker-Lewis Index (TLI)                            0.849
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -4947.299   -4947.299
##   Scaling correction factor                                  1.333
##       for the MLR correction                                      
##   Loglikelihood unrestricted model (H1)      -4919.375   -4919.375
##   Scaling correction factor                                  1.808
##       for the MLR correction                                      
##                                                                   
##   Akaike (AIC)                                9920.598    9920.598
##   Bayesian (BIC)                              9988.165    9988.165
##   Sample-size adjusted Bayesian (SABIC)       9946.870    9946.870
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.123       0.113
##   90 Percent confidence interval - lower         0.095       0.087
##   90 Percent confidence interval - upper         0.154       0.141
##   P-value H_0: RMSEA <= 0.050                    0.000       0.000
##   P-value H_0: RMSEA >= 0.080                    0.994       0.980
##                                                                   
##   Robust RMSEA                                               0.121
##   90 Percent confidence interval - lower                     0.089
##   90 Percent confidence interval - upper                     0.156
##   P-value H_0: Robust RMSEA <= 0.050                         0.000
##   P-value H_0: Robust RMSEA >= 0.080                         0.982
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.064       0.064
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## 
## Group 1 [1]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1      (L1)    0.449    0.037   12.223    0.000    0.449    0.488
##     GRI3      (L3)    0.498    0.059    8.444    0.000    0.498    0.601
##     GRI5      (L5)    0.451    0.035   12.835    0.000    0.451    0.589
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1      (I1)    1.684    0.026   66.010    0.000    1.684    1.829
##    .GRI3     (SI3)    1.526    0.024   63.788    0.000    1.526    1.842
##    .GRI5      (I5)    1.455    0.022   66.365    0.000    1.455    1.899
##     GAMBLING          0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1      (R1)    0.646    0.075    8.653    0.000    0.646    0.762
##    .GRI3      (R3)    0.438    0.060    7.325    0.000    0.438    0.638
##    .GRI5     (SR5)    0.384    0.046    8.405    0.000    0.384    0.653
##     GAMBLING          1.000                               1.000    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.238
##     GRI3              0.362
##     GRI5              0.347
## 
## 
## Group 2 [0]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1      (L1)    0.449    0.037   12.223    0.000    0.804    0.707
##     GRI3      (L3)    0.498    0.059    8.444    0.000    0.892    0.803
##     GRI5      (L5)    0.451    0.035   12.835    0.000    0.808    0.500
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1      (I1)    1.684    0.026   66.010    0.000    1.684    1.481
##    .GRI3    (NSI3)    0.299    0.202    1.482    0.138    0.299    0.269
##    .GRI5      (I5)    1.455    0.022   66.365    0.000    1.455    0.900
##     GAMBLIN           2.871    0.318    9.041    0.000    1.603    1.603
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1      (R1)    0.646    0.075    8.653    0.000    0.646    0.500
##    .GRI3      (R3)    0.438    0.060    7.325    0.000    0.438    0.355
##    .GRI5    (NSR5)    1.958    0.241    8.110    0.000    1.958    0.750
##     GAMBLIN           3.210    0.708    4.533    0.000    1.000    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.500
##     GRI3              0.645
##     GRI5              0.250
# likelihood ratio test: all loadings tested at once:
anova(modelScalar.fit1, modelResidual.fit1)
## 
## Scaled Chi-Squared Difference Test (method = "satorra.bentler.2001")
## 
## lavaan->lavTestLRT():  
##    lavaan NOTE: The "Chisq" column contains standard test statistics, not the 
##    robust test that should be reported per model. A robust difference test is 
##    a function of two standard (not robust) statistics.
##                    Df    AIC    BIC  Chisq Chisq diff Df diff Pr(>Chisq)    
## modelScalar.fit1    3 9877.3 9955.2  8.507                                  
## modelResidual.fit1  5 9920.6 9988.2 55.848     50.484       2   1.09e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# looks like a problem...investigate MIs
residualMI2 = lavTestScore(modelResidual.fit1)
## Warning: lavaan->lavTestScore():  
##    se is not `standard'; not implemented yet; falling back to ordinary score 
##    test
#change label values
labelMap = data.frame(
  lhs = modelResidual.fit1@ParTable$plabel,
  parameter = modelResidual.fit1@ParTable$label
)

residualMI2$uni = merge(x = residualMI2$uni, y = labelMap, by = "lhs", all.x = TRUE)

# reorder by decreasing values of X2:
residualMI2$uni = residualMI2$uni[order(residualMI2$uni$X2, decreasing = TRUE),]

#restrict to only means shown
residualMI2$uni[grep(x = residualMI2$uni$parameter, pattern = "R"),]
##    lhs op   rhs       X2 df      p.value parameter
## 6 .p7. == .p18. 57.12852  1 4.085621e-14        R1
## 7 .p8. == .p19. 26.69074  1 2.387628e-07        R3
# free parameter for R1:

residual.syntax2 = "
#===================================================================================================
#Factor loadings all freely estimated in both groups with label for each group
  GAMBLING =~ c(L1, L1)*GRI1 + c(L3, L3)*GRI3 + c(L5, L5)*GRI5
#===================================================================================================
#Item intercepts all freely estimated in both groups with label for each group
  GRI1 ~ c(I1, I1)*1
  GRI3 ~ c(SI3, NSI3)*1
  GRI5 ~ c(I5, I5)*1
#===================================================================================================
#Redidual variances all freely estimated with label for each group
  GRI1 ~~ c(SR1, NSR1)*GRI1
  GRI3 ~~ c(R3, R3)*GRI3
  GRI5 ~~ c(SR5, NSR5)*GRI5
#===================================================================================================
#Factor variances all freely estimated in one group but not other for identification
  GAMBLING ~~ c(1, NA)*GAMBLING
#===================================================================================================
#Factor means all freely estimated in one group but not other for identification
  GAMBLING ~ c(0, NA)*1
#===================================================================================================  
"

modelResidual.fit2 = lavaan(
  residual.syntax2, 
  data=data01, 
  estimator = "MLR", 
  mimic="Mplus", 
  group = "Student"
)

summary(modelResidual.fit2, fit.measures=TRUE, rsquare=TRUE, standardized=TRUE)
## lavaan 0.6-19 ended normally after 46 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        20
##   Number of equality constraints                     6
## 
##   Number of observations per group:                   
##     1                                             1192
##     0                                              144
##   Number of missing patterns per group:               
##     1                                                1
##     0                                                1
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                                12.651      10.087
##   Degrees of freedom                                 4           4
##   P-value (Chi-square)                           0.013       0.039
##   Scaling correction factor                                  1.254
##     Yuan-Bentler correction (Mplus variant)                       
##   Test statistic for each group:
##     1                                            0.213       0.213
##     0                                            9.874       9.874
## 
## Model Test Baseline Model:
## 
##   Test statistic                               401.955     217.534
##   Degrees of freedom                                 6           6
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.848
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.978       0.971
##   Tucker-Lewis Index (TLI)                       0.967       0.957
##                                                                   
##   Robust Comparative Fit Index (CFI)                         0.982
##   Robust Tucker-Lewis Index (TLI)                            0.972
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -4925.701   -4925.701
##   Scaling correction factor                                  1.377
##       for the MLR correction                                      
##   Loglikelihood unrestricted model (H1)      -4919.375   -4919.375
##   Scaling correction factor                                  1.808
##       for the MLR correction                                      
##                                                                   
##   Akaike (AIC)                                9879.402    9879.402
##   Bayesian (BIC)                              9952.166    9952.166
##   Sample-size adjusted Bayesian (SABIC)       9907.694    9907.694
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.057       0.048
##   90 Percent confidence interval - lower         0.023       0.015
##   90 Percent confidence interval - upper         0.094       0.081
##   P-value H_0: RMSEA <= 0.050                    0.318       0.488
##   P-value H_0: RMSEA >= 0.080                    0.165       0.058
##                                                                   
##   Robust RMSEA                                               0.052
##   90 Percent confidence interval - lower                     0.000
##   90 Percent confidence interval - upper                     0.096
##   P-value H_0: Robust RMSEA <= 0.050                         0.402
##   P-value H_0: Robust RMSEA >= 0.080                         0.169
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.013       0.013
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## 
## Group 1 [1]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1      (L1)    0.416    0.036   11.519    0.000    0.416    0.473
##     GRI3      (L3)    0.571    0.050   11.353    0.000    0.571    0.688
##     GRI5      (L5)    0.423    0.037   11.426    0.000    0.423    0.552
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1      (I1)    1.684    0.025   66.430    0.000    1.684    1.914
##    .GRI3     (SI3)    1.526    0.024   63.788    0.000    1.526    1.839
##    .GRI5      (I5)    1.455    0.022   66.338    0.000    1.455    1.898
##     GAMBLING          0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1     (SR1)    0.600    0.069    8.752    0.000    0.600    0.776
##    .GRI3      (R3)    0.363    0.059    6.146    0.000    0.363    0.527
##    .GRI5     (SR5)    0.409    0.046    8.914    0.000    0.409    0.695
##     GAMBLING          1.000                               1.000    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.224
##     GRI3              0.473
##     GRI5              0.305
## 
## 
## Group 2 [0]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1      (L1)    0.416    0.036   11.519    0.000    0.673    0.487
##     GRI3      (L3)    0.571    0.050   11.353    0.000    0.923    0.838
##     GRI5      (L5)    0.423    0.037   11.426    0.000    0.685    0.427
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1      (I1)    1.684    0.025   66.430    0.000    1.684    1.219
##    .GRI3    (NSI3)   -0.024    0.234   -0.102    0.919   -0.024   -0.022
##    .GRI5      (I5)    1.455    0.022   66.338    0.000    1.455    0.907
##     GAMBLIN           3.071    0.362    8.490    0.000    1.898    1.898
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1    (NSR1)    1.454    0.164    8.877    0.000    1.454    0.762
##    .GRI3      (R3)    0.363    0.059    6.146    0.000    0.363    0.298
##    .GRI5    (NSR5)    2.106    0.239    8.813    0.000    2.106    0.818
##     GAMBLIN           2.617    0.622    4.207    0.000    1.000    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.238
##     GRI3              0.702
##     GRI5              0.182
# likelihood ratio test: all loadings tested at once:
anova(modelScalar.fit1, modelResidual.fit2)
## 
## Scaled Chi-Squared Difference Test (method = "satorra.bentler.2001")
## 
## lavaan->lavTestLRT():  
##    lavaan NOTE: The "Chisq" column contains standard test statistics, not the 
##    robust test that should be reported per model. A robust difference test is 
##    a function of two standard (not robust) statistics.
##                    Df    AIC    BIC  Chisq Chisq diff Df diff Pr(>Chisq)  
## modelScalar.fit1    3 9877.3 9955.2  8.507                                
## modelResidual.fit2  4 9879.4 9952.2 12.651     4.1339       1    0.04203 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# ignore and continue with structural model


#MODEL Structural: Testing equality of mean/variance of GRI by student status ======================

structural.syntax = "
#===================================================================================================
#Factor loadings all freely estimated in both groups with label for each group
  GAMBLING =~ c(L1, L1)*GRI1 + c(L3, L3)*GRI3 + c(L5, L5)*GRI5
#===================================================================================================
#Item intercepts all freely estimated in both groups with label for each group
  GRI1 ~ c(I1, I1)*1
  GRI3 ~ c(SI3, NSI3)*1
  GRI5 ~ c(I5, I5)*1
#===================================================================================================
#Redidual variances all freely estimated with label for each group
  GRI1 ~~ c(SR1, NSR1)*GRI1
  GRI3 ~~ c(R3, R3)*GRI3
  GRI5 ~~ c(SR5, NSR5)*GRI5
#===================================================================================================
#Factor variances fixed to compare against previous model
  GAMBLING ~~ c(1, 1)*GAMBLING
#===================================================================================================
#Factor means fixed to compare against previous model
  GAMBLING ~ c(0,0)*1
#===================================================================================================  
"
modelStructural.fit = lavaan(
  structural.syntax, 
  data=data01, 
  estimator = "MLR", 
  mimic="Mplus", 
  group = "Student"
)

summary(modelStructural.fit, fit.measures=TRUE, rsquare=TRUE, standardized=TRUE)
## lavaan 0.6-19 ended normally after 33 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        18
##   Number of equality constraints                     6
## 
##   Number of observations per group:                   
##     1                                             1192
##     0                                              144
##   Number of missing patterns per group:               
##     1                                                1
##     0                                                1
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                               207.431     178.319
##   Degrees of freedom                                 6           6
##   P-value (Chi-square)                           0.000       0.000
##   Scaling correction factor                                  1.163
##     Yuan-Bentler correction (Mplus variant)                       
##   Test statistic for each group:
##     1                                            8.456       8.456
##     0                                          169.863     169.863
## 
## Model Test Baseline Model:
## 
##   Test statistic                               401.955     217.534
##   Degrees of freedom                                 6           6
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.848
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.491       0.185
##   Tucker-Lewis Index (TLI)                       0.491       0.185
##                                                                   
##   Robust Comparative Fit Index (CFI)                         0.492
##   Robust Tucker-Lewis Index (TLI)                            0.492
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -5023.091   -5023.091
##   Scaling correction factor                                  1.420
##       for the MLR correction                                      
##   Loglikelihood unrestricted model (H1)      -4919.375   -4919.375
##   Scaling correction factor                                  1.808
##       for the MLR correction                                      
##                                                                   
##   Akaike (AIC)                               10070.181   10070.181
##   Bayesian (BIC)                             10132.550   10132.550
##   Sample-size adjusted Bayesian (SABIC)      10094.432   10094.432
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.224       0.207
##   90 Percent confidence interval - lower         0.199       0.184
##   90 Percent confidence interval - upper         0.251       0.232
##   P-value H_0: RMSEA <= 0.050                    0.000       0.000
##   P-value H_0: RMSEA >= 0.080                    1.000       1.000
##                                                                   
##   Robust RMSEA                                               0.223
##   90 Percent confidence interval - lower                     0.194
##   90 Percent confidence interval - upper                     0.254
##   P-value H_0: Robust RMSEA <= 0.050                         0.000
##   P-value H_0: Robust RMSEA >= 0.080                         1.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.104       0.104
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## 
## Group 1 [1]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1      (L1)    0.442    0.043   10.267    0.000    0.442    0.496
##     GRI3      (L3)    0.612    0.050   12.338    0.000    0.612    0.706
##     GRI5      (L5)    0.453    0.040   11.270    0.000    0.453    0.582
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1      (I1)    1.728    0.028   61.209    0.000    1.728    1.940
##    .GRI3     (SI3)    1.549    0.025   61.539    0.000    1.549    1.789
##    .GRI5      (I5)    1.488    0.024   60.963    0.000    1.488    1.909
##     GAMBLING          0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1     (SR1)    0.598    0.069    8.643    0.000    0.598    0.754
##    .GRI3      (R3)    0.376    0.055    6.772    0.000    0.376    0.501
##    .GRI5     (SR5)    0.402    0.045    8.907    0.000    0.402    0.662
##     GAMBLING          1.000                               1.000    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.246
##     GRI3              0.499
##     GRI5              0.338
## 
## 
## Group 2 [0]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1      (L1)    0.442    0.043   10.267    0.000    0.442    0.253
##     GRI3      (L3)    0.612    0.050   12.338    0.000    0.612    0.706
##     GRI5      (L5)    0.453    0.040   11.270    0.000    0.453    0.238
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1      (I1)    1.728    0.028   61.209    0.000    1.728    0.991
##    .GRI3    (NSI3)    1.535    0.090   17.023    0.000    1.535    1.773
##    .GRI5      (I5)    1.488    0.024   60.963    0.000    1.488    0.780
##     GAMBLIN           0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1    (NSR1)    2.843    0.361    7.872    0.000    2.843    0.936
##    .GRI3      (R3)    0.376    0.055    6.772    0.000    0.376    0.501
##    .GRI5    (NSR5)    3.432    0.458    7.490    0.000    3.432    0.943
##     GAMBLIN           1.000                               1.000    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.064
##     GRI3              0.499
##     GRI5              0.057
# likelihood ratio test: all parameters tested at once:
anova(modelResidual.fit2, modelStructural.fit)
## 
## Scaled Chi-Squared Difference Test (method = "satorra.bentler.2001")
## 
## lavaan->lavTestLRT():  
##    lavaan NOTE: The "Chisq" column contains standard test statistics, not the 
##    robust test that should be reported per model. A robust difference test is 
##    a function of two standard (not robust) statistics.
##                     Df     AIC     BIC   Chisq Chisq diff Df diff Pr(>Chisq)
## modelResidual.fit2   4  9879.4  9952.2  12.651                              
## modelStructural.fit  6 10070.2 10132.6 207.431     198.51       2  < 2.2e-16
##                        
## modelResidual.fit2     
## modelStructural.fit ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# conclusion: difference between students and non-students on GAMBLING factor ======================
summary(modelResidual.fit2, fit.measures=TRUE, rsquare=TRUE, standardized=TRUE)
## lavaan 0.6-19 ended normally after 46 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        20
##   Number of equality constraints                     6
## 
##   Number of observations per group:                   
##     1                                             1192
##     0                                              144
##   Number of missing patterns per group:               
##     1                                                1
##     0                                                1
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                                12.651      10.087
##   Degrees of freedom                                 4           4
##   P-value (Chi-square)                           0.013       0.039
##   Scaling correction factor                                  1.254
##     Yuan-Bentler correction (Mplus variant)                       
##   Test statistic for each group:
##     1                                            0.213       0.213
##     0                                            9.874       9.874
## 
## Model Test Baseline Model:
## 
##   Test statistic                               401.955     217.534
##   Degrees of freedom                                 6           6
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.848
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.978       0.971
##   Tucker-Lewis Index (TLI)                       0.967       0.957
##                                                                   
##   Robust Comparative Fit Index (CFI)                         0.982
##   Robust Tucker-Lewis Index (TLI)                            0.972
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -4925.701   -4925.701
##   Scaling correction factor                                  1.377
##       for the MLR correction                                      
##   Loglikelihood unrestricted model (H1)      -4919.375   -4919.375
##   Scaling correction factor                                  1.808
##       for the MLR correction                                      
##                                                                   
##   Akaike (AIC)                                9879.402    9879.402
##   Bayesian (BIC)                              9952.166    9952.166
##   Sample-size adjusted Bayesian (SABIC)       9907.694    9907.694
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.057       0.048
##   90 Percent confidence interval - lower         0.023       0.015
##   90 Percent confidence interval - upper         0.094       0.081
##   P-value H_0: RMSEA <= 0.050                    0.318       0.488
##   P-value H_0: RMSEA >= 0.080                    0.165       0.058
##                                                                   
##   Robust RMSEA                                               0.052
##   90 Percent confidence interval - lower                     0.000
##   90 Percent confidence interval - upper                     0.096
##   P-value H_0: Robust RMSEA <= 0.050                         0.402
##   P-value H_0: Robust RMSEA >= 0.080                         0.169
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.013       0.013
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## 
## Group 1 [1]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1      (L1)    0.416    0.036   11.519    0.000    0.416    0.473
##     GRI3      (L3)    0.571    0.050   11.353    0.000    0.571    0.688
##     GRI5      (L5)    0.423    0.037   11.426    0.000    0.423    0.552
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1      (I1)    1.684    0.025   66.430    0.000    1.684    1.914
##    .GRI3     (SI3)    1.526    0.024   63.788    0.000    1.526    1.839
##    .GRI5      (I5)    1.455    0.022   66.338    0.000    1.455    1.898
##     GAMBLING          0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1     (SR1)    0.600    0.069    8.752    0.000    0.600    0.776
##    .GRI3      (R3)    0.363    0.059    6.146    0.000    0.363    0.527
##    .GRI5     (SR5)    0.409    0.046    8.914    0.000    0.409    0.695
##     GAMBLING          1.000                               1.000    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.224
##     GRI3              0.473
##     GRI5              0.305
## 
## 
## Group 2 [0]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1      (L1)    0.416    0.036   11.519    0.000    0.673    0.487
##     GRI3      (L3)    0.571    0.050   11.353    0.000    0.923    0.838
##     GRI5      (L5)    0.423    0.037   11.426    0.000    0.685    0.427
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1      (I1)    1.684    0.025   66.430    0.000    1.684    1.219
##    .GRI3    (NSI3)   -0.024    0.234   -0.102    0.919   -0.024   -0.022
##    .GRI5      (I5)    1.455    0.022   66.338    0.000    1.455    0.907
##     GAMBLIN           3.071    0.362    8.490    0.000    1.898    1.898
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1    (NSR1)    1.454    0.164    8.877    0.000    1.454    0.762
##    .GRI3      (R3)    0.363    0.059    6.146    0.000    0.363    0.298
##    .GRI5    (NSR5)    2.106    0.239    8.813    0.000    2.106    0.818
##     GAMBLIN           2.617    0.622    4.207    0.000    1.000    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.238
##     GRI3              0.702
##     GRI5              0.182
# comparison models: 
#MODEL 03a: Structural Equation Model #2 --  prediction of GRI items by Student -------------------------------

model03a.syntax = "
  GRI5 ~ Student
  GRI3 ~ Student
  GAMBLING =~ GRI1 + GRI3 + GRI5
  GAMBLING ~ Student
"

model03a.fit = sem(model03a.syntax, data=data01, estimator = "MLR", mimic="Mplus", fixed.x=FALSE)
## Warning: lavaan->lav_model_vcov():  
##    The variance-covariance matrix of the estimated parameters (vcov) does not 
##    appear to be positive definite! The smallest eigenvalue (= -6.261671e-20) 
##    is smaller than zero. This may be a symptom that the model is not 
##    identified.
summary(model03a.fit, fit.measures=TRUE, rsquare=TRUE, standardized=TRUE)
## lavaan 0.6-19 ended normally after 39 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        14
## 
##   Number of observations                          1336
##   Number of missing patterns                         1
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                                 0.000       0.000
##   Degrees of freedom                                 0           0
## 
## Model Test Baseline Model:
## 
##   Test statistic                               853.812     391.369
##   Degrees of freedom                                 6           6
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  2.182
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000       1.000
##   Tucker-Lewis Index (TLI)                       1.000       1.000
##                                                                   
##   Robust Comparative Fit Index (CFI)                         1.000
##   Robust Tucker-Lewis Index (TLI)                            1.000
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -5399.664   -5399.664
##   Loglikelihood unrestricted model (H1)      -5399.664   -5399.664
##                                                                   
##   Akaike (AIC)                               10827.328   10827.328
##   Bayesian (BIC)                             10900.092   10900.092
##   Sample-size adjusted Bayesian (SABIC)      10855.620   10855.620
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000          NA
##   90 Percent confidence interval - lower         0.000          NA
##   90 Percent confidence interval - upper         0.000          NA
##   P-value H_0: RMSEA <= 0.050                       NA          NA
##   P-value H_0: RMSEA >= 0.080                       NA          NA
##                                                                   
##   Robust RMSEA                                               0.000
##   90 Percent confidence interval - lower                     0.000
##   90 Percent confidence interval - upper                     0.000
##   P-value H_0: Robust RMSEA <= 0.050                            NA
##   P-value H_0: Robust RMSEA >= 0.080                            NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.000       0.000
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1              1.000                               0.639    0.623
##     GRI3              1.092    0.145    7.521    0.000    0.699    0.807
##     GRI5              0.997    0.131    7.632    0.000    0.637    0.654
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GRI5 ~                                                                
##     Student           0.018    0.208    0.088    0.930    0.018    0.006
##   GRI3 ~                                                                
##     Student           1.213    0.209    5.802    0.000    1.213    0.435
##   GAMBLING ~                                                            
##     Student          -1.296    0.118  -11.031    0.000   -2.027   -0.629
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1              2.979    0.115   25.967    0.000    2.979    2.901
##    .GRI3              1.729    0.094   18.435    0.000    1.729    1.998
##    .GRI5              2.729    0.131   20.875    0.000    2.729    2.801
##     Student           0.892    0.008  105.162    0.000    0.892    2.877
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1              0.646    0.069    9.365    0.000    0.646    0.612
##    .GRI3              0.450    0.052    8.621    0.000    0.450    0.601
##    .GRI5              0.548    0.052   10.537    0.000    0.548    0.577
##    .GAMBLING          0.247    0.045    5.439    0.000    0.605    0.605
##     Student           0.096    0.007   14.450    0.000    0.096    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.388
##     GRI3              0.399
##     GRI5              0.423
##     GAMBLING          0.395
#MODEL 03b: Structural Equation Model #2 -- NO prediction of GRI 3 by Student -------------------------------
model03b.syntax = "
  GRI3 ~ Student
  GAMBLING =~ GRI1 + GRI3 + GRI5
  GAMBLING ~ 0*Student
"

model03b.fit = sem(model03b.syntax, data=data01, estimator = "MLR", mimic="Mplus", fixed.x=FALSE)
## Warning: lavaan->lav_model_vcov():  
##    The variance-covariance matrix of the estimated parameters (vcov) does not 
##    appear to be positive definite! The smallest eigenvalue (= -1.223538e-19) 
##    is smaller than zero. This may be a symptom that the model is not 
##    identified.
summary(model03b.fit, fit.measures=TRUE, rsquare=TRUE, standardized=TRUE)
## lavaan 0.6-19 ended normally after 26 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        12
## 
##   Number of observations                          1336
##   Number of missing patterns                         1
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                               342.402     163.776
##   Degrees of freedom                                 2           2
##   P-value (Chi-square)                           0.000       0.000
##   Scaling correction factor                                  2.091
##     Yuan-Bentler correction (Mplus variant)                       
## 
## Model Test Baseline Model:
## 
##   Test statistic                               853.812     391.369
##   Degrees of freedom                                 6           6
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  2.182
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.598       0.580
##   Tucker-Lewis Index (TLI)                      -0.205      -0.259
##                                                                   
##   Robust Comparative Fit Index (CFI)                         0.600
##   Robust Tucker-Lewis Index (TLI)                           -0.200
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -5570.865   -5570.865
##   Scaling correction factor                                  2.167
##       for the MLR correction                                      
##   Loglikelihood unrestricted model (H1)      -5399.664   -5399.664
##   Scaling correction factor                                  2.156
##       for the MLR correction                                      
##                                                                   
##   Akaike (AIC)                               11165.730   11165.730
##   Bayesian (BIC)                             11228.100   11228.100
##   Sample-size adjusted Bayesian (SABIC)      11189.981   11189.981
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.357       0.246
##   90 Percent confidence interval - lower         0.326       0.224
##   90 Percent confidence interval - upper         0.389       0.268
##   P-value H_0: RMSEA <= 0.050                    0.000       0.000
##   P-value H_0: RMSEA >= 0.080                    1.000       1.000
##                                                                   
##   Robust RMSEA                                               0.354
##   90 Percent confidence interval - lower                     0.298
##   90 Percent confidence interval - upper                     0.415
##   P-value H_0: Robust RMSEA <= 0.050                         0.000
##   P-value H_0: Robust RMSEA >= 0.080                         1.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.165       0.165
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1              1.000                               0.639    0.622
##     GRI3              0.857    0.097    8.800    0.000    0.548    0.611
##     GRI5              0.993    0.106    9.357    0.000    0.635    0.651
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GRI3 ~                                                                
##     Student           0.435    0.096    4.545    0.000    0.435    0.151
##   GAMBLING ~                                                            
##     Student           0.000                               0.000    0.000
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1              1.823    0.028   64.871    0.000    1.823    1.775
##    .GRI3              1.160    0.087   13.329    0.000    1.160    1.295
##    .GRI5              1.593    0.027   59.749    0.000    1.593    1.635
##     Student           0.892    0.008  105.162    0.000    0.892    2.877
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1              0.647    0.072    8.931    0.000    0.647    0.613
##    .GRI3              0.485    0.048   10.064    0.000    0.485    0.604
##    .GRI5              0.547    0.056    9.754    0.000    0.547    0.576
##    .GAMBLING          0.408    0.062    6.582    0.000    1.000    1.000
##     Student           0.096    0.007   14.450    0.000    0.096    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.387
##     GRI3              0.396
##     GRI5              0.424
##     GAMBLING         -0.000
#MODEL 04a: Model 03b with Standardized Factor -------------------------------
model04a.syntax = "
  GRI3 ~ Student
  GAMBLING =~ GRI1 + GRI3 + GRI5
  GAMBLING ~ 0*Student
"

model04a.fit = sem(model04a.syntax, data=data01, estimator = "MLR", mimic="Mplus", fixed.x=FALSE, std.lv = TRUE)
## Warning: lavaan->lav_model_vcov():  
##    The variance-covariance matrix of the estimated parameters (vcov) does not 
##    appear to be positive definite! The smallest eigenvalue (= -2.261016e-19) 
##    is smaller than zero. This may be a symptom that the model is not 
##    identified.
summary(model04a.fit, fit.measures=TRUE, rsquare=TRUE, standardized=TRUE)
## lavaan 0.6-19 ended normally after 20 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        12
## 
##   Number of observations                          1336
##   Number of missing patterns                         1
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                               342.402     163.776
##   Degrees of freedom                                 2           2
##   P-value (Chi-square)                           0.000       0.000
##   Scaling correction factor                                  2.091
##     Yuan-Bentler correction (Mplus variant)                       
## 
## Model Test Baseline Model:
## 
##   Test statistic                               853.812     391.369
##   Degrees of freedom                                 6           6
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  2.182
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.598       0.580
##   Tucker-Lewis Index (TLI)                      -0.205      -0.259
##                                                                   
##   Robust Comparative Fit Index (CFI)                         0.600
##   Robust Tucker-Lewis Index (TLI)                           -0.200
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -5570.865   -5570.865
##   Scaling correction factor                                  2.167
##       for the MLR correction                                      
##   Loglikelihood unrestricted model (H1)      -5399.664   -5399.664
##   Scaling correction factor                                  2.156
##       for the MLR correction                                      
##                                                                   
##   Akaike (AIC)                               11165.730   11165.730
##   Bayesian (BIC)                             11228.100   11228.100
##   Sample-size adjusted Bayesian (SABIC)      11189.981   11189.981
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.357       0.246
##   90 Percent confidence interval - lower         0.326       0.224
##   90 Percent confidence interval - upper         0.389       0.268
##   P-value H_0: RMSEA <= 0.050                    0.000       0.000
##   P-value H_0: RMSEA >= 0.080                    1.000       1.000
##                                                                   
##   Robust RMSEA                                               0.354
##   90 Percent confidence interval - lower                     0.298
##   90 Percent confidence interval - upper                     0.415
##   P-value H_0: Robust RMSEA <= 0.050                         0.000
##   P-value H_0: Robust RMSEA >= 0.080                         1.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.165       0.165
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1              0.639    0.049   13.164    0.000    0.639    0.622
##     GRI3              0.548    0.047   11.544    0.000    0.548    0.611
##     GRI5              0.635    0.049   12.869    0.000    0.635    0.651
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GRI3 ~                                                                
##     Student           0.435    0.096    4.545    0.000    0.435    0.151
##   GAMBLING ~                                                            
##     Student           0.000                               0.000    0.000
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1              1.823    0.028   64.871    0.000    1.823    1.775
##    .GRI3              1.160    0.087   13.329    0.000    1.160    1.295
##    .GRI5              1.593    0.027   59.749    0.000    1.593    1.635
##     Student           0.892    0.008  105.162    0.000    0.892    2.877
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1              0.647    0.072    8.931    0.000    0.647    0.613
##    .GRI3              0.485    0.048   10.064    0.000    0.485    0.604
##    .GRI5              0.547    0.056    9.754    0.000    0.547    0.576
##    .GAMBLING          1.000                               1.000    1.000
##     Student           0.096    0.007   14.450    0.000    0.096    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.387
##     GRI3              0.396
##     GRI5              0.424
##     GAMBLING          0.000
#MODEL 04b: Model 03a with Standardized Gambling Factor  -------------------------------
model04b.syntax = "
  GRI3 ~ Student
  GAMBLING =~ GRI1 + GRI3 + GRI5
  GAMBLING ~ Student
"

model04b.fit = sem(model04b.syntax, data=data01, estimator = "MLR", mimic="Mplus", fixed.x=FALSE, std.lv = TRUE)
## Warning: lavaan->lav_model_vcov():  
##    The variance-covariance matrix of the estimated parameters (vcov) does not 
##    appear to be positive definite! The smallest eigenvalue (= -2.011605e-19) 
##    is smaller than zero. This may be a symptom that the model is not 
##    identified.
summary(model04b.fit, fit.measures=TRUE, rsquare=TRUE, standardized=TRUE)
## lavaan 0.6-19 ended normally after 37 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        13
## 
##   Number of observations                          1336
##   Number of missing patterns                         1
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                                 0.014       0.008
##   Degrees of freedom                                 1           1
##   P-value (Chi-square)                           0.906       0.929
##   Scaling correction factor                                  1.765
##     Yuan-Bentler correction (Mplus variant)                       
## 
## Model Test Baseline Model:
## 
##   Test statistic                               853.812     391.369
##   Degrees of freedom                                 6           6
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  2.182
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000       1.000
##   Tucker-Lewis Index (TLI)                       1.007       1.015
##                                                                   
##   Robust Comparative Fit Index (CFI)                         1.000
##   Robust Tucker-Lewis Index (TLI)                            1.012
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -5399.671   -5399.671
##   Scaling correction factor                                  2.186
##       for the MLR correction                                      
##   Loglikelihood unrestricted model (H1)      -5399.664   -5399.664
##   Scaling correction factor                                  2.156
##       for the MLR correction                                      
##                                                                   
##   Akaike (AIC)                               10825.342   10825.342
##   Bayesian (BIC)                             10892.909   10892.909
##   Sample-size adjusted Bayesian (SABIC)      10851.613   10851.613
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000       0.000
##   90 Percent confidence interval - lower         0.000       0.000
##   90 Percent confidence interval - upper         0.031       0.000
##   P-value H_0: RMSEA <= 0.050                    0.982       0.999
##   P-value H_0: RMSEA >= 0.080                    0.001       0.000
##                                                                   
##   Robust RMSEA                                               0.000
##   90 Percent confidence interval - lower                     0.000
##   90 Percent confidence interval - upper                     0.030
##   P-value H_0: Robust RMSEA <= 0.050                         0.973
##   P-value H_0: Robust RMSEA >= 0.080                         0.006
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.001       0.001
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1              0.500    0.039   12.897    0.000    0.641    0.624
##     GRI3              0.543    0.049   11.135    0.000    0.697    0.805
##     GRI5              0.494    0.039   12.712    0.000    0.633    0.649
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GRI3 ~                                                                
##     Student           1.203    0.176    6.827    0.000    1.203    0.431
##   GAMBLING ~                                                            
##     Student          -2.587    0.261   -9.909    0.000   -2.018   -0.626
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1              2.976    0.111   26.768    0.000    2.976    2.898
##    .GRI3              1.729    0.094   18.435    0.000    1.729    1.998
##    .GRI5              2.732    0.123   22.176    0.000    2.732    2.804
##     Student           0.892    0.008  105.162    0.000    0.892    2.877
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1              0.644    0.067    9.603    0.000    0.644    0.611
##    .GRI3              0.450    0.052    8.603    0.000    0.450    0.601
##    .GRI5              0.549    0.049   11.105    0.000    0.549    0.578
##    .GAMBLING          1.000                               0.608    0.608
##     Student           0.096    0.007   14.450    0.000    0.096    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.389
##     GRI3              0.399
##     GRI5              0.422
##     GAMBLING          0.392
#MODEL 05: ANALYSIS WITH SUM SCORE INSTEAD OF FACTOR ----------------------------

#creating sum score for GAMBLING 3-item Survey
data02 = data01
data02$GRI135sum = data02$GRI1 + data02$GRI3 + data02$GRI5


model05.syntax = "
  GRI135sum ~ Student 
"
model05.fit = sem(model05.syntax, data=data02, estimator = "MLR", mimic="Mplus", fixed.x=FALSE)
## Warning: lavaan->lav_model_vcov():  
##    The variance-covariance matrix of the estimated parameters (vcov) does not 
##    appear to be positive definite! The smallest eigenvalue (= -7.093836e-20) 
##    is smaller than zero. This may be a symptom that the model is not 
##    identified.
summary(model05.fit, fit.measures=TRUE, rsquare=TRUE, standardized=TRUE)
## lavaan 0.6-19 ended normally after 2 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         5
## 
##   Number of observations                          1336
##   Number of missing patterns                         1
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                                 0.000       0.000
##   Degrees of freedom                                 0           0
## 
## Model Test Baseline Model:
## 
##   Test statistic                               226.437     114.487
##   Degrees of freedom                                 1           1
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.978
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000       1.000
##   Tucker-Lewis Index (TLI)                       1.000       1.000
##                                                                   
##   Robust Comparative Fit Index (CFI)                         1.000
##   Robust Tucker-Lewis Index (TLI)                            1.000
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -3153.864   -3153.864
##   Loglikelihood unrestricted model (H1)      -3153.864   -3153.864
##                                                                   
##   Akaike (AIC)                                6317.728    6317.728
##   Bayesian (BIC)                              6343.715    6343.715
##   Sample-size adjusted Bayesian (SABIC)       6327.832    6327.832
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000          NA
##   90 Percent confidence interval - lower         0.000          NA
##   90 Percent confidence interval - upper         0.000          NA
##   P-value H_0: RMSEA <= 0.050                       NA          NA
##   P-value H_0: RMSEA >= 0.080                       NA          NA
##                                                                   
##   Robust RMSEA                                               0.000
##   90 Percent confidence interval - lower                     0.000
##   90 Percent confidence interval - upper                     0.000
##   P-value H_0: Robust RMSEA <= 0.050                            NA
##   P-value H_0: Robust RMSEA >= 0.080                            NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.000       0.000
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GRI135sum ~                                                           
##     Student          -2.773    0.260  -10.673    0.000   -2.773   -0.395
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI135sum         7.437    0.254   29.242    0.000    7.437    3.415
##     Student           0.892    0.008  105.162    0.000    0.892    2.877
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI135sum         4.004    0.240   16.664    0.000    4.004    0.844
##     Student           0.096    0.007   14.450    0.000    0.096    1.000
## 
## R-Square:
##                    Estimate
##     GRI135sum         0.156
#getting correct standardizedSolution value (nox as Student is a coded variable):
standardizedSolution(model05.fit, type="std.nox")[1,]
##         lhs op     rhs est.std    se       z pvalue ci.lower ci.upper
## 1 GRI135sum  ~ Student  -1.273 0.105 -12.106      0   -1.479   -1.067
#getting similar standardizedSolution values for model 03 (best fit--but with predictor of GRI3)
standardizedSolution(model03a.fit, type="std.nox")[5,]
##        lhs op  rhs est.std    se      z pvalue ci.lower ci.upper
## 5 GAMBLING =~ GRI5   0.654 0.063 10.435      0    0.531    0.777
#getting similar standardizedSolution values for model 02 (terrible fit, but similar in in that no predictor of GRI3 is part of model)
standardizedSolution(model02.fit, type="std.nox")[4,]
##        lhs op     rhs est.std    se       z pvalue ci.lower ci.upper
## 4 GAMBLING  ~ Student  -1.745 0.135 -12.882      0   -2.011    -1.48
#Model 06: Calculation of Alpha Reliablity with Tau-Equivalent CFA Model -----------------------------
model06.syntax = "
  GAMBLING =~ (loading)*GRI1 + (loading)*GRI3 + (loading)*GRI5
  GRI1 ~~ (U1)*GRI1
  GRI3 ~~ (U3)*GRI3
  GRI5 ~~ (U5)*GRI5

  GCalpha := (3*loading*loading)/( (3*loading*loading) + (U1 + U3 + U5))
"
model06.fit = sem(model06.syntax, data=data02, estimator = "MLR", mimic="Mplus", fixed.x=FALSE, std.lv = TRUE)
summary(model06.fit, fit.measures=TRUE, rsquare=TRUE, standardized=TRUE)
## lavaan 0.6-19 ended normally after 10 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         9
##   Number of equality constraints                     2
## 
##   Number of observations                          1336
##   Number of missing patterns                         1
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                                18.897       8.482
##   Degrees of freedom                                 2           2
##   P-value (Chi-square)                           0.000       0.014
##   Scaling correction factor                                  2.228
##     Yuan-Bentler correction (Mplus variant)                       
## 
## Model Test Baseline Model:
## 
##   Test statistic                               480.988     199.641
##   Degrees of freedom                                 3           3
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  2.409
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.965       0.967
##   Tucker-Lewis Index (TLI)                       0.947       0.951
##                                                                   
##   Robust Comparative Fit Index (CFI)                         0.970
##   Robust Tucker-Lewis Index (TLI)                            0.954
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -5264.058   -5264.058
##   Scaling correction factor                                  1.741
##       for the MLR correction                                      
##   Loglikelihood unrestricted model (H1)      -5254.609   -5254.609
##   Scaling correction factor                                  2.236
##       for the MLR correction                                      
##                                                                   
##   Akaike (AIC)                               10542.115   10542.115
##   Bayesian (BIC)                             10578.497   10578.497
##   Sample-size adjusted Bayesian (SABIC)      10556.262   10556.262
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.080       0.049
##   90 Percent confidence interval - lower         0.049       0.028
##   90 Percent confidence interval - upper         0.114       0.073
##   P-value H_0: RMSEA <= 0.050                    0.053       0.475
##   P-value H_0: RMSEA >= 0.080                    0.537       0.015
##                                                                   
##   Robust RMSEA                                               0.074
##   90 Percent confidence interval - lower                     0.028
##   90 Percent confidence interval - upper                     0.128
##   P-value H_0: Robust RMSEA <= 0.050                         0.170
##   P-value H_0: Robust RMSEA >= 0.080                         0.485
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.040       0.040
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   GAMBLING =~                                                           
##     GRI1    (ldng)    0.567    0.027   20.821    0.000    0.567    0.560
##     GRI3    (ldng)    0.567    0.027   20.821    0.000    0.567    0.638
##     GRI5    (ldng)    0.567    0.027   20.821    0.000    0.567    0.589
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1              1.823    0.028   64.871    0.000    1.823    1.801
##    .GRI3              1.548    0.024   65.365    0.000    1.548    1.743
##    .GRI5              1.593    0.027   59.749    0.000    1.593    1.656
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .GRI1      (U1)    0.703    0.063   11.116    0.000    0.703    0.686
##    .GRI3      (U3)    0.468    0.043   10.903    0.000    0.468    0.593
##    .GRI5      (U5)    0.603    0.054   11.148    0.000    0.603    0.653
##     GAMBLING          1.000                               1.000    1.000
## 
## R-Square:
##                    Estimate
##     GRI1              0.314
##     GRI3              0.407
##     GRI5              0.347
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     GCalpha           0.352    0.026   13.628    0.000    0.352    0.328