【问题标题】:Adding imputed data from a model to a dataset - hmisc aregImpute将模型中的插补数据添加到数据集 - hmisc aregImpute
【发布时间】:2020-05-03 06:50:39
【问题描述】:

我正在尝试使用 hmisc 从数据集中估算值。我正在关注this guide

这是我的代码的可重现示例:

#Create dataset and add 0.1 NA values randomly
data <- iris
library(missForest)
library(Hmisc)
iris.mis <- prodNA(iris, noNA = 0.1)

#Calculating imputed values with aregImpute
impute_arg <- aregImpute(~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width + Species, data = iris.mis, n.impute = 5)

completeData2 <- impute.transcan(impute_arg, imputation=1, data=iris.mis, list.out=TRUE,pr=FALSE, check=FALSE) 
head(completeData2)

#creating a fit model
library(rms)
fmi <- fit.mult.impute(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width + Species, ols, impute_arg, data=iris.mis)

我的问题是:如何将此拟合模型应用于我的数据并在我的数据集 (iris.mis) 中估算 NA 值?

非常感谢带有代码 sn-ps 的答案。

【问题讨论】:

    标签: r hmisc


    【解决方案1】:

    您需要做的就是获得模型的预测:

    model_predictions <- predict(fmi)
    

    现在您可以检查数据缺失索引处的预测:

    missing <- which(is.na(iris.mis$Sepal.Length))
    imputed <- model_predictions[missing]
    imputed
    #>         5        22        27        32        34        35        54        60 
    #> 5.073695* 5.119113* 5.182343* 4.949794* 5.381427* 4.863149* 5.565716* 5.596861* 
    #>        89       102       107       117       131       135       145       149 
    #> 5.950823* 6.217764* 5.757642* 6.829916* 7.116657* 6.726274* 6.738296* 6.662452* 
    #>       150 
    #> 6.428420* 
    

    看看它们与实际值的比较:

    actual <- iris$Sepal.Length[missing]
    
    plot(x = actual, y = imputed, xlim = c(4, 8), ylim = c(4, 8), col = "red",
         xlab = "Actual", ylab = "Imputed", main = "Imputed vs Actual Sepal Length")
    lines(c(4, 8), c(4, 8), lty = 2)
    

    #>  # calculate residuals
    imputed - actual 
    #>            5           22           27           32           34           35 
    #>  0.07369483*  0.01911295*  0.18234346* -0.45020634* -0.11857279* -0.03685114* 
    #>           54           60           89          102          107          117 
    #>  0.06571631*  0.39686061*  0.35082282*  0.41776385*  0.85764178*  0.32991602* 
    #>          131          135          145          149          150 
    #> -0.28334270*  0.62627448*  0.03829600*  0.46245174*  0.52842038* 
    #>
    #> # sum of squared errors
    sum((imputed - actual)^2)
    #> [1] 2.52802
    

    因此,如果您希望集合中的新列具有您可以执行的插补功能

    iris.mis$Sepal.Length.Imputed <- iris.mis$Sepal.Length
    iris.mis$Sepal.Length.Imputed[is.na(iris.mis$Sepal.Length.Imputed)] <- imputed
    

    【讨论】:

      猜你喜欢
      • 2017-06-01
      • 2015-10-16
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-12
      相关资源
      最近更新 更多