Search code examples
rparameter-passingdata-sciencelogistic-regressionmass-package

Passing a list as a parameter for the direction argument of the stepAIC function in the MASS package


I'd like to build three logistic regression models based on different methods for variable selection: a model with backward selection, a model with forward selection, and a model with stepwise selection.

I'm using base R glm() for setting up the regression model and stepAIC() from the MASS package for model selection. stepAIC() has a direction argument that can be set to 'backward' (backward selection), 'forward' (forward selection), or 'both' (stepwise selection).

I'm trying to pass a list of the different variable selection methods ('backward', 'forward', 'both') to the direction argument, but I get an error message.

I've checked out https://www.rdocumentation.org/packages/MASS/versions/7.3-51.4/topics/stepAIC but am none the wiser.

# Loading toy dataset 
mtcars

# Loading packages
library(dplyr)
library(MASS)

# The list of variable selection methods 
my_model_types = list(c("both","backward","forward"))

# Model building function  
my_modelling_function = function(direction, model) {  


model = glm(am ~ mpg + disp + hp + drat + wt + qsec, data = mtcars,     
family = binomial) %>%
  stepAIC(trace = FALSE, direction = my_model_types)  

}


# Building models using different variable selection methods   
lapply(my_model_types, my_modelling_function)

# Error message 
Error in match.arg(direction) : 'arg' must be NULL or a character vector

I'd have expected the output for three different models, but instead I get an error message: Error in match.arg(direction) : 'arg' must be NULL or a character vector.

Where am I going wrong? Thanks for any help!


Solution

  • You referenced my_model_types within your function, instead of the function parameter direction. Additionally you have a second function parameter model that is not passed to the function, and not used.

    Here is a correction of your code

    # Loading toy dataset 
    mtcars
    
    # Loading packages
    library(dplyr)
    library(MASS)
    
    # The list of variable selection methods 
    my_model_types = list("both","backward","forward")
    
    # Model building function  
    my_modelling_function = function(direction) {  
    
    
      glm(am ~ mpg + disp + hp + drat + wt + qsec, data = mtcars,     
                  family = binomial) %>%
        stepAIC(trace = FALSE, direction = direction)  
    
    }
    
    
    # Building models using different variable selection methods   
    lapply(my_model_types, my_modelling_function)