在R中进行具有两个中介变量的中介调节分析。

huangapple go评论85阅读模式
英文:

Mediated moderation analysis with two mediators in R

问题

这是我的数据框(df)。IV1和IV2之间存在显著的交互作用。我想要执行多重中介分析,其中两个中介变量(M1和M2)解释了IV1和DV之间的关联。IV2调节了DV和中介变量之间的关联。

id <- rep(c(1, 2, 3), each = 4)
IV1 <- rep(c(2, 1), each = 2, times = 3)
IV2 <- rep(c(1, 2), times = 6)
DV <- c(4,3,2,4,4,4,5,4,4,4,3,5)
M1 <- c(1,1,1,3,3,5,3,4,3,4,2,4)
M2 <- c(3,4,4,4,4,5,4,5,4,4,4,4)
df2 <- data.frame(id, IV1, IV2, DV, M1, M2)
df2
> df2
# A tibble: 696 × 6
      ID IV1   IV2      DV    M1    M2
   <dbl> <fct> <fct> <dbl> <dbl> <dbl>
 1     1 2     1         4     1     3
 2     1 2     2         3     1     4
 3     1 1     1         2     1     4
 4     1 1     2         4     3     4
 5     2 2     1         4     3     4
 6     2 2     2         4     5     5
 7     2 1     1         5     3     4
 8     2 1     2         4     4     5
 9     3 2     1         4     3     4
10     3 2     2         4     4     4
# … with 686 more rows
# ℹ Use `print(n = ...)` to see more rows

我尝试了mediation和lavaan包,但未能获得结果。

model <- "
  # 从IV到中介变量的回归路径
  M1 ~ IV1 * IV2
  M2 ~ IV1 * IV2
  
  # 从中介变量到DV的回归路径
  DV ~ IV1 + IV2
  
  # 间接效应
  indirect1 := IV1 * IV2 * M1
  indirect2 := IV1 * IV2 * M2
"

fit <- lavaan::cfa(model, data = df2)

> Error in lav_partable_constraints_def(partable, con = LIST, debug = debug) : 
  lavaan ERROR: unknown label(s) in variable definition(s): IV2 M1 M2

# 提取中介分析结果
mediation_results <- lavaan::parameterEstimates(fit)
英文:

This is my df. There is a significant interaction between IV1 and IV2. I'd like to perform a multiple mediation analysis where the two mediators (M1 and M2) explain the association between IV1 and DV. IV2 moderates the association between the DV and the mediators.

id <- rep(c(1, 2, 3), each = 4)
IV1 <- rep(c(2, 1), each = 2, times = 3)
IV2 <- rep(c(1, 2), times = 6)
DV <- c(4,3,2,4,4,4,5,4,4,4,3,5)
M1 <- c(1,1,1,3,3,5,3,4,3,4,2,4)
M2 <- c(3,4,4,4,4,5,4,5,4,4,4,4)
df2 <- data.frame(id, IV1, IV2, DV, M1, M2)
df2
> df2
# A tibble: 696 × 6
      ID IV1   IV2      DV    M1    M2
   <dbl> <fct> <fct> <dbl> <dbl> <dbl>
 1     1 2     1         4     1     3
 2     1 2     2         3     1     4
 3     1 1     1         2     1     4
 4     1 1     2         4     3     4
 5     2 2     1         4     3     4
 6     2 2     2         4     5     5
 7     2 1     1         5     3     4
 8     2 1     2         4     4     5
 9     3 2     1         4     3     4
10     3 2     2         4     4     4
# … with 686 more rows
# ℹ Use `print(n = ...)` to see more rows

I tried mediation and laavan packages but failed to get results.

model <- "
  # Regression paths from IV to mediators
  M1 ~ IV1 * IV2
  M2 ~ IV1 * IV2
  
  # Regression paths from mediators to DV
  DV ~ IV1 + IV2
  
  # Indirect effects
  indirect1 := IV1 * IV2 * M1
  indirect2 := IV1 * IV2 * M2
"

fit <- lavaan::cfa(model, data = df2)

> Error in lav_partable_constraints_def(partable, con = LIST, debug = debug) : 
  lavaan ERROR: unknown label(s) in variable definition(s): IV2 M1 M2

# Extract mediation results
mediation_results <- lavaan::parameterEstimates(fit)

答案1

得分: 1

以下是您要翻译的内容:

"I think the problem is how you're specifying the interactions. Rather than using IV1*IV2, you need IV1 + IV2 + IV1:IV2. However, it may be easier to calculate the indirect effects if you supply parameter names for the paths. You can do this by putting the parameter name before the variable and then separating the parameter and variable name with an asterisk (e.g., a1*IV1 + a2*IV2 + a3*IV1:IV2). The second problem is how you're calculating the indirect effect. If you think about the mathematical notation of the model, you're proposing something like this (as far as I can tell)"

在下面的代码中,我使用了三种不同水平的 IV2 来计算 IV1 的间接效应:均值 (1.5)、均值减去一个标准差 (1) 和均值加上一个标准差 (2)。我也对 IV1 的值在 IV2 的间接效应中做了相同的处理。

