教学目标

探索性因子分析

# 直接file-》import dataset,选择文件即可
library(haven)
# 导入数据默认为数据框格式(data.frame)
eee_data <- read_sav("https://gitee.com/vv_victorwei/r-language-data-analysis/raw/master/Untitled2.sav")
# head会呈现eee_data的前六行数据,以预览概况
head(eee_data)
## # A tibble: 6 × 55
##   V1    time_used IP      id    gender   age birth…¹ roman…² hukou  hair homet…³
##   <chr>     <dbl> <chr>   <chr>  <dbl> <dbl>   <dbl>   <dbl> <dbl> <dbl>   <dbl>
## 1 175         594 223.66… 2225…      1    23       1       0     0     1       0
## 2 124         178 101.88… 2222…      1    23       1       0     0     1       0
## 3 138         207 211.16… 2225…      1    22       0       0     1     1       0
## 4 74          176 58.246… 2122…      1    23       1       0     1     1       0
## 5 149         232 120.25… 2222…      1    22       0       0     0     1       0
## 6 11          142 58.40.… 1601…      1    23       1       0     1     1       0
## # … with 44 more variables: family_income <dbl>, minzhu <dbl>, zhiyuan <dbl>,
## #   height <dbl>, weight <dbl>, zhengzhi <dbl>, personality <dbl>, a1 <dbl>,
## #   a2 <dbl>, a3 <dbl>, a4 <dbl>, a5 <dbl>, a6 <dbl>, a7 <dbl>, a8 <dbl>,
## #   a9 <dbl>, a10 <dbl>, a11 <dbl>, a12 <dbl>, a13 <dbl>, b1 <dbl>, b2 <dbl>,
## #   b3 <dbl>, b4 <dbl>, b5 <dbl>, b6 <dbl>, importance_ranking <chr>,
## #   importance_ranking_d <dbl>, daode <dbl>, zhishi <dbl>, jineng <dbl>,
## #   yishu <dbl>, food <chr>, chuan_food <dbl>, yue_food <dbl>, …
#查看分布情况
summary(eee_data[19:31])
##        a1              a2             a3             a4              a5       
##  Min.   :0.000   Min.   :0.00   Min.   :0.00   Min.   :0.000   Min.   :0.000  
##  1st Qu.:3.000   1st Qu.:3.00   1st Qu.:2.00   1st Qu.:3.000   1st Qu.:3.000  
##  Median :3.000   Median :3.00   Median :3.00   Median :3.000   Median :3.000  
##  Mean   :3.137   Mean   :3.24   Mean   :2.52   Mean   :3.274   Mean   :3.229  
##  3rd Qu.:4.000   3rd Qu.:4.00   3rd Qu.:3.00   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :4.000   Max.   :4.00   Max.   :4.00   Max.   :4.000   Max.   :4.000  
##        a6              a7              a8              a9             a10      
##  Min.   :0.000   Min.   :0.000   Min.   :0.000   Min.   :0.000   Min.   :0.00  
##  1st Qu.:3.000   1st Qu.:3.000   1st Qu.:2.000   1st Qu.:3.000   1st Qu.:3.00  
##  Median :4.000   Median :4.000   Median :3.000   Median :3.000   Median :4.00  
##  Mean   :3.503   Mean   :3.463   Mean   :2.583   Mean   :3.343   Mean   :3.56  
##  3rd Qu.:4.000   3rd Qu.:4.000   3rd Qu.:3.000   3rd Qu.:4.000   3rd Qu.:4.00  
##  Max.   :4.000   Max.   :4.000   Max.   :4.000   Max.   :4.000   Max.   :4.00  
##       a11            a12             a13       
##  Min.   :0.00   Min.   :1.000   Min.   :0.000  
##  1st Qu.:2.00   1st Qu.:3.000   1st Qu.:3.000  
##  Median :2.00   Median :3.000   Median :4.000  
##  Mean   :2.52   Mean   :3.206   Mean   :3.577  
##  3rd Qu.:3.00   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :4.00   Max.   :4.000   Max.   :4.000
#查看内部相关
library(corrgram)
corrgram(eee_data[19:31],upper.panel=panel.cor)
## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

## Warning in par(usr): argument 1 does not name a graphical parameter

# 相关分析中,最后一道题与其他题目相关弱,建议删除
#13 16、为了教会孩子遵守规则,我会采用体罚(如打屁股、打手心)的方式
# 跟专业认同确实没有什么关系

-前提条件

# KMO检验和Bartlett球形检验,反映内在相关情况,达到一定标准才可以继续分析
# KMO值应该达到0.7以上,越大越好
# Bartlett球形检验必须显著
library(psych)
KMO(eee_data[19:30])
## Kaiser-Meyer-Olkin factor adequacy
## Call: KMO(r = eee_data[19:30])
## Overall MSA =  0.84
## MSA for each item = 
##   a1   a2   a3   a4   a5   a6   a7   a8   a9  a10  a11  a12 
## 0.92 0.84 0.82 0.88 0.85 0.83 0.89 0.80 0.76 0.68 0.81 0.83
library(psych)
cortest.bartlett(eee_data[19:30])
## R was not square, finding R from data
## $chisq
## [1] 776.7912
## 
## $p.value
## [1] 6.248433e-122
## 
## $df
## [1] 66

-正式开始

# 查看特征根与碎石图
ev <- eigen(cor(eee_data[19:30]))
ev$values
##  [1] 4.8294278 1.3838786 1.1066449 0.9029497 0.8174793 0.5861894 0.5552474
##  [8] 0.4508588 0.4221654 0.3883831 0.2971355 0.2596400
scree(eee_data[19:30], pc=FALSE)

