kosukeimai / mediation

R package mediation
58 stars 29 forks source link

Error in if (xhat == 0) out <- 1 else { : missing value where TRUE/FALSE needed #21

Open almogsi opened 4 years ago

almogsi commented 4 years ago

I'm having trouble with mediation::mediate()

nb.terror.y.anx <- glm(data = terror_removed2,
                   certain~cond + event + WC + anx,
                   control = glm.control(maxit = 50000),
                   family = "poisson")

nb.terror.m.anx <- glm(data = terror_removed2,
                   anx~cond + event + WC + certain,
                   control = glm.control(maxit = 50000),
                   family = "poisson")

med.anx <- mediation::mediate(nb.terror.m.anx,nb.terror.y.anx, treat = "cond", 
                              mediator = "anx", sim = 2)

Works fine, but whenever sim>2, I get:

Error in if (xhat == 0) out <- 1 else { : 
  missing value where TRUE/FALSE needed

I saw there's still an open topic in a realted issue

Hope it's ok to flag it up again.

Thanks!

mdtrinh commented 4 years ago

I can't replicate this without the data but #24 is likely to fix this problem

DrLilei commented 3 years ago

this may help you to fix this problem. in mediate.R, you can find "For binary response models, the 'mediator' must be a numeric variable with values 0 or 1 as opposed to a factor." (Line 53-54)

elitefan04 commented 2 years ago

I'm having trouble with mediation::mediate()

nb.terror.y.anx <- glm(data = terror_removed2,
                   certain~cond + event + WC + anx,
                   control = glm.control(maxit = 50000),
                   family = "poisson")

nb.terror.m.anx <- glm(data = terror_removed2,
                   anx~cond + event + WC + certain,
                   control = glm.control(maxit = 50000),
                   family = "poisson")

med.anx <- mediation::mediate(nb.terror.m.anx,nb.terror.y.anx, treat = "cond", 
                              mediator = "anx", sim = 2)

Works fine, but whenever sim>2, I get:

Error in if (xhat == 0) out <- 1 else { : 
  missing value where TRUE/FALSE needed

I saw there's still an open topic in a realted issue

Hope it's ok to flag it up again.

Thanks!

@almogsi Hello! I met the same problem. Can I ask how did you solve this problem eventually? Many thanks!

b-staley commented 1 year ago

I am also running into the issue and getting this error. The response in #24 has not helped. I have a continuous mediator, binary (0/1) exposure and survival outcome. Could someone please advise?

Error in if (xhat == 0) out <- 1 else { : missing value where TRUE/FALSE needed

Effy-runrun commented 1 year ago

I am also running into the issue and getting this error. The response in #24 has not helped. I have a continuous mediator, binary (0/1) exposure and survival outcome. Could someone please advise?

Error in if (xhat == 0) out <- 1 else { : missing value where TRUE/FALSE needed

Hi, I'm wondering if you have found a solution to this issue?

Sabrinaeder1424 commented 1 year ago

@b-staley I have the same issue, did you figure it out? : )

b-staley commented 1 year ago

@Sabrinaeder1424 and @Effy-runrun, I have not found a solution yet. Please let me know if you do.

Best,

Brooke

Effy-runrun commented 1 year ago

@Sabrinaeder1424 and @Effy-runrun, I have not found a solution yet. Please let me know if you do.

Best,

Brooke

Hi, I have given up trying with this package, and found another package call "regmedint" while searching. It was developed based on SAS and SPSS macro "mediation" by Valeri & VanderWeele, which also allows for mediation analysis concerning survival data. I succeeded in analyzing my data with this package, and the authors have written a detailed paper on how to use this package. https://www.researchgate.net/publication/359526597_A_Brief_Primer_on_Conducting_Regression-Based_Causal_Mediation_Analysis

Hope this will work for you too.

alessiohappy commented 1 year ago

I was facing the same problem using a continuous exposure and a binary mediator. Here's what I did:

  1. df$mediator <- as.factor(df$mediator)

  2. df$mediator <- as.numeric(df$mediator) The binary mediator records were so "converted" from 0 and 1 to 1 and 2, respectively.

  3. df$mediator <- ifelse(df$mediator == 1, 0, ifelse(df$mediator == 2, 1, df$mediator))

df$mediator <- ifelse(df$mediator == 1, 0, ifelse(df$mediator == 2, 1, df$mediator))

Finally, i run the mediate function once more and that worked with no more issues.

liulinsmu commented 10 months ago

Thanks for @alessiohappy inspiration. I also found a similar solution. To rum mediate function, we have to keep the specific class of variables.

  1. Outcome variables and mediators should be continuous variables with values "0" and "1", despite glm can fit the model with character outcomes, but it seems that mediate can not.
  2. Exposure variables should be binary or continuous.

I don't know why we have to set the class of the variables, but it indeed works.