purrr::pmap и функция с несколькими условиями
Всякий раз, когда я использую библиотечную функцию purrr pmap()
с функцией, которая содержит оператор if с несколькими условиями, оператор if, кажется, не работает должным образом. Чтобы показать вам, что я имею в виду, вот воспроизводимый пример, использующий набор данных gapminder.
library(tidyverse)
library(gapminder)
library(broom)
# Nest the tibble into separate dataframes for each country-continent combination
by_country <- gapminder %>%
group_by(country, continent) %>%
nest()
Теперь я хочу построить модель линейной регрессии для каждого сгруппированного кадра данных. Суть в том, что я хочу использовать другую переменную x в моей модели в зависимости от страны и континента. Вот моя функция, где я подозреваю, что есть проблема с оператором if:
# My function
country_model <- function(df, cont, count) {
if(cont == "Asia" & count == "Afghanistan") { # 2 conditions
lm(lifeExp ~ year, data = df)
} else {
lm(lifeExp ~ pop, data = df)
}
}
Теперь я собираюсь взять эту функцию и применить ее ко всем сгруппированным фреймам данных. Я ожидаю, что итоговый вывод модели покажет, что модель для набора данных по Афганистану будет иметь коэффициент для year
скорее, чем pop
,
by_country2 <- by_country %>%
mutate(model = pmap(list(data, continent, country), country_model),
modelsum = map(model, tidy)) %>%
unnest(modelsum, .drop = TRUE)
by_country2
Мой вывод показывает, что коэффициент для Афганистана pop
не year
,
A tibble: 284 × 7
country continent term estimate std.error statistic p.value
<fctr> <fctr> <chr> <dbl> <dbl> <dbl> <dbl>
1 Afghanistan Asia (Intercept) 2.834615e+01 2.314395e+00 12.247758 2.410050e-07
2 Afghanistan Asia pop 5.771517e-07 1.343425e-07 4.296121 1.570999e-03
3 Albania Europe (Intercept) 4.963274e+01 1.935933e+00 25.637630 1.871817e-10
4 Albania Europe pop 7.286188e-06 7.171585e-07 10.159802 1.374311e-06
5 Algeria Africa (Intercept) 3.565187e+01 1.632853e+00 21.834099 9.087006e-10
6 Algeria Africa pop 1.176242e-06 7.588190e-08 15.500960 2.548769e-08
7 Angola Africa (Intercept) 2.855043e+01 1.922225e+00 14.852803 3.843692e-08
8 Angola Africa pop 1.276860e-06 2.482137e-07 5.144195 4.351004e-04
9 Argentina Americas (Intercept) 5.323586e+01 3.784907e-01 140.653008 8.102227e-18
10 Argentina Americas pop 5.532629e-07 1.282987e-08 43.123018 1.079775e-12
# ... with 274 more rows
Что странно для меня, так это то, что когда я использую только 1 условие в своей функции if, тогда, кажется, это работает отлично:
country_model <- function(df, cont) {
if(cont == "Asia") { # Only 1 condition
lm(lifeExp ~ year, data = df)
} else {
lm(lifeExp ~ pop, data = df)
}
}
by_country2 <- by_country %>%
mutate(model = map2(data, continent, country_model),
modelsum = map(model, tidy)) %>%
unnest(modelsum, .drop = TRUE)
by_country2
# A tibble: 284 × 7
country continent term estimate std.error statistic p.value
<fctr> <fctr> <chr> <dbl> <dbl> <dbl> <dbl>
1 Afghanistan Asia (Intercept) -5.075343e+02 4.048416e+01 -12.536613 1.934055e-07
2 Afghanistan Asia year 2.753287e-01 2.045093e-02 13.462890 9.835213e-08
3 Albania Europe (Intercept) 4.963274e+01 1.935933e+00 25.637630 1.871817e-10
4 Albania Europe pop 7.286188e-06 7.171585e-07 10.159802 1.374311e-06
5 Algeria Africa (Intercept) 3.565187e+01 1.632853e+00 21.834099 9.087006e-10
6 Algeria Africa pop 1.176242e-06 7.588190e-08 15.500960 2.548769e-08
7 Angola Africa (Intercept) 2.855043e+01 1.922225e+00 14.852803 3.843692e-08
8 Angola Africa pop 1.276860e-06 2.482137e-07 5.144195 4.351004e-04
9 Argentina Americas (Intercept) 5.323586e+01 3.784907e-01 140.653008 8.102227e-18
10 Argentina Americas pop 5.532629e-07 1.282987e-08 43.123018 1.079775e-12
# ... with 274 more rows
Я не уверен, что моя проблема с pmap()
или мое заявление if.
1 ответ
Это связано с этой проблемой GitHub.
Кажется pmap
отправляет через continent
а также country
в виде чисел, что можно подтвердить, поместив оператор print в вашу функцию.
test_fun <- function(df, cont, xx) {
print(paste(cont, xx))
}
temp <-by_country %>%
mutate(model = pmap(list(data, continent, country), test_fun))
Печать:
[1] "3 1" [1] "4 2" [1] "1 3" [1] "1 4" [1] "2 5" [1] "5 6" [1] "4 7" [1] "3 8" [1] "3 9" etc
Это не происходит в map2
и, следовательно, ваша вторая попытка работает.
Принуждение к персонажу решает проблему:
by_country %>%
mutate(model = pmap(list(data, as.character(continent), as.character(country)), country_model),
modelsum = map(model, broom::tidy)) %>%
unnest(modelsum, .drop = TRUE)
# A tibble: 284 x 7 country continent term estimate std.error statistic p.value <fctr> <fctr> <chr> <dbl> <dbl> <dbl> <dbl> 1 Afghanistan Asia (Intercept) -5.075343e+02 4.048416e+01 -12.536613 1.934055e-07 2 Afghanistan Asia year 2.753287e-01 2.045093e-02 13.462890 9.835213e-08 3 Albania Europe (Intercept) 4.963274e+01 1.935933e+00 25.637630 1.871817e-10 4 Albania Europe pop 7.286188e-06 7.171585e-07 10.159802 1.374311e-06 5 Algeria Africa (Intercept) 3.565187e+01 1.632853e+00 21.834099 9.087006e-10 6 Algeria Africa pop 1.176242e-06 7.588190e-08 15.500960 2.548769e-08 7 Angola Africa (Intercept) 2.855043e+01 1.922225e+00 14.852803 3.843692e-08 8 Angola Africa pop 1.276860e-06 2.482137e-07 5.144195 4.351004e-04 9 Argentina Americas (Intercept) 5.323586e+01 3.784907e-01 140.653008 8.102227e-18 10 Argentina Americas pop 5.532629e-07 1.282987e-08 43.123018 1.079775e-12 # ... with 274 more rows