# 析取因子,cutoff表示0.3以下的因子符合不限制
Nfacs <- 2
fit <- factanal(eee_data[19:30], Nfacs, rotation="promax")
print(fit, digits=2, cutoff=0.3, sort=TRUE)
## 
## Call:
## factanal(x = eee_data[19:30], factors = Nfacs, rotation = "promax")
## 
## Uniquenesses:
##   a1   a2   a3   a4   a5   a6   a7   a8   a9  a10  a11  a12 
## 0.50 0.55 0.53 0.48 0.49 0.42 0.51 0.77 0.78 0.85 0.26 0.75 
## 
## Loadings:
##     Factor1 Factor2
## a1   0.68          
## a2   0.74          
## a4   0.69          
## a5   0.81          
## a6   0.85          
## a7   0.61          
## a8           0.56  
## a11          0.98  
## a3   0.31    0.44  
## a9                 
## a10                
## a12          0.41  
## 
##                Factor1 Factor2
## SS loadings       3.48    1.86
## Proportion Var    0.29    0.15
## Cumulative Var    0.29    0.44
## 
## Factor Correlations:
##         Factor1 Factor2
## Factor1    1.00   -0.68
## Factor2   -0.68    1.00
## 
## Test of the hypothesis that 2 factors are sufficient.
## The chi square statistic is 136.54 on 43 degrees of freedom.
## The p-value is 1.15e-11
Nfacs <- 3
fit <- factanal(eee_data[19:30], Nfacs, rotation="promax")
print(fit, digits=2, cutoff=0.3, sort=TRUE)
## 
## Call:
## factanal(x = eee_data[19:30], factors = Nfacs, rotation = "promax")
## 
## Uniquenesses:
##   a1   a2   a3   a4   a5   a6   a7   a8   a9  a10  a11  a12 
## 0.47 0.53 0.51 0.47 0.50 0.43 0.52 0.79 0.63 0.00 0.19 0.76 
## 
## Loadings:
##     Factor1 Factor2 Factor3
## a1   0.72                  
## a2   0.76                  
## a4   0.69                  
## a5   0.79                  
## a6   0.82                  
## a7   0.61                  
## a11          0.99          
## a10                  1.03  
## a3   0.39    0.44          
## a8           0.48          
## a9                   0.45  
## a12          0.36          
## 
##                Factor1 Factor2 Factor3
## SS loadings       3.47    1.62    1.32
## Proportion Var    0.29    0.14    0.11
## Cumulative Var    0.29    0.42    0.53
## 
## Factor Correlations:
##         Factor1 Factor2 Factor3
## Factor1    1.00    0.39   -0.42
## Factor2    0.39    1.00   -0.63
## Factor3   -0.42   -0.63    1.00
## 
## Test of the hypothesis that 3 factors are sufficient.
## The chi square statistic is 85.72 on 33 degrees of freedom.
## The p-value is 1.43e-06
# 2-3个似乎是合适的,但第三个因子只有两道题目,采用2个,删除9题和10题
#9  16、我认为幼师行业是社会分工中最重要的职业之一
#10 16、当别人赞美幼儿教师时,我很欣慰

Nfacs <- 2
fit <- factanal(eee_data[c(19:26,29,30)], Nfacs, rotation="promax")
print(fit, digits=2, cutoff=0.3, sort=TRUE)
## 
## Call:
## factanal(x = eee_data[c(19:26, 29, 30)], factors = Nfacs, rotation = "promax")
## 
## Uniquenesses:
##   a1   a2   a3   a4   a5   a6   a7   a8  a11  a12 
## 0.49 0.54 0.55 0.49 0.49 0.42 0.52 0.82 0.06 0.77 
## 
## Loadings:
##     Factor1 Factor2
## a1   0.70          
## a2   0.74          
## a4   0.70          
## a5   0.78          
## a6   0.84          
## a7   0.63          
## a11          1.06  
## a3   0.39    0.36  
## a8           0.42  
## a12          0.35  
## 
##                Factor1 Factor2
## SS loadings       3.44    1.62
## Proportion Var    0.34    0.16
## Cumulative Var    0.34    0.51
## 
## Factor Correlations:
##         Factor1 Factor2
## Factor1    1.00    0.61
## Factor2    0.61    1.00
## 
## Test of the hypothesis that 2 factors are sufficient.
## The chi square statistic is 73.13 on 26 degrees of freedom.
## The p-value is 2.3e-06
# 3题在两个因子上都有相同负荷,建议删除
#3  16、在社交方面,我常常因为自己将来是幼儿教师感到自豪

Nfacs <- 2
fit <- factanal(eee_data[c(19:20,22:26,29,30)], Nfacs, rotation="promax")
print(fit, digits=2, cutoff=0.3, sort=TRUE)
## 
## Call:
## factanal(x = eee_data[c(19:20, 22:26, 29, 30)], factors = Nfacs,     rotation = "promax")
## 
## Uniquenesses:
##   a1   a2   a4   a5   a6   a7   a8  a11  a12 
## 0.51 0.57 0.51 0.47 0.40 0.49 0.79 0.20 0.74 
## 
## Loadings:
##     Factor1 Factor2
## a1   0.66          
## a2   0.69          
## a4   0.67          
## a5   0.78          
## a6   0.83          
## a7   0.63          
## a11          0.95  
## a8           0.47  
## a12          0.40  
## 
##                Factor1 Factor2
## SS loadings       3.09    1.33
## Proportion Var    0.34    0.15
## Cumulative Var    0.34    0.49
## 
## Factor Correlations:
##         Factor1 Factor2
## Factor1    1.00   -0.57
## Factor2   -0.57    1.00
## 
## Test of the hypothesis that 2 factors are sufficient.
## The chi square statistic is 57.46 on 19 degrees of freedom.
## The p-value is 9.7e-06
# 绘制因子归属示意图
loads <- fit$loadings
fa.diagram(loads)

