select_covariates {asympDiag} | R Documentation |
Select covariates
Description
Select covariates
Usage
select_covariates(
model,
threshold = 0.15,
direction = c("both", "backward", "forward"),
addable_coefs = names(get_fixef(model)),
measure_fn = function(x) summary(x)[["coefficients"]][, 4],
measure_one_at_time = FALSE,
minimize_only = FALSE,
max_steps = 1000,
return_step_results = FALSE,
do_not_remove = c("(Intercept)"),
...
)
Arguments
model |
A model with |
threshold |
Value threshold to remove variable. It can be a fixed value
or a function. The variable is removed if |
direction |
The direction of variable selection. Options include "backward", "forward", or "both". Defaults to "both". |
addable_coefs |
A vector of coefficients that can be added during forward selection. Defaults to all coefficients in the model. |
measure_fn |
Function with model as argument and returns values to be used by
|
measure_one_at_time |
Boolean indicating to apply |
minimize_only |
Logical indicating that during backward model update
it should minimize the |
max_steps |
The maximum number of steps for the variable selection process. Defaults to 1000. |
return_step_results |
Logical. If TRUE, the function returns a list containing the final fitted model and a log of the selection steps. Defaults to FALSE. |
do_not_remove |
A character vector specifying variables that should not be removed during backward selection. Defaults to "(Intercept)". |
... |
Extra arguments to |
Value
A fitted model with selected covariates based on the variable selection process.
If return_step_results
is TRUE, a list containing the final fitted model
and a log of the selection steps is returned.
Examples
model <- lm(mpg ~ ., data = mtcars)
select_covariates(model)
## measure_fn with two parameters
lrt <- function(model1, model2) {
lrt_stat <- 2 * (logLik(model1)[1L] - logLik(model2)[1L])
return(1 - pchisq(lrt_stat, 1))
}
select_covariates(model, measure_fn = lrt)
## AICc selection
AICc <- function(model) {
loglike <- logLik(model)
df <- attr(loglike, "df")
nobs <- attr(loglike, "nobs")
aic <- -2 * as.numeric(loglike) + 2 * df
aicc <- aic + (2 * (df^2) + 2 * df) / (nobs - df - 1)
return(aicc)
}
selection <- select_covariates(model,
measure_fn = AICc,
threshold = AICc,
measure_one_at_time = TRUE,
minimize_only = TRUE,
direction = "both",
data = mtcars
)