1. Evaluating the fit of many models
In the last series of exercises you leveraged the tidy() function from broom to explore the coefficients of your models. By doing so you gained insight into how life expectancy changed with time for each of the 77 countries in your dataset.
Now you will learn how to use the glance() function to measure how well each of the 77 models fit their underlying data.
2. The fit of our models
One way you can measure the fit of a model is to calculate its rsquared metric.
The R-squared metric measures the relationship between the variation explained by the regression model and the total variation in the data. It takes on values between 0 and 1.
3. The fit of our models
Here are two example datasets with a low and a high Rsquared value.
On the left, is a dataset with an Rsquared value close to 0 indicating that a linear model is capturing a proportionally small amount of the variation in the data and hence is not a good fit.
In contrast, the model on the right has an Rsquared value closer to one indicating that this linear model fits the data well.
You can evaluate the fit of all 77 of your models by measuring the Rsquared value for each model.
4. Glance across your models
To do this you use map() and glance() to create a data frame of summary statistics for each model stored as the coef column. You can then simplify these data frames by using the unnest() function.
This results in a tibble containing the model statistics for every country model.
Looking at the rsquared values of the first 6 models you can see that all 6 of these models have a high rsquared suggesting that they have fit the data for that country well.
5. Best & worst fitting models
You can now explore the fit of all 77 models.
For instance, you can use the slice_max() function to find the best fitting models like so.
Likewise, you can find the models with the worst fit by using the slice_min() function.
6. Let's practice!
In the next series of exercises you will build this data frame and explore it to learn more about the fit of each of your 77 models.