#检查原始题目,给factor命名

#1  16、我喜欢和幼儿在一起
#2  16、我很认真地学习专业课知识
#4  16、我希望自己在幼教行业有所成就
#5  16、我相信自己能成为一名合格的幼儿教师
#6  16、我具备爱的能力,发自内心尊重每一位幼儿

#7  16、孩子健康快乐成长是我未来工作的目标
#8  16、当幼儿教师不能实现我的人生价值
#11 16、即使幼教工作再辛苦,我仍想从事幼儿教育事业
#12 16、我会留意观察幼儿教育的新动向

# 不是特别明显,但也看出前五道题偏向于幼教态度和情感,后面四道题偏向职业认同
# factor 1命名为幼教态度和情感,factor 2命名为幼教职业认同

验证性因子分析

library(lavaan)
## This is lavaan 0.6-12
## lavaan is FREE software! Please report any bugs.
## 
## Attaching package: 'lavaan'
## The following object is masked from 'package:psych':
## 
##     cor2cov
# 建立模型,=~表示由后面的题目测量
HS.model <- ' visual  =~ x1 + x2 + x3 
              textual =~ x4 + x5 + x6
              speed   =~ x7 + x8 + x9 '
#进行分析
fit <- cfa(HS.model, data = HolzingerSwineford1939)
summary(fit, fit.measures = TRUE)
## lavaan 0.6-12 ended normally after 35 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        21
## 
##   Number of observations                           301
## 
## Model Test User Model:
##                                                       
##   Test statistic                                85.306
##   Degrees of freedom                                24
##   P-value (Chi-square)                           0.000
## 
## Model Test Baseline Model:
## 
##   Test statistic                               918.852
##   Degrees of freedom                                36
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.931
##   Tucker-Lewis Index (TLI)                       0.896
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -3737.745
##   Loglikelihood unrestricted model (H1)      -3695.092
##                                                       
##   Akaike (AIC)                                7517.490
##   Bayesian (BIC)                              7595.339
##   Sample-size adjusted Bayesian (BIC)         7528.739
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.092
##   90 Percent confidence interval - lower         0.071
##   90 Percent confidence interval - upper         0.114
##   P-value RMSEA <= 0.05                          0.001
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.065
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   visual =~                                           
##     x1                1.000                           
##     x2                0.554    0.100    5.554    0.000
##     x3                0.729    0.109    6.685    0.000
##   textual =~                                          
##     x4                1.000                           
##     x5                1.113    0.065   17.014    0.000
##     x6                0.926    0.055   16.703    0.000
##   speed =~                                            
##     x7                1.000                           
##     x8                1.180    0.165    7.152    0.000
##     x9                1.082    0.151    7.155    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   visual ~~                                           
##     textual           0.408    0.074    5.552    0.000
##     speed             0.262    0.056    4.660    0.000
##   textual ~~                                          
##     speed             0.173    0.049    3.518    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .x1                0.549    0.114    4.833    0.000
##    .x2                1.134    0.102   11.146    0.000
##    .x3                0.844    0.091    9.317    0.000
##    .x4                0.371    0.048    7.779    0.000
##    .x5                0.446    0.058    7.642    0.000
##    .x6                0.356    0.043    8.277    0.000
##    .x7                0.799    0.081    9.823    0.000
##    .x8                0.488    0.074    6.573    0.000
##    .x9                0.566    0.071    8.003    0.000
##     visual            0.809    0.145    5.564    0.000
##     textual           0.979    0.112    8.737    0.000
##     speed             0.384    0.086    4.451    0.000
# 输入完整拟合指数结果
fitMeasures(fit)
##                npar                fmin               chisq                  df 
##              21.000               0.142              85.306              24.000 
##              pvalue      baseline.chisq         baseline.df     baseline.pvalue 
##               0.000             918.852              36.000               0.000 
##                 cfi                 tli                nnfi                 rfi 
##               0.931               0.896               0.896               0.861 
##                 nfi                pnfi                 ifi                 rni 
##               0.907               0.605               0.931               0.931 
##                logl   unrestricted.logl                 aic                 bic 
##           -3737.745           -3695.092            7517.490            7595.339 
##              ntotal                bic2               rmsea      rmsea.ci.lower 
##             301.000            7528.739               0.092               0.071 
##      rmsea.ci.upper        rmsea.pvalue                 rmr          rmr_nomean 
##               0.114               0.001               0.082               0.082 
##                srmr        srmr_bentler srmr_bentler_nomean                crmr 
##               0.065               0.065               0.065               0.073 
##         crmr_nomean          srmr_mplus   srmr_mplus_nomean               cn_05 
##               0.073               0.065               0.065             129.490 
##               cn_01                 gfi                agfi                pgfi 
##             152.654               0.943               0.894               0.503 
##                 mfi                ecvi 
##               0.903               0.423
#输出修正指数
modificationindices(fit, sort = TRUE, maximum.number = 5)
##        lhs op rhs     mi    epc sepc.lv sepc.all sepc.nox
## 30  visual =~  x9 36.411  0.577   0.519    0.515    0.515
## 76      x7 ~~  x8 34.145  0.536   0.536    0.859    0.859
## 28  visual =~  x7 18.631 -0.422  -0.380   -0.349   -0.349
## 78      x8 ~~  x9 14.946 -0.423  -0.423   -0.805   -0.805
## 33 textual =~  x3  9.151 -0.272  -0.269   -0.238   -0.238
#输出结构图(仅供参考,文章中使用时需要重新在ppt里绘制)
library(lavaanPlot)
lavaanPlot(model = fit,coefs = TRUE,stand = TRUE, sig = 0.05,stars = c("regress",'latent'))

