Gestione dei dati mancanti con imputazioni in R
Michal Oleszak
Machine Learning Engineer
Usa modelli di machine learning per prevedere i valori mancanti!
In questo corso: pacchetto missForest, basato su 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() fornisce una stima OOB (out-of-bag) dell’errore di imputazione:
In entrambi i casi, buone prestazioni → valore vicino a 0; valori ~1 indicano un risultato scarso.
imp_res <- missForest(nhanes)
imp_res$OOBerror
NRMSE PFC
0.147687025 0.003676471
missForest() fornisce una stima OOB (out-of-bag) dell’errore di imputazione:
In entrambi i casi, buone prestazioni → valore vicino a 0; valori ~1 indicano un risultato scarso.
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
Addestrare molte random forest può richiedere tempo.
Idea: sacrificare un po’ di accuratezza e ridurre la dimensione della foresta per diminuire i tempi.
ntree).mtry).L’effetto sul tempo varia:
ntree ha effetto lineare.mtry accelera di più quando le variabili sono molte.Impostazioni predefinite:
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
Foreste ridotte:
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
Gestione dei dati mancanti con imputazioni in R