Predicting votes with linear models

Before we can make any predictions, we need to specify a model and train it with our training data (data_train) so that it learns how to provide us with the predictions we're looking for. This means that we will solve an optimization problem that outputs certain numbers that will be used as parameters for our model's predictions. R makes it very easy for us to accomplish such a task.

The standard way of specifying a linear regression model in R is using the lm() function with the model we want to build expressed as a formula and the data that should be used, and save it into an object (in this case fit) that we can use to explore the results in detail. For example, the simplest model we can build is one ...

Get R Programming By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.