第 44 章 tidyverse中的purrr之美1
44.1 接、化、发
向马保国老师的武术精髓致敬。
f <- list(
body_mass_g ~ bill_length_mm,
body_mass_g ~ bill_length_mm + bill_depth_mm,
body_mass_g ~ bill_length_mm + bill_depth_mm + flipper_length_mm
)
f %>%
set_names(paste("model", 1:3)) %>% # 接
map( ~ tidy(lm(.x, data = penguins)) ) %>% # 化
list_rbind(names_to = "model") # 发
model |
term |
estimate |
std.error |
statistic |
p.value |
---|---|---|---|---|---|
model 1 |
|||||
(Intercept) |
388.845 |
289.817 |
1.342 |
0.181 |
|
bill_length_mm |
86.792 |
6.538 |
13.276 |
0.000 |
|
model 2 |
|||||
(Intercept) |
3,413.452 |
437.911 |
7.795 |
0.000 |
|
bill_length_mm |
74.813 |
6.076 |
12.313 |
0.000 |
|
bill_depth_mm |
-145.507 |
16.873 |
-8.624 |
0.000 |
|
model 3 |
|||||
(Intercept) |
-6,445.476 |
566.130 |
-11.385 |
0.000 |
|
bill_length_mm |
3.293 |
5.366 |
0.614 |
0.540 |
|
bill_depth_mm |
17.836 |
13.826 |
1.290 |
0.198 |
|
flipper_length_mm |
50.762 |
2.497 |
20.327 |
0.000 |
也可以不用broom::tidy()
, 而使用gtsummary::tbl_regression()
library(gtsummary)
f %>%
set_names() %>% # 接
map(~ tbl_regression(lm(.x, data = penguins)) ) %>% # 化
tbl_merge(tab_spanner = paste("model", 1:3)) # 发
Characteristic |
model 1
|
model 2
|
model 3
|
||||||
---|---|---|---|---|---|---|---|---|---|
Beta | 95% CI1 | p-value | Beta | 95% CI1 | p-value | Beta | 95% CI1 | p-value | |
bill_length_mm | 87 | 74, 100 | <0.001 | 75 | 63, 87 | <0.001 | 3.3 | -7.3, 14 | 0.5 |
bill_depth_mm | -146 | -179, -112 | <0.001 | 18 | -9.4, 45 | 0.2 | |||
flipper_length_mm | 51 | 46, 56 | <0.001 | ||||||
1 CI = Confidence Interval |