missForest
missForest 是一種非參數填補方法,適用於 R 中 混合型 表格資料。它能同時處理 數值型與分類型 變數,透過迭代訓練隨機森林,從已觀測到的資料預測缺失值。不需明確建模假設、不進行矩陣分解——僅以強大的預測基準直接運作並取得良好成效。
- 支援 任意組合的數值與因子欄位
- 捕捉 非線性關係 與 互動效應
- 回報 袋外(OOB) 填補誤差(NRMSE/PFC)
- 支援 平行執行(每變數或每森林)
- 提供兩種森林後端:
ranger(預設)與randomForest(舊版/相容)
安裝
# CRAN (recommended)
install.packages("missForest")Development version (from GitHub)
install.packages("remotes")
remotes::install_github("stekhoven/missForest")Quick start
library(missForest)Example data
data(iris)Introduce ~20% MCAR missingness
set.seed(81)
iris_mis <- prodNA(iris, noNA = 0.20)Impute with default backend (ranger)
imp <- missForest(iris_mis, xtrue = iris, verbose = TRUE)Imputed data
head(imp$ximp)Estimated OOB errors (NRMSE for numeric, PFC for factors)
imp$OOBerrorTrue error if xtrue was provided (for benchmarking only)
imp$error選擇後端
# Legacy behavior using randomForest
imp_rf <- missForest(iris_mis, backend = "randomForest")Explicitly use ranger with limited threads
imp_rg <- missForest(iris_mis, backend = "ranger", num.threads = 2)平行化
可透過 parallelize 使用兩種模式:
"variables":為不同變數同時建立森林(需註冊 foreach 後端)。"forests":在單一變數的森林內進行平行化(ranger 執行緒;或 randomForest 的 foreach 子森林)。
# Not run:
library(doParallel)
registerDoParallel(2)
imp_vars <- missForest(iris_mis, parallelize = "variables", verbose = TRUE)
imp_fors <- missForest(iris_mis, parallelize = "forests", verbose = TRUE, num.threads = 2)
API 總覽
missForest(xmis, ...)
核心插補函數。
主要參數:
xmis— 含有缺失值的資料框/矩陣(欄必須為numeric或factor)。maxiter— 最大迭代次數(預設為10)。ntree— 每個森林的樹數(預設為100)。mtry— 每次分裂嘗試的變數數量(預設為sqrt(p))。nodesize— 長度為 2 的數值向量:c(numeric, factor) 的最小節點大小。預設為c(5, 1)。variablewise— 若為TRUE,則回傳每個變數的 OOB 誤差。parallelize—"no"、"variables"或"forests"。num.threads— 給ranger用的執行緒數(randomForest忽略此參數)。backend—"ranger"(預設)或"randomForest"。xtrue— 可選的完整資料供基準測試(會增加$error欄位)。
backend = "ranger" 之參數對應關係:ntree → num.treesnodesize → min.bucket(回歸/分類分開設定;預設為c(5,1))sampsize(數量)→sample.fraction(比例;可為整體或分群)classwt → class.weightscutoff以機率森林擬合,並於後處理門檻
公用工具
mixError(ximp, xmis, xtrue)— 計算真實缺失項的 NRMSE(數值)與 PFC(類別)。nrmse(ximp, xmis, xtrue)— 僅連續型資料的 NRMSE。prodNA(x, noNA = 0.1)— 向資料框注入 MCAR 缺失。varClass(x)— 回傳每欄"numeric"/"factor"。
小技巧與最佳實踐
- 在呼叫
missForest前,請將字元欄位轉換為因子(factors)。 - 對於寬型資料,建議使用
parallelize = "variables";對於深度/高計算量的樹,建議使用parallelize = "forests"。 - 設定隨機種子以獲得準可重現的結果:
set.seed(123); imp <- missForest(x)
``
- 在原型設計階段,可以降低
ntree 以加快迭代速度。---
引用
如果您使用 missForest,請引用:
Stekhoven, D. J. & Bühlmann, P. (2012). MissForest—nonparametric missing value imputation for mixed-type data.* Bioinformatics, 28(1), 112–118. https://doi.org/10.1093/bioinformatics/btr597
您也可以引用此套件:
r
citation("missForest")
``Contributing
Issues and pull requests are welcome. Please include a minimal reproducible example when reporting bugs. For performance discussions, share small benchmarks and session info.
License
GPL (≥ 2)
Contact
Daniel J. Stekhoven — stekhoven@nexus.ethz.ch
--- Tranlated By Open Ai Tx | Last indexed: 2026-07-17 ---