kosukeimai / mediation

R package mediation
58 stars 29 forks source link

Problems in ivmediate function #41

Open JHPark9090 opened 2 years ago

JHPark9090 commented 2 years ago

Hello, Thank you for developing such a great package. It is highly useful in implementing many different kinds of mediation analyses, and I'm getting a lot of help for my projects. But recently, I ran into trouble while using the "ivmediate" function.

The codes I ran was like this: `############################################## IV Mediation with mediation package ##############################

library(mediation) library(car) library(fastDummies)

treatment = 'rh_adi_bi_0y' instrument = 'section8_0y' mediator = 'discount_rate_1y_z' outcome = 'totalscore_pps_1y_z'

data<-dummy_cols(data, select_columns = c('sex_0y')) data$sex_0y<-as.factor(data$sex_0y) data$married_0y<-as.factor(data$married_0y) data$race_g<-as.factor(data$race_g) data$parent_identity_0y<-as.factor(data$parent_identity_0y) data$gender_identity_0y<-as.factor(data$gender_identity_0y) data$foreign_born_0y<-as.factor(data$foreign_born_0y) data$foreign_born_family_0y<-as.factor(data$foreign_born_family_0y) data$gay_parent_0y<-as.factor(data$gay_parent_0y) data$gay_youth_0y<-as.factor(data$gay_youth_0y) data$race_ethnicity_0y<-as.factor(data$race_ethnicity_0y)

fmla1 <- as.formula("rh_adi_bi_0y~section8_0y+nihtbx_totalcomp_uncorrected_0y_z+iqeur2+ vol_z+age_0y_z+bmi_0y_z+income_0y_z+high_educ_0y_z+parent_age_0y_z+history_ratio_0y_z+vol_z+ sex_0y_F+married_0y+parent_identity_0y+gender_identity_0y+ foreign_born_family_0y+foreign_born_0y+gay_parent_0y+gay_youth_0y+race_ethnicity_0y")

fmla2 <- as.formula("discount_rate_1y_z~rh_adi_bi_0y+section8_0y+nihtbx_totalcomp_uncorrected_0y_z+iqeur2+ vol_z+age_0y_z+bmi_0y_z+income_0y_z+high_educ_0y_z+parent_age_0y_z+history_ratio_0y_z+vol_z+ sex_0y_F+married_0y+parent_identity_0y+gender_identity_0y+ foreign_born_family_0y+foreign_born_0y+gay_parent_0y+gay_youth_0y+race_ethnicity_0y")

fmla3 <-as.formula("totalscore_pps_1y_z ~ discount_rate_1y_z*(rh_adi_bi_0y+section8_0y)+nihtbx_totalcomp_uncorrected_0y_z+iqeur2+ vol_z+age_0y_z+bmi_0y_z+income_0y_z+high_educ_0y_z+parent_age_0y_z+history_ratio_0y_z+vol_z+ sex_0y_F+married_0y+parent_identity_0y+gender_identity_0y+ foreign_born_family_0y+foreign_born_0y+gay_parent_0y+gay_youth_0y+race_ethnicity_0y")

model_treatment <- glm(fmla1, data = data, family = binomial) model_mediator <- lm(fmla2, data = data) model_outcome <- lm(fmla3, data = data)

ivmediation_result <- ivmediate(model_treatment, model_mediator, model_outcome, sims = 500, enc = 'section8_0y', treat='rh_adi_bi_0y', mediator='discount_rate_1y_z')

summary(ivmediation_result) plot(ivmediation_result)`

and the error messages that I got were like this:

Error in integrate(LACME1.integrand, -Inf, Inf, i = i, stop.on.error = F) : evaluation of function gave a result of wrong type In addition: Warning messages: 1: Using formula(x) is deprecated when x is a character vector of length > 1. Consider formula(paste(x, collapse = " ")) instead. 2: Using formula(x) is deprecated when x is a character vector of length > 1. Consider formula(paste(x, collapse = " ")) instead. 3: Using formula(x) is deprecated when x is a character vector of length > 1. Consider formula(paste(x, collapse = " ")) instead. 4: Using formula(x) is deprecated when x is a character vector of length > 1. Consider formula(paste(x, collapse = " ")) instead. 5: glm.fit: algorithm did not converge 6: Using formula(x) is deprecated when x is a character vector of length > 1. Consider formula(paste(x, collapse = " ")) instead. 7: Using formula(x) is deprecated when x is a character vector of length > 1. Consider formula(paste(x, collapse = " ")) instead.

I think I've done the same thing as shown in the example below, but this just doesn't seem to work for some reason. https://www.rdocumentation.org/packages/mediation/versions/4.5.0/topics/ivmediate

`a <- lm(comply ~ treat + sex + age + marital + nonwhite + educ + income, data = jobs) b <- glm(job_dich ~ comply + treat + sex + age + marital + nonwhite + educ + income, data = jobs, family = binomial) c <- lm(depress2 ~ job_dich * (comply + treat) + sex + age + marital + nonwhite + educ + income, data = jobs)

out <- ivmediate(a, b, c, sims = 50, boot = FALSE, enc = "treat", treat = "comply", mediator = "job_dich")

summary(out) plot(out)`

Have I missed something, or is there some problem with the ivmediate function that makes ends up with this error?

I'll really appreciate it if you can help. Thanks in advance!