1 year ago
#305763
Yohhei Hamada
Inputing systematically missing level-2 variables using miceadd
I'm conducting an individual participant data meta-analysis. My data sets have 4 levels with the highest level being study. I would like to impute level 2 variables that are systematically missing in some studies. When I run imputation using miceadds, I get the following error:
Error in if (stats::sd(weight.obs) > 0) { :
missing value where TRUE/FALSE needed
Below is the code that reproduces the same error. Id4 is study id and y3 is the level 2 variable that is systematically missing in one of the studies (id4=6).
data(data.ma07, package="miceadds")
dat <- data.ma07
dat$id4<-1
dat2<-dat[sample(1:nrow(dat),1600),]
dat2$id4<-2
dat<-rbind(dat,dat2)
dat2<-dat[sample(1:nrow(dat),1600),]
dat2$id4<-3
dat<-rbind(dat,dat2)
dat2<-dat[sample(1:nrow(dat),1600),]
dat2$id4<-4
dat<-rbind(dat,dat2)
dat2<-dat[sample(1:nrow(dat),1600),]
dat2$id4<-5
dat<-rbind(dat,dat2)
dat<-mutate(dat,id6=paste0(id2,"_",id4))
dat$y3<-NA
t<-unique(dat$id6)[sample(1:length(unique(dat$id6)),600)]
t1<-t[sample(1:length(t),250)]
t0<-t[!t %in% t1]
dat<-mutate(dat,y3=ifelse(id6 %in%t1,1,
ifelse(id6 %in% t0,0,NA)
))
dat2<-dat[sample(1:nrow(dat),1600),]
dat2$id4<-6
dat2$y3<-NA
dat<-rbind(dat,dat2)
variables_levels <- miceadds:::mice_imputation_create_type_vector( colnames(dat), value="")
# leave variables at lowest level blank (i.e., "")
variables_levels[ c("y1","y2") ] <- "id2"
variables_levels[ c("z1","z2") ] <- "id3"
variables_levels[ c("y3") ] <- "id2"
predmat <- mice::make.predictorMatrix(data=dat)
predmat[, c("id2", "id3","id4") ] <- 0
method <- "ml.lmer"
levels_id <- list()
#** hierarchical structure for variable x1
levels_id[["x1"]] <- c("id2", "id3","id4")
#** hierarchical structure for variable y1
levels_id[["y1"]] <- c("id3","id4")
#** hierarchical structure for variable y3
levels_id[["y3"]] <- c("id3","id4")
#** hierarchical structure for variable z1
levels_id[["z1"]] <- c("id4")
model<-"pmm"
imp1 <- mice::mice( dat, maxit=10, m=5, method=method,
predictorMatrix=predmat, levels_id=levels_id,
variables_levels=variables_levels,model=model )
Thank you for your help.
r
r-mice
0 Answers
Your Answer