模型的多组比较

三种水平的模型等值 结构相同configural invariance,负荷相同metric invariance, 截距(或残差)相同scale invariance 分别建立三个模型:其中第一个模型负荷和截距都自由估计,第二个模型负荷限定组间相等,截距自由估计,第三个模型限定负荷和截距都组间相等

library(lavaan)
HS.model <- ' visual  =~ x1 + x2 + x3
              textual =~ x4 + x5 + x6
              speed   =~ x7 + x8 + x9 '

## 对男女样本进行结构不变性检验
# configural invariance
fit1 <- cfa(HS.model, data = HolzingerSwineford1939, group = "sex")

# weak invariance
fit2 <- cfa(HS.model, data = HolzingerSwineford1939, group = "sex",
            group.equal = "loadings")

# strong invariance
fit3 <- cfa(HS.model, data = HolzingerSwineford1939, group = "sex",
            group.equal = c("intercepts", "loadings"))

# model comparison tests
lavTestLRT(fit1, fit2, fit3)
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC  Chisq Chisq diff Df diff Pr(>Chisq)   
## fit1 48 7517.6 7740.1 105.80                                 
## fit2 54 7526.1 7726.2 126.23     20.431       6   0.002320 **
## fit3 60 7532.8 7710.8 145.00     18.771       6   0.004567 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 对不同学校中结构不变形进行检验
HS.model <- ' visual  =~ x1 + x2 + x3
              textual =~ x4 + x5 + x6
              speed   =~ x7 + x8 + x9 '

# configural invariance
fit1 <- cfa(HS.model, data = HolzingerSwineford1939, group = "school")

# weak invariance
fit2 <- cfa(HS.model, data = HolzingerSwineford1939, group = "school",
            group.equal = "loadings")

# strong invariance
fit3 <- cfa(HS.model, data = HolzingerSwineford1939, group = "school",
            group.equal = c("intercepts", "loadings"))

# model comparison tests
lavTestLRT(fit1, fit2, fit3)
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC  Chisq Chisq diff Df diff Pr(>Chisq)    
## fit1 48 7484.4 7706.8 115.85                                  
## fit2 54 7480.6 7680.8 124.04      8.192       6     0.2244    
## fit3 60 7508.6 7686.6 164.10     40.059       6  4.435e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

结构方程建模

model <- '
  # measurement model
    ind60 =~ x1 + x2 + x3
    dem60 =~ y1 + y2 + y3 + y4
    dem65 =~ y5 + y6 + y7 + y8
  # regressions
    dem60 ~ ind60
    dem65 ~ ind60 + dem60
  # residual correlations
    y1 ~~ y5
    y2 ~~ y4 + y6
    y3 ~~ y7
    y4 ~~ y8
    y6 ~~ y8
'

fit <- sem(model, data = PoliticalDemocracy)
summary(fit, standardized = TRUE)
## lavaan 0.6-12 ended normally after 68 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        31
## 
##   Number of observations                            75
## 
## Model Test User Model:
##                                                       
##   Test statistic                                38.125
##   Degrees of freedom                                35
##   P-value (Chi-square)                           0.329
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   ind60 =~                                                              
##     x1                1.000                               0.670    0.920
##     x2                2.180    0.139   15.742    0.000    1.460    0.973
##     x3                1.819    0.152   11.967    0.000    1.218    0.872
##   dem60 =~                                                              
##     y1                1.000                               2.223    0.850
##     y2                1.257    0.182    6.889    0.000    2.794    0.717
##     y3                1.058    0.151    6.987    0.000    2.351    0.722
##     y4                1.265    0.145    8.722    0.000    2.812    0.846
##   dem65 =~                                                              
##     y5                1.000                               2.103    0.808
##     y6                1.186    0.169    7.024    0.000    2.493    0.746
##     y7                1.280    0.160    8.002    0.000    2.691    0.824
##     y8                1.266    0.158    8.007    0.000    2.662    0.828
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   dem60 ~                                                               
##     ind60             1.483    0.399    3.715    0.000    0.447    0.447
##   dem65 ~                                                               
##     ind60             0.572    0.221    2.586    0.010    0.182    0.182
##     dem60             0.837    0.098    8.514    0.000    0.885    0.885
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .y1 ~~                                                                 
##    .y5                0.624    0.358    1.741    0.082    0.624    0.296
##  .y2 ~~                                                                 
##    .y4                1.313    0.702    1.871    0.061    1.313    0.273
##    .y6                2.153    0.734    2.934    0.003    2.153    0.356
##  .y3 ~~                                                                 
##    .y7                0.795    0.608    1.308    0.191    0.795    0.191
##  .y4 ~~                                                                 
##    .y8                0.348    0.442    0.787    0.431    0.348    0.109
##  .y6 ~~                                                                 
##    .y8                1.356    0.568    2.386    0.017    1.356    0.338
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .x1                0.082    0.019    4.184    0.000    0.082    0.154
##    .x2                0.120    0.070    1.718    0.086    0.120    0.053
##    .x3                0.467    0.090    5.177    0.000    0.467    0.239
##    .y1                1.891    0.444    4.256    0.000    1.891    0.277
##    .y2                7.373    1.374    5.366    0.000    7.373    0.486
##    .y3                5.067    0.952    5.324    0.000    5.067    0.478
##    .y4                3.148    0.739    4.261    0.000    3.148    0.285
##    .y5                2.351    0.480    4.895    0.000    2.351    0.347
##    .y6                4.954    0.914    5.419    0.000    4.954    0.443
##    .y7                3.431    0.713    4.814    0.000    3.431    0.322
##    .y8                3.254    0.695    4.685    0.000    3.254    0.315
##     ind60             0.448    0.087    5.173    0.000    1.000    1.000
##    .dem60             3.956    0.921    4.295    0.000    0.800    0.800
##    .dem65             0.172    0.215    0.803    0.422    0.039    0.039
fitMeasures(fit)
##                npar                fmin               chisq                  df 
##              31.000               0.254              38.125              35.000 
##              pvalue      baseline.chisq         baseline.df     baseline.pvalue 
##               0.329             730.654              55.000               0.000 
##                 cfi                 tli                nnfi                 rfi 
##               0.995               0.993               0.993               0.918 
##                 nfi                pnfi                 ifi                 rni 
##               0.948               0.603               0.996               0.995 
##                logl   unrestricted.logl                 aic                 bic 
##           -1547.791           -1528.728            3157.582            3229.424 
##              ntotal                bic2               rmsea      rmsea.ci.lower 
##              75.000            3131.720               0.035               0.000 
##      rmsea.ci.upper        rmsea.pvalue                 rmr          rmr_nomean 
##               0.092               0.611               0.276               0.276 
##                srmr        srmr_bentler srmr_bentler_nomean                crmr 
##               0.044               0.044               0.044               0.049 
##         crmr_nomean          srmr_mplus   srmr_mplus_nomean               cn_05 
##               0.049               0.045               0.045              98.970 
##               cn_01                 gfi                agfi                pgfi 
##             113.803               0.923               0.854               0.489 
##                 mfi                ecvi 
##               0.979               1.335
library(lavaanPlot)
lavaanPlot(model = fit,coefs = TRUE,stand = TRUE, sig = 0.05,stars = c("regress",'latent'))

