I have a dataframe of 12 predictors and a list of numbers called BEI(which I want to predict). I want to run stepwise selection on every 12 rows of data, for example 1:12, 2:13 and etc. For each rolling, I want to return the coefficients and use the coefficients to predict BEI. Below is my code:
k = length(BEI)
coef.list <- numeric()
predicted.list <- numeric()
for(i in 1:(k-11)){
BEI.subset <- BEI[i:(i+11)]
predictors.subset <- predictors[c(i:(i+11)),]
fit.stepwise <- regsubsets(BEI.subset~., data = predictors.subset, nvmax = 10, method = "forward")
fit.summary <- summary(fit.stepwise)
id <- which.min(fit.summary$cp)
coefficients <- coef(fit.stepwise,id)
coef.list <- append(coef.list, coefficients)
form <- as.formula(fit.stepwise$call[[2]])
mat <- model.matrix(form,predictors.subset)
predicted.stepwise <- mat[,names(coefficients)]%*%coefficients
predicted.list <- append(predicted.list, predicted.stepwise)
}
and I got the errors like this: Reordering variables and trying again: There were 50 or more warnings (use warnings() to see the first 50)
the warnings are: 1: In leaps.setup(x, y, wt = wt, nbest = nbest, nvmax = nvmax, ... : 1 linear dependencies found 2: In leaps.setup(x, y, wt = wt, nbest = nbest, nvmax = nvmax, ... : 1 linear dependencies found 3: In leaps.setup(x, y, wt = wt, nbest = nbest, nvmax = nvmax, ... : 1 linear dependencies found .... etc.
How do I fix this? Or is this a better way to write the codes?