gpt4 book ai didi

r - 如何将预测函数与 mice() 的汇总结果一起使用?

转载 作者:行者123 更新时间:2023-12-04 02:57:00 25 4
gpt4 key购买 nike

您好,我刚开始在学校使用 R 作为模块的一部分。我有一个缺少数据的数据集,我使用 mice() 来估算丢失的数据。我现在正尝试将预测函数与我的合并结果一起使用。但是,我观察到以下错误:

UseMethod("predict") 错误: 没有适用于“预测”的适用方法应用于“c('mipo','data.frame')”类的对象

我在下面包含了我的全部代码,如果你们能帮助新手,我将不胜感激。谢谢!

```{r}
library(magrittr)
library(dplyr)
train = read.csv("Train_Data.csv", na.strings=c("","NA"))
test = read.csv("Test_Data.csv", na.strings=c("","NA"))
cols <- c("naCardiac", "naFoodNutrition", "naGenitourinary", "naGastrointestinal", "naMusculoskeletal", "naNeurological", "naPeripheralVascular", "naPain", "naRespiratory", "naSkin")
train %<>%
mutate_each_(funs(factor(.)),cols)
test %<>%
mutate_each_(funs(factor(.)),cols)
str(train)
str(test)
```

```{r}
library(mice)
md.pattern(train)
```

```{r}
miTrain = mice(train, m = 5, maxit = 50, meth = "pmm")
```

```{r}
model = with(miTrain, lm(LOS ~ Age + Gender + Race + Temperature + RespirationRate + HeartRate + SystolicBP + DiastolicBP + MeanArterialBP + CVP + Braden + SpO2 + FiO2 + PO2_POCT + Haemoglobin + NumWBC + Haematocrit + NumPlatelets + ProthrombinTime + SerumAlbumin + SerumChloride + SerumPotassium + SerumSodium + SerumLactate + TotalBilirubin + ArterialpH + ArterialpO2 + ArterialpCO2 + ArterialSaO2 + Creatinine + Urea + GCS + naCardiac + GCS + naCardiac + naFoodNutrition + naGenitourinary + naGastrointestinal + naMusculoskeletal + naNeurological + naPeripheralVascular + naPain + naRespiratory + naSkin))
model
summary(model)
```

```{r}
modelResults = pool(model)
modelResults
```

```{r}
pred = predict(modelResults, newdata = test)
PredTest = data.frame(test$PatientID, modelResults)
str(PredTest)
summary(PredTest)
```

最佳答案

实现此目的的一种稍微有点老套的方法可能是采用 fit() 创建的拟合模型之一,并将存储的系数替换为最终的合并估计。我没有做过详细的测试,但它似乎正在处理这个简单的例子:

library(mice)

imp <- mice(nhanes, maxit = 2, m = 2)
fit <- with(data = imp, exp = lm(bmi ~ hyp + chl))
pooled <- pool(fit)

# Copy one of the fitted lm models fit to
# one of the imputed datasets
pooled_lm = fit$analyses[[1]]
# Replace the fitted coefficients with the pooled
# estimates (need to check they are replaced in
# the correct order)
pooled_lm$coefficients = summary(pooled)$estimate

# Predict - predictions seem to match the
# pooled coefficients rather than the original
# lm that was copied
predict(fit$analyses[[1]], newdata = nhanes)
predict(pooled_lm, newdata = nhanes)

据我所知,线性回归的 predict() 应该只依赖于在系数上,所以你不应该更换其他在拟合模型中存储值(但如果应用predict() 以外的方法。

关于r - 如何将预测函数与 mice() 的汇总结果一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52713733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com