Tidy modeling by topic and country
1. Tidy modeling by topic and country
In Chapter 3, you used the broom package to fit a separate linear model for each country that measured the trend of percentage of yes votes over time. This let you find the countries whose rate of agreement was increasing or decreasing most quickly.2. Detecting a trend by topic
With the new datasets you've built in this chapter, you fit these trends within each country *and* within each topic. For example, you could fit trends for the United Kingdom's voting behavior within each of these six topics, as seen here.3. Tidy modeling by country
Recall that there were several steps to fitting a model for each country. You FIRST nested all columns besides country into their own sub-datasets in a list column. You then used map CLICK to fit a linear model to each of these sub-datasets, and then tidied each of them into a table of coefficients. Finally, you used unnest to bring those coefficients back into the main data frame, resulting in a combined table of slopes and intercepts. Now that you have a topic column in your by_year_country_topic summary, there's only one change you need to make to this workflow to fit a model within each country/topic combination.4. Tidy modeling by country and topic
In the nest statement, simply nest all columns besides country and topic. The other steps are identical. What results is a table with the estimated coefficients for each specific topic for each country. For example, these rows5. Tidy modeling by country and topic
where the term equals "year" show the estimated slopes on the topics of Colonialism, Economic development, human rights, and so on within Afghanistan. This dataset will let you explore which countries had the sharpest strongest within particular topics- for example, which country had most changed its voting pattern on the topic of colonialism. This analysis demonstrates the flexibility of the nest, model, and unnest pattern in exploratory analysis. You could have chosen to slice your data in many other ways using alternative data sources, and the tidyr, dplyr, and broom packages will always give you the tools to answer the questions you're interested in.6. Let's practice!
Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.