amices / mice

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

Error message in multilevel imputation #567

Closed Shaminie76 closed 1 year ago

Shaminie76 commented 1 year ago

I am receiving this error message when I am trying out multilevel imputation: Error in mice.impute.2l.pan(y = c(240.04, 163.04, 201.64, 273.19, 187.51, : No class variable What should I do? The pred do specify cluster/class variable

My code:

ini <- mice(GLASS_weight_2, maxit = 0) pred_2 <- ini$pred pred_2[, "userid"] <- 0 pred_2[, "group"] <- 0 pred_2[, "BMI_group"] <- 0 pred_2[, "adapt_days_at_start"] <- 0 pred_2[, "gender"] <- 0 pred_2[,"study_baseline_BMI"]<-0 pred_2[, "StudyBaselineAge"] <- 0 pred_2[, "VirtaBaselineWeight"] <- 0 pred_2[, "LOCF_weight_180"] <- 0 pred_2[, "LOCF_day_180"] <- 0 pred_2[, "LOCF_weight_365"] <- 0 pred_2[, "LOCF_day_365"] <- 0 pred_2[, "LOCF_weight_545"] <- 0 pred_2[, "LOCF_day_545"] <- 0 pred_2[, "LOCF_weight_730"] <- 0 pred_2[, "LOCF_day_730"] <- 0 pred_2[,"weight_0" ] <- c(2, -2, -2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) pred_2[,"weight_180"] <- c(2, -2, -2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0) pred_2[,"weight_365"] <- c(2, -2, -2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0) pred_2[,"weight_545"] <- c(2, -2, -2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0) pred_2[,"weight_730"] <- c(2, -2, -2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2) meth_3 <- ini$meth meth_3[c(9,10,11,12,13)] <-"2l.pan" meth_3[c(6)] <-"" imp_check <- mice(GLASS_weight_2, pred = pred_2, meth = meth_3, print = FALSE)

Shaminie76 commented 1 year ago

I even tried after changing the fixed effect "1" in the predictor into random effect "2".

gerkovink commented 1 year ago

It is quite hard to troubleshoot this when your predictor specification is in a different order than your variable columns. Assuming that it is. Without a reprex it is challenging to help you.

Do you by any chance have missings in the class variable whilst simultaneously excluding it from imputation?

Shaminie76 commented 1 year ago

Thank you for responding. There were no missingness in the class variable. The order of variables in the dataset as listed below: columns 1 to 21

1) userid 2) group 3) BMI_group 4) adapt_days_at_start 5) gender 6) study_baseline_BMI 7) StudyBaselineAge 8) VirtaBaselineWeight 9) weight_0 10) weight_180 11) weight_365 12) weight_545 13) weight_730 14) LOCF_weight_180 15) LOCF_day_180 16) LOCF_day_365 17) LOCF_weight_365 18) LOCF_day_545 19) LOCF_weight_545 20) LOCF_day_730 21) LOCF_weight_730

I am only multiple imputing variable weight_0, weight_180, weight_365, weight_545 and weight_730. There is one missing value in study_baseline_BMI (I am using it as fixed effect predictor in the imputation). When I tried using multilevel imputation 2l.pan, 2l.norm or 2l.lmer, I am getting error messages. I don't have issue when I am doing single level imputation. Please advise. Thank you and appreciate it. Do you need to see my dataset

stefvanbuuren commented 1 year ago

I believe the problem is in the organisation of your data. 2l methods expect data in long form with person as class variable, while you seem to feed it wide format. See https://stefvanbuuren.name/fimd/sec-longandwide.html.

Closing because this is not a bug.