Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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)
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"))
@AlKavaev
Copy link

AlKavaev commented Mar 11, 2017

Kia Ora,
This is wonderful. Thank you for your time and work.
When I run source("gapminder-shiny.R") it throws back Warning: Error in UseMethod: no applicable method for 'margin' applied to an object of class "c('double', 'numeric')".
But if I remove or # this line: theme(plot.margin = margin(0, 0, 0, 0)) + from the ggplot() in the gapminder-shiny.R everything works properly.

BR,
Alex

@dannyGee86
Copy link

dannyGee86 commented Sep 15, 2017

Just found the youtube video on this, thanks alot really useful!!

@harrisjono
Copy link

harrisjono commented Mar 20, 2019

in gapminder-eda.R, line 22, lm(lifeExp ~ year1950, data = df)
should this not be: lm(lifeExp ~ year1950, data = gapminder)
?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment