Frage

I'm trying to use the mice package in R for a project and discovered that the pooled results seemed to change the dummy code I had for one of the variables in the output.

To elaborate, let's say I have a factor, foo, with two levels: 0 and 1. Using a regular lm would typically yield an estimate for foo1. Using mice and the pool function, however, yields an estimate for foo2. I included a reproducible example below using the nhanes dataset from the mice package. Any ideas why the might be occurring?

require(mice)

# Create age as: 0, 1, 2
nhanes$age <- as.factor(nhanes$age - 1)
head(nhanes)

#     age  bmi hyp chl
#  1   0   NA  NA  NA
#  2   1 22.7   1 187
#  3   0   NA   1 187
#  4   2   NA  NA  NA
#  5   0 20.4   1 113
#  6   2   NA  NA 184

# Use a regular lm with missing data just to see output
# age1 and age2 come up as expected

lm(chl ~ age + bmi, data = nhanes)

# Call:
#   lm(formula = chl ~ age + bmi, data = nhanes)

# Coefficients:
#   (Intercept)      age1         age2          bmi  
#     -28.948       55.810      104.724        6.921 

imp <- mice(nhanes)
str(complete(imp)) # still the same coding

fit <- with(imp, lm(chl ~ age + bmi))
pool(fit)

# Now the estimates are for age2 and age3

# Call: pool(object = fit)

# Pooled coefficients:
#   (Intercept)        age2        age3         bmi 
#    29.88431       43.76159    56.57606     5.05537 
War es hilfreich?

Lösung

Apparently the mice function sets contrasts for factors. So you get the following (check out the column names):

contrasts(nhanes$age)
##    1 2
##  0 0 0
##  1 1 0
##  2 0 1
contrasts(imp$data$age)
##    2 3
##  0 0 0
##  1 1 0
##  2 0 1

You can just change the contrasts of the imputed data, then you get the same dummy coding:

imp <- mice(nhanes)
contrasts(imp$data$age) <- contrasts(nhanes$age)
fit <- with(imp, lm(chl ~ age + bmi))
pool(fit)

##  Call: pool(object = fit)
##  
##  Pooled coefficients:
##  (Intercept)        age1        age2         bmi 
##    0.9771566  47.6351257  63.1332336   6.2589887 
##  
##  Fraction of information about the coefficients missing due to nonresponse: 
##  (Intercept)        age1        age2         bmi 
##    0.3210118   0.5554399   0.6421063   0.3036489 
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top