Tải dữ liệu

library(lavaan)
## This is lavaan 0.6-12
## lavaan is FREE software! Please report any bugs.
library(thongke.club)
dulieu <-thangdo
head(dulieu)
##      b1 b2 b3 b4 b5 c1 c2 c3 c4 c5 d1 d2 d3 d4 d5 e1 e2 e3 e4 e5
## [1,]  1  1  1  1  1  4  4  4  4  4  1  1  1  1  1  4  4  4  4  4
## [2,]  4  4  4  4  4  5  1  1  5  4  4  4  4  4  4  4  5  4  5  5
## [3,]  1  1  1  1  1  4  5  5  4  5  5  5  5  5  5  1  1  5  1  1
## [4,]  5  5  5  5  5  5  4  5  4  5  4  4  4  4  4  1  1  1  1  4
## [5,]  4  4  4  4  4  4  5  5  5  5  5  5  5  5  5  1  4  1  5  5
## [6,]  4  1  1  1  1  1  1  1  5  1  4  1  5  1  4  1  5  1  1  1

Fit a Confirmatory Factor Analysis (CFA) model

mohinh <- "
tinhyeu =~ b1 + b2 + b3 + b4 + b5
vatchat =~ c1 + c2 +c3 +c4 +c5
quanhe =~ d1 + d2 +d3 +d4 +d5
kethon =~ e1 + e2 +e3 +e4 +e5
"
fitcfa <-cfa(mohinh, data=dulieu)
summary(fitcfa)
## lavaan 0.6-12 ended normally after 44 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        46
## 
##   Number of observations                           400
## 
## Model Test User Model:
##                                                       
##   Test statistic                               320.154
##   Degrees of freedom                               164
##   P-value (Chi-square)                           0.000
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   tinhyeu =~                                          
##     b1                1.000                           
##     b2                1.118    0.066   16.976    0.000
##     b3                1.224    0.067   18.362    0.000
##     b4                1.034    0.064   16.183    0.000
##     b5                1.055    0.064   16.583    0.000
##   vatchat =~                                          
##     c1                1.000                           
##     c2                0.987    0.057   17.267    0.000
##     c3                0.807    0.056   14.474    0.000
##     c4                0.945    0.057   16.676    0.000
##     c5                1.044    0.055   19.159    0.000
##   quanhe =~                                           
##     d1                1.000                           
##     d2                1.032    0.077   13.322    0.000
##     d3                1.018    0.076   13.456    0.000
##     d4                1.161    0.080   14.554    0.000
##     d5                1.132    0.081   13.970    0.000
##   kethon =~                                           
##     e1                1.000                           
##     e2                0.920    0.063   14.627    0.000
##     e3                1.000    0.064   15.627    0.000
##     e4                1.032    0.064   16.120    0.000
##     e5                0.874    0.063   13.886    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   tinhyeu ~~                                          
##     vatchat           0.067    0.020    3.286    0.001
##     quanhe            0.078    0.018    4.370    0.000
##     kethon            0.063    0.019    3.314    0.001
##   vatchat ~~                                          
##     quanhe            0.095    0.020    4.803    0.000
##     kethon            0.069    0.021    3.304    0.001
##   quanhe ~~                                           
##     kethon            0.066    0.018    3.683    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .b1                0.232    0.019   12.142    0.000
##    .b2                0.204    0.018   11.267    0.000
##    .b3                0.147    0.016    9.369    0.000
##    .b4                0.220    0.019   11.877    0.000
##    .b5                0.204    0.018   11.595    0.000
##    .c1                0.194    0.018   10.753    0.000
##    .c2                0.235    0.021   11.437    0.000
##    .c3                0.288    0.023   12.689    0.000
##    .c4                0.246    0.021   11.788    0.000
##    .c5                0.158    0.016    9.617    0.000
##    .d1                0.251    0.021   11.798    0.000
##    .d2                0.261    0.022   11.738    0.000
##    .d3                0.242    0.021   11.623    0.000
##    .d4                0.207    0.020   10.279    0.000
##    .d5                0.250    0.022   11.105    0.000
##    .e1                0.227    0.020   11.152    0.000
##    .e2                0.239    0.020   11.746    0.000
##    .e3                0.211    0.019   10.916    0.000
##    .e4                0.191    0.018   10.336    0.000
##    .e5                0.262    0.022   12.182    0.000
##     tinhyeu           0.327    0.037    8.766    0.000
##     vatchat           0.388    0.040    9.575    0.000
##     quanhe            0.269    0.034    7.854    0.000
##     kethon            0.333    0.038    8.690    0.000
fitMeasures(fitcfa)
##                npar                fmin               chisq                  df 
##              46.000               0.400             320.154             164.000 
##              pvalue      baseline.chisq         baseline.df     baseline.pvalue 
##               0.000            4401.336             190.000               0.000 
##                 cfi                 tli                nnfi                 rfi 
##               0.963               0.957               0.957               0.916 
##                 nfi                pnfi                 ifi                 rni 
##               0.927               0.800               0.963               0.963 
##                logl   unrestricted.logl                 aic                 bic 
##           -7017.250           -6857.173           14126.500           14310.108 
##              ntotal                bic2               rmsea      rmsea.ci.lower 
##             400.000           14164.146               0.049               0.041 
##      rmsea.ci.upper        rmsea.pvalue                 rmr          rmr_nomean 
##               0.057               0.588               0.022               0.022 
##                srmr        srmr_bentler srmr_bentler_nomean                crmr 
##               0.039               0.039               0.039               0.041 
##         crmr_nomean          srmr_mplus   srmr_mplus_nomean               cn_05 
##               0.041               0.039               0.039             244.486 
##               cn_01                 gfi                agfi                pgfi 
##             262.184               0.925               0.904               0.722 
##                 mfi                ecvi 
##               0.823               1.030

