-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathPart_1.Rmd
636 lines (452 loc) · 17.1 KB
/
Part_1.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
---
title: "Applied Machine Learning"
subtitle: "Introduction"
author: Max Kuhn and Davis Vaughan (RStudio)
date: "`r Sys.Date()`"
session: 01
output:
xaringan::moon_reader:
css: ["default", "assets/css/aml-theme.css", "assets/css/aml-fonts.css"]
seal: false
lib_dir: libs
nature:
# autoplay: 5000
highlightStyle: solarized-light
highlightLanguage: R
highlightLines: true
countIncrementalSlides: false
ratio: "16:9"
beforeInit: "https://platform.twitter.com/widgets.js"
includes:
in_header: [assets/header.html]
params:
wifi_network: "rstudio20"
wifi_password: "tidyverse20"
site_link: "http://rstd.io/class"
cloud_link: "http://rstd.io/class"
github_link: "applied-ml"
editor_options:
chunk_output_type: console
---
```{r knitr, include = FALSE}
library(knitr)
opts_chunk$set(digits = 3, fig.path = "images/part-1-")
hexes <- function(x) {
x <- rev(sort(x))
markup <- function(pkg) glue::glue('<img src="images/{pkg}.png" class="title-hex">')
res <- purrr::map_chr(x, markup)
paste0(res, collapse = "")
}
```
class: title-slide, center
<span class="fa-stack fa-4x">
<i class="fa fa-circle fa-stack-2x" style="color: #ffffff;"></i>
<strong class="fa-stack-1x" style="color:#E7553C;">`r rmarkdown::metadata$session`</strong>
</span>
# `r rmarkdown::metadata$title`
## `r rmarkdown::metadata$subtitle`
---
class: middle, center
.pull-left[
# <i class="fas fa-wifi"></i>
Wifi network name
`r params$wifi_network`
]
.pull-left[
# <i class="fas fa-key"></i>
Wifi password
`r params$wifi_password`
]
---
class: middle, center
# <i class="fas fa-cloud"></i>
# Go here and log in (free):
[`r params$cloud_link`](`r params$cloud_link`)
---
layout: true
<div class="my-footer"><span>`r params$site_link`</span></div>
---
# Why R for Modeling?
.pull-left[
* _R has cutting edge models_.
Machine learning developers in some domains use R as their primary computing environment and their work often results in R packages.
* _It is easy to port or link to other applications_.
R doesn't try to be everything to everyone. If you prefer models implemented in C, C++, `tensorflow`, `keras`, `python`, `stan`, or `Weka`, you can access these applications without leaving R.
]
.pull-right[
* _R and R packages are built by people who **do** data analysis_.
* _The S language is very mature_.
* The machine learning environment in R is extremely rich.
]
---
# Downsides to Modeling in R
.pull-left[
* R is a data analysis language and is not C or Java. If a high performance deployment is required, R can be treated like a prototyping language.
* R is mostly memory-bound. There are plenty of exceptions to this though.
]
.pull-right[
The main issue is one of _consistency of interface_. For example:
* There are two methods for specifying what terms are in a model<sup>1</sup>. Not all models have both.
* 99% of model functions automatically generate dummy variables.
* Sparse matrices can be used (unless they can't).
]
.footnote[[1] There are now three but the last one is brand new and will be discussed later.]
---
# Syntax for Computing Predicted Class Probabilities
|Function |Package |Code |
|:------------|:------------|:----------------------------------------------------|
|`lda` |`MASS` |`predict(obj)` |
|`glm` |`stats` |`predict(obj, type = "response")` |
|`gbm` |`gbm` |`predict(obj, type = "response", n.trees)` |
|`mda` |`mda` |`predict(obj, type = "posterior")` |
|`rpart` |`rpart` |`predict(obj, type = "prob")` |
|`Weka` |`RWeka` |`predict(obj, type = "probability")` |
|`logitboost` |`LogitBoost` |`predict(obj, type = "raw", nIter)` |
|`pamr.train` |`pamr` |`pamr.predict(obj, type = "posterior", threshold)` |
We'll see a solution for this later in the class.
---
# `tidymodels` Collection of Packages `r I(hexes(c("tidymodels")))`
.code90[
```{r tm}
library(tidymodels)
```
]
```{r others, include = FALSE}
library(kableExtra)
library(ggthemes)
library(countdown)
```
Plus [`tidypredict`](http://tidypredict.netlify.com/), [`tidyposterior`](https://tidymodels.github.io/tidyposterior/), [`tidytext`](https://github.com/juliasilge/tidytext), and more in development.
```{r ggplot, include = FALSE}
thm <- theme_bw() +
theme(
panel.background = element_rect(fill = "transparent", colour = NA),
plot.background = element_rect(fill = "transparent", colour = NA),
legend.position = "top",
legend.background = element_rect(fill = "transparent", colour = NA),
legend.key = element_rect(fill = "transparent", colour = NA)
)
theme_set(thm)
```
---
# Example Data Set - House Prices
For our examples, we will use the Ames IA housing data. There are 2,930 properties in the data.
The Sale Price was recorded along with 81 predictors, including:
* Location (e.g. neighborhood) and lot information.
* House components (garage, fireplace, pool, porch, etc.).
* General assessments such as overall quality and condition.
* Number of bedrooms, baths, and so on.
More details can be found in [De Cock (2011, Journal of Statistics Education)](http://ww2.amstat.org/publications/jse/v19n3/decock.pdf).
The raw data are at [`http://bit.ly/2whgsQM`](http://bit.ly/2whgsQM) but we will use a processed version found in the [`AmesHousing`](https://github.com/topepo/AmesHousing) package.
---
# Example Data Set - House Prices
```{r ames-map, echo = FALSE, message = FALSE, fig.align='center', dev = "svg"}
library(AmesHousing)
library(leaflet)
library(htmltools)
library(Cairo)
ames <- make_ames()
col_key <- c(
'NAmes', '#0000FF',
'CollgCr', '#FF0000',
'OldTown', '#FFFFFF',
'Edwards', '#FF00B6',
'Somerst', '#FF3030',
'NridgHt', '#009FFF',
'Gilbert', '#DD00FF',
'Sawyer', '#9A4D42',
'NWAmes', '#00FFBE',
'SawyerW', '#1F9698',
'Mitchel', '#FFACFD',
'BrkSide', '#720055',
'Crawfor', '#F1085C',
'IDOTRR', '#FE8F42',
'Timber', '#004CFF',
'NoRidge', '#ffff00',
'StoneBr', '#B1CC71',
'SWISU', '#02AD24',
'ClearCr', '#FFD300',
'MeadowV', '#886C00',
'BrDale', '#FFB79F',
'Blmngtn', '#858567',
'Veenker', '#A10300',
'NPkVill', '#00479E',
'Blueste', '#DC5E93',
'Greens', '#93D4FF',
'GreenHills', '#e5f2e5',
'Landmrk', '#C8FF00'
)
col_key <- as.data.frame(matrix(col_key, byrow = TRUE, ncol = 2))
names(col_key) <- c("Neighborhood", "color")
col_key <- col_key %>%
mutate(
Neighborhood =
dplyr::recode(
Neighborhood,
"Blmngtn" = "Bloomington_Heights",
"Bluestem" = "Bluestem",
"BrDale" = "Briardale",
"BrkSide" = "Brookside",
"ClearCr" = "Clear_Creek",
"CollgCr" = "College_Creek",
"Crawfor" = "Crawford",
"Edwards" = "Edwards",
"Gilbert" = "Gilbert",
"Greens" = "Greens",
"GreenHills" = "Green_Hills",
"IDOTRR" = "Iowa_DOT_and_Rail_Road",
"Landmrk" = "Landmark",
"MeadowV" = "Meadow_Village",
"Mitchel" = "Mitchell",
"NAmes" = "North_Ames",
"NoRidge" = "Northridge",
"NPkVill" = "Northpark_Villa",
"NridgHt" = "Northridge_Heights",
"NWAmes" = "Northwest_Ames",
"OldTown" = "Old_Town",
"SWISU" = "South_and_West_of_Iowa_State_University",
"Sawyer" = "Sawyer",
"SawyerW" = "Sawyer_West",
"Somerst" = "Somerset",
"StoneBr" = "Stone_Brook",
"Timber" = "Timberland",
"Veenker" = "Veenker"
))
lon_rnd <- range(ames$Longitude)
lat_rnd <- range(ames$Latitude)
ia_map <- leaflet(width = "100%") %>%
addProviderTiles(providers$Stamen.Toner)
for(i in 1:nrow(col_key)) {
ia_map <- ia_map %>%
addCircles(
data = subset(ames, Neighborhood == col_key$Neighborhood[i]),
lng = ~Longitude, lat = ~Latitude,
color = col_key$color[i],
fill = TRUE,
fillColor = col_key$color[i],
radius = 6,
popup = htmlEscape(col_key$Neighborhood[i]),
opacity = .25)
}
ia_map
```
---
# Tidyverse Syntax `r I(hexes(c("dplyr")))`
Many tidyverse functions have syntax unlike base R code. For example:
* Vectors of variable names are eschewed in favor of _functional programming_. For example:
```{r func-ex, eval = FALSE}
contains("Sepal")
# instead of
c("Sepal.Width", "Sepal.Length")
```
* The _pipe_ operator is preferred. For example:
```{r pipe-ex, eval = FALSE}
merged <- inner_join(a, b)
# is equal to
merged <- a %>%
inner_join(b)
```
* Functions are more _modular_ than their traditional analogs (`dplyr`'s `filter()` and `select()` vs `base::subset()`)
---
# Some Example Data Manipulation Code `r I(hexes(c("readr", "dplyr")))`
.font10[
```{r tidy-example, message = FALSE, warning = FALSE}
library(tidyverse)
ames_prices <- "http://bit.ly/2whgsQM" %>%
read_delim(delim = "\t", guess_max = 2000) %>%
rename_at(vars(contains(' ')), list(~gsub(' ', '_', .))) %>%
dplyr::rename(Sale_Price = SalePrice) %>%
dplyr::filter(!is.na(Electrical)) %>%
dplyr::select(-Order, -PID, -Garage_Yr_Blt)
ames_prices %>%
group_by(Alley) %>%
summarize(
mean_price = mean(Sale_Price / 1000),
n = sum(!is.na(Sale_Price))
)
```
]
---
# Examples of `purrr::map*` `r I(hexes(c("purrr", "dplyr")))`
purrr contains functions that _iterate over lists_ without the explicit use of loops. They are similar to the family of apply functions in base R, but are type stable.
.pull-left[
```{r purrr-example-lhs}
# purrr loaded with tidyverse or tidymodels package
mini_ames <- ames_prices %>%
dplyr::select(Alley, Sale_Price, Yr_Sold) %>%
dplyr::filter(!is.na(Alley))
head(mini_ames, n = 5)
```
]
.pull-right[
```{r purrr-split-map}
by_alley <- split(mini_ames, mini_ames$Alley)
# map(.x, .f, ...)
map(by_alley, head, n = 2)
```
]
---
# Examples of `purrr::map*` `r I(hexes(c("purrr", "dplyr")))`
.pull-left[
```{r purrr-map-nrow}
map(by_alley, nrow)
```
`map()` always returns a list. Use suffixed versions for simplification of the result.
```{r purrr-map-int}
map_int(by_alley, nrow)
```
]
.pull-right[
Complex operations can be specified using a _formula notation_. Access the current
thing you are iterating over with `.x`.
```{r purrr-map-summarise}
map(
by_alley,
~summarise(.x, max_price = max(Sale_Price))
)
```
]
---
# `purrr` and list-columns `r I(hexes(c("purrr", "dplyr", "tidyr")))`
Rather than using `split()`, we can `tidyr::nest()` by `Alley` to get a data frame with
a _list-column_. We often use these when working with _multiple models_.
.pull-left[
```{r tidyr-nest}
ames_lst_col <- nest(mini_ames, -Alley)
ames_lst_col
```
]
.pull-right[
```{r list-col-mutate}
ames_lst_col %>%
mutate(
n_row = map_int(data, nrow),
max = map_dbl(data, ~ max(.x$Sale_Price))
)
```
]
---
# Quick Data Investigation
To get warmed up, let's load the real Ames data and do some basic investigations into the variables, such as exploratory visualizations or summary statistics. The idea is to get a feel for the data.
Let's take 10 minutes to work on your own or with someone next to you. Collaboration is highly encouraged!
To get the data:
```{r load-ames}
library(AmesHousing)
ames <- make_ames()
```
`r countdown(minutes = 10, left = 1)`
---
# The Modeling _Process_
Common steps during model building are:
* estimating model parameters (i.e. training models)
* determining the values of _tuning parameters_ that cannot be directly calculated from the data
* model selection (within a model type) and model comparison (between types)
* calculating the performance of the final model that will generalize to new data
Many books and courses portray predictive modeling as a short sprint. A better analogy would be a marathon or campaign (depending on how hard the problem is).
---
# What the Modeling Process Usually Looks Like
```{r mod-process, echo = FALSE, out.width = '95%', fig.width=8, fig.height=2.5, fig.align='center', dev = 'svg', dev.args = list(bg = "transparent"), warning=FALSE}
widths <- c(8, 4, 10, 2, 6, 6,
rep(1, 19), 2,
rep(1, 19), 2,
rep(1, 19), 2,
rep(1, 19), 2,
4, 8, 15,
rep(1, 29), 6,
rep(1, 29), 4,
1)
srt <- c(1, cumsum(widths))
stp <- srt[-1]
srt <- srt[-length(srt)]
diag_cols <- c(EDA = "#377EB8", "Quantitative Analysis" = "#A6CEE3",
"Feature Engineering" = "#4DAF4A", "Model Fit" = "#E41A1C",
"Model Tuning" = "grey")
bar_loc <- data.frame(srt = srt,
stp = stp,
g = c("EDA", "Quantitative Analysis", "EDA", "Quantitative Analysis", "EDA", "Feature Engineering",
rep(c("Model Fit", "Model Tuning"), 40),
"Quantitative Analysis", "EDA", "Feature Engineering",
rep(c("Model Fit", "Model Tuning"), 14), "Model Fit", "Feature Engineering",
rep(c("Model Fit", "Model Tuning"), 14), "Model Fit", "Quantitative Analysis",
"Model Fit"))
bar_loc$ytop <- 1.9
bar_loc$ybot <- 1
bar_loc$g <- factor(as.character(bar_loc$g),
levels = c("EDA", "Quantitative Analysis", "Feature Engineering",
"Model Fit", "Model Tuning"))
text_loc <- data.frame(x = c(1, 8, 30, 36, 120, 124, 132, 147, 211, 215)+1,
y = 2.1)
text_loc$label <- letters[1:nrow(text_loc)]
mod_loc <- data.frame(x = c(45, 66, 87, 107, 162, 195)+1,
y = .75,
label = c("Model\n#1", "Model\n#2", "Model\n#3", "Model\n#4",
"Model\n#2", "Model\n#4"))
ggplot(bar_loc) +
geom_rect(aes(fill = g, xmin = srt, xmax = stp,
ymin = ybot, ymax = ytop), alpha = .7) +
theme(
legend.position = "bottom",
legend.background = element_blank(),
axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_text(hjust = .05),
axis.title.y = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_blank()
) +
scale_fill_manual(values = diag_cols, name = "") +
geom_text(data = text_loc, aes(x = x, y = y, label = label)) +
geom_text(data = mod_loc, aes(x = x, y = y, label = label), size = 3) +
xlab("Time") +
ylim(c(.5, 2.25))
```
---
# What Are We Doing with the Data?
.pull-left[
We often think of the model as the _only_ real data analysis step in this process.
However, there are other procedures that are often applied before or after the model fit that are data-driven and have an impact.
If we only think of the model as being important, we might end up accidentally overfitting to the data in-hand. This is very similar to the problems of "the garden of forking paths" and "p-hacking" [(pdf)](http://www.stat.columbia.edu/~gelman/research/unpublished/p_hacking.pdf).
]
.pull-right[
```{r simple-proc, echo = FALSE, out.width = '75%', fig.align='center', dev = 'svg', dev.args = list(bg = "transparent"), warning=FALSE}
knitr::include_graphics("images/diagram-simple.svg")
```
]
---
# Define the Data Analysis Process
.pull-left[
Let's conceptualize a process or _workflow_ that involves all of the steps where the data are analyzed in a significant way. The includes the model but might also include other _estimation_ steps:
* data preparation steps (e.g. imputation, encoding, transformations, etc)
* selection of which terms go into the model
and so on.
Admittedly, there is some grey area here.
]
.pull-right[
```{r realistic-proc, echo = FALSE, out.width = '95%', fig.align='center', dev = 'svg', dev.args = list(bg = "transparent"), warning=FALSE}
knitr::include_graphics("images/diagram-complex.svg")
```
This concept will become important when we talk about measuring performance of the modeling process.
]
---
# Some naming conventions
There are a few suffixes that we'll use for certain types of objects:
* `_mod` for a `parsnip` model specification
* `_fit` for a fitted model
* `_rec` for a recipe
* `_wfl` for a workflow
* `_tune` for a tuning object
* `_res` for a general result
---
# Resources
* [`http://www.tidyverse.org/`](http://www.tidyverse.org/)
* [R for Data Science](http://r4ds.had.co.nz/)
* Jenny's [`purrr` tutorial](https://jennybc.github.io/purrr-tutorial/) or [Happy R Users Purrr](https://www.rstudio.com/resources/videos/happy-r-users-purrr-tutorial/)
* Programming with `dplyr` [vignette](https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html)
* Selva Prabhakaran's [`ggplot2` tutorial](http://r-statistics.co/Complete-Ggplot2-Tutorial-Part1-With-R-Code.html)
* `caret` package [documentation](https://topepo.github.io/caret/)
* [CRAN Machine Learning Task View](https://cran.r-project.org/web/views/MachineLearning.html)