jmpsteen / medflex

Flexible mediation analysis using natural effect models in R
23 stars 6 forks source link

xFit=expFit not found in a simulation environment #14

Closed heledi closed 6 years ago

heledi commented 6 years ago

Hi, there seems to be a problem with the xFit=expFit argument in a simulation environment. Though the expFit model is defined in the simulation, the model object is not found when it is called in the neModel-function. The error message says: Error in eval(args$xFit) : object 'expFit' not found. All packages are installed (R-Version 3.4.4). The problem does not occur outside a simulation environment. Here is a simple example:

library(simcausal) library(medflex) set.seed(41188)

M <- DAG.empty() M <- M + node("c",
distr="rnorm", mean=0, sd=1)+ node("a", distr = "rbern", prob = plogis(0.1-c)) + node("m", distr="rnorm", mean=10-2a+c, sd=2)+ node("y", distr="rnorm", mean=15-3a+0.2*m+c, sd=3) Mset <- set.DAG(M)

ATE<--3.4 NDE<--3 NIE<--0.4 true_dec<-c(ATE, NDE, NIE)

mysimulation <- function(runs=2,N=10000,B=100){ param <- 3 #total effect, natural direct and natural indirect effect M1_est<- matrix(NA,nrow=param,ncol=runs) # Point estimates

for(r in 1:runs)try({
simdat <- simcausal::sim(DAG = Mset, n = N, verbose=F)

expFit <- glm(factor(a)~c, family=binomial, data = simdat)
print(expFit)
impData <- neImpute(y ~  factor(a) + m + c, nMed = 1, data = simdat)

neMod1 <- neModel(y ~ a0+a1 +c,
                  family = gaussian, expData = impData, xFit = expFit,
                  se = "robust")

effdecomp <- neEffdecomp(neMod1)
coef.nem<-coef(effdecomp)
NDE.nem<-coef.nem[1]
NIE.nem<-coef.nem[2]
ATE.nem<-coef.nem[3]
M1_est[,r]<-cbind(ATE.nem, NDE.nem, NIE.nem)

})

M1_ub <- apply(M1_est[,!apply(M1_est,2,is.na)[1], drop=FALSE],1,mean, na.rm=TRUE) BIAS <- cbind(abs(M1_ub-true_dec)) results <- list(M1_est, BIAS) } set.seed(4118) sim <- mysimulation() sim

Thank you for your help!

paul-fink commented 6 years ago

This issue seems to be the same as in Issue #6, but this time for 'xFit' argument.

Minimal (not) working example (function content taken from the example section of neModel)

rm(list = ls())
data(UPBdata)
x <- function() {
expFit <- glm(att ~ gender + educ + age, data = UPBdata)
impData <- neImpute(UPB ~ att * negaff + gender + educ + age, 
                    family = binomial, data = UPBdata)
impFit2r <- neModel(UPB ~ att0 * att1, family = binomial, 
                    expData = impData, xFit = expFit, se = "robust")
}
x()

The issue can be solved in a similar manner as #6 by adding args$xFit <- quote(xFit) to the function 'neModel' prior to the evaluation, e.g. after line 335 in https://github.com/jmpsteen/medflex/blob/master/R/neModel.R

For users: To mitigate the issue in the current CRAN version, one can name the object which is passed to the argument xFit by the same name 'xFit'.

jmpsteen commented 6 years ago

Many thanks for noticing and for offering a solution! Should work now.

jmpsteen commented 6 years ago

Can you please verify if you get rid of all errors?

heledi commented 6 years ago

Yes, I did. Thank you very much!