library(lavaan)
#> This is lavaan 0.6-15
#> lavaan is FREE software! Please report any bugs.
id <- rep(c(1, 2, 3), each = 4)
IV1 <- rep(c(2, 1), each = 2, times = 3)
IV2 <- rep(c(1, 2), times = 6)
DV <- c(4,3,2,4,4,4,5,4,4,4,3,5)
M1 <- c(1,1,1,3,3,5,3,4,3,4,2,4)
M2 <- c(3,4,4,4,4,5,4,5,4,4,4,4)
df2 <- data.frame(id, IV1, IV2, DV, M1, M2)
df2
#>    id IV1 IV2 DV M1 M2
#> 1   1   2   1  4  1  3
#> 2   1   2   2  3  1  4
#> 3   1   1   1  2  1  4
#> 4   1   1   2  4  3  4
#> 5   2   2   1  4  3  4
#> 6   2   2   2  4  5  5
#> 7   2   1   1  5  3  4
#> 8   2   1   2  4  4  5
#> 9   3   2   1  4  3  4
#> 10  3   2   2  4  4  4
#> 11  3   1   1  3  2  4
#> 12  3   1   2  5  4  4
model <- "
  # Regression paths from IV to mediators
  M1 ~ a1*IV1 + a2*IV2 + a3*IV1:IV2
  M2 ~ b1*IV1 + b2*IV2 + b3*IV1:IV2
  
  # Regression paths from mediators to DV
  DV ~ c1*M1 + c2*M2
  
  # Indirect effects
  indirect1_lo := (a1 + a3*1)*c1 + (b1 + b3*1)*c2
  indirect1_ave := (a1 + a3*1.5)*c1 + (b1 + b3*1.5)*c2
  indirect1_lo := (a1 + a3*2)*c1 + (b1 + b3*2)*c2
  indirect2_lo := (a2 + a3*1)*c1 + (b2 + b3*1)*c2
  indirect2_ave := (a2 + a3*1.5)*c1 + (b2 + b3*1.5)*c2
  indirect2_lo := (a2 + a3*2)*c1 + (b2 + b3*2)*c2
";

fit <- lavaan::cfa(model, data = df2)
fit
#> lavaan 0.6.15 ended normally after 1 iteration
#> 
#>   Estimator                                         ML
#>   Optimization method                           NLMINB
#>   Number of model parameters                        11
#> 
#>   Number of observations                            12
#> 
#> Model Test User Model:
#>                                                       
#>   Test statistic                                 7.563
#>   Degrees of freedom                                 4
#>   P-value (Chi-square)                           0.109

创建于 2023-07-03,使用 reprex v2.0.2

英文:

I think the problem is how you're specifying the interactions. Rather than using IV1*IV2, you need IV1 + IV2 + IV1:IV2. However, it may be easier to calculate the indirect effects if you supply parameter names for the paths. You can do this by putting the parameter name before the variable and then separating the parameter and variable name with an asterisk (e.g., a1*IV1 + a2*IV2 + a3*IV1:IV2). The second problem is how you're calculating the indirect effect. If you think about the mathematical notation of the model, you're proposing something like this (as far as I can tell)

在R中进行具有两个中介变量的中介调节分析。

In the code below, I use three different levels of IV2 to calculate the indirect effect of IV1: the mean (1.5), the mean minus a SD (1) and the mean plus a SD (2). I did the same for the values of IV1 in the indirect effect of IV2.

library(lavaan)
#&gt; This is lavaan 0.6-15
#&gt; lavaan is FREE software! Please report any bugs.
id &lt;- rep(c(1, 2, 3), each = 4)
IV1 &lt;- rep(c(2, 1), each = 2, times = 3)
IV2 &lt;- rep(c(1, 2), times = 6)
DV &lt;- c(4,3,2,4,4,4,5,4,4,4,3,5)
M1 &lt;- c(1,1,1,3,3,5,3,4,3,4,2,4)
M2 &lt;- c(3,4,4,4,4,5,4,5,4,4,4,4)
df2 &lt;- data.frame(id, IV1, IV2, DV, M1, M2)
df2
#&gt;    id IV1 IV2 DV M1 M2
#&gt; 1   1   2   1  4  1  3
#&gt; 2   1   2   2  3  1  4
#&gt; 3   1   1   1  2  1  4
#&gt; 4   1   1   2  4  3  4
#&gt; 5   2   2   1  4  3  4
#&gt; 6   2   2   2  4  5  5
#&gt; 7   2   1   1  5  3  4
#&gt; 8   2   1   2  4  4  5
#&gt; 9   3   2   1  4  3  4
#&gt; 10  3   2   2  4  4  4
#&gt; 11  3   1   1  3  2  4
#&gt; 12  3   1   2  5  4  4
model &lt;- &quot;
  # Regression paths from IV to mediators
  M1 ~ a1*IV1 + a2*IV2 + a3*IV1:IV2
  M2 ~ b1*IV1 + b2*IV2 + b3*IV1:IV2
  
  # Regression paths from mediators to DV
  DV ~ c1*M1 + c2*M2
  
  # Indirect effects
  indirect1_lo := (a1 + a3*1)*c1 + (b1 + b3*1)*c2
  indirect1_ave := (a1 + a3*1.5)*c1 + (b1 + b3*1.5)*c2
  indirect1_lo := (a1 + a3*2)*c1 + (b1 + b3*2)*c2
  indirect2_lo := (a2 + a3*1)*c1 + (b2 + b3*1)*c2
  indirect2_ave := (a2 + a3*1.5)*c1 + (b2 + b3*1.5)*c2
  indirect2_lo := (a2 + a3*2)*c1 + (b2 + b3*2)*c2
&quot;

fit &lt;- lavaan::cfa(model, data = df2)
fit
#&gt; lavaan 0.6.15 ended normally after 1 iteration
#&gt; 
#&gt;   Estimator                                         ML
#&gt;   Optimization method                           NLMINB
#&gt;   Number of model parameters                        11
#&gt; 
#&gt;   Number of observations                            12
#&gt; 
#&gt; Model Test User Model:
#&gt;                                                       
#&gt;   Test statistic                                 7.563
#&gt;   Degrees of freedom                                 4
#&gt;   P-value (Chi-square)                           0.109

<sup>Created on 2023-07-03 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年7月3日 18:24:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定