Now that we have seen how we can run a single time series regression, we can move on to automating separate regressions and extracting the coefficients over all of the categories.
There are several ways to do this. One way is by using the do() function within the dplyr package. Here is the sequence of events:
- The data is first grouped by category.
- Then, a linear regression (lm() function) is run for each category, with Year as the independent variable, and Not.Covered as the dependent variable. This is all wrapped within a do() function.
- The coefficient is extracted from the model. The coefficient will act as a proxy for the direction and magnitude of the trend.
- Finally, a dataframe of lists is created (fitted.models ...