Well, I have been trying to use the functions step() and stepAIC() to compare models using different criteria for variable selection. However, the results yield the same models.
I am uncertain whether this is highly unlikely but possible, or if there is an issue with the code or function itself.Could you please help me identify any potential issues or provide any suggestions for troubleshooting? Here is my code:
## Checking if criterion is unequal.This silenced are all equal.
library(MASS)
data(Boston)
# Adjust
model <- lm(medv ~ ., data = Boston)
# Var select
stepwise_model_aic <- step(model, direction = "both", criterion = "AIC")
stepwise_model_bic <- step(model, direction = "both", criterion = "BIC")
stepwise_model_cp <- step(model, direction = "both", criterion = "Cp")
stepwise_model_adjr2 <- step(model, direction = "both", criterion = "adjr2")
# View
summary(stepwise_model_aic)
summary(stepwise_model_bic)
summary(stepwise_model_cp)
summary(stepwise_model_adjr2)
# Data sim
set.seed(123)
n <- 100
x1 <- rnorm(n)
x2 <- rnorm(n)
x3 <- rnorm(n)
y <- 2*x1 + 3*x2 + 0.5*x3 + rnorm(n)
df <- data.frame(x1, x2, x3, y)
# Ajustar un modelo inicial con todas las variables
model <- lm(y ~ ., data = df)
# Var sel
stepwise_model_aic <- step(model, direction = "both", criterion = "AIC")
stepwise_model_bic <- step(model, direction = "both", criterion = "BIC")
stepwise_model_cp <- step(model, direction = "both", criterion = "Cp")
stepwise_model_adjr2 <- step(model, direction = "both", criterion = "adjr2")
# View
summary(stepwise_model_aic)
summary(stepwise_model_bic)
summary(stepwise_model_cp)
summary(stepwise_model_adjr2)