-
Notifications
You must be signed in to change notification settings - Fork 0
/
eda.Rmd
240 lines (190 loc) · 5.03 KB
/
eda.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
---
title: "Exploratory Data Analysis"
author: "Jessica Lavery"
date: "9/26/2019"
output: github_document
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
library(tidyverse)
library(viridis)
library(knitr)
knitr::opts_chunk$set(echo = TRUE,
warning = FALSE,
fig.width = 8,
fig.height = 6,
out.width = "90%")
options(
ggplot2.continuous.colour = "viridis",
ggplot2.continuous.fill = "viridis"
)
scale_colour_discrete = scale_colour_viridis_d
scale_fill_discrete = scale_fill_viridis_d
theme_set(theme_minimal() + theme(legend.position = "bottom"))
```
```{r, cache = TRUE}
weather_df =
rnoaa::meteo_pull_monitors(c("USW00094728", "USC00519397", "USS0023B17S"),
var = c("PRCP", "TMIN", "TMAX"),
date_min = "2017-01-01",
date_max = "2017-12-31") %>%
mutate(
name = recode(id, USW00094728 = "CentralPark_NY",
USC00519397 = "Waikiki_HA",
USS0023B17S = "Waterhole_WA"),
tmin = tmin / 10,
tmax = tmax / 10,
month = lubridate::floor_date(date, unit = "month")) %>%
select(name, id, date, month, everything())
```
## `group_by` and counting
```{r}
weather_df %>%
group_by(name, month)
```
```{r}
weather_df %>%
group_by(name, month) %>%
summarize(n_obs = n())
```
### `n_distinct`
```{r}
weather_df %>%
group_by(month) %>%
summarize(n_obs = n(),
n_unique = n_distinct(date))
```
### `count`
Count shortens using `group_by` and `summarize`.
```{r}
# default varible name for count is n
weather_df %>%
count(name)
# can also customize this
weather_df %>%
count(name, name = "n_days")
```
** Never use base R's table() function **
```{r}
tbl <- weather_df %>%
pull(name) %>%
table()
# print the result
tbl
# result is of class "table", not a data frame!
class(tbl)
# same as table(weather_df$name)
```
## Kable
Let's make a nice table
```{r}
weather_df %>%
count(name) %>%
knitr::kable()
```
## 2x2 tables
```{r}
weather_df %>%
filter(name != "Waikiki_HA") %>%
mutate(
cold = case_when(
tmax < 5 ~ "cold",
tmax >= 5 ~ "not cold",
TRUE ~ ""
)
) %>%
group_by(name, cold) %>%
count() %>%
pivot_wider(
names_from = cold,
values_from = n
)
```
Alternative approach using `janitor::tabyl()`
https://cran.r-project.org/web/packages/janitor/vignettes/tabyls.html
```{r}
weather_df %>%
filter(name != "Waikiki_HA") %>%
mutate(
cold = case_when(
tmax < 5 ~ "cold",
tmax >= 5 ~ "not cold",
TRUE ~ ""
)
) %>%
janitor::tabyl(name, cold) %>%
janitor::adorn_percentages("row") %>%
janitor::adorn_pct_formatting(digits = 2) %>%
janitor::adorn_ns() %>%
kable()
```
## general summaries
By deafult, any time that you compute a numeric summary of something that includes an NA, R returns an NA. Options: drop these rows from the data, modify function to ignore NAs in computation via na.rm = TRUE. Don't do the latter option by default, look at the missing values and make sure you understand what's going on.
Can also pipe in the summary into ggplot to create a visual of the summary that was created.
```{r}
weather_df %>%
group_by(name, month) %>%
summarize(
n = n(),
mean_tmax = mean(tmax, na.rm = TRUE),
sd_tmax = sd(tmax, na.rm = TRUE),
median_prcp = median(prcp, na.rm = TRUE)
) %>%
ggplot(aes(x = month, y = mean_tmax, color = name)) +
geom_point() + geom_line() +
theme(legend.position = "bottom")
```
### user-friendly summaries
The output won't be in "tidy" format, but will be more user-friendly.
```{r}
weather_df %>%
group_by(name, month) %>%
summarize(
mean_tmax = mean(tmax, na.rm = TRUE)
) %>%
pivot_wider(
names_from = name,
values_from = mean_tmax) %>%
knitr::kable()
```
### ungrouping
```{r}
weather_df %>%
group_by(name) %>%
ungroup()
```
### grouping & mutating
All mutating will be group-specific.
```{r}
weather_df %>%
group_by(name) %>%
mutate(
mean_tmax = mean(tmax, na.rm = TRUE),
centered_tmax = tmax - mean_tmax
) %>%
ggplot(aes(x = date, y = centered_tmax, color = name)) +
geom_point()
```
#### Window functions in grouped mutates
To get the rank in terms of order (first coldest day, 2nd, etc.), want to take all tmax values and sort and rank.
```{r}
# look at ?min_rank for description of windowed rank functions
weather_df %>%
group_by(name, month) %>%
mutate(
tmax_rank = min_rank(tmax),
tmax_rank_desc = min_rank(desc(tmax))
) %>%
arrange(name, month, tmax_rank) %>%
filter(tmax_rank == 1) # to get the coldest day in each location in each month
```
### lag
Look at change in max temperature from current day to next day
Opposite of the lag function is the lead function.
```{r}
weather_df %>%
group_by(name) %>%
arrange(name, date) %>%
mutate(one_day_tmax_change = tmax - lag(tmax)) %>% summarize(sd_daily_chnge = sd(one_day_tmax_change, na.rm = TRUE))
```