1

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.

cliftjc1
  • 43
  • 6
  • Have you tried supplying a `data = mids_object` as an argument to the `glm` call? – coffeinjunky May 22 '20 at 16:27
  • ```test <- with(as.mids(df_long),glm(DEATH~.,family = binomial,data=as.mids(df_long))) Error in as.data.frame.default(data) : cannot coerce class ‘"mids"’ to a data.frame ``` – cliftjc1 May 22 '20 at 16:29

1 Answers1

0

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.