#交叉滞后模型 - t1:x,y;t2:x,y - 传统的交叉滞后模型类似于路径分析,投稿时可能受到质疑,要求进行随机截距的交叉滞后模型 - 随机截距的交叉滞后模型:https://jeroendmulder.github.io/RI-CLPM/lavaan.html

x1,y1,x2,y2

autoregressive effects

x2~x1 y2~y1 # cross-lagged effects y2~x1 x2~y1

covariance

x1y1 x2y2

variance

x1x1 x2x2 y1y1 y2y2

# cross lagged panel modelling
dat <- read.table("https://gitee.com/vv_victorwei/r-language-data-analysis/raw/master/%E7%9B%B8%E5%85%B3%E5%88%86%E6%9E%90/RICLPM.dat", col.names = c("x1", "x2", "x3", "x4", "x5", "y1", "y2", "y3", "y4", "y5")) 

CLPM <-'# autoregressive and cross-lagged effects
x2+y2~x1+y1
x3+y3~x2+y2
x4+y4~x3+y3
x5+y5~x4+y4

# covariance

x1~~y1
x2~~y2
x3~~y3
x4~~y4
x5~~y5

# variance
x1~~x1
y1~~y1
x2~~x2
y2~~y2
x3~~x3
y3~~y3
x4~~x4
y4~~y4
x5~~x5
y5~~y5
'
CLPM.fit <- lavaan(CLPM, data = dat, missing = 'ML') 
## Warning in lav_partable_check(lavpartable, categorical = lavoptions$.categorical, : lavaan WARNING: automatically added intercepts are set to zero:
##     [x2 y2 x3 y3 x4 y4 x5 y5 x1 y1]
summary(CLPM.fit, standardized = T)
## lavaan 0.6-12 ended normally after 96 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        31
## 
##   Number of observations                          1189
##   Number of missing patterns                         1
## 
## Model Test User Model:
##                                                       
##   Test statistic                              2098.471
##   Degrees of freedom                                34
##   P-value (Chi-square)                           0.000
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Observed
##   Observed information based on                Hessian
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   x2 ~                                                                  
##     x1                0.398    0.022   17.734    0.000    0.398    0.516
##     y1                0.140    0.017    8.263    0.000    0.140    0.241
##   y2 ~                                                                  
##     x1                0.359    0.039    9.293    0.000    0.359    0.262
##     y1                0.538    0.029   18.456    0.000    0.538    0.520
##   x3 ~                                                                  
##     x2                0.500    0.028   18.043    0.000    0.500    0.485
##     y2                0.169    0.016   10.832    0.000    0.169    0.291
##   y3 ~                                                                  
##     x2                0.336    0.042    7.934    0.000    0.336    0.195
##     y2                0.605    0.024   25.386    0.000    0.605    0.623
##   x4 ~                                                                  
##     x3                0.449    0.026   16.967    0.000    0.449    0.517
##     y3                0.070    0.016    4.459    0.000    0.070    0.136
##   y4 ~                                                                  
##     x3                0.471    0.044   10.590    0.000    0.471    0.253
##     y3                0.664    0.027   25.032    0.000    0.664    0.597
##   x5 ~                                                                  
##     x4                0.439    0.028   15.510    0.000    0.439    0.447
##     y4                0.096    0.013    7.273    0.000    0.096    0.210
##   y5 ~                                                                  
##     x4                0.205    0.043    4.725    0.000    0.205    0.096
##     y4                0.762    0.020   37.764    0.000    0.762    0.765
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   x1 ~~                                                                 
##     y1                0.112    0.006   19.961    0.000    0.112    0.710
##  .x2 ~~                                                                 
##    .y2                0.019    0.002   10.367    0.000    0.019    0.315
##  .x3 ~~                                                                 
##    .y3                0.019    0.002   11.030    0.000    0.019    0.338
##  .x4 ~~                                                                 
##    .y4                0.018    0.002   10.074    0.000    0.018    0.305
##  .x5 ~~                                                                 
##    .y5                0.013    0.002    7.972    0.000    0.013    0.238
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .x2                0.000                               0.000    0.000
##    .y2                0.000                               0.000    0.000
##    .x3                0.000                               0.000    0.000
##    .y3                0.000                               0.000    0.000
##    .x4                0.000                               0.000    0.000
##    .y4                0.000                               0.000    0.000
##    .x5                0.000                               0.000    0.000
##    .y5                0.000                               0.000    0.000
##     x1                0.000                               0.000    0.000
##     y1                0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     x1                0.120    0.005   24.382    0.000    0.120    1.000
##     y1                0.210    0.009   24.382    0.000    0.210    1.000
##    .x2                0.036    0.001   24.382    0.000    0.036    0.500
##    .y2                0.105    0.004   24.382    0.000    0.105    0.468
##    .x3                0.038    0.002   24.382    0.000    0.038    0.498
##    .y3                0.088    0.004   24.382    0.000    0.088    0.416
##    .x4                0.035    0.001   24.382    0.000    0.035    0.621
##    .y4                0.100    0.004   24.382    0.000    0.100    0.380
##    .x5                0.036    0.001   24.382    0.000    0.036    0.646
##    .y5                0.083    0.003   24.382    0.000    0.083    0.320
fitMeasures(CLPM.fit)
##                npar                fmin               chisq                  df 
##              31.000               0.882            2098.471              34.000 
##              pvalue      baseline.chisq         baseline.df     baseline.pvalue 
##               0.000            3166.400              45.000               0.000 
##                 cfi                 tli                nnfi                 rfi 
##               0.339               0.125               0.125               0.123 
##                 nfi                pnfi                 ifi                 rni 
##               0.337               0.255               0.341               0.339 
##                logl   unrestricted.logl                 aic                 bic 
##            -504.583             544.652            1071.166            1228.673 
##              ntotal                bic2               rmsea      rmsea.ci.lower 
##            1189.000            1130.205               0.226               0.218 
##      rmsea.ci.upper        rmsea.pvalue                 rmr          rmr_nomean 
##               0.234               0.000               0.128               0.071 
##                srmr        srmr_bentler srmr_bentler_nomean                crmr 
##               0.833               0.833               0.810               0.466 
##         crmr_nomean          srmr_mplus   srmr_mplus_nomean               cn_05 
##               0.254               0.573               0.473              28.538 
##               cn_01                 gfi                agfi                pgfi 
##              32.764               0.747               0.517               0.391 
##                 mfi                ecvi 
##               0.420               1.817
dat <- read.table("https://gitee.com/vv_victorwei/r-language-data-analysis/raw/master/%E7%9B%B8%E5%85%B3%E5%88%86%E6%9E%90/RICLPM.dat", col.names = c("x1", "x2", "x3", "x4", "x5", "y1", "y2", "y3", "y4", "y5")) 

