[Mediation-information] Mediator must be a number
Angel Rodriguez
angel.rodriguez at matiainstituto.net
Wed Sep 24 13:18:39 CEST 2014
Dear Subscribers,
I copy the solution that Kentaro very kindly found for the problem I posted on the 19th of September:
Hi Angel,
You have to change the mediator into number like 0 and 1.
---------------------------------------------------------------------------
library("mediation")
library("sandwich")
set.seed(2014)
load("~/Desktop/kentaro.rda")
kentaro$estudic.new <- 0
kentaro$estudic.new[kentaro$estudic == "Menos que primarios"] <- 1
kentaro$estudic <- kentaro$estudic.new
med.fit <- glm(estudic ~ edad_c + sexo + regalf + deprinf, family="binomial" ,data=kentaro)
out.fit <- glm(benvii ~ edad_c + sexo + regalf + deprinf + estudic, family="binomial" ,data=kentaro)
med.out <- mediate(med.fit, out.fit, treat = "deprinf", mediator = "estudic", robustSE = TRUE, sims = 100, control.value = "no", treat.value = "s\xed")
summary(med.out)
---------------------------------------------------------------------------
Best,
Kentaro
Now, with the same database but this time using a multimed procedure, I encounter this problem:
> Xnames <- c("edad_c", "sexo", "regalf")
> m.med <- multimed(outcome = "benvii", med.main = "p207dic", med.alt = "estuprimas", treat = "deprinf", covariates = Xnames, data = child65, sims = 1000)
Error: variable 'deprinf' was fitted with type "factor" but type "numeric" was supplied
In addition: Warning messages:
1: In Ops.factor(data.1[, med.main], 2) : ^ not meaningful for factors
2: In Ops.factor(data.1[, treat], data.1[, med.main]^2) :
* not meaningful for factors
3: In model.response(mf, "numeric") :
using type = "numeric" with a factor response will be ignored
4: In Ops.factor(y, z$residuals) : - not meaningful for factors
5: In model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) :
variable 'deprinf' is not a factor
> str(child65$deprinf)
Factor w/ 2 levels "no","sí": 1 2 2 2 1 2 2 1 1 1 ...
So, deprinf is a factor but is considered numeric by multimed.
Any clue?
Thank you very much.
Angel Rodríguez Laso
-----Mensaje original-----
De: Kentaro Hirose [mailto:hirose at Princeton.EDU]
Enviado el: vie 19/09/2014 14:33
Para: Angel Rodriguez; mediation-information at r-forge.wu-wien.ac.at
Asunto: RE: [Mediation-information] (no subject)
I guess it's somehow related to the data you're using. I'll figure out the problem if you send me a small subset of your data and code.
Best,
Kentaro
________________________________________
From: Angel Rodriguez [angel.rodriguez at matiainstituto.net]
Sent: Friday, September 19, 2014 8:22 AM
To: Kentaro Hirose; mediation-information at r-forge.wu-wien.ac.at
Subject: RE: [Mediation-information] (no subject)
Thank you, Kentaro. The treatment variable has two values only. With your command that warning disappears but, what about the error:
Error in if (xhat == 0) out <- 1 else { :
missing value where TRUE/FALSE needed
?
Angel
________________________________
De: mediation-information-bounces at lists.r-forge.r-project.org en nombre de Kentaro Hirose
Enviado el: vie 19/09/2014 14:17
Para: mediation-information at r-forge.wu-wien.ac.at
Asunto: Re: [Mediation-information] (no subject)
Hi Angel,
The warning message "treatment and control values do not match factor levels" pops up when your treatment has more than two values but you don't specify the specific values of the treatment that you're interested when deriving the treatment effect. So, for example, if you're interested in the treatment effect when the value of the treatment changes from no to si, then you should use the following command:
med.out <- mediate(med.fit, out.fit, treat = "deprinf", mediator = "estudic", robustSE = TRUE, sims = 100, control.value = "no", treat.value = "si")
Best,
Kentaro
________________________________________
From: mediation-information-bounces at r-forge.wu-wien.ac.at [mediation-information-bounces at r-forge.wu-wien.ac.at] on behalf of Angel Rodriguez [angel.rodriguez at matiainstituto.net]
Sent: Friday, September 19, 2014 7:15 AM
To: mediation-information at r-forge.wu-wien.ac.at
Subject: Re: [Mediation-information] (no subject)
Dear Subscribers,
I've encountered the following error message. Edad_c is numeric. Estudic, sexo, deprinf and benvii are dichotomic. Regalf has three categories.
Any help would be very valuable.
Best regards,
Angel Rodriguez-Laso
> med.fit <- glm(estudic ~ edad_c + sexo + regalf + deprinf, family="binomial" ,data=child65)
> out.fit <- glm(benvii ~ edad_c + sexo + regalf + deprinf + estudic, family="binomial" ,data=child65)
> library("mediation")
Loading required package: MASS
Loading required package: Matrix
Loading required package: lpSolve
Loading required package: mvtnorm
mediation: Causal Mediation Analysis
Version: 4.4.2
> library("sandwich")
> set.seed(2014)
> med.out <- mediate(med.fit, out.fit, treat = "deprinf", mediator = "estudic", robustSE = TRUE, sims = 100)
Error in if (xhat == 0) out <- 1 else { :
missing value where TRUE/FALSE needed
In addition: Warning message:
In mediate(med.fit, out.fit, treat = "deprinf", mediator = "estudic", :
treatment and control values do not match factor levels; using no and sí as control and treatment, respectively
________________________________
_______________________________________________
Mediation-information mailing list
Mediation-information at lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/mediation-information
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/mediation-information/attachments/20140924/7c5bebfa/attachment.html>
More information about the Mediation-information
mailing list