Reputation: 225
Stepwise selection method (stepAIC in MASS library) provide the subset of variables in its last step. Instead of typing the final model I would like to use it through call function. I don't know how to do that! Following is an example:
x = as.data.frame(matrix(cbind(rnorm(100), rnorm(100, 10), rnorm(100, 100), rpois(100, 4)), 100, 4))
fit = lm(V1 ~., data=x)
st = stepAIC(fit, direction = "both")
st$call
# lm(formula = V1 ~ 1, data = x)
How to use st$call
for further use? I tried the following with a hope that it will give me the regression results, but it's not working:
fm = st$call
summary(call)
Any help is appreciated.
Upvotes: 1
Views: 166
Reputation: 61933
The selected model is returned. You can just use st
directly. The returned object has a little bit more information attached to it since it was the result of the stepwise procedure but the returned object can and should do everything that you'd want to do from fitting that model 'by hand'.
Upvotes: 0
Reputation: 10350
You can reuse a function call with eval()
.
data(iris)
model <- lm(Sepal.Length~., data = iris)
model
# Call:
# lm(formula = Sepal.Length ~ ., data = iris)
#
# Coefficients:
# (Intercept) Sepal.Width Petal.Length Petal.Width Speciesversicolor Speciesvirginica
# 2.1713 0.4959 0.8292 -0.3152 -0.7236 -1.0235
eval(model$call)
# Call:
# lm(formula = Sepal.Length ~ ., data = iris)
#
# Coefficients:
# (Intercept) Sepal.Width Petal.Length Petal.Width Speciesversicolor Speciesvirginica
# 2.1713 0.4959 0.8292 -0.3152 -0.7236 -1.0235
As you can see the output of both calls are the same.
Upvotes: 2