資料與管理

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

一個獨變項,兩個調節變項

方法一:調節效果,用 PROCESS 功能分析

#載入 PROCESS,特別記得要讓 process.r 可讀取(在同目錄,或特定目錄)
source('process.r')
## 
## ********************* PROCESS for R Version 4.3.1 ********************* 
##  
##            Written by Andrew F. Hayes, Ph.D.  www.afhayes.com              
##    Documentation available in Hayes (2022). www.guilford.com/p/hayes3   
##  
## *********************************************************************** 
##  
## PROCESS is now ready for use.
## Copyright 2020-2023 by Andrew F. Hayes ALL RIGHTS RESERVED
## Workshop schedule at http://haskayne.ucalgary.ca/CCRAM
## 
#用 PROCESS 處理
#套件的變項要用字串符號括入(統計能力好,程式能力待加強)
process (data = dta, y = 'Y', x = 'X', w ='Z1',z='Z2', model = 3,
         moments = 1,jn = 1,plot=1, modelbt= 1, boot = 999)
## 
## ********************* PROCESS for R Version 4.3.1 ********************* 
##  
##            Written by Andrew F. Hayes, Ph.D.  www.afhayes.com              
##    Documentation available in Hayes (2022). www.guilford.com/p/hayes3   
##  
## *********************************************************************** 
##           
## Model : 3 
##     Y : Y 
##     X : X 
##     W : Z1
##     Z : Z2
## 
## Sample size: 500
## 
## Random seed: 936195
## 
## 
## *********************************************************************** 
## Outcome Variable: Y
## 
## Model Summary: 
##           R      R-sq       MSE         F       df1       df2         p
##      0.6104    0.3725    1.0190   41.7300    7.0000  492.0000    0.0000
## 
## Model: 
##              coeff        se         t         p      LLCI      ULCI
## constant    0.0608    0.0454    1.3382    0.1814   -0.0284    0.1499
## X           0.4460    0.0467    9.5559    0.0000    0.3543    0.5378
## Z1          0.4296    0.0455    9.4334    0.0000    0.3401    0.5191
## Int_1       0.0248    0.0472    0.5254    0.5995   -0.0680    0.1177
## Z2          0.0305    0.0458    0.6657    0.5059   -0.0595    0.1206
## Int_2       0.1014    0.0502    2.0202    0.0439    0.0028    0.2000
## Int_3       0.0096    0.0435    0.2208    0.8254   -0.0758    0.0950
## Int_4       0.4911    0.0487   10.0826    0.0000    0.3954    0.5869
## 
## Product terms key:
## Int_1  :  X  x  Z1      
## Int_2  :  X  x  Z2      
## Int_3  :  Z1  x  Z2      
## Int_4  :  X  x  Z1  x  Z2
## 
## Test(s) of highest order unconditional interaction(s):
##         R2-chng         F       df1       df2         p
## X*W*Z    0.1296  101.6592    1.0000  492.0000    0.0000
## ----------
## Focal predictor: X (X)
##       Moderator: Z1 (W)
##       Moderator: Z2 (Z)
## 
## Test of conditional X*W interaction at value(s) of Z:
##          Z2    effect         F       df1       df2         p
##     -0.9666   -0.4499   38.0268    1.0000  492.0000    0.0000
##      0.0262    0.0377    0.6428    1.0000  492.0000    0.4231
##      1.0190    0.5253   73.1136    1.0000  492.0000    0.0000
## 
## Conditional effects of the focal predictor at values of the moderator(s):
##          Z1        Z2    effect        se         t         p      LLCI
##     -1.0132   -0.9666    0.8039    0.1021    7.8724    0.0000    0.6033
##     -1.0132    0.0262    0.4105    0.0655    6.2720    0.0000    0.2819
##     -1.0132    1.0190    0.0171    0.0913    0.1874    0.8515   -0.1622
##     -0.0023   -0.9666    0.3491    0.0674    5.1780    0.0000    0.2166
##     -0.0023    0.0262    0.4486    0.0467    9.6085    0.0000    0.3569
##     -0.0023    1.0190    0.5482    0.0692    7.9263    0.0000    0.4123
##      1.0086   -0.9666   -0.1058    0.0977   -1.0827    0.2795   -0.2977
##      1.0086    0.0262    0.4867    0.0678    7.1818    0.0000    0.3536
##      1.0086    1.0190    1.0792    0.0946   11.4093    0.0000    0.8934
##        ULCI
##      1.0046
##      0.5391
##      0.1965
##      0.4815
##      0.5404
##      0.6840
##      0.0862
##      0.6199
##      1.2651
## 
## Moderator value(s) defining Johnson-Neyman significance region(s):
##       Value   % below   % above
##     -0.2552   38.0000   62.0000
##      0.1351   52.6000   47.4000
## 
## Conditional X*W interaction at values of the moderator Z:
##          Z2    effect        se         t         p      LLCI      ULCI
##     -2.6953   -1.2990    0.1480   -8.7759    0.0000   -1.5898   -1.0082
##     -2.3750   -1.1417    0.1333   -8.5655    0.0000   -1.4035   -0.8798
##     -2.0547   -0.9843    0.1188   -8.2871    0.0000   -1.2177   -0.7510
##     -1.7344   -0.8270    0.1046   -7.9073    0.0000   -1.0325   -0.6215
##     -1.4141   -0.6697    0.0909   -7.3705    0.0000   -0.8483   -0.4912
##     -1.0938   -0.5124    0.0779   -6.5819    0.0000   -0.6654   -0.3594
##     -0.7735   -0.3551    0.0660   -5.3829    0.0000   -0.4847   -0.2255
##     -0.4532   -0.1978    0.0559   -3.5354    0.0004   -0.3077   -0.0879
##     -0.2552   -0.1005    0.0512   -1.9648    0.0500   -0.2010   -0.0000
##     -0.1329   -0.0405    0.0489   -0.8269    0.4087   -0.1366    0.0557
##      0.1351    0.0912    0.0464    1.9648    0.0500   -0.0000    0.1823
##      0.1874    0.1168    0.0463    2.5224    0.0120    0.0258    0.2079
##      0.5077    0.2742    0.0488    5.6145    0.0000    0.1782    0.3701
##      0.8280    0.4315    0.0558    7.7372    0.0000    0.3219    0.5410
##      1.1483    0.5888    0.0657    8.9560    0.0000    0.4596    0.7180
##      1.4686    0.7461    0.0776    9.6151    0.0000    0.5936    0.8986
##      1.7889    0.9034    0.0906    9.9722    0.0000    0.7254    1.0814
##      2.1092    1.0607    0.1043   10.1692    0.0000    0.8558    1.2657
##      2.4295    1.2180    0.1185   10.2797    0.0000    0.9852    1.4509
##      2.7498    1.3754    0.1330   10.3418    0.0000    1.1141    1.6367
##      3.0701    1.5327    0.1477   10.3758    0.0000    1.2424    1.8229
##      3.3904    1.6900    0.1626   10.3932    0.0000    1.3705    2.0095
## 
## Data for visualizing the conditional effect of the focal predictor:
##           X        Z1        Z2         Y
##     -0.9382   -1.0132   -0.9666   -1.1489
##      0.0346   -1.0132   -0.9666   -0.3668
##      1.0074   -1.0132   -0.9666    0.4152
##     -0.9382   -1.0132    0.0262   -0.7591
##      0.0346   -1.0132    0.0262   -0.3598
##      1.0074   -1.0132    0.0262    0.0395
##     -0.9382   -1.0132    1.0190   -0.3694
##      0.0346   -1.0132    1.0190   -0.3528
##      1.0074   -1.0132    1.0190   -0.3361
##     -0.9382   -0.0023   -0.9666   -0.2972
##      0.0346   -0.0023   -0.9666    0.0424
##      1.0074   -0.0023   -0.9666    0.3819
##     -0.9382   -0.0023    0.0262   -0.3603
##      0.0346   -0.0023    0.0262    0.0761
##      1.0074   -0.0023    0.0262    0.5125
##     -0.9382   -0.0023    1.0190   -0.4234
##      0.0346   -0.0023    1.0190    0.1098
##      1.0074   -0.0023    1.0190    0.6430
##     -0.9382    1.0086   -0.9666    0.5544
##      0.0346    1.0086   -0.9666    0.4515
##      1.0074    1.0086   -0.9666    0.3486
##     -0.9382    1.0086    0.0262    0.0385
##      0.0346    1.0086    0.0262    0.5119
##      1.0074    1.0086    0.0262    0.9854
##     -0.9382    1.0086    1.0190   -0.4775
##      0.0346    1.0086    1.0190    0.5723
##      1.0074    1.0086    1.0190    1.6221
## 
## *********************************************************************** 
## Bootstrapping progress:
## 
  |                                                                    
  |                                                              |   0%
  |                                                                    
  |                                                              |   1%
  |                                                                    
  |>                                                             |   1%
  |                                                                    
  |>                                                             |   2%
  |                                                                    
  |>>                                                            |   2%
  |                                                                    
  |>>                                                            |   3%
  |                                                                    
  |>>                                                            |   4%
  |                                                                    
  |>>>                                                           |   4%
  |                                                                    
  |>>>                                                           |   5%
  |                                                                    
  |>>>                                                           |   6%
  |                                                                    
  |>>>>                                                          |   6%
  |                                                                    
  |>>>>                                                          |   7%
  |                                                                    
  |>>>>>                                                         |   7%
  |                                                                    
  |>>>>>                                                         |   8%
  |                                                                    
  |>>>>>                                                         |   9%
  |                                                                    
  |>>>>>>                                                        |   9%
  |                                                                    
  |>>>>>>                                                        |  10%
  |                                                                    
  |>>>>>>>                                                       |  10%
  |                                                                    
  |>>>>>>>                                                       |  11%
  |                                                                    
  |>>>>>>>                                                       |  12%
  |                                                                    
  |>>>>>>>>                                                      |  12%
  |                                                                    
  |>>>>>>>>                                                      |  13%
  |                                                                    
  |>>>>>>>>                                                      |  14%
  |                                                                    
  |>>>>>>>>>                                                     |  14%
  |                                                                    
  |>>>>>>>>>                                                     |  15%
  |                                                                    
  |>>>>>>>>>>                                                    |  15%
  |                                                                    
  |>>>>>>>>>>                                                    |  16%
  |                                                                    
  |>>>>>>>>>>                                                    |  17%
  |                                                                    
  |>>>>>>>>>>>                                                   |  17%
  |                                                                    
  |>>>>>>>>>>>                                                   |  18%
  |                                                                    
  |>>>>>>>>>>>                                                   |  19%
  |                                                                    
  |>>>>>>>>>>>>                                                  |  19%
  |                                                                    
  |>>>>>>>>>>>>                                                  |  20%
  |                                                                    
  |>>>>>>>>>>>>>                                                 |  20%
  |                                                                    
  |>>>>>>>>>>>>>                                                 |  21%
  |                                                                    
  |>>>>>>>>>>>>>                                                 |  22%
  |                                                                    
  |>>>>>>>>>>>>>>                                                |  22%
  |                                                                    
  |>>>>>>>>>>>>>>                                                |  23%
  |                                                                    
  |>>>>>>>>>>>>>>>                                               |  23%
  |                                                                    
  |>>>>>>>>>>>>>>>                                               |  24%
  |                                                                    
  |>>>>>>>>>>>>>>>                                               |  25%
  |                                                                    
  |>>>>>>>>>>>>>>>>                                              |  25%
  |                                                                    
  |>>>>>>>>>>>>>>>>                                              |  26%
  |                                                                    
  |>>>>>>>>>>>>>>>>                                              |  27%
  |                                                                    
  |>>>>>>>>>>>>>>>>>                                             |  27%
  |                                                                    
  |>>>>>>>>>>>>>>>>>                                             |  28%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>                                            |  28%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>                                            |  29%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>                                            |  30%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>                                           |  30%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>                                           |  31%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>                                          |  31%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>                                          |  32%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>                                          |  33%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>                                         |  33%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>                                         |  34%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>                                         |  35%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>                                        |  35%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>                                        |  36%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>                                       |  36%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>                                       |  37%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>                                       |  38%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>                                      |  38%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>                                      |  39%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>                                      |  40%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>                                     |  40%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>                                     |  41%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>                                    |  41%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>                                    |  42%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>                                    |  43%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>                                   |  43%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>                                   |  44%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>                                  |  44%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>                                  |  45%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>                                  |  46%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                                 |  46%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                                 |  47%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                                 |  48%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                                |  48%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                                |  49%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                               |  49%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                               |  50%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                               |  51%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                              |  51%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                              |  52%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                             |  52%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                             |  53%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                             |  54%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                            |  54%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                            |  55%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                            |  56%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                           |  56%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                           |  57%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                          |  57%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                          |  58%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                          |  59%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                         |  59%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                         |  60%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                        |  60%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                        |  61%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                        |  62%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                       |  62%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                       |  63%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                       |  64%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                      |  64%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                      |  65%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                     |  65%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                     |  66%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                     |  67%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                    |  67%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                    |  68%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                    |  69%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                   |  69%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                   |  70%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                  |  70%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                  |  71%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                  |  72%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                 |  72%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                 |  73%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                |  73%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                |  74%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                |  75%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>               |  75%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>               |  76%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>               |  77%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>              |  77%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>              |  78%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>             |  78%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>             |  79%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>             |  80%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>            |  80%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>            |  81%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>           |  81%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>           |  82%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>           |  83%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>          |  83%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>          |  84%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>          |  85%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>         |  85%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>         |  86%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>        |  86%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>        |  87%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>        |  88%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>       |  88%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>       |  89%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>       |  90%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>      |  90%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>      |  91%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>     |  91%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>     |  92%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>     |  93%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>    |  93%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>    |  94%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>   |  94%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>   |  95%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>   |  96%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  |  96%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  |  97%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  |  98%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |  98%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |  99%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>|  99%
  |                                                                    
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>| 100%
## 
## ********** BOOTSTRAP RESULTS FOR REGRESSION MODEL PARAMETERS **********
## 
## Outcome variable: Y
## 
##              Coeff  BootMean    BootSE  BootLLCI  BootULCI
## constant    0.0608    0.0613    0.0455   -0.0268    0.1484
## X           0.4460    0.4469    0.0488    0.3543    0.5422
## Z1          0.4296    0.4287    0.0469    0.3352    0.5218
## Int_1       0.0248    0.0264    0.0466   -0.0662    0.1176
## Z2          0.0305    0.0304    0.0435   -0.0551    0.1156
## Int_2       0.1014    0.1048    0.0442    0.0178    0.1907
## Int_3       0.0096    0.0094    0.0412   -0.0733    0.0893
## Int_4       0.4911    0.4911    0.0450    0.4011    0.5762
## 
## ******************** ANALYSIS NOTES AND ERRORS ************************ 
## 
## Level of confidence for all confidence intervals in output: 95
## 
## Number of bootstraps for percentile bootstrap confidence intervals: 5000
## 
## W values in conditional tables are the mean and +/- SD from the mean.
## 
## Z values in conditional tables are the mean and +/- SD from the mean.
#畫圖
m3 <- lm(Y ~ X+Z1+Z2+X:Z1+X:Z2+Z1:Z2+X:Z1:Z2, data = dta)

