【发布时间】:2020-10-26 22:52:50
【问题描述】:
我正在使用 MICE 在多级模型中估算数据。然后我使用 MICE 中的“with()”函数使用 lmer() 进行多级分析。第一个数据集着眼于总犯罪率。然后我对犯罪类型进行另一组分析,因此收集了暴力犯罪和财产犯罪。最初,对于第二个数据集,我想在估算之前收集财产和暴力犯罪,然后估算预测变量,但这需要 FOREVER(超过 20 小时)。有没有办法收集 MICE 输出(下面的mice.crime),以便我可以对暴力和财产犯罪值进行建模?
非常感谢!
这是一个可重复的例子,本质上,我只是想收集暴力和财产犯罪:
library(mice)
df.1 <- data.frame(place_id = as.integer(seq (1:n)),
property_crime = as.numeric(sample(c(20:90), n, rep = TRUE)),
violent_crime = as.numeric(sample(c(20:90), n, rep = TRUE)),
med_income = sample(c(20:90), n, rep = TRUE))%>%
mutate(crime_total = property_crime + violent_crime)
df <- apply(subset(df.1,select=-c(place_id, property_crime, violent_crime, crime_total)), 2, function(x)
{x[sample(c(1:n),floor(n/10))]<-NA; x})
df.1 <- df.1%>%select(-(med_income))
df <- cbind(df.1, df)
impmethod.1 <- character(ncol(df))
names(impmethod.1) <- colnames(df)
impmethod.1[c("med_income")] <- "2l.lmer"
impmethod.1[c("place_id", "property_crime", "violent_crime", "crime_total")] <- ""
impmethod.1
pm <- make.predictorMatrix(df)
pm["place_id",] <- -c(0,0,0,0,2)
pm["property_crime",] <- -c(-2,0,0,0,2)
pm["violent_crime",] <- -c(-2,0,0,0,2)
pm["crime_total",] <- -c(-2,0,0,0,2)
pm["med_income",] <- -c(-2,0,0,0,0)
mice.crime <- mice(df, m=5, predictorMatrix = pm,
method=impmethod.1, maxit=10, printFlag = FALSE, seed=1874)
【问题讨论】: