I created a stepwise regression using the codes below:
set.seed(1)
y=rnorm(100,20)
a=sqrt(rnorm(100,40))-15
b=rnorm(100,50)/abs(a)
c=sqrt(b+y)
d=rnorm(100,13)+sqrt(abs(a))
test_data <- data.frame(y,a,b,c,d)
step1<- step(lm(y~a+b+c+d,data=test_data),direction="backward")
summary(step1)
The stepwise regression gave me this formula lm(formula = y ~ b + c, data = test_data)
. Meaning I can get the best model if I used b and c to predict y.
What I want to know is if there is an automatic code/way to create a data frame containing the significant independent variables and dependent variable like: data.frame(test_data$y,test_data$b,test_data$c)
.
Thanks in advance.