
Programatically finding the best model
Now that we have seen how to produce scores that represent how good or bad a model's predictive power is, you may go ahead and start specifying lots of models manually by changing the combinations of variables sent to the lm() function, compute each model's scores, and then choose the ones with the highest predictive power. This can potentially take a large amount of time, and you may want to delegate it to someone else since it's tedious work. However, fear not. There's a better way! Computers are good at repetitive and tedious tasks, and now we'll see how to tell the computer to find the best model for us with a little bit of programming.
The following sections will increase the programming level, but don't worry we'll explain the code in detail to make sure that everything is understood. If at any point you feel confused, you can always copy-paste small snippets of code into your R terminal and see what each of them are doing inpidually to gradually get a sense of the whole thing.