Skip to content

Instantly share code, notes, and snippets.

@robwschlegel
Created June 20, 2017 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robwschlegel/3d693951f290288bf432475c201cad92 to your computer and use it in GitHub Desktop.
Save robwschlegel/3d693951f290288bf432475c201cad92 to your computer and use it in GitHub Desktop.
# Or we may control for years of sampling
# This allows us to see if the relationship changes much over time
lm_data_year <- data_long %>%
left_join(., gdp_per_capita[,-5], by = c("ccode", "country.name", "year")) %>%
rename(., value = value.x, gdp = value.y) %>%
group_by(year, variable) %>%
mutate(n = sum(!is.na(gdp))) %>%
ungroup() %>%
filter(n > 10) %>% # Require at least 10 data points
nest(-n, -year, -variable) %>%
mutate(fit = map(data, ~ lm(value ~ gdp, data = .)),
results = map(fit, glance)) %>%
unnest(results) %>%
select(n, variable, year, adj.r.squared, p.value) %>%
arrange(-adj.r.squared) %>%
filter(variable != "gdp_per_capita")
# And now for a time series
ggplot(data = lm_data_year, aes(x = year, y = adj.r.squared)) +
geom_line(aes(colour = variable)) +
labs(y = "R^2")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment