Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with writing to a table from a looped stepwise regression

I have a total of 95 potential predictor variables, I'd like to reduce that number to those variables with more predictive power. My plan thus far has been to write some code to:

  • within a loop select 6 random predictors and perform a stepwise regression (direction=both) upon them.
  • this loop will continue for 100,000 iterations to ensure that every possible combination is entered.
  • The significance of the predictor (from the summary command) will be based on the p values. Where significant values <0.05 are coded as '1' and >0.05 are '0' for the 6 predictors (or less) which make it through. The predictor name is preserved in the loop output table.

I cannot seem to create a single output table with the 95 columns and write to each individual line using the 6 column ones generated for each iteration of the loop.

So is there any way to add to an array created with:

results <- array(NA,c(100000,95)) 

with column names assigned by:

colnames(results)<-c(<inputdata>)
like image 257
Matt S Avatar asked Jul 06 '26 01:07

Matt S


2 Answers

Instead of choosing variables at random, why not use a shrinkage and variable selection method, such as the lasso or least angle regression. Both will automatically select variables that are most correlated with the outcome.

There is a mature R package for this.

like image 178
NPE Avatar answered Jul 08 '26 14:07

NPE


aix and Ben Bolker have both made good suggestions. I'd also recommend glmnet, and take a look at the settings for dfmax and pmax, which allow you to constrain the number of active variables in a model and the total number of variables considered along a particular sequence of models.

Essentially, stepwise regression, one variable at a time, is a little antiquated (oh, when I was a young iterator, doing my first iterations, I did stepwise regression all the time), but it's good to move on to a different methodology entirely. There are instances where it's still reasonable, but they're few and rather specialized. All-subsets modeling, however, should be avoided: it simply doesn't scale, and virtually nothing is gained from all of that computational effort.

like image 28
Iterator Avatar answered Jul 08 '26 15:07

Iterator