fif a sem

mohinh2 <- "
kethon ~ tinhyeu + vatchat + quanhe
quanhe ~ tinhyeu + vatchat
tinhyeu ~ vatchat
tinhyeu =~ b1 + b2 + b3 + b4 + b5
vatchat =~ c1 + c2 +c3 +c4 +c5
quanhe =~ d1 + d2 +d3 +d4 +d5
kethon =~ e1 + e2 +e3 +e4 +e5
"
fitsem <-sem(mohinh2, data=dulieu)
summary(fitsem)
## lavaan 0.6-12 ended normally after 43 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        46
## 
##   Number of observations                           400
## 
## Model Test User Model:
##                                                       
##   Test statistic                               320.154
##   Degrees of freedom                               164
##   P-value (Chi-square)                           0.000
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   tinhyeu =~                                          
##     b1                1.000                           
##     b2                1.118    0.066   16.976    0.000
##     b3                1.224    0.067   18.362    0.000
##     b4                1.034    0.064   16.183    0.000
##     b5                1.055    0.064   16.583    0.000
##   vatchat =~                                          
##     c1                1.000                           
##     c2                0.987    0.057   17.267    0.000
##     c3                0.807    0.056   14.474    0.000
##     c4                0.945    0.057   16.676    0.000
##     c5                1.044    0.055   19.159    0.000
##   quanhe =~                                           
##     d1                1.000                           
##     d2                1.032    0.077   13.322    0.000
##     d3                1.018    0.076   13.456    0.000
##     d4                1.161    0.080   14.554    0.000
##     d5                1.132    0.081   13.970    0.000
##   kethon =~                                           
##     e1                1.000                           
##     e2                0.920    0.063   14.626    0.000
##     e3                1.000    0.064   15.627    0.000
##     e4                1.032    0.064   16.120    0.000
##     e5                0.874    0.063   13.886    0.000
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   kethon ~                                            
##     tinhyeu           0.130    0.058    2.241    0.025
##     vatchat           0.115    0.054    2.110    0.035
##     quanhe            0.168    0.068    2.468    0.014
##   quanhe ~                                            
##     tinhyeu           0.196    0.051    3.838    0.000
##     vatchat           0.211    0.048    4.448    0.000
##   tinhyeu ~                                           
##     vatchat           0.172    0.051    3.376    0.001
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .b1                0.232    0.019   12.142    0.000
##    .b2                0.204    0.018   11.266    0.000
##    .b3                0.147    0.016    9.369    0.000
##    .b4                0.220    0.019   11.877    0.000
##    .b5                0.204    0.018   11.595    0.000
##    .c1                0.194    0.018   10.753    0.000
##    .c2                0.235    0.021   11.437    0.000
##    .c3                0.288    0.023   12.689    0.000
##    .c4                0.246    0.021   11.788    0.000
##    .c5                0.158    0.016    9.617    0.000
##    .d1                0.251    0.021   11.798    0.000
##    .d2                0.261    0.022   11.738    0.000
##    .d3                0.242    0.021   11.623    0.000
##    .d4                0.207    0.020   10.279    0.000
##    .d5                0.250    0.022   11.105    0.000
##    .e1                0.227    0.020   11.152    0.000
##    .e2                0.239    0.020   11.746    0.000
##    .e3                0.211    0.019   10.916    0.000
##    .e4                0.191    0.018   10.336    0.000
##    .e5                0.262    0.022   12.182    0.000
##    .tinhyeu           0.315    0.036    8.737    0.000
##     vatchat           0.388    0.040    9.575    0.000
##    .quanhe            0.234    0.030    7.749    0.000
##    .kethon            0.306    0.035    8.613    0.000
fitMeasures(fitsem)
##                npar                fmin               chisq                  df 
##              46.000               0.400             320.154             164.000 
##              pvalue      baseline.chisq         baseline.df     baseline.pvalue 
##               0.000            4401.336             190.000               0.000 
##                 cfi                 tli                nnfi                 rfi 
##               0.963               0.957               0.957               0.916 
##                 nfi                pnfi                 ifi                 rni 
##               0.927               0.800               0.963               0.963 
##                logl   unrestricted.logl                 aic                 bic 
##           -7017.250           -6857.173           14126.500           14310.108 
##              ntotal                bic2               rmsea      rmsea.ci.lower 
##             400.000           14164.146               0.049               0.041 
##      rmsea.ci.upper        rmsea.pvalue                 rmr          rmr_nomean 
##               0.057               0.588               0.022               0.022 
##                srmr        srmr_bentler srmr_bentler_nomean                crmr 
##               0.039               0.039               0.039               0.041 
##         crmr_nomean          srmr_mplus   srmr_mplus_nomean               cn_05 
##               0.041               0.039               0.039             244.486 
##               cn_01                 gfi                agfi                pgfi 
##             262.184               0.925               0.904               0.722 
##                 mfi                ecvi 
##               0.823               1.030

