Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

select_parameters

Automated selection of model parameters


Description

This function performs an automated selection of the 'best' parameters, updating and returning the "best" model.

Usage

select_parameters(model, ...)

## S3 method for class 'lm'
select_parameters(model, direction = "both", steps = 1000, k = 2, ...)

## S3 method for class 'merMod'
select_parameters(model, direction = "backward", steps = 1000, ...)

## S3 method for class 'stanreg'
select_parameters(model, method = NULL, cross_validation = FALSE, ...)

Arguments

model

A statistical model (of class lm, glm, merMod, stanreg or brmsfit).

...

Arguments passed to or from other methods.

direction

the mode of stepwise search, can be one of "both", "backward", or "forward", with a default of "both". If the scope argument is missing the default for direction is "backward". Values can be abbreviated.

steps

the maximum number of steps to be considered. The default is 1000 (essentially as many as required). It is typically used to stop the process early.

k

the multiple of the number of degrees of freedom used for the penalty. Only k = 2 gives the genuine AIC: k = log(n) is sometimes referred to as BIC or SBC.

method

The method used in the variable selection. Can be NULL (default), "forward" or "L1". See projpred::varsel.

cross_validation

Select with cross-validation.

Details

Classical lm and glm

For frequentist GLMs, select_parameters() performs an AIC-based stepwise selection.

Mixed models

For mixed-effects models of class merMod, stepwise selection is based on stepcAIC(). This step function only searches the "best" model based on the random-effects structure, i.e. select_parameters() adds or excludes random-effects until the cAIC can't be improved further.

Bayesian models

For Bayesian models, it uses the projpred package.

Value

The model refitted with optimal number of parameters.

Examples

model <- lm(mpg ~ ., data = mtcars)
select_parameters(model)

model <- lm(mpg ~ cyl * disp * hp * wt, data = mtcars)
select_parameters(model)

# lme4 -------------------------------------------
if (require("lme4")) {
  model <- lmer(
    Sepal.Width ~ Sepal.Length * Petal.Width * Petal.Length + (1 | Species),
    data = iris
  )
  select_parameters(model)
}


## Not run: 
# rstanarm -------------------------------------------
if (require("rstanarm") && require("projpred")) {
  model <- stan_glm(
    mpg ~ .,
    data = mtcars,
    iter = 500, refresh = 0, verbose = FALSE
  )
  select_parameters(model, cross_validation = TRUE)

  model <- stan_glm(
    mpg ~ cyl * disp * hp,
    data = mtcars,
    iter = 500, refresh = 0, verbose = FALSE
  )
  select_parameters(model, cross_validation = FALSE)
}

## End(Not run)

parameters

Processing of Model Parameters

v0.13.0
GPL-3
Authors
Daniel Lüdecke [aut, cre] (<https://orcid.org/0000-0002-8895-3206>, @strengejacke), Dominique Makowski [aut] (<https://orcid.org/0000-0001-5375-9967>), Mattan S. Ben-Shachar [aut] (<https://orcid.org/0000-0002-4287-4801>), Indrajeet Patil [aut] (<https://orcid.org/0000-0003-1995-6531>, @patilindrajeets), Søren Højsgaard [aut], Zen J. Lau [ctb], Vincent Arel-Bundock [ctb] (<https://orcid.org/0000-0003-1995-6531>, @vincentab), Jeffrey Girard [ctb] (<https://orcid.org/0000-0002-7359-3746>, @jeffreymgirard)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.