I am trying to do forward selection and am having difficulties using string formulas:
> df <- data.frame(x0=c(1,2,3), x1=c(3,2,1), y=c(1,0,1))
> f0 <- lm("y ~ 1", data=df)
> f1 <- formula(lm("y ~ .", data=df))
> step(f0, direction="forward", scope=f1)
Start: AIC=-2.51
y ~ 1
Error in eval(predvars, data, env) :
invalid 'envir' argument of type 'closure'
I know I could just omit the string-formula portion if I wanted:
> f0 <- lm(y ~ 1, data=df)
> f1 <- formula(lm(y ~ ., data=df))
> step(f0, direction="forward", scope=f1)
Start: AIC=-2.51
y ~ 1
Df Sum of Sq RSS AIC
<none> 0.66667 -2.51223
+ x0 1 0 0.66667 -0.51223
+ x1 1 0 0.66667 -0.51223
Call:
lm(formula = y ~ 1, data = df)
Coefficients:
(Intercept)
0.6667
But I'd like to be able to dynamically name my dependent variable without having to hardcode it.