Algorithms for automatic model selection I would like to implement an algorithm for automatic model selection. 
I am thinking of doing stepwise regression but anything will do (it has to be based on linear regressions though). 
My problem is that I am unable to find a methodology, or an open source implementation (I am woking in java). The methodology I have in mind would be something like:


*

*calculate the correlation matrix of all the factors

*pick the factors that have a low correlation to each other

*remove the factors that have a low t-stat

*add other factors (still based on the low correlation factor found in 2.).

*reiterate several times until some criterion (e.g AIC) is over a certain threshold or cannot or we can't find a larger value.


I realize there is an R implementation for this (stepAIC), but I find the code quite hard to understand. Also I have not been able to find articles describing the stepwise regression.
 A: Here's an answer out of left field- instead of using linear regression, use a regression tree (rpart package). This is suitable for automatic model selection because with a little work you can automate the selection of cp, the parameter used to avoid over-fitting.
A: Check out the caret package in R.  It will help you cross-validate step-wise regression models (use method='lmStepAIC' or method='glmStepAIC'), and might help you understand how these sorts of models tend to have poor predictive performance.  Furthermore, you can use the findCorrelation function in caret to identify and eliminate collinear variables, and the rfe function in caret to eliminate variables with a low t-statistic (use rfeControl=rfeControl(functions=lmFuncs)).
However, as mentioned in the previous answers, these methods of variable selection are likely to get you in trouble, particularly if you do them iteratively.  Make absolutely certain you evaluate your performance on a COMPLETELY held-out test set.  Don't even look at the test set until you are happy with your algorithm!
Finally, it might be better (and simpler) to use predictive model with "built-in" feature selection, such as ridge regression, the lasso, or the elastic net.  Specifically, try the method=glmnet argument for caret, and compare the cross-validated accuracy of that model to the method=lmStepAIC argument.  My guess is that the former will give you much higher out-of-sample accuracy, and you don't have to worry about implementing and validating your custom variable selection algorithm.
A: I fully concur with the problems outlined by @gung. That said, realistically speaking, model selection is a real problem in need of a real solution. Here's something I would use in practice.


*

*Split your data into training, validation, and test sets.

*Train models on your training set.

*Measure model performance on the validation set using a metric such as prediction RMSE, and choose the model with the lowest prediction error.

*Devise new models as necessary, repeat steps 2-3.

*Report how well the model performs on the test set.


For an example of the use of this method in the real world, I believe that it was used in the Netflix Prize competition.
A: linear model can be optimised by implementing genetic algorithm in the way of choosing most valuable independant variables. The variables are represented as genes in the algorithm, and the best chromosome (set of genes) are then being selected after crossover, mutation  etc. operators. It is based on natural selection - then best 'generation' may survive, in other words, the algorithm optimises estimation function that depends on the particular model.  
A: I think this approach is mistaken, but perhaps it will be more helpful if I explain why.  Wanting to know the best model given some information about a large number of variables is quite understandable.  Moreover, it is a situation in which people seem to find themselves regularly.  In addition, many textbooks (and courses) on regression cover stepwise selection methods, which implies that they must be legitimate.  Unfortunately, however, they are not, and the pairing of this situation and goal is quite difficult to successfully navigate.  The following is a list of problems with automated stepwise model selection procedures (attributed to Frank Harrell, and copied from here):


*

*It yields R-squared values that are badly biased to be high.


*The F and chi-squared tests quoted next to each variable on the  printout do not have the claimed distribution.


*The method yields confidence intervals for effects and predicted values that are falsely narrow; see Altman and Andersen (1989).


*It yields p-values that do not have the proper meaning, and the proper correction for them is a difficult problem.


*It gives biased regression coefficients that need shrinkage (the coefficients for remaining variables are too large; see Tibshirani
[1996]).


*It has severe problems in the presence of collinearity.


*It is based on methods (e.g., F tests for nested models) that were intended to be used to test prespecified hypotheses.


*Increasing the sample size does not help very much; see Derksen and Keselman (1992).


*It allows us to not think about the problem.


*It uses a lot of paper.

The question is, what's so bad about these procedures / why do these problems occur?  Most people who have taken a basic regression course are familiar with the concept of regression to the mean, so this is what I use to explain these issues.  (Although this may seem off-topic at first, bear with me, I promise it's relevant.)
Imagine a high school track coach on the first day of tryouts.  Thirty kids show up.  These kids have some underlying level of intrinsic ability to which neither the coach nor anyone else, has direct access.  As a result, the coach does the only thing he can do, which is have them all run a 100m dash.  The times are presumably a measure of their intrinsic ability and are taken as such.  However, they are probabilistic; some proportion of how well someone does is based on their actual ability, and some proportion is random.  Imagine that the true situation is the following:
set.seed(59)
intrinsic_ability = runif(30, min=9, max=10)
time = 31 - 2*intrinsic_ability + rnorm(30, mean=0, sd=.5)

