PLEASE HELP!! Data Manipulation. Needing to pull Coefficient and RSE output into Matrix

I'm developing a regression algorithm for predicting the future value each day ahead, from days 1 through n. The dataset I'm working with has over 100 variables.
When I run the regression for Future Day 1, variables b, c, and d may be significant and kept in, but running for Day 3 variables c, f, and g may be the ones kept.

I'm looking for a way to have output that will take the regression coefficients, as well as the RSE for whichever model is being predicted on.

Ideally, the output would be be able to bind / merge (I apologize for wrong terminology) to look a little like this.

image Future_1 Future_2 Future_3
RSE 1.32 1.54 1.85
Intercept 1 1.2 1.4
A
B 1.2
C 1.3 1.43 1.5
D 1.5 1.4
E
F 1.9
G -2.1 1.2
H -1.3

i <- 1
for( i in 1:10) {
  modeldata <- na.omit(AAPL[,c(1:3,5,6+i,17:ncol(AAPL))])
  formula <- lm(paste("modeldata$Future_",i," ~ .", sep=""), data = modeldata)
  #Modelling
  model.Everything <- stepAIC(formula,
                              steps=10000, direction="both", validation = "CV", k=3.5)
  coeff <- model.Everything$coefficients
  rse <- sigma(model.Everything)

  i <- i + 1
  }

Here is a sketch of how you can do that. I did not build a loop but you could change to code where is says

Results[names(vec1), 1] <- vec1

to

Results[names(vec1), i] <- vec1

and maybe make a few more adjustments.
By the way, you should not manually increment the value of i within the for loop. That is, do not have the line.

i <- i + 1

The for statement takes care of the incrementing.

set.seed(10)
#Make a matrix to hold the results
Results <- matrix(NA, nrow = 7, ncol = 2) 
dimnames(Results) <- list(c("(Intercept)", "X1", "X2", "X3", "X4", "X5", "RSE"),
                          c("F1", "F2"))
Results
#>             F1 F2
#> (Intercept) NA NA
#> X1          NA NA
#> X2          NA NA
#> X3          NA NA
#> X4          NA NA
#> X5          NA NA
#> RSE         NA NA

#Make some data to fit
DF <- data.frame(X1 = rnorm(100), X2 = rnorm(100, 4,2), X3 = rnorm( 100, -8, 2),
                 X4 = rnorm(100), X5 = rnorm(100, 15,4))
DF$Y <- DF$X1 * 3.33 + rnorm(100, 0,2) + DF$X3 * 9.1+ rnorm(100, 0,2) - 
  DF$X5 * 0.97 + rnorm(100, 0,3) + 6.78 

#Do the fit and store the result in the Results matrix
FIT <- lm(Y ~ ., data = DF)
FITaic <- MASS::stepAIC(FIT,
                  steps=10000, direction="both", validation = "CV", k=3.5)
#> Start:  AIC=299.4
#> Y ~ X1 + X2 + X3 + X4 + X5
#> 
#>        Df Sum of Sq     RSS    AIC
#> - X2    1       5.7  1624.0 296.25
#> - X4    1      14.6  1633.0 296.80
#> <none>               1618.3 299.40
#> - X1    1    1279.7  2898.0 354.16
#> - X5    1    1823.5  3441.8 371.36
#> - X3    1   29661.7 31280.0 592.06
#> 
#> Step:  AIC=296.25
#> Y ~ X1 + X3 + X4 + X5
#> 
#>        Df Sum of Sq     RSS    AIC
#> - X4    1      15.2  1639.2 293.68
#> <none>               1624.0 296.25
#> + X2    1       5.7  1618.3 299.40
#> - X1    1    1274.0  2898.0 350.66
#> - X5    1    1862.8  3486.8 369.16
#> - X3    1   29987.5 31611.5 589.61
#> 
#> Step:  AIC=293.68
#> Y ~ X1 + X3 + X5
#> 
#>        Df Sum of Sq     RSS    AIC
#> <none>               1639.2 293.68
#> + X4    1      15.2  1624.0 296.25
#> + X2    1       6.3  1633.0 296.80
#> - X1    1    1287.1  2926.3 348.13
#> - X5    1    1856.3  3495.6 365.91
#> - X3    1   29975.6 31614.8 586.12
vec1 <- c(FITaic$coefficients, RSE = (sum(FITaic$residuals^2)/FITaic$df.residual)^0.5)
vec1
#> (Intercept)          X1          X3          X5         RSE 
#>   6.0960822   3.8315487   9.0471842  -0.9682519   4.1322434
Results[names(vec1), 1] <- vec1
Results
#>                     F1 F2
#> (Intercept)  6.0960822 NA
#> X1           3.8315487 NA
#> X2                  NA NA
#> X3           9.0471842 NA
#> X4                  NA NA
#> X5          -0.9682519 NA
#> RSE          4.1322434 NA

Created on 2020-01-09 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.