简体   繁体   中英

"Error in terms.formula(formula, data = data) : '.' in formula and no 'data' argument" within mice's "with()" function

I'm using MICE package to impute some data and then pool the regression results together. For some reason I'm getting an error when trying to use the "." operator, meaning I want to use all the variables in the data as an independent variable. Example of my code: pooled_results <- with(mids_object,glm(DEATH~.,family=binomial))

and I get the error:

Error in terms.formula(formula, data = data): '.' in formula and no 'data' argument

Is there some other convention for doing this type of thing within the "with()" function? I don't understand why this isnt working.

When you call a fitting functions, you can normally use ~. as a shortcut to mean "include all other available variables on the right-hand side". See this question for some explanation, Meaning of ~. (tilde dot) argument? . But this only works when a data argument is given to the function, otherwise the function won't know where it should find the other variables. It won't start searching your environment for anything that it thinks looks useful, which is a good thing.

To avoid this error, you should explicitly list the variables you want used. So, this will not work

library(mice)
imps <- mice(airquality, m = 5, predictorMatrix = quickpred(airquality)
mods <- with(imps, glm(Ozone ~ .))
#> Error in terms.formula(formula, data = data): '.' in formula and no 'data' argument

But changing to this will work

mods <- with(imps, glm(Ozone ~ Solar.R + Wind + Temp + Month + Day))

We can also use a trick where we set the data argument as data = data.frame(mget(ls())) , to pull all the data out of the imputation into a dataframe and pass it to the fitting function. That will also work, so we could use

mods <- with(imps, glm(Ozone ~ ., data = data.frame(mget(ls()))))

I prefer to list the variables I want to use in the model. I think that makes it easier to read the code and follow what's going on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM