pacman::p_load(lavaan, psych, GPArotation, semTools)
#data
Dat <- read.csv("C:/Users/Liz/Desktop/NTNU/test/CFAInClassPracticeForMT/inClassPracticeDat.csv")
str(Dat)
## 'data.frame': 300 obs. of 7 variables:
## $ friends : num -1.3104 -0.0926 1.675 0.5052 -0.4784 ...
## $ familyMember: num -1.495 -0.892 0.375 0.597 -1.163 ...
## $ Colleague : num -0.887 0.442 -1.398 -3.597 -0.835 ...
## $ Boss : num -0.924 0.342 -0.277 -2.931 -1.264 ...
## $ happy : int 1 4 4 3 4 3 4 4 4 3 ...
## $ grateful : int 1 4 4 3 4 3 4 4 4 3 ...
## $ satisfied : int 1 3 4 3 4 1 3 4 1 2 ...
CFA
mod1
mod1 <- '
social =~ friends + familyMember + Colleague + Boss
mental =~ happy + grateful + satisfied
social ~~ mental
'
res1 <- cfa(mod1, data= Dat, estimator= "ML", missing= "FIML" )
## Warning in lav_object_post_check(object): lavaan WARNING: some estimated ov
## variances are negative
summary(res1, fit.measures = T)
## lavaan 0.6.16 ended normally after 30 iterations
##
## Estimator ML
## Optimization method NLMINB
## Number of model parameters 22
##
## Number of observations 300
## Number of missing patterns 1
##
## Model Test User Model:
##
## Test statistic 172.502
## Degrees of freedom 13
## P-value (Chi-square) 0.000
##
## Model Test Baseline Model:
##
## Test statistic 957.101
## Degrees of freedom 21
## P-value 0.000
##
## User Model versus Baseline Model:
##
## Comparative Fit Index (CFI) 0.830
## Tucker-Lewis Index (TLI) 0.725
##
## Robust Comparative Fit Index (CFI) 0.830
## Robust Tucker-Lewis Index (TLI) 0.725
##
## Loglikelihood and Information Criteria:
##
## Loglikelihood user model (H0) -3036.290
## Loglikelihood unrestricted model (H1) -2950.039
##
## Akaike (AIC) 6116.580
## Bayesian (BIC) 6198.063
## Sample-size adjusted Bayesian (SABIC) 6128.292
##
## Root Mean Square Error of Approximation:
##
## RMSEA 0.202
## 90 Percent confidence interval - lower 0.176
## 90 Percent confidence interval - upper 0.230
## P-value H_0: RMSEA <= 0.050 0.000
## P-value H_0: RMSEA >= 0.080 1.000
##
## Robust RMSEA 0.202
## 90 Percent confidence interval - lower 0.176
## 90 Percent confidence interval - upper 0.230
## 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.109
##
## Parameter Estimates:
##
## Standard errors Standard
## Information Observed
## Observed information based on Hessian
##
## Latent Variables:
## Estimate Std.Err z-value P(>|z|)
## social =~
## friends 1.000
## familyMember 0.955 0.079 12.023 0.000
## Colleague 0.582 0.086 6.779 0.000
## Boss 0.426 0.071 5.996 0.000
## mental =~
## happy 1.000
## grateful 1.160 0.071 16.262 0.000
## satisfied 0.605 0.060 10.126 0.000
##
## Covariances:
## Estimate Std.Err z-value P(>|z|)
## social ~~
## mental 0.188 0.075 2.522 0.012
##
## Intercepts:
## Estimate Std.Err z-value P(>|z|)
## .friends -0.075 0.075 -1.006 0.314
## .familyMember -0.033 0.073 -0.459 0.646
## .Colleague -0.016 0.078 -0.206 0.836
## .Boss -0.013 0.065 -0.199 0.843
## .happy 2.530 0.071 35.672 0.000
## .grateful 2.557 0.070 36.564 0.000
## .satisfied 2.400 0.069 34.561 0.000
## social 0.000
## mental 0.000
##
## Variances:
## Estimate Std.Err z-value P(>|z|)
## .friends 0.533 0.092 5.796 0.000
## .familyMember 0.549 0.085 6.479 0.000
## .Colleague 1.456 0.130 11.206 0.000
## .Boss 1.058 0.092 11.470 0.000
## .happy 0.407 0.065 6.306 0.000
## .grateful -0.017 0.076 -0.219 0.827
## .satisfied 1.043 0.088 11.852 0.000
## social 1.143 0.153 7.476 0.000
## mental 1.102 0.131 8.415 0.000
modindices(res1, sort = T) #repair
## lhs op rhs mi epc sepc.lv sepc.all sepc.nox
## 45 Colleague ~~ Boss 114.289 0.813 0.813 0.655 0.655
## 34 friends ~~ familyMember 113.649 2.816 2.816 5.206 5.206
## 52 happy ~~ grateful 18.395 3.323 3.323 40.362 40.362
## 29 social =~ satisfied 18.394 0.267 0.285 0.237 0.237
## 35 friends ~~ Colleague 14.512 -0.397 -0.397 -0.450 -0.450
## 36 friends ~~ Boss 11.258 -0.260 -0.260 -0.346 -0.346
## 41 familyMember ~~ Boss 9.794 -0.232 -0.232 -0.305 -0.305
## 40 familyMember ~~ Colleague 8.687 -0.293 -0.293 -0.327 -0.327
## 48 Colleague ~~ satisfied 7.690 0.202 0.202 0.164 0.164
## 51 Boss ~~ satisfied 5.849 0.149 0.149 0.142 0.142
## 32 mental =~ Colleague 5.040 0.155 0.163 0.120 0.120
## 49 Boss ~~ happy 4.277 -0.079 -0.079 -0.120 -0.120
## 31 mental =~ familyMember 3.404 -0.103 -0.108 -0.085 -0.085
## 47 Colleague ~~ grateful 1.712 0.055 0.055 0.353 0.353
## 46 Colleague ~~ happy 1.692 -0.058 -0.058 -0.076 -0.076
## 50 Boss ~~ grateful 1.617 0.045 0.045 0.340 0.340
## 43 familyMember ~~ grateful 1.529 -0.039 -0.039 -0.410 -0.410
## 39 friends ~~ satisfied 1.528 0.068 0.068 0.091 0.091
## 27 social =~ happy 1.104 -0.041 -0.044 -0.036 -0.036
## 54 grateful ~~ satisfied 1.104 -0.189 -0.189 -1.431 -1.431
## 33 mental =~ Boss 0.465 0.040 0.042 0.037 0.037
## 53 happy ~~ satisfied 0.307 -0.080 -0.080 -0.123 -0.123
## 28 social =~ grateful 0.307 -0.024 -0.025 -0.021 -0.021
## 42 familyMember ~~ happy 0.210 0.015 0.015 0.032 0.032
## 30 mental =~ friends 0.206 0.026 0.027 0.021 0.021
## 38 friends ~~ grateful 0.065 -0.008 -0.008 -0.088 -0.088
## 37 friends ~~ happy 0.009 0.003 0.003 0.007 0.007
## 44 familyMember ~~ satisfied 0.002 0.002 0.002 0.003 0.003
mod2
mod2 <- '
social =~ friends + familyMember + Colleague + Boss
mental =~ happy + grateful + satisfied
social ~~ mental
Colleague ~~ Boss
friends ~~ familyMember
happy ~~ grateful
'
res2 <- cfa(mod2, data= Dat, estimator= "ML", missing= "FIML" )
summary(res2, fit.measures = T)
## lavaan 0.6.16 ended normally after 55 iterations
##
## Estimator ML
## Optimization method NLMINB
## Number of model parameters 25
##
## Number of observations 300
## Number of missing patterns 1
##
## Model Test User Model:
##
## Test statistic 9.139
## Degrees of freedom 10
## P-value (Chi-square) 0.519
##
## Model Test Baseline Model:
##
## Test statistic 957.101
## Degrees of freedom 21
## P-value 0.000
##
## User Model versus Baseline Model:
##
## Comparative Fit Index (CFI) 1.000
## Tucker-Lewis Index (TLI) 1.002
##
## Robust Comparative Fit Index (CFI) 1.000
## Robust Tucker-Lewis Index (TLI) 1.002
##
## Loglikelihood and Information Criteria:
##
## Loglikelihood user model (H0) -2954.609
## Loglikelihood unrestricted model (H1) -2950.039
##
## Akaike (AIC) 5959.217
## Bayesian (BIC) 6051.812
## Sample-size adjusted Bayesian (SABIC) 5972.527
##
## Root Mean Square Error of Approximation:
##
## RMSEA 0.000
## 90 Percent confidence interval - lower 0.000
## 90 Percent confidence interval - upper 0.059
## P-value H_0: RMSEA <= 0.050 0.901
## P-value H_0: RMSEA >= 0.080 0.005
##
## Robust RMSEA 0.000
## 90 Percent confidence interval - lower 0.000
## 90 Percent confidence interval - upper 0.059
## P-value H_0: Robust RMSEA <= 0.050 0.901
## P-value H_0: Robust RMSEA >= 0.080 0.005
##
## Standardized Root Mean Square Residual:
##
## SRMR 0.023
##
## Parameter Estimates:
##
## Standard errors Standard
## Information Observed
## Observed information based on Hessian
##
## Latent Variables:
## Estimate Std.Err z-value P(>|z|)
## social =~
## friends 1.000
## familyMember 0.905 0.116 7.778 0.000
## Colleague 1.299 0.327 3.975 0.000
## Boss 0.881 0.234 3.762 0.000
## mental =~
## happy 1.000
## grateful 1.174 0.079 14.891 0.000
## satisfied 2.030 0.514 3.951 0.000
##
## Covariances:
## Estimate Std.Err z-value P(>|z|)
## social ~~
## mental 0.186 0.064 2.895 0.004
## .Colleague ~~
## .Boss 0.484 0.141 3.431 0.001
## .friends ~~
## .familyMember 0.719 0.126 5.715 0.000
## .happy ~~
## .grateful 0.897 0.119 7.512 0.000
##
## Intercepts:
## Estimate Std.Err z-value P(>|z|)
## .friends -0.075 0.075 -1.006 0.314
## .familyMember -0.033 0.073 -0.459 0.646
## .Colleague -0.016 0.078 -0.206 0.836
## .Boss -0.013 0.065 -0.199 0.843
## .happy 2.530 0.071 35.672 0.000
## .grateful 2.557 0.070 36.564 0.000
## .satisfied 2.400 0.069 34.561 0.000
## social 0.000
## mental 0.000
##
## Variances:
## Estimate Std.Err z-value P(>|z|)
## .friends 1.220 0.151 8.053 0.000
## .familyMember 1.219 0.136 8.972 0.000
## .Colleague 1.074 0.205 5.242 0.000
## .Boss 0.912 0.124 7.346 0.000
## .happy 1.185 0.120 9.855 0.000
## .grateful 1.019 0.135 7.544 0.000
## .satisfied 0.109 0.320 0.340 0.734
## social 0.456 0.148 3.086 0.002
## mental 0.325 0.105 3.106 0.002
mod2 - ordinal
# happy,grateful,and satisfied are measured on Likert type scale
# (i.e., these data are ordinal)
res2 <- cfa(mod2, data= Dat, estimator= "WLSMv", ordered = c("happy","grateful","satisfied") )
## Warning in lav_object_post_check(object): lavaan WARNING: some estimated ov
## variances are negative
summary(res2, fit.measures = T) # 4點量表只有3個間隔...所以t1 t2 t3
## lavaan 0.6.16 ended normally after 60 iterations
##
## Estimator DWLS
## Optimization method NLMINB
## Number of model parameters 28
##
## Number of observations 300
##
## Model Test User Model:
## Standard Scaled
## Test Statistic 3.413 9.576
## Degrees of freedom 10 10
## P-value (Chi-square) 0.970 0.478
## Scaling correction factor 0.503
## Shift parameter 2.791
## simple second-order correction
##
## Model Test Baseline Model:
##
## Test statistic 4701.242 2904.199
## Degrees of freedom 21 21
## P-value 0.000 0.000
## Scaling correction factor 1.623
##
## User Model versus Baseline Model:
##
## Comparative Fit Index (CFI) 1.000 1.000
## Tucker-Lewis Index (TLI) 1.003 1.000
##
## Robust Comparative Fit Index (CFI) NA
## Robust Tucker-Lewis Index (TLI) NA
##
## 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.000 0.061
## P-value H_0: RMSEA <= 0.050 0.998 0.884
## P-value H_0: RMSEA >= 0.080 0.000 0.006
##
## Robust RMSEA NA
## 90 Percent confidence interval - lower NA
## 90 Percent confidence interval - upper NA
## 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.022 0.022
##
## Parameter Estimates:
##
## Standard errors Robust.sem
## Information Expected
## Information saturated (h1) model Unstructured
##
## Latent Variables:
## Estimate Std.Err z-value P(>|z|)
## social =~
## friends 1.000
## familyMember 0.866 0.108 8.027 0.000
## Colleague 1.258 0.363 3.462 0.001
## Boss 0.810 0.257 3.153 0.002
## mental =~
## happy 1.000
## grateful 1.208 0.089 13.520 0.000
## satisfied 2.628 0.903 2.911 0.004
##
## Covariances:
## Estimate Std.Err z-value P(>|z|)
## social ~~
## mental 0.136 0.056 2.413 0.016
## .Colleague ~~
## .Boss 0.501 0.175 2.858 0.004
## .friends ~~
## .familyMember 0.702 0.138 5.096 0.000
## .happy ~~
## .grateful 0.690 0.083 8.325 0.000
##
## Intercepts:
## Estimate Std.Err z-value P(>|z|)
## .friends -0.075 0.075 -0.997 0.319
## .familyMember -0.033 0.073 -0.459 0.647
## .Colleague -0.016 0.079 -0.206 0.837
## .Boss -0.013 0.065 -0.198 0.843
## .happy 0.000
## .grateful 0.000
## .satisfied 0.000
## social 0.000
## mental 0.000
##
## Thresholds:
## Estimate Std.Err z-value P(>|z|)
## happy|t1 -0.515 0.076 -6.767 0.000
## happy|t2 -0.017 0.072 -0.231 0.818
## happy|t3 0.449 0.075 5.972 0.000
## grateful|t1 -0.544 0.077 -7.106 0.000
## grateful|t2 -0.084 0.073 -1.153 0.249
## grateful|t3 0.477 0.076 6.313 0.000
## satisfied|t1 -0.385 0.074 -5.174 0.000
## satisfied|t2 0.008 0.072 0.115 0.908
## satisfied|t3 0.664 0.079 8.448 0.000
##
## Variances:
## Estimate Std.Err z-value P(>|z|)
## .friends 1.180 0.152 7.759 0.000
## .familyMember 1.221 0.146 8.345 0.000
## .Colleague 1.058 0.260 4.067 0.000
## .Boss 0.941 0.144 6.551 0.000
## .happy 0.809
## .grateful 0.721
## .satisfied -0.321
## social 0.496 0.167 2.973 0.003
## mental 0.191 0.076 2.523 0.012
##
## Scales y*:
## Estimate Std.Err z-value P(>|z|)
## happy 1.000
## grateful 1.000
## satisfied 1.000