Automatically chooses between imputation and row deletion based on
two criteria: dataset size (n) and overall missing rate.
Usage
handle_missing(
analysis_result = NULL,
data = NULL,
method = c("linear", "knn"),
knn_k = 5L,
verbose = TRUE
)Arguments
- analysis_result
The list returned by
missing_analysis(). Alternatively, pass a plaindata.frametodata.- data
A
data.frame. Used only whenanalysis_resultisNULL.- method
Character. Imputation method:
"linear"(default) or"knn".- knn_k
Integer. Number of neighbours for KNN (default 5).
- verbose
Logical. Print progress messages (default
TRUE).
Value
An invisible list with elements:
data_cleanThe cleaned
data.frame.actionCharacter:
"drop"or"impute".methodImputation method used (or
"none").imputed_maskLogical
data.framemarking imputed cells (same dimensions asdata_clean). AllFALSEwhen action is"drop".n_imputedInteger. Number of cells that were imputed.
reportA short character string summarising the action taken.
Details
Decision rules:
n <= 50-> always impute (data too small to lose rows)n > 50andmissing > 5%-> imputen > 50andmissing <= 5%-> drop rows with NA
When imputing, linear interpolation is the default; KNN imputation is available as an alternative.
Examples
data(airquality)
ana <- missing_analysis(airquality, plot = FALSE)
#>
#> ============================================================
#> STEP 3/7 : Missing Value Analysis
#> ============================================================
#>
#> Rows : 153 | Cols: 6 | Total cells: 918
#> Missing : 4.79% (44 cells) | 2 / 6 cols affected
#> Action : DROP -- missing = 4.79% <= 5% threshold, safe to remove rows
#>
#> ------------------------------------------------------------
#> Missing per column
#> ------------------------------------------------------------
#> variable n_missing pct_missing
#> Ozone 37 24.18%
#> Solar.R 7 4.58%
#>
clean <- handle_missing(ana)
#>
#> ============================================================
#> STEP 5/9 : Handle Missing Values [DROP]
#> ============================================================
#>
#> Missing : 4.79% | Reason: 50 < n = 153 < 1000 and missing = 4.79% <= 5% -> drop rows
#>
#> Rows before : 153 -> after: 111 (-42 removed)
#>
clean$report
#> [1] "Action: ROW DELETION | Removed 42 rows | 50 < n = 153 < 1000 and missing = 4.79% <= 5% -> drop rows"