我想基于不同的变量选择方法构建三个逻辑回归模型:一个后向选择模型、一个前向选择模型和一个逐步选择模型。
我使用 base R glm() 设置回归模型,并使用 MASS 包中的 stepAIC() 进行模型选择。stepAIC() 有一个方向参数,可以设置为“backward”(向后选择)、“forward”(向前选择)或“both”(逐步选择)。
我正在尝试将不同变量选择方法('backward'、'forward'、'both')的列表传递给方向参数,但我收到一条错误消息。
我已经查看了https://www.rdocumentation.org/packages/MASS/versions/7.3-51.4/topics/stepAIC但我并不明智。
# 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
我本来期望三个不同模型的输出,但我收到一条错误消息: match.arg(direction) 中的错误:'arg' 必须为 NULL 或字符向量。
我哪里错了?谢谢你的帮助!