RICLPM <- '
  # Create between components (random intercepts)
  RIx =~ 1*x1 + 1*x2 + 1*x3 + 1*x4 + 1*x5
  RIy =~ 1*y1 + 1*y2 + 1*y3 + 1*y4 + 1*y5
  
  # Create within-person centered variables
  wx1 =~ 1*x1
  wx2 =~ 1*x2
  wx3 =~ 1*x3 
  wx4 =~ 1*x4
  wx5 =~ 1*x5
  wy1 =~ 1*y1
  wy2 =~ 1*y2
  wy3 =~ 1*y3
  wy4 =~ 1*y4
  wy5 =~ 1*y5

  # Estimate the lagged effects between the within-person centered variables.
  wx2 + wy2 ~ wx1 + wy1
  wx3 + wy3 ~ wx2 + wy2
  wx4 + wy4 ~ wx3 + wy3
  wx5 + wy5 ~ wx4 + wy4

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wx1 ~~ wy1 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wx2 ~~ wy2
  wx3 ~~ wy3
  wx4 ~~ wy4
  wx5 ~~ wy5
  
  # Estimate the variance and covariance of the random intercepts. 
  RIx ~~ RIx
  RIy ~~ RIy
  RIx ~~ RIy

  # Estimate the (residual) variance of the within-person centered variables.
  wx1 ~~ wx1 # Variances
  wy1 ~~ wy1 
  wx2 ~~ wx2 # Residual variances
  wy2 ~~ wy2 
  wx3 ~~ wx3 
  wy3 ~~ wy3 
  wx4 ~~ wx4 
  wy4 ~~ wy4 
  wx5 ~~ wx5
  wy5 ~~ wy5