Chạy Modification Indices

modindices(fitsem, minimum.value = 10, sort = TRUE)
##     lhs op rhs     mi    epc sepc.lv sepc.all sepc.nox
## 229  c3 ~~  d5 26.117  0.079   0.079    0.296    0.296
## 258  d1 ~~  d4 20.069  0.074   0.074    0.327    0.327
## 121  b1 ~~  d2 19.811 -0.063  -0.063   -0.257   -0.257
## 225  c3 ~~  d1 18.039 -0.065  -0.065   -0.240   -0.240
## 172  b4 ~~  d2 11.976  0.048   0.048    0.202    0.202
## 256  d1 ~~  d2 11.764 -0.057  -0.057   -0.221   -0.221
## 265  d2 ~~  d3 11.719  0.056   0.056    0.224    0.224
## 149  b3 ~~  b5 10.211 -0.045  -0.045   -0.262   -0.262

Chạy bootstrap

#bootstrapLavaan(fitsem,100) -> bfitsem
#summary(bfitsem)

Vài kiểm định

lavTestLRT(fitsem, fitcfa)
## Warning in lavTestLRT(fitsem, fitcfa): lavaan WARNING: some models have the same
## degrees of freedom
## Chi-Squared Difference Test
## 
##         Df   AIC   BIC  Chisq  Chisq diff Df diff Pr(>Chisq)
## fitsem 164 14126 14310 320.15                               
## fitcfa 164 14126 14310 320.15 -5.1557e-09       0

Kiểm định

library(semTools)
## 
## ###############################################################################
## This is semTools 0.5-6
## All users of R (or SEM) are invited to submit functions or ideas for functions.
## ###############################################################################
AVE(fitsem)
## tinhyeu vatchat  quanhe  kethon 
##   0.658   0.615   0.560   0.579
compRelSEM(fitcfa)
## tinhyeu vatchat  quanhe  kethon 
##   0.905   0.888   0.864   0.873
#cov(dulieu)
#htmt(fitcfa, dulieu)

Đồ thị