cww
cww

Reputation: 141

problems with mice in R: cannot coerce class '"mids"' into a data.frame

I have a dataset with about 11,500 rows and 15 factors. I only need to impute values for 3 of the factors, with only 2 of the factors having any significant number of missing values. I have been trying to use mice to create imputed datasets, and I am using the following code:

dataset<-read.csv("filename.csv",header=TRUE)

model<-success~1+course+medium+ethnicity+gender+age+enrollment+HSGPA+GPA+Pell+ethnicity*medium

library(mice)

vempty<-c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
v12<-c(0,0,0,0,0,0,0,1,1,1,1,0,1,1,1)
v13<-c(0,0,0,0,0,0,0,1,1,1,1,1,0,1,1)
v14<-c(0,0,0,0,0,0,0,1,1,1,1,1,1,0,1)
list<-list(vempty,vempty,vempty,vempty,vempty,vempty,vempty,vempty,vempty,vempty,vempty,v12,v13,v14,vempty)
predmatrix<-do.call(rbind,list)


MIdataset<-mice(dataset,m=2,predictorMatrix=predmatrix)
MIoutput<- pool(glm(model, data=MIdataset, family=binomial))

After this code, I get the error message:

Error in as.data.frame.default(data) : cannot coerce class '"mids"' into a data.frame

I'm totally at a loss as to what this means. I had no trouble doing this same analysis just deleting the missing data and using regular glm. I'd also like to do a multilvel logistic model on imputed datasets using lmer (that's the next step after I get this to work with glm), so if there is anything I am doing wrong that will also impact that next step, that would be good to know, too. I've tried to search this error on the internet, and I'm not getting anywhere. I'm just really learning R, so I'm also not that familiar with the environment yet.

Thanks for your time!

Upvotes: 2

Views: 5257

Answers (1)

Erich Studerus
Erich Studerus

Reputation: 557

You need to apply the with.mids function. I think the last line in your code should look like this:

pool(with(MIdataset, glm(formula(model), family = binomial)))

You could also try this:

expr <- 'glm(success ~ course, family = binomial)'
pool(with(MIdataset, parse(text = expr)))

Upvotes: 2

Related Questions