I'm trying to use the step()
function in R for variable selection of a linear model. My model looks like this:
#I have some code first that randomly samples part of iris.
step_mod <- step(glm(Species~., iris_sample, family = binomial),
direction = "both")
The step()
function minimizes AIC as the way of finding the best combination of variables. However, I care about the % correct for a certain species (also called the "sensitivity") of the final predictions a lot more than I do about the AIC.
Is there a way to change the method of selection to "sensitivity" in the step()
function? Or is there a different function/package that you can use where you can change the selection criterion?
Here's the code I'm using to make predictions after I get the model, since it might helpful:
probs <- predict(step_mod, newdata = iris_unused, type="response")
class_predictions <- ifelse(probs < 0.50, "setosa", "versicolor")
final_check <- cbind(iris_unused, class_predictions)
tablecheck <- confusionMatrix(data =
as.factor(final_check$class_predictions),
reference =
as.factor(final_check$Species),
prevalence = 2)
In the resulting confusion matrix, the proportion of setosa correct is the number I'd like to maximize when doing model selection.