IniziaInizia gratis

Nesting by topic and country

In the last chapter, you constructed a linear model for each country by nesting the data in each country, fitting a model to each dataset, then tidying each model with broom and unnesting the coefficients. The code looked something like this:

country_coefficients <- by_year_country %>%
  nest(-country) %>%
  mutate(model = map(data, ~ lm(percent_yes ~ year, data = .)),
         tidied = map(model, tidy)) %>%
  unnest(tidied)

Now, you'll again be modeling change in "percentage" yes over time, but instead of fitting one model for each country, you'll fit one for each combination of country and topic.

Questo esercizio fa parte del corso

Case Study: Exploratory Data Analysis in R

Visualizza il corso

Istruzioni dell'esercizio

  • Load the purrr, tidyr, and broom packages.
  • Print the by_country_year_topic dataset to the console.
  • Fit a linear model within each country and topic in this dataset, saving the result as country_topic_coefficients. You can use the provided code as a starting point.
  • Print the country_topic_coefficients dataset to the console.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# Load purrr, tidyr, and broom


# Print by_country_year_topic


# Fit model on the by_country_year_topic dataset


# Print country_topic_coefficients
Modifica ed esegui il codice