| 1 |
## ----------------------------------------------------------------------------- |
| 2 |
knitr::opts_chunk$set(message = FALSE, warning = FALSE) |
| 3 |
|
| 4 |
if (rlang::is_installed("ggplot2")) { |
| 5 |
run <- TRUE |
| 6 |
} else { |
| 7 |
run <- FALSE |
| 8 |
} |
| 9 |
|
| 10 |
knitr::opts_chunk$set( |
| 11 |
eval = run |
| 12 |
) |
| 13 |
|
| 14 |
## ----------------------------------------------------------------------------- |
| 15 |
library(broom) |
| 16 |
library(tibble) |
| 17 |
library(ggplot2) |
| 18 |
library(dplyr) |
| 19 |
library(tidyr) |
| 20 |
library(purrr) |
| 21 |
|
| 22 |
theme_set(theme_minimal()) |
| 23 |
|
| 24 |
## ----------------------------------------------------------------------------- |
| 25 |
data(Orange) |
| 26 |
|
| 27 |
Orange <- as_tibble(Orange) |
| 28 |
Orange |
| 29 |
|
| 30 |
## ----------------------------------------------------------------------------- |
| 31 |
cor(Orange$age, Orange$circumference) |
| 32 |
|
| 33 |
ggplot(Orange, aes(age, circumference, color = Tree)) + |
| 34 |
geom_line() |
| 35 |
|
| 36 |
## ----------------------------------------------------------------------------- |
| 37 |
Orange |> |
| 38 |
group_by(Tree) |> |
| 39 |
summarize(correlation = cor(age, circumference)) |
| 40 |
|
| 41 |
## ----------------------------------------------------------------------------- |
| 42 |
ct <- cor.test(Orange$age, Orange$circumference) |
| 43 |
ct |
| 44 |
|
| 45 |
## ----------------------------------------------------------------------------- |
| 46 |
tidy(ct) |
| 47 |
|
| 48 |
## ----------------------------------------------------------------------------- |
| 49 |
nested <- Orange |> |
| 50 |
nest(data = -Tree) |
| 51 |
|
| 52 |
## ----------------------------------------------------------------------------- |
| 53 |
nested |> |
| 54 |
mutate(test = map(data, ~ cor.test(.x$age, .x$circumference))) |
| 55 |
|
| 56 |
## ----------------------------------------------------------------------------- |
| 57 |
nested |> |
| 58 |
mutate( |
| 59 |
test = map(data, ~ cor.test(.x$age, .x$circumference)), # S3 list-col |
| 60 |
tidied = map(test, tidy) |
| 61 |
) |
| 62 |
|
| 63 |
## ----------------------------------------------------------------------------- |
| 64 |
Orange |> |
| 65 |
nest(data = -Tree) |> |
| 66 |
mutate( |
| 67 |
test = map(data, ~ cor.test(.x$age, .x$circumference)), # S3 list-col |
| 68 |
tidied = map(test, tidy) |
| 69 |
) |> |
| 70 |
unnest(tidied) |
| 71 |
|
| 72 |
## ----------------------------------------------------------------------------- |
| 73 |
lm_fit <- lm(age ~ circumference, data = Orange) |
| 74 |
summary(lm_fit) |
| 75 |
|
| 76 |
## ----------------------------------------------------------------------------- |
| 77 |
tidy(lm_fit) |
| 78 |
|
| 79 |
## ----------------------------------------------------------------------------- |
| 80 |
Orange |> |
| 81 |
nest(data = -Tree) |> |
| 82 |
mutate( |
| 83 |
fit = map(data, ~ lm(age ~ circumference, data = .x)), |
| 84 |
tidied = map(fit, tidy) |
| 85 |
) |> |
| 86 |
unnest(tidied) |
| 87 |
|
| 88 |
## ----------------------------------------------------------------------------- |
| 89 |
data(mtcars) |
| 90 |
mtcars <- as_tibble(mtcars) # to play nicely with list-cols |
| 91 |
mtcars |
| 92 |
|
| 93 |
mtcars |> |
| 94 |
nest(data = -am) |> |
| 95 |
mutate( |
| 96 |
fit = map(data, ~ lm(wt ~ mpg + qsec + gear, data = .x)), # S3 list-col |
| 97 |
tidied = map(fit, tidy) |
| 98 |
) |> |
| 99 |
unnest(tidied) |
| 100 |
|
| 101 |
## ----------------------------------------------------------------------------- |
| 102 |
regressions <- mtcars |> |
| 103 |
nest(data = -am) |> |
| 104 |
mutate( |
| 105 |
fit = map(data, ~ lm(wt ~ mpg + qsec + gear, data = .x)), |
| 106 |
tidied = map(fit, tidy), |
| 107 |
glanced = map(fit, glance), |
| 108 |
augmented = map(fit, augment) |
| 109 |
) |
| 110 |
|
| 111 |
regressions |> |
| 112 |
unnest(tidied) |
| 113 |
|
| 114 |
regressions |> |
| 115 |
unnest(glanced) |
| 116 |
|
| 117 |
regressions |> |
| 118 |
unnest(augmented) |
| 119 |
|