'
RICLPM.fit <- lavaan(RICLPM, data = dat, missing = 'ML', meanstructure = T, int.ov.free = T) 
summary(RICLPM.fit, standardized = T)
## lavaan 0.6-12 ended normally after 115 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        44
## 
##   Number of observations                          1189
##   Number of missing patterns                         1
## 
## Model Test User Model:
##                                                       
##   Test statistic                                25.806
##   Degrees of freedom                                21
##   P-value (Chi-square)                           0.214
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Observed
##   Observed information based on                Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   RIx =~                                                                
##     x1                1.000                               0.096    0.390
##     x2                1.000                               0.096    0.473
##     x3                1.000                               0.096    0.475
##     x4                1.000                               0.096    0.461
##     x5                1.000                               0.096    0.465
##   RIy =~                                                                
##     y1                1.000                               0.178    0.569
##     y2                1.000                               0.178    0.558
##     y3                1.000                               0.178    0.535
##     y4                1.000                               0.178    0.525
##     y5                1.000                               0.178    0.533
##   wx1 =~                                                                
##     x1                1.000                               0.227    0.921
##   wx2 =~                                                                
##     x2                1.000                               0.179    0.881
##   wx3 =~                                                                
##     x3                1.000                               0.178    0.880
##   wx4 =~                                                                
##     x4                1.000                               0.185    0.887
##   wx5 =~                                                                
##     x5                1.000                               0.183    0.885
##   wy1 =~                                                                
##     y1                1.000                               0.257    0.822
##   wy2 =~                                                                
##     y2                1.000                               0.265    0.830
##   wy3 =~                                                                
##     y3                1.000                               0.281    0.845
##   wy4 =~                                                                
##     y4                1.000                               0.288    0.851
##   wy5 =~                                                                
##     y5                1.000                               0.282    0.846
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   wx2 ~                                                                 
##     wx1               0.232    0.028    8.314    0.000    0.294    0.294
##     wy1               0.009    0.026    0.329    0.742    0.012    0.012
##   wy2 ~                                                                 
##     wx1               0.174    0.045    3.888    0.000    0.149    0.149
##     wy1               0.004    0.046    0.092    0.927    0.004    0.004
##   wx3 ~                                                                 
##     wx2               0.241    0.037    6.509    0.000    0.242    0.242
##     wy2               0.026    0.024    1.082    0.279    0.039    0.039
##   wy3 ~                                                                 
##     wx2               0.156    0.054    2.871    0.004    0.099    0.099
##     wy2               0.262    0.039    6.747    0.000    0.247    0.247
##   wx4 ~                                                                 
##     wx3               0.279    0.038    7.267    0.000    0.269    0.269
##     wy3               0.010    0.023    0.431    0.666    0.015    0.015
##   wy4 ~                                                                 
##     wx3               0.185    0.055    3.367    0.001    0.114    0.114
##     wy3               0.296    0.035    8.362    0.000    0.288    0.288
##   wx5 ~                                                                 
##     wx4               0.290    0.035    8.244    0.000    0.293    0.293
##     wy4              -0.004    0.022   -0.186    0.852   -0.006   -0.006
##   wy5 ~                                                                 
##     wx4               0.124    0.048    2.612    0.009    0.082    0.082
##     wy4               0.392    0.031   12.644    0.000    0.400    0.400
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   wx1 ~~                                                                
##     wy1               0.021    0.002    9.372    0.000    0.364    0.364
##  .wx2 ~~                                                                
##    .wy2               0.009    0.002    5.168    0.000    0.196    0.196
##  .wx3 ~~                                                                
##    .wy3               0.013    0.002    7.837    0.000    0.274    0.274
##  .wx4 ~~                                                                
##    .wy4               0.013    0.002    8.177    0.000    0.277    0.277
##  .wx5 ~~                                                                
##    .wy5               0.007    0.001    4.916    0.000    0.160    0.160
##   RIx ~~                                                                
##     RIy               0.010    0.001    7.992    0.000    0.587    0.587
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .x1                0.241    0.007   33.687    0.000    0.241    0.977
##    .x2                0.173    0.006   29.331    0.000    0.173    0.851
##    .x3                0.186    0.006   31.646    0.000    0.186    0.918
##    .x4                0.117    0.006   19.288    0.000    0.117    0.559
##    .x5                0.111    0.006   18.427    0.000    0.111    0.534
##    .y1                0.336    0.009   37.099    0.000    0.336    1.076
##    .y2                0.348    0.009   37.686    0.000    0.348    1.093
##    .y3                0.319    0.010   33.098    0.000    0.319    0.960
##    .y4                0.384    0.010   39.097    0.000    0.384    1.134
##    .y5                0.388    0.010   40.056    0.000    0.388    1.162
##     RIx               0.000                               0.000    0.000
##     RIy               0.000                               0.000    0.000
##     wx1               0.000                               0.000    0.000
##    .wx2               0.000                               0.000    0.000
##    .wx3               0.000                               0.000    0.000
##    .wx4               0.000                               0.000    0.000
##    .wx5               0.000                               0.000    0.000
##     wy1               0.000                               0.000    0.000
##    .wy2               0.000                               0.000    0.000
##    .wy3               0.000                               0.000    0.000
##    .wy4               0.000                               0.000    0.000
##    .wy5               0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     RIx               0.009    0.001    8.722    0.000    1.000    1.000
##     RIy               0.032    0.003   12.351    0.000    1.000    1.000
##     wx1               0.052    0.002   21.067    0.000    1.000    1.000
##     wy1               0.066    0.004   17.985    0.000    1.000    1.000
##    .wx2               0.029    0.001   20.793    0.000    0.911    0.911
##    .wy2               0.068    0.004   17.503    0.000    0.977    0.977
##    .wx3               0.030    0.001   20.467    0.000    0.935    0.935
##    .wy3               0.072    0.003   21.324    0.000    0.918    0.918
##    .wx4               0.032    0.001   21.445    0.000    0.925    0.925
##    .wy4               0.074    0.003   21.876    0.000    0.884    0.884
##    .wx5               0.031    0.001   21.680    0.000    0.915    0.915
##    .wy5               0.065    0.003   22.446    0.000    0.813    0.813
##    .x1                0.000                               0.000    0.000
##    .x2                0.000                               0.000    0.000
##    .x3                0.000                               0.000    0.000
##    .x4                0.000                               0.000    0.000
##    .x5                0.000                               0.000    0.000
##    .y1                0.000                               0.000    0.000
##    .y2                0.000                               0.000    0.000
##    .y3                0.000                               0.000    0.000
##    .y4                0.000                               0.000    0.000
##    .y5                0.000                               0.000    0.000
fitMeasures(RICLPM.fit)
##                npar                fmin               chisq                  df 
##              44.000               0.011              25.806              21.000 
##              pvalue      baseline.chisq         baseline.df     baseline.pvalue 
##               0.214            3166.400              45.000               0.000 
##                 cfi                 tli                nnfi                 rfi 
##               0.998               0.997               0.997               0.983 
##                 nfi                pnfi                 ifi                 rni 
##               0.992               0.463               0.998               0.998 
##                logl   unrestricted.logl                 aic                 bic 
##             531.750             544.652            -975.499            -751.941 
##              ntotal                bic2               rmsea      rmsea.ci.lower 
##            1189.000            -891.701               0.014               0.000 
##      rmsea.ci.upper        rmsea.pvalue                 rmr          rmr_nomean 
##               0.030               1.000               0.001               0.001 
##                srmr        srmr_bentler srmr_bentler_nomean                crmr 
##               0.014               0.014               0.015               0.014 
##         crmr_nomean          srmr_mplus   srmr_mplus_nomean               cn_05 
##               0.015               0.013               0.014            1506.306 
##               cn_01                 gfi                agfi                pgfi 
##            1794.811               0.997               0.992               0.322 
##                 mfi                ecvi 
##               0.998               0.096