interactions::interact_plot(m3, pred = X, modx = Z1,mod2=Z2, interval = TRUE,
  int.type = "confidence", int.width = .8)

一個獨變項,兩個調節變項

方法二:調節效果,用 lavaan 功能分析

dta$int1 <- dta$X*dta$Z1
dta$int2 <- dta$X*dta$Z2
dta$int3 <- dta$Z1*dta$Z2
dta$int4 <- dta$X*dta$Z1*dta$Z2

k11 <- mean(dta$Z1)-sd(dta$Z1)
k12 <- mean(dta$Z1)
k13 <- mean(dta$Z1)+sd(dta$Z1)
round(c(k11,k12,k13),3)
## [1] -1.013 -0.002  1.009
k21 <- mean(dta$Z2)-sd(dta$Z2)
k22 <- mean(dta$Z2)
k23 <- mean(dta$Z2)+sd(dta$Z2)
round(c(k21,k22,k23),3)
## [1] -0.967  0.026  1.019
model1 <-'
Y ~ b1*X + b2*Z1 + b3*Z2 + b4*int1 + b5*int2 + b6*int3 + b7*int4
sslope11 := b1+b4*(-1.013)+b5*(-0.967)+b7*(-1.013*-0.967)
sslope12 := b1+b4*(-1.013)+b5*(0.026)+b7*(-1.013*0.026)
sslope13 := b1+b4*(-1.013)+b5*(1.019)+b7*(-1.013*1.019)
sslope21 := b1+b4*(-0.002)+b5*(-0.967)+b7*(-0.002*-0.967)
sslope22 := b1+b4*(-0.002)+b5*(0.026)+b7*(-0.002*0.026)
sslope23 := b1+b4*(-0.002)+b5*(1.019)+b7*(-0.002*1.019)
sslope31 := b1+b4*(1.009)+b5*(-0.967)+b7*(1.009*-0.967)
sslope32 := b1+b4*(1.009)+b5*(0.026)+b7*(1.009*0.026)
sslope33 := b1+b4*(1.009)+b5*(1.019)+b7*(1.009*1.019)
'
#徑路分析報表
fit <- lavaan::sem(model1, data=dta)
summary(fit)
## lavaan 0.6.15 ended normally after 1 iteration
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         8
## 
##   Number of observations                           500
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   Y ~                                                 
##     X         (b1)    0.446    0.046    9.633    0.000
##     Z1        (b2)    0.430    0.045    9.510    0.000
##     Z2        (b3)    0.031    0.045    0.671    0.502
##     int1      (b4)    0.025    0.047    0.530    0.596
##     int2      (b5)    0.101    0.050    2.037    0.042
##     int3      (b6)    0.010    0.043    0.223    0.824
##     int4      (b7)    0.491    0.048   10.164    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .Y                 1.003    0.063   15.811    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     sslope11          0.804    0.101    7.936    0.000
##     sslope12          0.411    0.065    6.325    0.000
##     sslope13          0.017    0.091    0.190    0.849
##     sslope21          0.349    0.067    5.216    0.000
##     sslope22          0.449    0.046    9.686    0.000
##     sslope23          0.548    0.069    7.993    0.000
##     sslope31         -0.106    0.097   -1.095    0.273
##     sslope32          0.487    0.067    7.237    0.000
##     sslope33          1.079    0.094   11.502    0.000
#以拔靴法看徑路係數與簡單效果信賴區間
set.seed(1234)
fit <- lavaan::sem(model1, data=dta, test="bootstrap", bootstrap=501)
parameterEstimates(fit,ci=TRUE,boot.ci.type="bca.simple")
lhsoprhslabelestsezpvalueci.lowerci.upper
Y~Xb10.446  0.04639.63 0       0.355  0.537  
Y~Z1b20.43   0.04529.51 0       0.341  0.518  
Y~Z2b30.0305 0.04550.6710.502   -0.0586 0.12   
Y~int1b40.0248 0.04690.53 0.596   -0.067  0.117  
Y~int2b50.101  0.04982.04 0.0417  0.003820.199  
Y~int3b60.0096 0.04310.2230.824   -0.0749 0.0941 
Y~int4b70.491  0.048310.2  0       0.396  0.586  
Y~~Y1      0.063415.8  0       0.878  1.13   
X~~X0.944  0                0.944  0.944  
X~~Z1-0.051  0                -0.051  -0.051  
X~~Z20.0565 0                0.0565 0.0565 
X~~int1-0.0485 0                -0.0485 -0.0485 
X~~int20.006330                0.006330.00633
X~~int3-0.0223 0                -0.0223 -0.0223 
X~~int4-0.0442 0                -0.0442 -0.0442 
Z1~~Z11.02   0                1.02   1.02   
Z1~~Z20.051  0                0.051  0.051  
Z1~~int10.134  0                0.134  0.134  
Z1~~int2-0.0204 0                -0.0204 -0.0204 
Z1~~int30.131  0                0.131  0.131  
Z1~~int40.0454 0                0.0454 0.0454 
Z2~~Z20.984  0                0.984  0.984  
Z2~~int1-0.0192 0                -0.0192 -0.0192 
Z2~~int20.0288 0                0.0288 0.0288 
Z2~~int30.0401 0                0.0401 0.0401 
Z2~~int4-0.0644 0                -0.0644 -0.0644 
int1~~int10.972  0                0.972  0.972  
int1~~int2-0.042  0                -0.042  -0.042  
int1~~int30.0481 0                0.0481 0.0481 
int1~~int40.191  0                0.191  0.191  
int2~~int20.817  0                0.817  0.817  
int2~~int3-0.0679 0                -0.0679 -0.0679 
int2~~int40.0214 0                0.0214 0.0214 
int3~~int31.11   0                1.11   1.11   
int3~~int40.0832 0                0.0832 0.0832 
int4~~int40.908  0                0.908  0.908  
sslope11:=b1+b4*(-1.013)+b5*(-0.967)+b7*(-1.013*-0.967)sslope110.804  0.101 7.94 2e-15       0.605  1      
sslope12:=b1+b4*(-1.013)+b5*(0.026)+b7*(-1.013*0.026)sslope120.411  0.06496.32 2.54e-100.283  0.538  
sslope13:=b1+b4*(-1.013)+b5*(1.019)+b7*(-1.013*1.019)sslope130.0172 0.09050.19 0.849   -0.16   0.195  
sslope21:=b1+b4*(-0.002)+b5*(-0.967)+b7*(-0.002*-0.967)sslope210.349  0.06695.22 1.83e-070.218  0.48   
sslope22:=b1+b4*(-0.002)+b5*(0.026)+b7*(-0.002*0.026)sslope220.449  0.04639.69 0       0.358  0.539  
sslope23:=b1+b4*(-0.002)+b5*(1.019)+b7*(-0.002*1.019)sslope230.548  0.06867.99 1.33e-150.414  0.683  
sslope31:=b1+b4*(1.009)+b5*(-0.967)+b7*(1.009*-0.967)sslope31-0.106  0.0969-1.1  0.273   -0.296  0.0838 
sslope32:=b1+b4*(1.009)+b5*(0.026)+b7*(1.009*0.026)sslope320.487  0.06727.24 4.59e-130.355  0.618  
sslope33:=b1+b4*(1.009)+b5*(1.019)+b7*(1.009*1.019)sslope331.08   0.093811.5  0       0.895  1.26   
#畫圖看模型與估計值
lavaanPlot::lavaanPlot(model = fit,
                       edge_options = list(color = "grey"), 
                       coefs = TRUE,
                       stand = TRUE)
#畫圖
m3 <- lm(Y ~ X+Z1+Z2+X:Z1+X:Z2+Z1:Z2+X:Z1:Z2, data = dta)

interactions::interact_plot(m3, pred = X, modx = Z1,mod2=Z2, interval = TRUE,
  int.type = "confidence", int.width = .8)