Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

with.mids() using eval_tidy() breaks compatibility with metafor #292

Closed
wviechtb opened this issue Dec 2, 2020 · 2 comments
Closed

with.mids() using eval_tidy() breaks compatibility with metafor #292

wviechtb opened this issue Dec 2, 2020 · 2 comments

Comments

@wviechtb
Copy link

wviechtb commented Dec 2, 2020

Commit 4634094 changed with.mids() to use eval_tidy() in place of eval(expr = substitute(expr), envir = data.i, enclos = parent.frame()). This breaks compatibility with the metafor package. An example:

library(metafor)
library(mice)

dat <- dat.bcg
dat <- escalc(measure="RR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat)
dat$ablat[c(2,4,8)] <- NA

predMatrix <- make.predictorMatrix(dat)
predMatrix[,] <- 0
predMatrix["ablat", c("yi", "year")] <- 1

impMethod <- make.method(dat)
impMethod["ablat"] <- "pmm"
impMethod

imp <- mice(dat, print=FALSE, predictorMatrix=predMatrix, method=impMethod, seed=1234)
fit <- with(imp, rma(yi, vi, mods = ~ ablat + year))

This fails with error Error in eval(predvars, data, env) : object 'ablat' not found. Apparently, eval_tidy() is not able to find the predictor variables. Using eval() as before works:

withold <- function (data, expr) {
  call <- match.call()
  analyses <- as.list(seq_len(data$m))
  for (i in seq_along(analyses)) {
    data.i <- complete(data, i)
    analyses[[i]] <- eval(expr = substitute(expr), envir = data.i, enclos = parent.frame())
    if (is.expression(analyses[[i]]))
      analyses[[i]] <- eval(expr = analyses[[i]], envir = data.i, enclos = parent.frame())
  }
  object <- list(call = call, call1 = data$call, nmis = data$nmis, analyses = analyses)
  oldClass(object) <- c("mira", "matrix")
  object
}

fit <- withold(imp, rma(yi, vi, mods = ~ ablat + year))

I read help(eval_tidy, package="rlang") to figure out what it does differently than eval() but this gets quite technical. I think the crucial part may be "Functions that require the evaluation environment to correspond to a frame on the call stack do not work." In any case, is there any advantage to using eval_tidy() besides saving a few lines of code? If not, would you consider reverting to the old version of with.mids()?

@stefvanbuuren
Copy link
Member

Sorry about the hassle. I'd expected that the change wouldn't affect any downstream packages, but your problem proves that my expectation was not well grounded. I'd also expected that the change would solve a problem with dot expansion in formula (#265), but also that one didn't work out as I'd hoped for.

I have tried a few thing to make the metafor package work with tidy_eval() but I couldn't get it to work. I do not know exactly what is the culprit, but in the interest of time and efficiency, I have reverted with.mids() to the old version. I have also added your code as a test, which should function as a danger check for any future changes to with.mids().

@wviechtb
Copy link
Author

Thank you for reverting to the old version. I will let you know if I can figure out a way to make metafor work with tidy_eval() but there may just be an incompatibility in the way metafor functions look for the variables in the data frame and how tidy_eval() works. Maybe I can fix this, but for now I am of course happy with the present solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants