missForest
missForest เป็นวิธีการแทนค่าข้อมูลที่ขาดหายแบบไม่พึ่งสมมติฐานสำหรับข้อมูลตารางแบบ ผสมประเภท ใน R โดยรองรับตัวแปรทั้ง เชิงตัวเลขและเชิงหมวดหมู่ พร้อมกันด้วยการฝึก random forest แบบวนซ้ำเพื่อทำนายค่าที่ขาดหายจากค่าที่สังเกตได้ ไม่ต้องตั้งสมมติฐานแบบชัดเจน ไม่ต้องทำ matrix factorization—แค่ใช้ baseline ทำนายที่แข็งแกร่งซึ่งใช้งานได้ดีทันที
- ใช้งานได้กับ คอลัมน์ตัวเลขและแฟคเตอร์ทุกประเภท
- จับ ความไม่เชิงเส้น และ ปฏิสัมพันธ์
- รายงาน ข้อผิดพลาดการแทนค่าข้อมูล OOB (NRMSE/PFC)
- รองรับ การประมวลผลแบบขนาน (ต่อคอลัมน์หรือต่อป่า)
- มี backend ป่าสองตัว:
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": สร้างฟอเรสต์สำหรับตัวแปรแต่ละตัวแบบขนาน (ต้องลงทะเบียน backend ของ foreach)"forests": ประมวลผลแบบขนานภายในฟอเรสต์ของตัวแปรเดียว (ใช้เธรดของ ranger หรือใช้ foreach สำหรับ sub-forests ของ randomForest)
# 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— data frame/matrix ที่มีค่าขาดหาย (คอลัมน์ต้องเป็นnumericหรือfactor)maxiter— จำนวนรอบสูงสุด (ค่าเริ่มต้น10)ntree— จำนวนต้นไม้ต่อป่า (ค่าเริ่มต้น100)mtry— ตัวแปรที่สุ่มเลือกในแต่ละการแบ่ง (ค่าเริ่มต้นsqrt(p))nodesize— เวกเตอร์ตัวเลขขนาด 2: ขนาดโหนดขั้นต่ำสำหรับ c(numeric, factor) ค่าเริ่มต้นc(5, 1)variablewise— คืนค่า OOB error รายตัวแปรหากTRUEparallelize—"no","variables"หรือ"forests"num.threads— จำนวนเธรดสำหรับranger(ถูกละเว้นโดยrandomForest)backend—"ranger"(ค่าเริ่มต้น) หรือ"randomForest"xtrue— ข้อมูลสมบูรณ์สำหรับ การวัดประสิทธิภาพ (เพิ่ม$error)
backend = "ranger":ntree → num.treesnodesize → min.bucket(แยกสำหรับ regression/classification; ค่าเริ่มต้นc(5,1))sampsize(จำนวน) →sample.fraction(สัดส่วน; ทั้งหมดหรือแยกตามคลาส)classwt → class.weightscutoffถูกจัดการด้วยการสร้าง probability forests และการกำหนด threshold ภายหลัง
ยูทิลิตี้
mixError(ximp, xmis, xtrue)— คำนวณ NRMSE (ตัวเลข) และ PFC (factor) เฉพาะค่าที่ขาดจริงnrmse(ximp, xmis, xtrue)— NRMSE สำหรับข้อมูลเชิงต่อเนื่องเท่านั้นprodNA(x, noNA = 0.1)— สร้างค่าขาดหายแบบ MCAR ลงใน data framevarClass(x)— คืนค่า"numeric"/"factor"รายคอลัมน์
เคล็ดลับ & แนวปฏิบัติที่ดีที่สุด
- แปลงคอลัมน์ตัวอักษรเป็นแฟคเตอร์ก่อนเรียกใช้
missForest - สำหรับข้อมูลที่กว้าง ให้พิจารณาใช้
parallelize = "variables"สำหรับต้นไม้ที่ลึก/ใช้ทรัพยากรสูง ให้พิจารณาใช้parallelize = "forests" - ตั้งค่า seed เพื่อผลลัพธ์ที่สามารถทำซ้ำได้ในระดับหนึ่ง:
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 ---