R 中的缺失值填补处理
Michal Oleszak
Machine Learning Engineer
用机器学习模型预测缺失值!
本课使用:missForest 包,基于 randomForest


nhanes %>% is.na() %>% colSums()
Age Gender Weight Height Diabetes TotChol Pulse PhysActive
0 0 9 8 1 85 32 26
library(missForest)
imp_res <- missForest(nhanes)
nhanes_imp <- imp_res$ximp
nhanes_imp %>% is.na() %>% colSums()
Age Gender Weight Height Diabetes TotChol Pulse PhysActive
0 0 0 0 0 0 0 0
missForest() 提供袋外(OOB)插补误差估计:
两者中,值越接近 0 表示效果越好,接近 1 表示较差。
imp_res <- missForest(nhanes)
imp_res$OOBerror
NRMSE PFC
0.147687025 0.003676471
missForest() 提供袋外(OOB)插补误差估计:
两者中,值越接近 0 表示效果越好,接近 1 表示较差。
imp_res <- missForest(nhanes, variablewise = TRUE)
imp_res$OOBerror
MSE PFC MSE MSE PFC MSE MSE MSE
0.00000 0.00000 285.79563 40.42142 0.00735 0.53444 129.03609 0.17576
训练多个随机森林可能耗时较长。
思路:牺牲少量精度,缩小森林规模以减少计算时间。
ntree 参数)。mtry 参数)。对计算时间的影响不同:
ntree 呈线性加速。mtry 在变量很多时加速更明显。默认设置:
start_time <- Sys.time()
imp_res <- missForest(nhanes)
end_time <- Sys.time()
print(imp_res$OOBerror)
print(end_time - start_time)
NRMSE PFC
0.147687025 0.003676471
Time difference of 5.496582 secs
缩小森林:
start_time <- Sys.time()
imp_res <- missForest(nhanes,
ntree = 10,
mtry = 2)
end_time <- Sys.time()
print(imp_res$OOBerror)
print(end_time - start_time)
NRMSE PFC
0.162420139 0.007425743
Time difference of 0.516367 secs
R 中的缺失值填补处理