英文:
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)
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)
#> 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
<sup>Created on 2023-07-03 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论