如何在 miceadds 中使用 ml.lmer 方法估算数据

问题描述 投票:0回答:0

我正在尝试应用

ml.lmer
将缺失数据归因于以下数据框

dt = structure(list(f = c("3", "3", "3", "3", "3", "3", "3", "3", 
"3", "4", "4", "4", "4", "4", "4"), q = c("medium", "medium", 
"medium", "low", "low", "low", "high", "high", "high", "medium", 
"medium", "medium", "low", "low", "low"), w = c("west", "story", 
"side", "west", "story", "side", "west", "story", "side", "west", 
"story", "side", "west", "story", "side"), m = c("opono", "opono", 
"opono", "opono", "opono", "opono", "opono", "opono", "opono", 
"opono", "opono", "opono", "opono", "opono", "opono"), VAR1 = c(562.043178225707, 
565.281147016741, 565.124165466063, 556.105161267006, 558.9191937444, 
561.089399192576, 566.558391186459, 564.007838452381, 569.847746252395, 
NA, NA, NA, 576.997551474103, 571.430527371207, 581.548488568111
), VAR2 = c(568.970169826137, 570.567233719795, 570.51987609038, 
562.933778604826, 564.873410440901, 566.829488261267, 574.618113321021, 
570.133225344512, 576.573717673962, NA, NA, NA, 578.984901005381, 
572.72441513178, 583.422840017742), VAR3 = c(577.156037256044, 
577.180178600797, 577.357864860489, 566.859356471408, 569.356122801201, 
572.074153197824, 583.110754193518, 576.044526470253, 583.051110888242, 
NA, NA, NA, 579.820020157845, 573.83486757061, 582.339853113654
), VAR4 = c(567.498180346941, 570.006855833371, 569.353224777675, 
567.282762095302, 567.855921468843, 569.664181971811, 569.382903127564, 
569.674611444774, 569.628730897358, NA, NA, NA, 574.402637463373, 
572.659030491814, 575.104257647447), VAR5 = c(572.076341064459, 
572.407617246296, 570.525930106581, 570.296353575057, 570.231712506384, 
572.132317217948, 572.823622150601, 571.50936077199, 570.738647188268, 
NA, NA, NA, 573.79332702093, 571.992730934173, 575.338599386577
), VAR6 = c(574.774223561689, 575.827304179063, 573.368662115429, 
571.859463603291, 571.992766432843, 574.115841254771, 576.432607052833, 
573.176893704667, 572.838042674364, NA, NA, NA, 573.366626683085, 
571.758995435749, 574.187428289689), miss = c(1, 1, 1, 1, 1, 
1, 1, 1, 1, 0, 0, 0, 1, 1, 1)), row.names = c(NA, -15L), class = c("tbl_df", 
"tbl", "data.frame"))

我使用以下代码来估算数据

library(mice)
library(miceadds) 

variables_levels1 <- miceadds:::mice_imputation_create_type_vector( colnames(dt[-11]),value="")
names = names(dt[,5:10])

variables_levels1[names] <- c("q*m")
predmat1 <- mice::make.predictorMatrix(data=dt[-11])
predmat1[,c("f", "q", "w", "m" )] <- 0

method1 <- mice::make.method(data=dt[-11])
method1[names] <- "ml.lmer"

#----- specify hierarchical structure of imputation models
levels_id <- list()
#** hierarchical structure for variable x1
levels_id[["ID"]] <- names

mice::mice(dt[-11], maxit = 5, m= 14, method=method1,
           predictorMatrix=predmat1, levels_id=levels_id, 
           variables_levels = variables_levels1)

但是当插补开始时,我得到了以下错误

 iter imp variable
  1   1  VAR1Error in `[.data.frame`(data, , vname_level) : 
  selected not defined columns 

看不懂问题出在哪里。有人能帮忙吗?

r missing-data imputation r-mice
© www.soinside.com 2019 - 2024. All rights reserved.