Closed Alaiti closed 4 years ago
It's difficult to know the reason, but I would not use the variable names like Idade..anos.
which may cause some unexpeted behavior.
I'm experiencing a similar error. Running mediation
v4.5.0.
Replicating the framing
data example from the mediation
vignette, such that:
med.fit <- lm(emo ~ treat + age + educ + gender + income, data = framing)
out.fit <- glm(cong_mesg ~ emo + treat + age + educ + gender + income, data = framing, family = binomial("probit"))
med.out <- mediate(med.fit, out.fit, treat = "treat", mediator = "emo", robustSE = TRUE, sims = 100)
and then subsequently:
sens.out <- medsens(med.out, rho.by = 0.1, effect.type = "indirect", sims = 100)
summary(sens.out)
This works fine and as expected.
Simple mediation test, beginning with:
N <- 100
X <- c(rep("Treat", N/2), rep("Control", N/2))
sd <- 0.1
M <- c(rnorm(N/4, 4, sd), rnorm(N/4, 3, sd), rnorm(N/4, 2, sd), rnorm(N/4, 1, sd))
Y <- c(rnorm(N/4, 4, 0.5), rnorm(N/4, 3, 0.5), rnorm(N/4, 2, 0.5), rnorm(N/4, 1, 0.5))
then constructing some models:
mod_m_x <- lm(M ~ X)
mod_y_mx <- lm(Y ~ X*M)
and finally, building a simple mediation model:
s <- mediate(model.m = mod_m_x,
model.y = mod_y_mx,
treat = "X",
mediator = "M",
boot = T,
boot.ci.type = "bca",
sims = 1000)
Now, we find:
ms <- medsens(s)
summary(ms)
leads to the error: Error in m.coefs[T.out, ] : subscript out of bounds
While it could be due to variable names, I find that unlikely considering "X" and "M", with trivial levels names on the "X" factor, work fine for the rest of the package's functions and are as simple as one could hope.
I have tested not using bootstrap (I think one of the main differences between the two examples), which leads to no change. Another difference I see is the probit model in Case 1, compared to a gaussian model in the second. Advice would be appreciated, hopefully my code reproduces.
(deleted my previous post) -- Correction: I can reproduce the case 2 error via reprex
library(mediation)
#> Loading required package: MASS
#> Loading required package: Matrix
#> Loading required package: mvtnorm
#> Loading required package: sandwich
#> mediation: Causal Mediation Analysis
#> Version: 4.5.0
# Simple mediation test, beginning with:
N <- 100
X <- c(rep("Treat", N/2), rep("Control", N/2))
sd <- 0.1
M <- c(rnorm(N/4, 4, sd), rnorm(N/4, 3, sd), rnorm(N/4, 2, sd), rnorm(N/4, 1, sd))
Y <- c(rnorm(N/4, 4, 0.5), rnorm(N/4, 3, 0.5), rnorm(N/4, 2, 0.5), rnorm(N/4, 1, 0.5))
# then constructing some models:
mod_m_x <- lm(M ~ X)
mod_y_mx <- lm(Y ~ X*M)
# and finally, building a simple mediation model:
s <- mediate(model.m = mod_m_x,
model.y = mod_y_mx,
treat = "X",
mediator = "M",
boot = TRUE,
boot.ci.type = "bca",
sims = 5)
#> Warning in mediate(model.m = mod_m_x, model.y = mod_y_mx, treat = "X", mediator
#> = "M", : treatment and control values do not match factor levels; using Control
#> and Treat as control and treatment, respectively
#> Running nonparametric bootstrap
# medsens
medsens(s)
#> Error in m.coefs[T.out, ]: subscript out of bounds
Created on 2020-07-11 by the reprex package (v0.3.0)
Can you code the treatment as a factor variable and see if that solves the problem?
That fixes it for this example! It can probably be documented in the help page.
library(mediation)
#> Loading required package: MASS
#> Loading required package: Matrix
#> Loading required package: mvtnorm
#> Loading required package: sandwich
#> mediation: Causal Mediation Analysis
#> Version: 4.5.0
# Simple mediation test, beginning with:
N <- 100
X <- factor(c(rep("Treat", N/2), rep("Control", N/2)))
sd <- 0.1
M <- c(rnorm(N/4, 4, sd), rnorm(N/4, 3, sd), rnorm(N/4, 2, sd), rnorm(N/4, 1, sd))
Y <- c(rnorm(N/4, 4, 0.5), rnorm(N/4, 3, 0.5), rnorm(N/4, 2, 0.5), rnorm(N/4, 1, 0.5))
# then constructing some models:
mod_m_x <- lm(M ~ X)
mod_y_mx <- lm(Y ~ X*M)
# and finally, building a simple mediation model:
s <- mediate(model.m = mod_m_x,
model.y = mod_y_mx,
treat = "X",
mediator = "M",
boot = TRUE,
boot.ci.type = "bca",
sims = 5)
#> Warning in mediate(model.m = mod_m_x, model.y = mod_y_mx, treat = "X", mediator
#> = "M", : treatment and control values do not match factor levels; using Control
#> and Treat as control and treatment, respectively
#> Running nonparametric bootstrap
# medsens
summary(medsens(s))
#>
#> Mediation Sensitivity Analysis: Average Mediation Effect
#>
#> Sensitivity Region: ACME for Control Group
#>
#> Rho ACME(control) 95% CI Lower 95% CI Upper R^2_M*R^2_Y* R^2_M~R^2_Y~
#> [1,] 0.5 0.4905 -0.1423 1.1233 0.25 0.0113
#> [2,] 0.6 0.0922 -0.5388 0.7232 0.36 0.0162
#> [3,] 0.7 -0.4389 -1.0713 0.1935 0.49 0.0221
#>
#> Rho at which ACME for Control Group = 0: 0.6
#> R^2_M*R^2_Y* at which ACME for Control Group = 0: 0.36
#> R^2_M~R^2_Y~ at which ACME for Control Group = 0: 0.0162
#>
#>
#> Sensitivity Region: ACME for Treatment Group
#>
#> Rho ACME(treated) 95% CI Lower 95% CI Upper R^2_M*R^2_Y* R^2_M~R^2_Y~
#> [1,] 0.4 0.5516 -0.1058 1.2091 0.16 0.0072
#> [2,] 0.5 0.2265 -0.4290 0.8821 0.25 0.0113
#> [3,] 0.6 -0.1718 -0.8272 0.4836 0.36 0.0162
#>
#> Rho at which ACME for Treatment Group = 0: 0.6
#> R^2_M*R^2_Y* at which ACME for Treatment Group = 0: 0.36
#> R^2_M~R^2_Y~ at which ACME for Treatment Group = 0: 0.0162
Created on 2020-07-11 by the reprex package (v0.3.0)
Good find, thank you!
Thanks for catching this. I'm guessing this is probably because stringsAsFactors now defaults to FALSE as of R 4.0. We'll look into it and release a fix.
I'm having trouble with mediation:: medsens()
The code works fine until med.out, but when I try to run a sensitivity analysis using medsens I get:
Someone might help me?
Thanks!