I am seeing some weird behavior with the stepAIC
function in the MASS
package when dealing with multinomial logistic models. Here is some sample code:
library(nnet)
library(MASS)
example("birthwt")
race.model <- multinom(race ~ smoke, bwt)
race.model2 <- stepAIC(race.model, k = 2)
In this case race.model
and race.model2
have identical terms; stepAIC
did not prune anything. However, I need to query certain attributes of the models, and I get an error with race.model2
:
formula(race.model)[2]
returns race()
but
formula(race.model2)[2]
gives the error:
Error in terms.formula(newformula, specials = names(attr(termobj, "specials"))) :
invalid model formula in ExtractVars
This behavior only seems to occur when stepAIC
does not remove terms from the model. In the following code, terms are removed by stepAIC
, and both models can be properly queried:
race.big <- multinom(race ~ ., bwt)
race.big2 <- stepAIC(race.big, k = 2)
formula(race.big)[2]
formula(race.big2)[2]
Any ideas about what is going wrong here?