使用 missforest 进行部分插补 - 将所选列与原始数据集相结合
Partial imputation with missforest - combining the selected columns with original dataset
对于一个相当简单的问题表示歉意,但我还没有成功解决这个简单的问题。
我的目标是只用 missforest 估算选定的列。然后,该模型仅输出数据集中选定的列。将这些与包含所有列的原始数据集结合起来的最优雅的方法是什么,因为每行没有一个唯一的连接键。
我试着按照 YohanK 的指示从这个 post: partial imputation with missForest
示例:
data(iris)
set.seed(81) iris.mis <- prodNA(iris, noNA = 0.1)
imputedData <- missForest(iris.mis[c( 1, 2, 3, 4)], verbose = T)
dataset <- data.frame(iris[1], imputedData)
这会导致此错误:
Error in as.data.frame.default(x[[i]], optional = TRUE,
stringsAsFactors = stringsAsFactors) : cannot coerce class
‘"missForest"’ to a data.frame
提前感谢您的帮助!
Br,米克
理想情况下,您将整个数据集提供给 missForest
,因为即使您只想估算某些列,其他列也会提供有用的信息以产生良好的估算结果。
使用 missForest,您可以实现例如只有第一列是这样的:
您先保存包含缺失值的数据集,不要覆盖它。
您对整个数据集执行了 missForest 插补。
之后,您用从 missForest 获得的估算列替换您想要在初始数据集中估算的列。
对于您的示例,它是这样工作的:
library(missForest)
data(iris)
set.seed(81)
# Here you artificially introduce NAs to the iris data
iris.mis <- prodNA(iris, noNA = 0.1)
# Do the imputation with missForest on the whole data
imp_missforest <- missForest(iris.mis, verbose = T)
# imp_missforest is a missForest object use $imp to get all the imputed values
iris.imp <- imp_missforest$ximp
# Replace the desired columns in the initial iris.mis dataset with the columns from iris.imp
iris.mis$Sepal.Length <- iris.imp$Sepal.Length
# As you can see, only the first column in iris.mis in now imputed and you can decide yourself, what to do with the remaining columns
iris.mis
在我的示例中,您只估算了第一列 Sepal.Length。 iris.mis中的其余部分保持不变。
对于一个相当简单的问题表示歉意,但我还没有成功解决这个简单的问题。
我的目标是只用 missforest 估算选定的列。然后,该模型仅输出数据集中选定的列。将这些与包含所有列的原始数据集结合起来的最优雅的方法是什么,因为每行没有一个唯一的连接键。
我试着按照 YohanK 的指示从这个 post: partial imputation with missForest
示例:
data(iris)
set.seed(81) iris.mis <- prodNA(iris, noNA = 0.1)
imputedData <- missForest(iris.mis[c( 1, 2, 3, 4)], verbose = T)
dataset <- data.frame(iris[1], imputedData)
这会导致此错误:
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class ‘"missForest"’ to a data.frame
提前感谢您的帮助!
Br,米克
理想情况下,您将整个数据集提供给 missForest
,因为即使您只想估算某些列,其他列也会提供有用的信息以产生良好的估算结果。
使用 missForest,您可以实现例如只有第一列是这样的:
您先保存包含缺失值的数据集,不要覆盖它。
您对整个数据集执行了 missForest 插补。
之后,您用从 missForest 获得的估算列替换您想要在初始数据集中估算的列。
对于您的示例,它是这样工作的:
library(missForest)
data(iris)
set.seed(81)
# Here you artificially introduce NAs to the iris data
iris.mis <- prodNA(iris, noNA = 0.1)
# Do the imputation with missForest on the whole data
imp_missforest <- missForest(iris.mis, verbose = T)
# imp_missforest is a missForest object use $imp to get all the imputed values
iris.imp <- imp_missforest$ximp
# Replace the desired columns in the initial iris.mis dataset with the columns from iris.imp
iris.mis$Sepal.Length <- iris.imp$Sepal.Length
# As you can see, only the first column in iris.mis in now imputed and you can decide yourself, what to do with the remaining columns
iris.mis
在我的示例中,您只估算了第一列 Sepal.Length。 iris.mis中的其余部分保持不变。