title: Using stepAIC to make out of sample predictions
body: <p>just had a quick question on using Step AIC to make prediction. I'm a beginner in R, so please pardon if the solution is obvious. Tried searching around but couldn't really find what I was looking for.</p>
  
  <p>So I'm trying to predict the response variable, after running stepwise AIC on a main model (main model has all the explanatory variables). The stepAIC gives out a new model that has a reduced number of variables. My question is how do I do an out of sample prediction using the new reduced model. In other words, how does I reduce the dataset so that when I feed it into predict.lm, it only has the variables that were selected in the reduced model.</p>
  
  <p>Here's my code below:</p>
  
  <hr>
  
  <pre><code># Specify start and end row of the first 5 year window
  start_row=1
  end_row=60
  
  #declare matrix that will contain the predicted returns by specifying dimensions
  predicted=matrix(0,179,7)
  
  y_var=as.matrix(orig_data[start_row:end_row,2:7])
  x_var=as.matrix(orig_data[start_row:end_row,8:27])
  
  # Perform linear regression on all factors and then select factors using stepwise AIC     method
  
  initial_model&lt;-    lm(y_var[,1]~x_var[,1]+x_var[,2]+x_var[,3]+x_var[,4]+x_var[,5]+x_var[,6]+x_var[,7]+x_var[,8]+x_var[,9]+x_var[,10]+x_var[,11]+x_var[,12]+x_var[,13]+x_var[,14]+x_var[,15]+x_var[,16]+x_var[,17]+x_var[,18]+x_var[,19]+x_var[,20])
  
  reduced_model&lt;-stepAIC(initial_model, direction="both")
  reduced_coefs&lt;-t(as.matrix(coef(reduced_model)))
  x_input&lt;-as.matrix(x_var[60,])
  </code></pre>
  
  <hr>
  
  <p>Basically how do I multiply the coefficients that I get from the reduced model to only the corresponding explanatory variables in "x_var" (which has all the explanatory variables)</p>
  
  <p>Thanks a lot for your help!</p>