比较 R 包 missForest 和 Hmisc 的性能
compare R packages missForest and Hmisc performance
我正在尝试比较 2 个 R 包的性能,当缺失值超过 50% 时,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
,我都不能像原始数据 iris 那样为每个特征设置 150 个值...以及每个特征中的值数量功能也不一样....
那么,有没有办法比较一下missForest和Hmisc在处理缺失值方面的表现呢?
第 1 部分
Hmisc::aregImpute
returns 估算值。对于名为 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
:
Value imputation error. In case of continuous variables only this is the normalized root mean squared error (NRMSE, see 'help(missForest)' for further details). In case of categorical variables onlty this is the proportion of falsely classified entries (PFC). In case of mixed-type variables both error measures are supplied.
要对 "Part 1" [iris.mis2
] 中的对象执行此操作,您只需使用 library(missForest)
中提供的 nrmse
函数。
我正在尝试比较 2 个 R 包的性能,当缺失值超过 50% 时,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
,我都不能像原始数据 iris 那样为每个特征设置 150 个值...以及每个特征中的值数量功能也不一样....
那么,有没有办法比较一下missForest和Hmisc在处理缺失值方面的表现呢?
第 1 部分
Hmisc::aregImpute
returns 估算值。对于名为 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
:
Value imputation error. In case of continuous variables only this is the normalized root mean squared error (NRMSE, see 'help(missForest)' for further details). In case of categorical variables onlty this is the proportion of falsely classified entries (PFC). In case of mixed-type variables both error measures are supplied.
要对 "Part 1" [iris.mis2
] 中的对象执行此操作,您只需使用 library(missForest)
中提供的 nrmse
函数。