資料與管理

#讀檔案,這是 CSV 檔(用逗號分隔的檔),可以用 notepad 或 EXCEL 開啟
dta <- read.csv("LInteraction.csv", header = TRUE)

潛在調節模型

用 lavaan 功能分析

#product term, centered
 dta$x1 <-  dta$x1-mean( dta$x1)
 dta$x2 <-  dta$x2-mean( dta$x2)
 dta$x3 <-  dta$x3-mean( dta$x3)
 dta$x4 <-  dta$x4-mean( dta$x4)
x1x3 <-  dta$x1* dta$x3
x1x4 <-  dta$x1* dta$x4
x2x3 <-  dta$x2* dta$x3
x2x4 <-  dta$x2* dta$x4

dta <-  data.frame(dta,x1x3,x1x4,x2x3,x2x4)
#Constrained approach
model1 <-'
f1 =~ 1*x1+a2*x2
f2 =~ 1*x3+a4*x4
f3 =~ 1*x1x3+a4*x1x4+a2*x2x3+a8*x2x4
f4 =~ y1+y2+y3
f4~f1+f2+f3
a8==a2*a4
f1~~c11*f1
f2~~c22*f2
f3~~c33*f3
f1+f2~~0*f3
f1~~c21*f2
c33 == c11*c22 + c21*c21
f1+f2~0*1
f3~c21*1
x1+x2+x3+x4~0*1
x1~~t11*x1
x2~~t22*x2
x3~~t33*x3
x4~~t44*x4
x1x3~~t55*x1x3
x1x4~~t66*x1x4
x2x3~~t77*x2x3
x2x4~~t88*x2x4
x1x3~~t65*x1x4
x2x3~~t75*x1x3
x2x4~~t86*x1x4
x2x3~~t87*x2x4
t55 == c11*t33+c22*t11+t11*t33
t65 ==a4*c22*t11
t66 ==c11*t44+a4^2*c22*t11+t11*t44
t75 ==a2*c11*t33
t77 ==a2^2*c11*t33+c22*t22+t22*t33
t86 ==a2*c11*t44
t87 ==a4*c22*t22
t88 ==a2^2*c11*t44+a4^2*c22*t22+t22*t44'
fit1 <- sem(model1,data=dta)
summary(fit1)
## lavaan 0.6.15 ended normally after 477 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        38
## 
##   Number of observations                          1000
## 
## Model Test User Model:
##                                                       
##   Test statistic                                58.460
##   Degrees of freedom                                52
##   P-value (Chi-square)                           0.250
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f1 =~                                               
##     x1                1.000                           
##     x2        (a2)    0.843    0.037   22.685    0.000
##   f2 =~                                               
##     x3                1.000                           
##     x4        (a4)    0.619    0.045   13.799    0.000
##   f3 =~                                               
##     x1x3              1.000                           
##     x1x4      (a4)    0.619    0.045   13.799    0.000
##     x2x3      (a2)    0.843    0.037   22.685    0.000
##     x2x4      (a8)    0.522    0.044   11.789    0.000
##   f4 =~                                               
##     y1                1.000                           
##     y2                0.927    0.071   13.132    0.000
##     y3                0.988    0.075   13.207    0.000
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f4 ~                                                
##     f1                0.225    0.022   10.287    0.000
##     f2                0.151    0.022    6.770    0.000
##     f3                0.146    0.020    7.155    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f1 ~~                                               
##     f3                0.000                           
##   f2 ~~                                               
##     f3                0.000                           
##   f1 ~~                                               
##     f2       (c21)    0.357    0.034   10.504    0.000
##  .x1x3 ~~                                             
##    .x1x4     (t65)    0.269    0.030    9.029    0.000
##    .x2x3     (t75)    0.245    0.064    3.848    0.000
##  .x1x4 ~~                                             
##    .x2x4     (t86)    0.426    0.029   14.836    0.000
##  .x2x3 ~~                                             
##    .x2x4     (t87)    0.146    0.021    7.100    0.000
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     f1                0.000                           
##     f2                0.000                           
##     f3       (c21)    0.357    0.034   10.504    0.000
##    .x1                0.000                           
##    .x2                0.000                           
##    .x3                0.000                           
##    .x4                0.000                           
##    .x1x3              0.029    0.056    0.524    0.601
##    .x1x4              0.008    0.044    0.181    0.856
##    .x2x3              0.000    0.046    0.005    0.996
##    .x2x4             -0.006    0.036   -0.171    0.864
##    .y1                0.005    0.024    0.227    0.821
##    .y2               -0.024    0.023   -1.040    0.299
##    .y3               -0.005    0.024   -0.210    0.834
##    .f4                0.000                           
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     f1       (c11)    0.992    0.056   17.749    0.000
##     f2       (c22)    1.042    0.083   12.555    0.000
##     f3       (c33)    1.161    0.106   10.948    0.000
##    .x1       (t11)    0.417    0.045    9.324    0.000
##    .x2       (t22)    0.226    0.031    7.241    0.000
##    .x3       (t33)    0.292    0.076    3.860    0.000
##    .x4       (t44)    0.509    0.033   15.580    0.000
##    .x1x3     (t55)    0.846    0.090    9.409    0.000
##    .x1x4     (t66)    0.883    0.042   20.962    0.000
##    .x2x3     (t77)    0.508    0.063    8.027    0.000
##    .x2x4     (t88)    0.565    0.029   19.517    0.000
##    .y1                0.361    0.022   16.678    0.000
##    .y2                0.350    0.020   17.340    0.000
##    .y3                0.385    0.022   17.169    0.000
##    .f4                0.104    0.015    6.806    0.000
## 
## Constraints:
##                                                |Slack|
##     a8 - (a2*a4)                                 0.000
##     c33 - (c11*c22+c21*c21)                      0.000
##     t55 - (c11*t33+c22*t11+t11*t33)              0.000
##     t65 - (a4*c22*t11)                           0.000
##     t66 - (c11*t44+a4^2*c22*t11+t11*t44)         0.000
##     t75 - (a2*c11*t33)                           0.000
##     t77 - (a2^2*c11*t33+c22*t22+t22*t33)         0.000
##     t86 - (a2*c11*t44)                           0.000
##     t87 - (a4*c22*t22)                           0.000
##     t88 - (a2^2*c11*t44+a4^2*c22*t22+t22*t44)    0.000
#unconstrained approach
model2 <-'
f1 =~ 1*x1+x2
f2 =~ 1*x3+x4
f3 =~ 1*x1x3+x1x4+x2x3+x2x4
f4 =~ y1+y2+y3
f4~f1+f2+f3
f1~~c21*f2
f1~0*1
f2~0*1
f3~c21*1
x1~0*1
x2~0*1
x3~0*1
x4~0*1
x1x3~~t65*x1x4
x2x3~~t75*x1x3
x2x4~~t86*x1x4
x2x3~~t87*x2x4
'
fit2 <- sem(model2,data=dta)
summary(fit2)
## lavaan 0.6.15 ended normally after 61 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        40
##   Number of equality constraints                     1
## 
##   Number of observations                          1000
## 
## Model Test User Model:
##                                                       
##   Test statistic                                33.673
##   Degrees of freedom                                38
##   P-value (Chi-square)                           0.670
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f1 =~                                               
##     x1                1.000                           
##     x2                0.831    0.043   19.259    0.000
##   f2 =~                                               
##     x3                1.000                           
##     x4                0.619    0.056   10.985    0.000
##   f3 =~                                               
##     x1x3              1.000                           
##     x1x4              0.635    0.077    8.224    0.000
##     x2x3              0.859    0.075   11.430    0.000
##     x2x4              0.541    0.082    6.639    0.000
##   f4 =~                                               
##     y1                1.000                           
##     y2                0.928    0.072   12.888    0.000
##     y3                0.990    0.076   12.970    0.000
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f4 ~                                                
##     f1                0.222    0.022   10.162    0.000
##     f2                0.158    0.024    6.597    0.000
##     f3                0.143    0.024    6.058    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f1 ~~                                               
##     f2       (c21)    0.370    0.043    8.507    0.000
##  .x1x3 ~~                                             
##    .x1x4     (t65)    0.254    0.069    3.671    0.000
##    .x2x3     (t75)    0.150    0.122    1.231    0.218
##  .x1x4 ~~                                             
##    .x2x4     (t86)    0.424    0.054    7.903    0.000
##  .x2x3 ~~                                             
##    .x2x4     (t87)    0.133    0.050    2.649    0.008
##   f1 ~~                                               
##     f3               -0.100    0.045   -2.211    0.027
##   f2 ~~                                               
##     f3               -0.121    0.046   -2.636    0.008
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     f1                0.000                           
##     f2                0.000                           
##     f3       (c21)    0.370    0.043    8.507    0.000
##    .x1                0.000                           
##    .x2                0.000                           
##    .x3                0.000                           
##    .x4                0.000                           
##    .x1x3              0.016    0.062    0.260    0.795
##    .x1x4             -0.006    0.054   -0.118    0.906
##    .x2x3             -0.017    0.059   -0.284    0.776
##    .x2x4             -0.020    0.049   -0.414    0.679
##    .y1                0.005    0.025    0.187    0.852
##    .y2               -0.025    0.024   -1.034    0.301
##    .y3               -0.006    0.025   -0.238    0.812
##    .f4                0.000                           
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .x1                0.420    0.052    8.044    0.000
##    .x2                0.219    0.035    6.244    0.000
##    .x3                0.319    0.088    3.637    0.000
##    .x4                0.517    0.040   12.816    0.000
##    .x1x3              0.788    0.182    4.324    0.000
##    .x1x4              0.829    0.079   10.429    0.000
##    .x2x3              0.396    0.127    3.111    0.002
##    .x2x4              0.577    0.059    9.757    0.000
##    .y1                0.362    0.022   16.658    0.000
##    .y2                0.350    0.020   17.309    0.000
##    .y3                0.384    0.022   17.111    0.000
##     f1                1.059    0.080   13.238    0.000
##     f2                1.028    0.104    9.849    0.000
##     f3                1.231    0.197    6.240    0.000
##    .f4                0.104    0.015    6.781    0.000
#Coenders approach
model3 <-'
f1 =~ 1*x1+a2*x2
f2 =~ 1*x3+a4*x4
f3 =~ 1*x1x3+a4*x1x4+a2*x2x3+a8*x2x4
f4 =~ y1+y2+y3
f4~f1+f2+f3
a8==a2*a4
f1~~c11*f1
f2~~c22*f2
f3~~c33*f3
f1+f2~~0*f3
f1~~c21*f2
f1+f2~0*1
f3~c21*1
x1+x2+x3+x4~0*1
x1x3~~t65*x1x4
x2x3~~t75*x1x3
x2x4~~t86*x1x4
x2x3~~t87*x2x4
'
fit3 <- sem(model3,data=dta)
summary(fit3)
## lavaan 0.6.15 ended normally after 181 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        38
## 
##   Number of observations                          1000
## 
## Model Test User Model:
##                                                       
##   Test statistic                                43.006
##   Degrees of freedom                                43
##   P-value (Chi-square)                           0.471
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f1 =~                                               
##     x1                1.000                           
##     x2        (a2)    0.831    0.038   22.082    0.000
##   f2 =~                                               
##     x3                1.000                           
##     x4        (a4)    0.611    0.046   13.259    0.000
##   f3 =~                                               
##     x1x3              1.000                           
##     x1x4      (a4)    0.611    0.046   13.259    0.000
##     x2x3      (a2)    0.831    0.038   22.082    0.000
##     x2x4      (a8)    0.507    0.045   11.309    0.000
##   f4 =~                                               
##     y1                1.000                           
##     y2                0.929    0.070   13.260    0.000
##     y3                0.990    0.074   13.336    0.000
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f4 ~                                                
##     f1                0.220    0.021   10.324    0.000
##     f2                0.152    0.023    6.748    0.000
##     f3                0.132    0.019    6.840    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f1 ~~                                               
##     f3                0.000                           
##   f2 ~~                                               
##     f3                0.000                           
##   f1 ~~                                               
##     f2       (c21)    0.371    0.043    8.610    0.000
##  .x1x3 ~~                                             
##    .x1x4     (t65)    0.230    0.046    5.051    0.000
##    .x2x3     (t75)    0.112    0.089    1.262    0.207
##  .x1x4 ~~                                             
##    .x2x4     (t86)    0.439    0.040   10.877    0.000
##  .x2x3 ~~                                             
##    .x2x4     (t87)    0.149    0.031    4.792    0.000
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     f1                0.000                           
##     f2                0.000                           
##     f3       (c21)    0.371    0.043    8.610    0.000
##    .x1                0.000                           
##    .x2                0.000                           
##    .x3                0.000                           
##    .x4                0.000                           
##    .x1x3              0.015    0.062    0.243    0.808
##    .x1x4              0.002    0.047    0.044    0.965
##    .x2x3             -0.007    0.051   -0.139    0.889
##    .x2x4             -0.008    0.039   -0.210    0.834
##    .y1                0.009    0.024    0.356    0.722
##    .y2               -0.021    0.023   -0.910    0.363
##    .y3               -0.002    0.024   -0.085    0.932
##    .f4                0.000                           
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     f1       (c11)    1.059    0.075   14.126    0.000
##     f2       (c22)    1.041    0.095   10.982    0.000
##     f3       (c33)    1.320    0.142    9.323    0.000
##    .x1                0.420    0.048    8.686    0.000
##    .x2                0.219    0.032    6.787    0.000
##    .x3                0.305    0.078    3.907    0.000
##    .x4                0.522    0.037   14.119    0.000
##    .x1x3              0.699    0.130    5.401    0.000
##    .x1x4              0.833    0.059   14.084    0.000
##    .x2x3              0.394    0.087    4.524    0.000
##    .x2x4              0.598    0.042   14.263    0.000
##    .y1                0.362    0.022   16.728    0.000
##    .y2                0.350    0.020   17.358    0.000
##    .y3                0.385    0.022   17.186    0.000
##    .f4                0.106    0.015    6.915    0.000
## 
## Constraints:
##                                                |Slack|
##     a8 - (a2*a4)                                 0.000
#畫圖看模型與估計值
lavaanPlot::lavaanPlot(model = fit1,
                       edge_options = list(color = "grey"))
#畫圖
dtafactor <- lavPredict(fit1)
m1 <- lm(f4 ~ f1+f2+f1:f2, data = dtafactor)

interactions::interact_plot(m1, pred = f1, modx = f2, interval = TRUE,
  int.type = "confidence", int.width = .8)