Skip to content

Instantly share code, notes, and snippets.

@DavisVaughan
Created November 24, 2018 16:18
Show Gist options
  • Save DavisVaughan/8bf3d425c7fbf3300e042e0f4ab70c27 to your computer and use it in GitHub Desktop.
Save DavisVaughan/8bf3d425c7fbf3300e042e0f4ab70c27 to your computer and use it in GitHub Desktop.
library(gapminder)
library(tidyverse)
library(zap)
library(broom)
# gapminder data for Asia only
gap <- gapminder %>%
filter(continent == "Asia") %>%
mutate(yr1952 = year - 1952)
gap <- gap %>%
mutate(country = fct_reorder2(country, .x = year, .y = lifeExp))
gap_nested <- gap %>%
group_by(country) %>%
nest()
# gap_fitted <- gap_nested %>%
# mutate(fit = map(data, ~ lm(lifeExp ~ yr1952, data = .x)))
gap_fitted <- zap(gap_nested, fit = ~lm(lifeExp ~ yr1952, data = data))
gap_fitted
#> # A tibble: 33 x 3
#> country data fit
#> <fct> <list> <list>
#> 1 Afghanistan <tibble [12 × 6]> <S3: lm>
#> 2 Bahrain <tibble [12 × 6]> <S3: lm>
#> 3 Bangladesh <tibble [12 × 6]> <S3: lm>
#> 4 Cambodia <tibble [12 × 6]> <S3: lm>
#> 5 China <tibble [12 × 6]> <S3: lm>
#> 6 Hong Kong, China <tibble [12 × 6]> <S3: lm>
#> 7 India <tibble [12 × 6]> <S3: lm>
#> 8 Indonesia <tibble [12 × 6]> <S3: lm>
#> 9 Iran <tibble [12 × 6]> <S3: lm>
#> 10 Iraq <tibble [12 × 6]> <S3: lm>
#> # … with 23 more rows
# gap_fitted <- gap_fitted %>%
# mutate(
# intercept = map_dbl(fit, ~ coef(.x)[["(Intercept)"]]),
# slope = map_dbl(fit, ~ coef(.x)[["yr1952"]])
# )
gap_fitted %>%
zap_dbl(intercept = ~coef(fit)[["(Intercept)"]]) %>%
zap_dbl(slope = ~coef(fit)[["yr1952"]])
#> # A tibble: 33 x 5
#> country data fit intercept slope
#> <fct> <list> <list> <dbl> <dbl>
#> 1 Afghanistan <tibble [12 × 6]> <S3: lm> 29.9 0.275
#> 2 Bahrain <tibble [12 × 6]> <S3: lm> 52.7 0.468
#> 3 Bangladesh <tibble [12 × 6]> <S3: lm> 36.1 0.498
#> 4 Cambodia <tibble [12 × 6]> <S3: lm> 37.0 0.396
#> 5 China <tibble [12 × 6]> <S3: lm> 47.2 0.531
#> 6 Hong Kong, China <tibble [12 × 6]> <S3: lm> 63.4 0.366
#> 7 India <tibble [12 × 6]> <S3: lm> 39.3 0.505
#> 8 Indonesia <tibble [12 × 6]> <S3: lm> 36.9 0.635
#> 9 Iran <tibble [12 × 6]> <S3: lm> 45.0 0.497
#> 10 Iraq <tibble [12 × 6]> <S3: lm> 50.1 0.235
#> # … with 23 more rows
# maybe even better
gap_fitted %>%
zap(coefs = ~tidy(fit)) %>%
unnest(coefs)
#> # A tibble: 66 x 6
#> country term estimate std.error statistic p.value
#> <fct> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 Afghanistan (Intercept) 29.9 0.664 45.0 7.00e-13
#> 2 Afghanistan yr1952 0.275 0.0205 13.5 9.84e- 8
#> 3 Bahrain (Intercept) 52.7 0.890 59.2 4.56e-14
#> 4 Bahrain yr1952 0.468 0.0274 17.0 1.02e- 8
#> 5 Bangladesh (Intercept) 36.1 0.530 68.1 1.13e-14
#> 6 Bangladesh yr1952 0.498 0.0163 30.5 3.37e-11
#> 7 Cambodia (Intercept) 37.0 3.06 12.1 2.69e- 7
#> 8 Cambodia yr1952 0.396 0.0942 4.20 1.82e- 3
#> 9 China (Intercept) 47.2 2.09 22.5 6.67e-10
#> 10 China yr1952 0.531 0.0645 8.23 9.21e- 6
#> # … with 56 more rows
#' Created on 2018-11-24 by the [reprex
#' package](http://reprex.tidyverse.org) (v0.2.0).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment