【问题标题】:compare R packages missForest and Hmisc performance比较 R 包 missForest 和 Hmisc 的性能
【发布时间】:2016-06-14 06:21:17
【问题描述】:

当缺失值超过 50% 时,我正在尝试比较 2 个 R 包 MissForest 和 Hmisc 在处理缺失值时的性能。

我通过这种方式得到了测试数据:

data("iris")
library(missForest)
iris.mis <- prodNA(iris, noNA = 0.6)
summary(iris.mis)

mis1 <- iris.mis
mis2 <- iris.mis

在missForest中,它有mixError()方法,可以让你将插补精度与原始数据进行比较。

# using missForest
missForest_imputed <- missForest(mis1, ntree = 100)
missForest_error <- mixError(missForest_imputed$ximp, mis1, iris)
dim(missForest_imputed$ximp)
missForest_error

hmisc 没有mixError() 方法,我用它强大的aregImpute() 来做插补,像这样:

# using Hmisc
library(Hmisc)
hmisc_imputed <- aregImpute(~Sepal.Length + Sepal.Width + Petal.Length + Petal.Width + Species, 
                        data = mis2, n.impute = 1)

我希望将估算的结果转换为missForest_imputed$ximp 之类的格式,以便我可以使用mixError() 方法。问题是,在aregImpute(),无论我尝试n.impute = 1 还是n.impute = 5,我都不能像原始数据虹膜一样为每个特征有150 个值......而且每个特征中的值的数量也不同.. ..

那么,有没有办法比较missForest和Hmisc处理缺失值的表现呢?

【问题讨论】:

    标签: r missing-data imputation


    【解决方案1】:

    第 1 部分

    Hmisc::aregImpute 返回估算值。对于名为 hmisc_imputed 的对象,可以在 hmisc_imputed$imputed 中找到它们。但是,imputed 对象是每个维度的列表。

    如果您想重新创建 missForest_imputed$ximp 的等价物,您必须自己动手。为此,我们可以使用以下事实:

    all.equal(as.integer(attr(xx$Sepal.Length, "dimnames")[[1]]), which(is.na(iris.mis$Sepal.Length))) ## returns true
    

    我在这里做什么:

    check_missing <- function(x, hmisc) {
      return(all.equal(which(is.na(x)), as.integer(attr(hmisc, "dimnames")[[1]])))
    }
    
    get_level_text <- function(val, lvls) {
      return(lvls[val])
    }
    
    convert <- function(miss_dat, hmisc) {
      m_p <- ncol(miss_dat)
      h_p <- length(hmisc)
      if (m_p != h_p) stop("miss_dat and hmisc must have the same number of variables")
      # assume matches for all if 1 matches
      if (!check_missing(miss_dat[[1]], hmisc[[1]]))
        stop("missing data an imputed data do not match")
    
      for (i in 1:m_p) {
        i_factor <- is.factor(miss_dat[[i]])
        if (!i_factor) {miss_dat[[i]][which(is.na(miss_dat[[i]]))] <- hmisc[[i]]}
        else {
          levels_i <- levels(miss_dat[[i]])
          miss_dat[[i]] <- as.character(miss_dat[[i]])
          miss_dat[[i]][which(is.na(miss_dat[[i]]))] <- sapply(hmisc[[i]], get_level_text, lvls= levels_i)
          miss_dat[[i]] <- factor(miss_dat[[i]])
        }
      }
      return(miss_dat)
    }
    
    iris.mis2 <- convert(iris.mis, hmisc_imputed$imputed)
    

    第 2 部分

    mixError 使用 RMSE 计算错误率,?mixError

    插补错误。在连续变量的情况下,这只是归一化均方根误差(NRMSE,有关更多详细信息,请参见“帮助(missForest)”)。在仅分类变量的情况下,这是错误分类条目 (PFC) 的比例。在混合类型变量的情况下,提供了两种错误度量。

    要对“第 1 部分”[iris.mis2] 中的对象执行此操作,您只需使用 nrmse 函数,该函数在 library(missForest) 中提供。

    【讨论】:

    • 这真的很棒!!非常感谢亚历克斯,我从你的 R 代码中学到了很多东西!对于第 2 部分,我认为仍然需要 PFC,因为物种是因素。总之,非常感谢!!
    猜你喜欢
    • 2012-03-29
    • 2015-05-22
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 2018-02-17
    • 2014-04-09
    相关资源
    最近更新 更多