interactions terms in multiple imputations (Amelia or other mi packages)

旧巷老猫 提交于 2019-12-06 02:09:36

I think , in any cases you give the information to Amelia that int is the result of a transformation , x1*x2. So it treats it as a simple variable. But you can perform a Post-transformation in the imputed data like this:

   df.mi = transform(df.mi, int = x2*x1)

Comparing to the original data you get this result:

mm <- cbind(df,df.mi$imputations$imp1)
mm[mm$x2==0 & is.na(mm$int),]
   x1 x2 int         x1 x2 int
45 NA  0  NA  0.3144084  0   0
49 NA  0  NA -1.1741704  0   0
76 NA  0  NA -0.2018450  0   0

EDIT I think I get better result using mice package which :

"The algorithm imputes an incomplete column (the target column) by generating 'plausible' synthetic values given other columns in the data."

Using your data , I compare the original data.frame to all the imputed data sets when x2 is equal to 0.

library(mice)
rr <- mice(df)
mm1 <- cbind(df,do.call(cbind,lapply(1:5,function(i)complete(rr , i))))
mm1[mm1$x2==0 & is.na(mm1$int),]

  x1 x2 int        x1 x2       int        x1 x2        int         x1 x2       int        x1 x2       int        x1 x2        int
20 NA  0  NA 0.5168547  0 -0.162311 0.6203798  0  0.0000000  0.8881394  0 0.0000000 0.9371405  0 0.8248701 0.5855288  0  0.0000000
23 NA  0  NA 0.5168547  0  0.000000 0.4911883  0  0.0000000 -1.8323773  0 0.0000000 0.5855288  0 0.0000000 0.5855288  0  0.0000000
31 NA  0  NA 0.5168547  0  0.000000 0.1495920  0 -0.3240866  2.3305120  0 1.6324456 1.1207127  0 0.8544517 0.5674033  0  0.0000000
60 NA  0  NA 0.5365237  0  0.000000 0.2542712  0  0.0000000  1.5934885  0 0.9371405 0.7094660  0 0.5168547 0.2542712  0 -0.3079534
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!