Last active
March 1, 2019 08:52
-
-
Save romainfrancois/6f1740c27d70f6f41afa8bf525e5c6ae 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(purrr) | |
library(rap) | |
library(gapminder) | |
library(dplyr, warn.conflicts = FALSE) | |
oceania <- gapminder::gapminder %>% | |
filter(continent == "Oceania") %>% | |
mutate(yr1952 = year - 1952) %>% | |
select(-continent) %>% | |
group_nest(country) | |
# using slam() to promote the formula into something | |
# pmap() can use | |
what <- ~ broom::tidy(stats::lm(lifeExp ~ yr1952, data)) | |
fn <- slam(oceania, what) | |
fn | |
#> function (country, data) | |
#> { | |
#> value(broom::tidy(stats::lm(lifeExp ~ yr1952, data))) | |
#> } | |
#> <environment: 0x7fd025e54e70> | |
pmap(oceania, fn) | |
#> [[1]] | |
#> # A tibble: 2 x 5 | |
#> term estimate std.error statistic p.value | |
#> <chr> <dbl> <dbl> <dbl> <dbl> | |
#> 1 (Intercept) 68.4 0.337 203. 2.07e-19 | |
#> 2 yr1952 0.228 0.0104 21.9 8.67e-10 | |
#> | |
#> [[2]] | |
#> # A tibble: 2 x 5 | |
#> term estimate std.error statistic p.value | |
#> <chr> <dbl> <dbl> <dbl> <dbl> | |
#> 1 (Intercept) 68.7 0.437 157. 2.66e-18 | |
#> 2 yr1952 0.193 0.0135 14.3 5.41e- 8 | |
# together | |
oceania %>% | |
pmap(slam(oceania, ~broom::tidy(stats::lm(lifeExp ~ yr1952, data)))) | |
#> [[1]] | |
#> # A tibble: 2 x 5 | |
#> term estimate std.error statistic p.value | |
#> <chr> <dbl> <dbl> <dbl> <dbl> | |
#> 1 (Intercept) 68.4 0.337 203. 2.07e-19 | |
#> 2 yr1952 0.228 0.0104 21.9 8.67e-10 | |
#> | |
#> [[2]] | |
#> # A tibble: 2 x 5 | |
#> term estimate std.error statistic p.value | |
#> <chr> <dbl> <dbl> <dbl> <dbl> | |
#> 1 (Intercept) 68.7 0.437 157. 2.66e-18 | |
#> 2 yr1952 0.193 0.0135 14.3 5.41e- 8 | |
# using pmap_dfr() to bind all results in one | |
oceania %>% | |
pmap_dfr(slam(oceania, ~broom::tidy(stats::lm(lifeExp ~ yr1952, data)))) | |
#> # A tibble: 4 x 5 | |
#> term estimate std.error statistic p.value | |
#> <chr> <dbl> <dbl> <dbl> <dbl> | |
#> 1 (Intercept) 68.4 0.337 203. 2.07e-19 | |
#> 2 yr1952 0.228 0.0104 21.9 8.67e-10 | |
#> 3 (Intercept) 68.7 0.437 157. 2.66e-18 | |
#> 4 yr1952 0.193 0.0135 14.3 5.41e- 8 | |
# wap() is less verbose to do the same thing | |
oceania %>% | |
wap(~broom::tidy(stats::lm(lifeExp ~ yr1952, data))) | |
#> [[1]] | |
#> # A tibble: 2 x 5 | |
#> term estimate std.error statistic p.value | |
#> <chr> <dbl> <dbl> <dbl> <dbl> | |
#> 1 (Intercept) 68.4 0.337 203. 2.07e-19 | |
#> 2 yr1952 0.228 0.0104 21.9 8.67e-10 | |
#> | |
#> [[2]] | |
#> # A tibble: 2 x 5 | |
#> term estimate std.error statistic p.value | |
#> <chr> <dbl> <dbl> <dbl> <dbl> | |
#> 1 (Intercept) 68.7 0.437 157. 2.66e-18 | |
#> 2 yr1952 0.193 0.0135 14.3 5.41e- 8 | |
# rap() turns it into a column of the data | |
oceania %>% | |
rap(model = ~broom::tidy(stats::lm(lifeExp ~ yr1952, data))) | |
#> # A tibble: 2 x 3 | |
#> country data model | |
#> <fct> <list> <list> | |
#> 1 Australia <tibble [12 × 5]> <tibble [2 × 5]> | |
#> 2 New Zealand <tibble [12 × 5]> <tibble [2 × 5]> | |
# which one can do with a mutate + pmap | |
oceania %>% | |
mutate( | |
model = pmap(., slam(., ~broom::tidy(stats::lm(lifeExp ~ yr1952, data)))) | |
) | |
#> # A tibble: 2 x 3 | |
#> country data model | |
#> <fct> <list> <list> | |
#> 1 Australia <tibble [12 × 5]> <tibble [2 × 5]> | |
#> 2 New Zealand <tibble [12 × 5]> <tibble [2 × 5]> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment