The enhanced randomForest.default.R file now includes a comprehensive type validation system that checks parameter types early in the function execution. This helps catch errors immediately with clear, actionable error messages instead of failing later with cryptic C-level errors.
Two helper functions handle parameter validation:
Validates that a parameter is a single TRUE or FALSE value.
Usage:
.validate_bool(replace, "replace")
.validate_bool(importance, "importance")What it checks:
- Parameter must be logical type (not numeric, not character)
- Parameter must be exactly one value (not a vector)
- Parameter must not be NA
Error message example:
`replace` must be TRUE or FALSE, not the value 1.
Validates that a parameter is a whole number with optional range checking.
Usage:
.validate_whole_number(ntree, "ntree", min_value = 1)
.validate_whole_number(maxnodes, "maxnodes", min_value = 1)What it checks:
- Parameter must be numeric type
- Parameter must be exactly one value (not a vector)
- Parameter must not be NA
- Parameter must be a whole number (no decimal part, e.g., 5.0 is OK, 5.5 is not)
- If
min_valuespecified: parameter must be >= min_value - If
max_valuespecified: parameter must be <= max_value
Error message examples:
`ntree` must be a whole number, not a vector of length 3.
`nPerm` must be at least 1, not -5.
`nodesize` must be a whole number (no decimal part), not 3.5.
These parameters are checked to ensure they are TRUE or FALSE:
replace- Sampling with/without replacementimportance- Compute variable importancelocalImp- Compute local importancenorm.votes- Normalize votes in classificationkeep.forest- Keep forest in outputcorr.bias- Correction biaskeep.inbag- Keep inbag information
These parameters are checked to be positive whole numbers:
ntree- Number of trees to grow (min: 1)nPerm- Number of times to permute features (min: 1)nodesize- Minimum node size (min: 1)maxnodes- Maximum number of terminal nodes (optional, min: 1)
do.trace- Can be boolean OR numeric (validation allows both)proximity- Optional parameter, checked only if provided
When randomForest() is called:
1. Function receives parameters
↓
2. Type validation checks run immediately
↓
3. If any type check fails → Stop with clear error message
↓
4. If all type checks pass → Continue with computation logic
↓
5. Further value validation (existing logic) runs
↓
6. Model computation proceeds
Errors are caught before expensive C-level computation begins, saving time.
Instead of cryptic C errors like "data length [301] is not a sub-multiple", users see:
`ntree` must be a whole number (no decimal part), not 100.5.
The type checks serve as inline documentation of what types each parameter expects.
Users immediately understand what went wrong and how to fix it.
# All parameters have correct types
rf <- randomForest(x, y,
ntree = 500, # whole number ✓
replace = TRUE, # boolean ✓
importance = TRUE) # boolean ✓
# → Computation proceeds# replace is numeric instead of boolean
rf <- randomForest(x, y, replace = 1)
# ERROR:
# `replace` must be TRUE or FALSE, not the value 1.# ntree has decimal part
rf <- randomForest(x, y, ntree = 100.5)
# ERROR:
# `ntree` must be a whole number (no decimal part), not 100.5.# maxnodes is vector instead of single value
rf <- randomForest(x, y, maxnodes = c(10, 20))
# ERROR:
# `maxnodes` must be a whole number, not a vector of length 2.# nPerm must be >= 1
rf <- randomForest(x, y, nPerm = -1)
# ERROR:
# `nPerm` must be at least 1, not -1.Lines 4-63 in randomForest.default.R:
.validate_bool()definition: lines 13-26.validate_whole_number()definition: lines 28-63
Lines 97-135 in the function body:
- Boolean parameter checks: lines 103-110
- Whole number checks: lines 112-115
- Optional parameter checks: lines 117-134
- Inline comments explain the validation purpose
- Section headers clearly mark the validation block
- Helper functions include roxygen2 documentation
The validation system:
- ✓ Runs before any existing logic
- ✓ Does NOT modify parameter values
- ✓ Does NOT change function behavior for valid inputs
- ✓ Complements (not replaces) existing validation logic
- ✓ Provides better error messages for type errors
Example: Both systems check mtry validity:
# Type validation: Would check if mtry is numeric (if desired)
# Existing validation (lines after type checks): Checks if mtry is in valid range
# Result: Multiple layers of defense against invalid inputsTo add validation for additional parameters:
.validate_bool(your_param, "your_param").validate_whole_number(your_param, "your_param", min_value = 1, max_value = 100)if (!your_condition) {
stop(sprintf(
"`%s` must satisfy your_condition, not %s.",
"param_name",
what_was_provided
), call. = FALSE)
}A demonstration script (type_validation_demo.R) shows:
- Valid parameter examples
- How each type error is caught
- The error messages generated
- Benefits of early validation
Run it with:
Rscript type_validation_demo.RThe type validation system in randomForest.default.R provides:
- Early parameter validation - Catches type errors immediately
- Clear error messages - Explains what went wrong in user-friendly language
- Minimal overhead - Simple checks before expensive computation
- Better documentation - Parameter types are explicit in code
- Consistent patterns - Reusable validation functions for similar parameters
This follows best practices from packages like the tidyverse and the rlang package for input validation.