潜增长模型

model <- ' i =~ 1*t1 + 1*t2 + 1*t3 + 1*t4
           s =~ 0*t1 + 1*t2 + 2*t3 + 3*t4 '
fit <- growth(model, data=Demo.growth)
summary(fit)
## lavaan 0.6-12 ended normally after 29 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         9
## 
##   Number of observations                           400
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 8.069
##   Degrees of freedom                                 5
##   P-value (Chi-square)                           0.152
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   i =~                                                
##     t1                1.000                           
##     t2                1.000                           
##     t3                1.000                           
##     t4                1.000                           
##   s =~                                                
##     t1                0.000                           
##     t2                1.000                           
##     t3                2.000                           
##     t4                3.000                           
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   i ~~                                                
##     s                 0.618    0.071    8.686    0.000
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .t1                0.000                           
##    .t2                0.000                           
##    .t3                0.000                           
##    .t4                0.000                           
##     i                 0.615    0.077    8.007    0.000
##     s                 1.006    0.042   24.076    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .t1                0.595    0.086    6.944    0.000
##    .t2                0.676    0.061   11.061    0.000
##    .t3                0.635    0.072    8.761    0.000
##    .t4                0.508    0.124    4.090    0.000
##     i                 1.932    0.173   11.194    0.000
##     s                 0.587    0.052   11.336    0.000
# 增加预测因子
model <- ' i =~ 1*t1 + 1*t2 + 1*t3 + 1*t4
           s =~ 0*t1 + 1*t2 + 2*t3 + 3*t4
           i + s ~ x1 + x2 '
fit <- growth(model, data=Demo.growth)
summary(fit)
## lavaan 0.6-12 ended normally after 31 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        13
## 
##   Number of observations                           400
## 
## Model Test User Model:
##                                                       
##   Test statistic                                10.873
##   Degrees of freedom                                 9
##   P-value (Chi-square)                           0.285
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   i =~                                                
##     t1                1.000                           
##     t2                1.000                           
##     t3                1.000                           
##     t4                1.000                           
##   s =~                                                
##     t1                0.000                           
##     t2                1.000                           
##     t3                2.000                           
##     t4                3.000                           
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   i ~                                                 
##     x1                0.609    0.060   10.079    0.000
##     x2                0.613    0.065    9.488    0.000
##   s ~                                                 
##     x1                0.263    0.029    9.157    0.000
##     x2                0.517    0.031   16.868    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##  .i ~~                                                
##    .s                 0.088    0.042    2.116    0.034
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .t1                0.000                           
##    .t2                0.000                           
##    .t3                0.000                           
##    .t4                0.000                           
##    .i                 0.586    0.062    9.400    0.000
##    .s                 0.958    0.030   32.382    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .t1                0.597    0.085    7.067    0.000
##    .t2                0.671    0.060   11.088    0.000
##    .t3                0.599    0.065    9.254    0.000
##    .t4                0.593    0.109    5.437    0.000
##    .i                 1.076    0.115    9.335    0.000
##    .s                 0.219    0.027    7.975    0.000

利用lavaan来检验中介作用

library(haven)
math_data <- read_sav("https://gitee.com/vv_victorwei/r-language-data-analysis/raw/master/%E7%9B%B8%E5%85%B3%E5%88%86%E6%9E%90/regression.sav")
head(math_data)
## # A tibble: 6 × 3
##     age intelligence maths
##   <dbl>        <dbl> <dbl>
## 1    61            4    17
## 2    60           12    17
## 3    60            4    19
## 4    69            5    20
## 5    67           13    21
## 6    64           10    21
library(lavaan)
model <- ' # direct effect
             maths ~ c*age
           # mediator
             intelligence ~ a*age
             maths ~ b*intelligence
           # indirect effect (a*b)
             ab := a*b
           # total effect
             total := c + (a*b)
         '
fit <- sem(model, data = math_data,se = "bootstrap")
summary(fit)
## lavaan 0.6-12 ended normally after 1 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         5
## 
##   Number of observations                           173
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Standard errors                            Bootstrap
##   Number of requested bootstrap draws             1000
##   Number of successful bootstrap draws            1000
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   maths ~                                             
##     age        (c)    0.386    0.207    1.864    0.062
##   intelligence ~                                      
##     age        (a)    0.477    0.109    4.388    0.000
##   maths ~                                             
##     intellignc (b)    1.002    0.154    6.488    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .maths            88.234    8.318   10.607    0.000
##    .intelligence     25.733    3.358    7.664    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     ab                0.478    0.125    3.813    0.000
##     total             0.863    0.227    3.807    0.000