The results of the first race are displayed in the following figure along with the coach's comments to the kids.

Note that partitioning the kids by their race times leaves overlaps on their intrinsic ability--this fact is crucial.  After praising some, and yelling at some others (as coaches tend to do), he has them run again.  Here are the results of the second race with the coach's reactions (simulated from the same model above):

Notice that their intrinsic ability is identical, but the times bounced around relative to the first race.  From the coach's point of view, those he yelled at tended to improve, and those he praised tended to do worse (I adapted this concrete example from the Kahneman quote listed on the wiki page), although actually regression to the mean is a simple mathematical consequence of the fact that the coach is selecting athletes for the team based on a measurement that is partly random.
Now, what does this have to do with automated (e.g., stepwise) model selection techniques?  Developing and confirming a model based on the same dataset is sometimes called data dredging.  Although there is some underlying relationship amongst the variables, and stronger relationships are expected to yield stronger scores (e.g., higher t-statistics), these are random variables, and the realized values contain error.  Thus, when you select variables based on having higher (or lower) realized values, they may be such because of their underlying true value, error, or both.  If you proceed in this manner, you will be as surprised as the coach was after the second race.  This is true whether you select variables based on having high t-statistics, or low intercorrelations.  True, using the AIC is better than using p-values, because it penalizes the model for complexity, but the AIC is itself a random variable (if you run a study several times and fit the same model, the AIC will bounce around just like everything else).  Unfortunately, this is just a problem intrinsic to the epistemic nature of reality itself.
I hope this is helpful.
A: Answers here advises against variable selection, but the problem is real ... and still done. One idea that should be tried out more in practice is blind analyses, as discussed in this nature paper Blind analysis: Hide results to seek the truth. 
This idea has been mentioned in another post at this site, Multiple comparison and secondary research.  The idea of blinding data or introducing extra, simulated noise variables have certainly been used in simulation studies to show problems with stepwise, but the idea here is to use it, blinded, in actual data analysis.
A: To answer the question, there are several options: 


*

*all-subset by AIC/BIC 

*stepwise by p-value 

*stepwise by AIC/BIC 

*regularisation such as LASSO (can be based on either AIC/BIC or CV) 

*genetic algorithm (GA) 

*others? 

*use of non-automatic, theory ("subject matter knowledge") oriented selection 
Next question would be which method is better. This paper (doi:10.1016/j.amc.2013.05.016) indicates “all possible regression” gave the same results to their proposed new method and stepwise is worse. A simple GA is between them. This paper (DOI:10.1080/10618600.1998.10474784) compares penalized regression (Bridge, Lasso etc) with “leaps-and-bounds” (seems an exhaustive search algorithm but quicker) and also found “the bridge model agrees with the best model from the subset selection by the leaps and bounds method”. This paper (doi:10.1186/1471-2105-15-88) shows GA is better than LASSO. This paper (DOI:10.1198/jcgs.2009.06164) proposed a method - essentially an all-subset (based on BIC) approach but cleverly reduce the computation time. They demonstrate this method is better than LASSO. Interestingly, this paper (DOI: 10.1111/j.1461-0248.2009.01361.x) shows methods (1)-(3) produce similar performance.
So overall the results are mixed but I got an impression that GA seems very good although stepwise may not be too bad and it is quick.
As for 7), the use of non-automatic, theory ("subject matter knowledge") oriented selection. It is time consuming and it is not necessarily better than automatic method. In fact in time-series literature, it is well established that automated method (especially commercial software) outperforms human experts "by a substantial margin" (doi:10.1016/S0169-2070(01)00119-4, page561 e.g. selecting various exponential smoothing and ARIMA models).
A: I see my question generated lots of interest and an interesting debate about the validity of the automatic model selection approach. While I agree that taking for granted the result of an automatic selection is risky, it can be used as a starting point. So here is how I implemented it for my particular problem, which is to find the best n factors to explain a given variable


*

*do all the regressions variable vs individual factors

*sort the regression by a given criterion (say AIC)

*remove the factors that have a low t-stat: they are useless in explaining our variable

*with the order given in 2., try to add the factors one by one to the model, and keep them when they improve our criterion. iterate for all the factors.


Again, this is very rough, there may be ways to improve the methodology, but that is my starting point. I am posting this answer hoping it can be useful for someone else. Comments are welcome!
