Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# 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