0

I am performing multilevel generalized linear models after multiple imputations however I got an error,

my variables are:

  • dm2: continuous variable
  • fld5: categorical variable (5 categories)
  • age: continuous variable
  • gender: categorical variable (2 categories)
  • race: categorical variable (5 categories)
  • locations: categorical variable (20 categories)
  • types: categorical variables (3 categories)

This is my code:

library(survey)
library(mitools)

#imputing data

imp<- mice(mydata,seed = 123, m=5, defaultMethod = c("pmm"))
implog<-mice::complete(imp, action="long", include = TRUE)

#converting data into an imputation list

imp_list <- imputationList(split(implog, implog$.imp)[-1])

# Set up the survey design object

designs<-svydesign(id =~ locations, weights =~ weight, data=imp_list)

#Generlized linear model
model<-MIcombine(with(designs,svyglm(dm2~fld5+age+Gender+race+(fld5|locations)+(fld5|types), family=binomial(link = "logit"))))

I got this error:

Warning: ‘|’ not meaningful for factorsWarning: ‘|’ not meaningful for factorsError in contrasts<-(tmp, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels

If I run this analysis without using multiple imputations. I do not get this error. Any suggestion?

James Z
  • 12,209
  • 10
  • 24
  • 44
Bkry
  • 1
  • 1
  • Can you make your post reproducible and provide `dput(mydata)`? https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – jrcalabrese Apr 23 '23 at 14:08
  • hi, prior to the imputation, does forcing the factor levels of each variable solve this? for example, this code forces levels 0, 1, and 2 in the new `am_levels` variable: `x <- mtcars ; x[ , 'am_levels' ] <- factor( x[ , 'am' ] , levels = 0:2 ) ; print( table( x[ , 'am_levels' ] ) )` – Anthony Damico Apr 27 '23 at 18:46
  • Hi, Thank you for your reply. Can you give me another example I tried your code but I got an error. Thank you! – Bkry Apr 27 '23 at 20:40

0 Answers0