setwd("~/R/DataHouse/SEM class/ANALYSIS")

library(foreign)
HINTS <-read.spss ("HINTS.sav", use.value.label=FALSE, to.data.frame=TRUE)
## Warning in read.spss("HINTS.sav", use.value.label = FALSE, to.data.frame
## = TRUE): HINTS.sav: Unrecognized record type 7, subtype 18 encountered in
## system file
hints.cfa2f <- 'mood =~ Angry + Anxious + Sad 
                 healthy=~ 1*Health
                  Health ~~ .1*Health 
                mood ~~ healthy'



mi <- measurementInvariance(hints.cfa2f, data=HINTS, group = "Gender")
## 
## Measurement invariance models:
## 
## Model 1 : fit.configural
## Model 2 : fit.loadings
## Model 3 : fit.intercepts
## Model 4 : fit.means
## 
## Chi Square Difference Test
## 
##                Df   AIC   BIC   Chisq Chisq diff Df diff Pr(>Chisq)    
## fit.configural  4 11639 11763  7.7517                                  
## fit.loadings    6 11636 11750  8.6941     0.9424       2  0.6242571    
## fit.intercepts  8 11649 11752 25.4482    16.7542       2  0.0002301 ***
## fit.means      10 11654 11747 35.0296     9.5814       2  0.0083067 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Fit measures:
## 
##                  cfi rmsea cfi.delta rmsea.delta
## fit.configural 0.996 0.038        NA          NA
## fit.loadings   0.997 0.027     0.001       0.012
## fit.intercepts 0.982 0.058     0.015       0.032
## fit.means      0.975 0.063     0.008       0.004
print("Configural")
## [1] "Configural"
summary(mi$fit.configural,fit.measures=TRUE,standardized=TRUE)
## lavaan (0.5-22) converged normally after  36 iterations
## 
##   Number of observations per group         
##   0                                                751
##   1                                                527
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic                7.752
##   Degrees of freedom                                 4
##   P-value (Chi-square)                           0.101
## 
## Chi-square for each group:
## 
##   0                                              4.450
##   1                                              3.302
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic             1004.196
##   Degrees of freedom                                12
##   P-value                                        0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    0.996
##   Tucker-Lewis Index (TLI)                       0.989
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -5795.579
##   Loglikelihood unrestricted model (H1)      -5791.703
## 
##   Number of free parameters                         24
##   Akaike (AIC)                               11639.158
##   Bayesian (BIC)                             11762.831
##   Sample-size adjusted Bayesian (BIC)        11686.595
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.038
##   90 Percent Confidence Interval          0.000  0.079
##   P-value RMSEA <= 0.05                          0.626
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.014
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
## 
## Group 1 [0]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   mood =~                                                               
##     Angry             1.000                               0.489    0.692
##     Anxious           1.322    0.089   14.867    0.000    0.646    0.682
##     Sad               1.364    0.090   15.182    0.000    0.667    0.801
##   healthy =~                                                            
##     Health            1.000                               0.862    0.939
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   mood ~~                                                               
##     healthy          -0.156    0.021   -7.612    0.000   -0.371   -0.371
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Angry             1.137    0.026   44.108    0.000    1.137    1.610
##    .Anxious           1.362    0.035   39.420    0.000    1.362    1.438
##    .Sad               1.153    0.030   37.926    0.000    1.153    1.384
##    .Health            2.558    0.034   76.339    0.000    2.558    2.786
##     mood              0.000                               0.000    0.000
##     healthy           0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Health            0.100                               0.100    0.119
##    .Angry             0.260    0.019   13.887    0.000    0.260    0.521
##    .Anxious           0.479    0.034   14.218    0.000    0.479    0.534
##    .Sad               0.249    0.027    9.237    0.000    0.249    0.359
##     mood              0.239    0.026    9.349    0.000    1.000    1.000
##     healthy           0.743    0.044   17.080    0.000    1.000    1.000
## 
## 
## Group 2 [1]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   mood =~                                                               
##     Angry             1.000                               0.452    0.646
##     Anxious           1.190    0.119   10.025    0.000    0.538    0.597
##     Sad               1.354    0.135   10.060    0.000    0.612    0.770
##   healthy =~                                                            
##     Health            1.000                               0.779    0.927
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   mood ~~                                                               
##     healthy          -0.115    0.021   -5.385    0.000   -0.327   -0.327
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Angry             1.152    0.030   37.801    0.000    1.152    1.647
##    .Anxious           1.235    0.039   31.511    0.000    1.235    1.373
##    .Sad               0.968    0.035   27.971    0.000    0.968    1.218
##    .Health            2.554    0.037   69.767    0.000    2.554    3.039
##     mood              0.000                               0.000    0.000
##     healthy           0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Health            0.100                               0.100    0.142
##    .Angry             0.285    0.025   11.311    0.000    0.285    0.583
##    .Anxious           0.521    0.041   12.603    0.000    0.521    0.643
##    .Sad               0.257    0.036    7.108    0.000    0.257    0.407
##     mood              0.204    0.030    6.708    0.000    1.000    1.000
##     healthy           0.606    0.044   13.934    0.000    1.000    1.000
print("Loadings")
## [1] "Loadings"
summary(mi$fit.loadings,fit.measures=TRUE,standardized=TRUE)
## lavaan (0.5-22) converged normally after  25 iterations
## 
##   Number of observations per group         
##   0                                                751
##   1                                                527
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic                8.694
##   Degrees of freedom                                 6
##   P-value (Chi-square)                           0.192
## 
## Chi-square for each group:
## 
##   0                                              4.761
##   1                                              3.933
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic             1004.196
##   Degrees of freedom                                12
##   P-value                                        0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    0.997
##   Tucker-Lewis Index (TLI)                       0.995
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -5796.050
##   Loglikelihood unrestricted model (H1)      -5791.703
## 
##   Number of free parameters                         22
##   Akaike (AIC)                               11636.100
##   Bayesian (BIC)                             11749.467
##   Sample-size adjusted Bayesian (BIC)        11679.584
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.027
##   90 Percent Confidence Interval          0.000  0.062
##   P-value RMSEA <= 0.05                          0.838
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.016
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
## 
## Group 1 [0]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   mood =~                                                               
##     Angry             1.000                               0.494    0.697
##     Anxious (.p2.)    1.276    0.071   17.927    0.000    0.631    0.671
##     Sad     (.p3.)    1.359    0.074   18.243    0.000    0.672    0.805
##   healthy =~                                                            
##     Health            1.000                               0.862    0.939
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   mood ~~                                                               
##     healthy          -0.158    0.020   -7.729    0.000   -0.370   -0.370
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Angry             1.137    0.026   43.943    0.000    1.137    1.603
##    .Anxious           1.362    0.034   39.687    0.000    1.362    1.448
##    .Sad               1.153    0.030   37.857    0.000    1.153    1.381
##    .Health            2.558    0.034   76.339    0.000    2.558    2.786
##     mood              0.000                               0.000    0.000
##     healthy           0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Health            0.100                               0.100    0.119
##    .Angry             0.259    0.018   14.122    0.000    0.259    0.514
##    .Anxious           0.487    0.033   14.945    0.000    0.487    0.550
##    .Sad               0.246    0.026    9.570    0.000    0.246    0.352
##     mood              0.244    0.023   10.437    0.000    1.000    1.000
##     healthy           0.743    0.044   17.080    0.000    1.000    1.000
## 
## 
## Group 2 [1]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   mood =~                                                               
##     Angry             1.000                               0.443    0.637
##     Anxious (.p2.)    1.276    0.071   17.927    0.000    0.565    0.621
##     Sad     (.p3.)    1.359    0.074   18.243    0.000    0.602    0.760
##   healthy =~                                                            
##     Health            1.000                               0.779    0.927
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   mood ~~                                                               
##     healthy          -0.112    0.020   -5.606    0.000   -0.326   -0.326
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Angry             1.152    0.030   38.032    0.000    1.152    1.657
##    .Anxious           1.235    0.040   31.167    0.000    1.235    1.358
##    .Sad               0.968    0.034   28.057    0.000    0.968    1.222
##    .Health            2.554    0.037   69.767    0.000    2.554    3.039
##     mood              0.000                               0.000    0.000
##     healthy           0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Health            0.100                               0.100    0.142
##    .Angry             0.287    0.023   12.620    0.000    0.287    0.594
##    .Anxious           0.508    0.039   12.938    0.000    0.508    0.614
##    .Sad               0.265    0.029    8.991    0.000    0.265    0.422
##     mood              0.196    0.022    9.031    0.000    1.000    1.000
##     healthy           0.606    0.044   13.934    0.000    1.000    1.000
print("Intercepts")
## [1] "Intercepts"
summary(mi$fit.intercepts,fit.measures=TRUE,standardized=TRUE)
## lavaan (0.5-22) converged normally after  31 iterations
## 
##   Number of observations per group         
##   0                                                751
##   1                                                527
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic               25.448
##   Degrees of freedom                                 8
##   P-value (Chi-square)                           0.001
## 
## Chi-square for each group:
## 
##   0                                             11.167
##   1                                             14.282
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic             1004.196
##   Degrees of freedom                                12
##   P-value                                        0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    0.982
##   Tucker-Lewis Index (TLI)                       0.974
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -5804.427
##   Loglikelihood unrestricted model (H1)      -5791.703
## 
##   Number of free parameters                         20
##   Akaike (AIC)                               11648.854
##   Bayesian (BIC)                             11751.915
##   Sample-size adjusted Bayesian (BIC)        11688.385
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.058
##   90 Percent Confidence Interval          0.034  0.085
##   P-value RMSEA <= 0.05                          0.259
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.027
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
## 
## Group 1 [0]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   mood =~                                                               
##     Angry             1.000                               0.484    0.686
##     Anxious (.p2.)    1.304    0.073   17.863    0.000    0.632    0.671
##     Sad     (.p3.)    1.401    0.077   18.132    0.000    0.679    0.810
##   healthy =~                                                            
##     Health            1.000                               0.862    0.939
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   mood ~~                                                               
##     healthy          -0.154    0.020   -7.707    0.000   -0.369   -0.369
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Angry   (.12.)    1.177    0.024   49.586    0.000    1.177    1.666
##    .Anxious (.13.)    1.358    0.031   43.312    0.000    1.358    1.443
##    .Sad     (.14.)    1.129    0.030   38.155    0.000    1.129    1.348
##    .Health  (.15.)    2.558    0.034   76.339    0.000    2.558    2.786
##     mood              0.000                               0.000    0.000
##     healthy           0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Health            0.100                               0.100    0.119
##    .Angry             0.265    0.018   14.458    0.000    0.265    0.530
##    .Anxious           0.487    0.033   14.897    0.000    0.487    0.549
##    .Sad               0.241    0.026    9.252    0.000    0.241    0.344
##     mood              0.235    0.023   10.296    0.000    1.000    1.000
##     healthy           0.743    0.044   17.080    0.000    1.000    1.000
## 
## 
## Group 2 [1]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   mood =~                                                               
##     Angry             1.000                               0.433    0.623
##     Anxious (.p2.)    1.304    0.073   17.863    0.000    0.565    0.621
##     Sad     (.p3.)    1.401    0.077   18.132    0.000    0.607    0.764
##   healthy =~                                                            
##     Health            1.000                               0.779    0.927
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   mood ~~                                                               
##     healthy          -0.110    0.020   -5.615    0.000   -0.327   -0.327
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Angry   (.12.)    1.177    0.024   49.586    0.000    1.177    1.691
##    .Anxious (.13.)    1.358    0.031   43.312    0.000    1.358    1.492
##    .Sad     (.14.)    1.129    0.030   38.155    0.000    1.129    1.422
##    .Health  (.15.)    2.558    0.034   76.339    0.000    2.558    3.044
##     mood             -0.089    0.030   -2.962    0.003   -0.205   -0.205
##     healthy          -0.004    0.050   -0.077    0.938   -0.005   -0.005
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Health            0.100                               0.100    0.142
##    .Angry             0.297    0.023   12.885    0.000    0.297    0.612
##    .Anxious           0.508    0.039   12.908    0.000    0.508    0.614
##    .Sad               0.262    0.030    8.781    0.000    0.262    0.416
##     mood              0.188    0.021    8.920    0.000    1.000    1.000
##     healthy           0.606    0.044   13.934    0.000    1.000    1.000
print("means+Intercepts")
## [1] "means+Intercepts"
summary(mi$fit.means,fit.measures=TRUE,standardized=TRUE)
## lavaan (0.5-22) converged normally after  28 iterations
## 
##   Number of observations per group         
##   0                                                751
##   1                                                527
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic               35.030
##   Degrees of freedom                                10
##   P-value (Chi-square)                           0.000
## 
## Chi-square for each group:
## 
##   0                                             15.305
##   1                                             19.725
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic             1004.196
##   Degrees of freedom                                12
##   P-value                                        0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    0.975
##   Tucker-Lewis Index (TLI)                       0.970
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -5809.218
##   Loglikelihood unrestricted model (H1)      -5791.703
## 
##   Number of free parameters                         18
##   Akaike (AIC)                               11654.436
##   Bayesian (BIC)                             11747.190
##   Sample-size adjusted Bayesian (BIC)        11690.014
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.063
##   90 Percent Confidence Interval          0.041  0.086
##   P-value RMSEA <= 0.05                          0.159
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.039
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
## 
## Group 1 [0]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   mood =~                                                               
##     Angry             1.000                               0.491    0.692
##     Anxious (.p2.)    1.295    0.072   17.893    0.000    0.636    0.674
##     Sad     (.p3.)    1.376    0.076   18.156    0.000    0.675    0.805
##   healthy =~                                                            
##     Health            1.000                               0.862    0.939
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   mood ~~                                                               
##     healthy          -0.156    0.020   -7.711    0.000   -0.369   -0.369
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Angry   (.12.)    1.138    0.020   57.949    0.000    1.138    1.606
##    .Anxious (.13.)    1.306    0.026   50.298    0.000    1.306    1.385
##    .Sad     (.14.)    1.074    0.023   46.824    0.000    1.074    1.281
##    .Health  (.15.)    2.558    0.025  103.520    0.000    2.558    2.786
##     mood              0.000                               0.000    0.000
##     healthy           0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Health            0.100                               0.100    0.119
##    .Angry             0.261    0.018   14.263    0.000    0.261    0.520
##    .Anxious           0.485    0.033   14.837    0.000    0.485    0.546
##    .Sad               0.247    0.026    9.529    0.000    0.247    0.352
##     mood              0.241    0.023   10.367    0.000    1.000    1.000
##     healthy           0.743    0.044   17.080    0.000    1.000    1.000
## 
## 
## Group 2 [1]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   mood =~                                                               
##     Angry             1.000                               0.440    0.632
##     Anxious (.p2.)    1.295    0.072   17.893    0.000    0.570    0.625
##     Sad     (.p3.)    1.376    0.076   18.156    0.000    0.606    0.758
##   healthy =~                                                            
##     Health            1.000                               0.779    0.927
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   mood ~~                                                               
##     healthy          -0.111    0.020   -5.567    0.000   -0.324   -0.324
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Angry   (.12.)    1.138    0.020   57.949    0.000    1.138    1.634
##    .Anxious (.13.)    1.306    0.026   50.298    0.000    1.306    1.433
##    .Sad     (.14.)    1.074    0.023   46.824    0.000    1.074    1.345
##    .Health  (.15.)    2.558    0.025  103.520    0.000    2.558    3.044
##     mood              0.000                               0.000    0.000
##     healthy           0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Health            0.100                               0.100    0.142
##    .Angry             0.291    0.023   12.711    0.000    0.291    0.601
##    .Anxious           0.506    0.039   12.841    0.000    0.506    0.609
##    .Sad               0.271    0.030    9.026    0.000    0.271    0.425
##     mood              0.194    0.022    8.977    0.000    1.000    1.000
##     healthy           0.606    0.044   13.934    0.000    1.000    1.000