amices / mice

Multivariate Imputation by Chained Equations
https://amices.org/mice/
GNU General Public License v2.0
447 stars 108 forks source link

Issue with MICE package and subsequent generation of logistic regression #664

Closed ronaldo7-star closed 3 months ago

ronaldo7-star commented 3 months ago

I am using a publicly available dataset (National Health Interview Survey). I am studying heart disease as the outcome variable and examining the impact of several clinicodemographic factors on prevalence and odds of heart disease.

I did multiple imputation with the MICE package in R (v 4.4.0) and had no issues with multiple imputation. After the multiple imputation, I extracted the first imputed complete dataset and used the head function to look at the first few rows to double check that, in fact, the multiple imputation procedure had worked. Everything was fine. Afterwards, I proceeded to try to complete a logistic regression analysis using the multiply imputed data. Please refer to the code below. In my first step, I turned the datasets into long format. Second, I converted I back to MIDS type data since MICE can work with this type of data. Third, I generate the logistic regression. However, whenever I try to do the logistic regression, I obtain an error that states: "Error in as.data.frame.default(merge8imp_long_mids) : cannot coerce class '"mids"' to a data.frame" I tried removing the pool() function in the multivariate model, but that did not help. I even tried to use the merge8_imp_long (the complete imputed dataset in long format), but that required me to specify weights = NULL and it never loaded (I waited for over half an hour and the logistic regression procedure never completed).

Here is all my code starting with the multiple imputation:

`imp2 <- mice(merge8, maxit = 2, predictorMatrix = predM, method = meth, print = TRUE)

merge8complete <- mice::complete(imp2, 1) head(merge8complete)

merge8imp_long <- mice::complete(imp2, action="long", include = TRUE)

merge8imp_long_mids <-as.mids(merge8imp_long)

model_fitimp <- pool(with(merge8imp_long_mids, glm(HeartDisease_Binary2~ AgeCat + Sex2 + RaceEthnicity + SexualOrientation + Education + IncomePovertyRatio + BMICategory, HealthInsurance2, data = merge8imp_long_mids, family = "binomial", weights = NULL)))

model_fitimp2 <- with(merge8imp_long_mids, glm(Psoriasis_Binary2 ~ AgeCat + Sex2 + RaceEthnicity + SexualOrientation + Education + IncomePovertyRatio + BMICategory, HealthInsurance2, data = merge8imp_long_mids, family = "binomial"))

`

Neither model_fitimp nor model_fitimp2 worked. I receive the error: "Error in as.data.frame.default(merge8imp_long_mids) : cannot coerce class '"mids"' to a data.frame"

Any help would be greatly appreciated! Thanks so much!

stefvanbuuren commented 3 months ago

It seems that you are doing more than needed. The with() function in mice does the data wrangling for you. For example, try something like

fit <- with(imp2, glm(HeartDisease_Binary2~ AgeCat + Sex2, family = "binomial", weights = NULL)))
summary(pool(fit))

If you want to track what with() exactly does, look at the source of mice:::with.mids.

ronaldo7-star commented 3 months ago

Thanks for commenting back so quickly! I tried the revised code, but I still receive an error message: "Error in model.frame.default(formula = HeartDisease_Binary2 ~ AgeCat + Sex2 + : 'data' must be a data.frame, environment, or list" I am not sure how to troubleshoot this issue. Please advise. Thanks so much! :)

ronaldo7-star commented 3 months ago

Also, as a side note, I cannot find the object "with". I tried to track what with() does, but I received the error: "Error: 'with.mids' is not an exported object from 'namespace:mice'"

stefvanbuuren commented 3 months ago

Two suggestions:

ronaldo7-star commented 3 months ago

I got it to work by using fit <-with(imp2, glm(HeartDisease_Binary2 ~ AgeCat + ..., family=binomial, weights = NULL)). I originally had an issue in my GLM statement that, when corrected, allowed this function to work properly. Thanks so much for all your help! :)

stefvanbuuren commented 3 months ago

Glad you could sort it out.