我试图预测多重填补后新的观测。无论是newdata和使用该模型是列表对象。该方法的正确性不是问题,但如何使用预测功能后,多重填补我们我有一个新的数据是一个列表。下面是我的代码。
library(betareg)
library(mice)
library(mgcv)
data(GasolineYield)
dat1 <- GasolineYield
dat1 <- GasolineYield
dat1$yield <- with(dat1,
ifelse(yield > 0.40 | yield < 0.17,NA,yield)) # created missing values
datim <- mice(dat1,m=30) #imputing missing values
mod1 <- with(datim,gam(yield ~ batch + emp,family=betar(link="logit"))) #fit models using gam
创建的数据集将被用于预测
datnew <- complete(datim,"long")
datsplit <- split(datnew,datnew$.imp)
下面只是测试代码中的预测没有newdata。我观察到的问题是,TP是由32矩阵保存为1×32矩阵,而不是30。但打印选项打印出30×32,但随后我不能将其保存为此类。
tot <- 0
for(i in 1:30){
tot <- mod1$analyses[[i]]
tp <- predict.gam(tot,type = "response")
print(tp)
}
下面的代码是我尝试使用newdata预测新的观察。在这里,我只是失去了我不知道如何去做。
datnew <- complete(datim,"long")
datsplit <- split(datnew,datnew$.imp)
tot <- 0
for(i in 1:30){
tot <- mod1$analyses[[i]]
tp <- predict.gam(tot,newdata=datsplit[[i]], type = "response")
print(tp)
}
有人可以帮我就如何更好地去了解它?
我终于发现解决了这个问题。这里是解决方案:
datnew <- complete(datim,"long")# stack all the imputation data
我假设,这不是在构建模型中使用。我打开这个#thread的目的是要解决如何预测/使用新的数据多重插补后使用具有多重插补数据集构建的模型观察的问题。
datsplit <- split(datnew,datnew$.imp)
tot <- list()
tot_ <- list()
for(i in 1:30){
for(j in 1:30){
tot[[j]] <- predict.gam(mod1$analyses[[i]],newdata=datsplit[[j]])
}
tot_[[i]] <- tot
}
# flatten the lists within lists
totfl <- tot_ %>% flatten()
#nrow is the number of observations to be predicted as contained in the
#newdata set (datsplit)
totn <- matrix(unlist(totfl),nrow=32)
apply(totn,1,mean) #takes the means of prediction across the 30 data set
我希望这可以帮助那些有类似的问题。我曾经跨越了如何多重填补后预测newdata一个问题就来了,我想这会回答一些包含在该线程的问题。