Created
February 24, 2017 17:28
-
-
Save hadley/056cf4074acedc164161d6abb751cb35 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(gapminder) | |
gapminder | |
gapminder <- gapminder %>% mutate(year1950 = year - 1950) | |
# Nested data ------------------------------------------------------------- | |
by_country <- gapminder %>% | |
group_by(continent, country) %>% | |
nest() | |
by_country | |
str(by_country) | |
by_country$data[[1]] | |
by_country[1, ] | |
by_country$data[[2]] | |
# Fit models -------------------------------------------------------------- | |
country_model <- function(df) { | |
lm(lifeExp ~ year1950, data = df) | |
} | |
models <- by_country %>% | |
mutate( | |
model = data %>% map(country_model) | |
) | |
models | |
models %>% filter(continent == "Africa") | |
# Broom ------------------------------------------------------------------- | |
models <- models %>% | |
mutate( | |
glance = model %>% map(broom::glance), | |
rsq = glance %>% map_dbl("r.squared"), | |
tidy = model %>% map(broom::tidy), | |
augment = model %>% map(broom::augment) | |
) | |
models | |
models %>% arrange(desc(rsq)) | |
models %>% filter(continent == "Africa") | |
models %>% | |
ggplot(aes(rsq, reorder(country, rsq))) + | |
geom_point(aes(colour = continent)) | |
source("gapminder-shiny.R") | |
# Unnest ------------------------------------------------------------------ | |
models | |
unnest(models, data) # back to where we started | |
unnest(models, glance, .drop = TRUE) %>% View() | |
unnest(models, tidy) | |
models %>% | |
unnest(tidy) %>% | |
select(continent, country, term, estimate, rsq) %>% | |
spread(term, estimate) %>% | |
ggplot(aes(`(Intercept)`, year1950)) + | |
geom_point(aes(colour = continent, size = rsq)) + | |
geom_smooth(se = FALSE) + | |
xlab("Life Expectancy (1950)") + | |
ylab("Yearly improvement") + | |
scale_size_area() | |
models %>% | |
unnest(augment) %>% | |
ggplot(aes(year1950, .resid)) + | |
geom_line(aes(group = country), alpha = 1/3) + | |
geom_hline(yintercept = 0, colour = 'white', size = 2) + | |
geom_smooth(se = FALSE) + | |
facet_wrap(~continent) | |
# Initial summary plot ---------------------------------------------------- | |
summary <- models %>% | |
transmute( | |
continent, | |
country, | |
slope = model %>% map(coef) %>% map_dbl(2), | |
rsq = glance %>% map_dbl("r.squared") | |
) | |
summary %>% | |
ggplot(aes(rsq, slope)) + | |
geom_point(aes(colour = continent)) + | |
xlab(quote(R ^ 2)) + | |
ylab("Estimated yearly increase in life expectancy") + | |
theme(legend.position = "top", aspect.ratio = 1) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
library(miniUI) | |
ui <- miniPage( | |
miniButtonBlock( | |
shiny::flowLayout( | |
sliderInput("rsquared", "R^2", 0, 1, c(0, 0.25)) | |
) | |
), | |
miniContentPanel( | |
plotOutput("plot", height = "100%") | |
) | |
) | |
server <- function(input, output) { | |
selected_models <- reactive({ | |
models %>% | |
filter(between(rsq, input$rsquared[1], input$rsquared[2])) %>% | |
head(20) | |
}) | |
rows <- reactive({ | |
ceiling(nrow(selected_models()) / 2) | |
}) | |
output$plot <- renderPlot({ | |
selected_models() %>% | |
semi_join(gapminder, ., by = "country") %>% | |
ggplot(aes(year, lifeExp)) + | |
geom_line() + | |
facet_wrap(~country, ncol = 2) + | |
theme(plot.margin = margin(0, 0, 0, 0)) + | |
xlab(NULL) + | |
ylab(NULL) | |
}, height = function(...) rows() * 150, res = 96) | |
} | |
runGadget(ui, server, viewer = paneViewer("maximize")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
in gapminder-eda.R, line 22, lm(lifeExp ~ year1950, data = df)
should this not be: lm(lifeExp ~ year1950, data = gapminder)
?