在 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)補值誤差估計:
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 中以插補處理遺漏值