Skip to content

Commit 18cb5f7

Browse files
Merge pull request #506 from UBC-DSCI/deprecation-fixes
Deprecation fixes
2 parents e98f10d + 54d31e2 commit 18cb5f7

File tree

4 files changed

+50
-46
lines changed

4 files changed

+50
-46
lines changed

source/classification1.Rmd

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ library(ggplot2)
1010
1111
knitr::opts_chunk$set(echo = TRUE,
1212
fig.align = "center")
13-
options(knitr.table.format = function() {
14-
if (knitr::is_latex_output()) 'latex' else 'pandoc'
15-
})
13+
options(knitr.table.format = ifelse(knitr::is_latex_output(), 'latex', 'html'))
1614
1715
cleanup_and_print <- function(output){
1816
for (i in seq_along(output)) {
@@ -360,7 +358,7 @@ perim_concav_with_new_point +
360358
y = new_point[2],
361359
xend = pull(neighbors[1, attrs[1]]),
362360
yend = pull(neighbors[1, attrs[2]])
363-
), color = "black", size = 0.5)
361+
), color = "black", linewidth = 0.5, show.legend = FALSE)
364362
```
365363

366364
```{r 05-knn-3, echo = FALSE}
@@ -407,7 +405,7 @@ perim_concav_with_new_point2 +
407405
y = new_point[2],
408406
xend = pull(neighbors[1, attrs[1]]),
409407
yend = pull(neighbors[1, attrs[2]])
410-
), color = "black", size = 0.5)
408+
), color = "black", linewidth = 0.5, show.legend = FALSE)
411409
```
412410

413411
To improve the prediction we can consider several
@@ -424,17 +422,17 @@ perim_concav_with_new_point2 +
424422
x = new_point[1], y = new_point[2],
425423
xend = pull(neighbors[1, attrs[1]]),
426424
yend = pull(neighbors[1, attrs[2]])
427-
), color = "black", size = 0.5) +
425+
), color = "black", linewidth = 0.5, show.legend = FALSE) +
428426
geom_segment(aes(
429427
x = new_point[1], y = new_point[2],
430428
xend = pull(neighbors[2, attrs[1]]),
431429
yend = pull(neighbors[2, attrs[2]])
432-
), color = "black", size = 0.5) +
430+
), color = "black", linewidth = 0.5, show.legend = FALSE) +
433431
geom_segment(aes(
434432
x = new_point[1], y = new_point[2],
435433
xend = pull(neighbors[3, attrs[1]]),
436434
yend = pull(neighbors[3, attrs[2]])
437-
), color = "black", size = 0.5)
435+
), color = "black", linewidth = 0.5, show.legend = FALSE)
438436
```
439437

440438
Here we chose the $K=3$ nearest observations, but there is nothing special
@@ -987,17 +985,17 @@ unscaled <- ggplot(unscaled_cancer, aes(x = Area,
987985
x = unlist(new_obs[1]), y = unlist(new_obs[2]),
988986
xend = unlist(neighbors[1, attrs[1]]),
989987
yend = unlist(neighbors[1, attrs[2]])
990-
), color = "black", size = 0.5) +
988+
), color = "black", linewidth = 0.5, show.legend = FALSE) +
991989
geom_segment(aes(
992990
x = unlist(new_obs[1]), y = unlist(new_obs[2]),
993991
xend = unlist(neighbors[2, attrs[1]]),
994992
yend = unlist(neighbors[2, attrs[2]])
995-
), color = "black", size = 0.5) +
993+
), color = "black", linewidth = 0.5, show.legend = FALSE) +
996994
geom_segment(aes(
997995
x = unlist(new_obs[1]), y = unlist(new_obs[2]),
998996
xend = unlist(neighbors[3, attrs[1]]),
999997
yend = unlist(neighbors[3, attrs[2]])
1000-
), color = "black", size = 0.5)
998+
), color = "black", linewidth = 0.5, show.legend = FALSE)
1001999
10021000
# create new scaled obs and get NNs
10031001
new_obs_scaled <- tibble(Area = -0.72, Smoothness = 2.8, Class = "unknown")
@@ -1032,17 +1030,17 @@ scaled <- ggplot(scaled_cancer, aes(x = Area,
10321030
x = unlist(new_obs_scaled[1]), y = unlist(new_obs_scaled[2]),
10331031
xend = unlist(neighbors_scaled[1, attrs[1]]),
10341032
yend = unlist(neighbors_scaled[1, attrs[2]])
1035-
), color = "black", size = 0.5) +
1033+
), color = "black", linewidth = 0.5, show.legend = FALSE) +
10361034
geom_segment(aes(
10371035
x = unlist(new_obs_scaled[1]), y = unlist(new_obs_scaled[2]),
10381036
xend = unlist(neighbors_scaled[2, attrs[1]]),
10391037
yend = unlist(neighbors_scaled[2, attrs[2]])
1040-
), color = "black", size = 0.5) +
1038+
), color = "black", linewidth = 0.5, show.legend = FALSE) +
10411039
geom_segment(aes(
10421040
x = unlist(new_obs_scaled[1]), y = unlist(new_obs_scaled[2]),
10431041
xend = unlist(neighbors_scaled[3, attrs[1]]),
10441042
yend = unlist(neighbors_scaled[3, attrs[2]])
1045-
), color = "black", size = 0.5)
1043+
), color = "black", linewidth = 0.5, show.legend = FALSE)
10461044
10471045
ggarrange(unscaled, scaled, ncol = 2, common.legend = TRUE, legend = "bottom")
10481046
@@ -1070,17 +1068,17 @@ ggplot(unscaled_cancer, aes(x = Area,
10701068
x = unlist(new_obs[1]), y = unlist(new_obs[2]),
10711069
xend = unlist(neighbors[1, attrs[1]]),
10721070
yend = unlist(neighbors[1, attrs[2]])
1073-
), color = "black") +
1071+
), color = "black", show.legend = FALSE) +
10741072
geom_segment(aes(
10751073
x = unlist(new_obs[1]), y = unlist(new_obs[2]),
10761074
xend = unlist(neighbors[2, attrs[1]]),
10771075
yend = unlist(neighbors[2, attrs[2]])
1078-
), color = "black") +
1076+
), color = "black", show.legend = FALSE) +
10791077
geom_segment(aes(
10801078
x = unlist(new_obs[1]), y = unlist(new_obs[2]),
10811079
xend = unlist(neighbors[3, attrs[1]]),
10821080
yend = unlist(neighbors[3, attrs[2]])
1083-
), color = "black") +
1081+
), color = "black", show.legend = FALSE) +
10841082
facet_zoom(x = ( Area > 380 & Area < 420) ,
10851083
y = (Smoothness > 0.08 & Smoothness < 0.14), zoom.size = 2) +
10861084
theme_bw() +
@@ -1185,7 +1183,7 @@ for (i in 1:7) {
11851183
x = new_point[1],
11861184
y = new_point[2],
11871185
xend = pull(neighbors[i, attrs[1]]),
1188-
yend = pull(neighbors[i, attrs[2]]), color = clr
1186+
yend = pull(neighbors[i, attrs[2]]), color = clr, show.legend = FALSE
11891187
)
11901188
}
11911189
rare_plot + geom_point(aes(x = new_point[1], y = new_point[2]),

source/regression2.Rmd

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -263,16 +263,16 @@ hidden_print(lm_fit)
263263
> the best fit coefficients are usually easier to interpret afterward.
264264
265265
Our coefficients are
266-
(intercept) $\beta_0=$ `r format(round(pull(tidy(pull_workflow_fit(lm_fit)), estimate)[1]), scientific=FALSE)`
267-
and (slope) $\beta_1=$ `r format(round(pull(tidy(pull_workflow_fit(lm_fit)), estimate)[2]), scientific=FALSE)`.
266+
(intercept) $\beta_0=$ `r format(round(pull(tidy(extract_fit_parsnip(lm_fit)), estimate)[1]), scientific=FALSE)`
267+
and (slope) $\beta_1=$ `r format(round(pull(tidy(extract_fit_parsnip(lm_fit)), estimate)[2]), scientific=FALSE)`.
268268
This means that the equation of the line of best fit is
269269

270-
$$\text{house sale price} = `r format(round(pull(tidy(pull_workflow_fit(lm_fit)), estimate)[1]), scientific=FALSE)` + `r format(round(pull(tidy(pull_workflow_fit(lm_fit)), estimate)[2]), scientific=FALSE)`\cdot (\text{house size}).$$
270+
$$\text{house sale price} = `r format(round(pull(tidy(extract_fit_parsnip(lm_fit)), estimate)[1]), scientific=FALSE)` + `r format(round(pull(tidy(extract_fit_parsnip(lm_fit)), estimate)[2]), scientific=FALSE)`\cdot (\text{house size}).$$
271271

272272
In other words, the model predicts that houses
273-
start at \$`r format(round(pull(tidy(pull_workflow_fit(lm_fit)), estimate)[1]), big.mark=",", nsmall=0, scientific=FALSE)` for 0 square feet, and that
273+
start at \$`r format(round(pull(tidy(extract_fit_parsnip(lm_fit)), estimate)[1]), big.mark=",", nsmall=0, scientific=FALSE)` for 0 square feet, and that
274274
every extra square foot increases the cost of
275-
the house by \$`r format(round(pull(tidy(pull_workflow_fit(lm_fit)), estimate)[2]), scientific=FALSE)`. Finally,
275+
the house by \$`r format(round(pull(tidy(extract_fit_parsnip(lm_fit)), estimate)[2]), scientific=FALSE)`. Finally,
276276
we predict on the test data set to assess how well our model does:
277277

278278
```{r 08-assessFinal}
@@ -314,12 +314,12 @@ lm_plot_final
314314

315315
We can extract the coefficients from our model by accessing the
316316
fit object that is output by the `fit` \index{tidymodels!fit} function; we first have to extract
317-
it from the workflow using the `pull_workflow_fit` function, and then apply
317+
it from the workflow using the `extract_fit_parsnip` function, and then apply
318318
the `tidy` function to convert the result into a data frame:
319319

320320
```{r 08-lm-get-coeffs}
321321
coeffs <- lm_fit |>
322-
pull_workflow_fit() |>
322+
extract_fit_parsnip() |>
323323
tidy()
324324
325325
coeffs
@@ -550,7 +550,7 @@ as shown below:
550550

551551
```{r 08-lm-multi-get-coeffs}
552552
mcoeffs <- mlm_fit |>
553-
pull_workflow_fit() |>
553+
extract_fit_parsnip() |>
554554
tidy()
555555
556556
mcoeffs
@@ -728,7 +728,7 @@ lm_fit1 <- workflow() |>
728728
add_model(lm_spec) |>
729729
fit(data = sacramento_train)
730730
731-
coeffs <- tidy(pull_workflow_fit(lm_fit1))
731+
coeffs <- tidy(extract_fit_parsnip(lm_fit1))
732732
733733
icept1 <- format(round(coeffs |>
734734
filter(term == "(Intercept)") |>
@@ -750,7 +750,7 @@ lm_fit2 <- workflow() |>
750750
add_model(lm_spec) |>
751751
fit(data = sacramento_train)
752752
753-
coeffs <- tidy(pull_workflow_fit(lm_fit2))
753+
coeffs <- tidy(extract_fit_parsnip(lm_fit2))
754754
icept2 <- format(round(coeffs |>
755755
filter(term == "(Intercept)") |>
756756
pull(estimate)),
@@ -771,7 +771,7 @@ lm_fit3 <- workflow() |>
771771
add_model(lm_spec) |>
772772
fit(data = sacramento_train)
773773
774-
coeffs <- tidy(pull_workflow_fit(lm_fit3))
774+
coeffs <- tidy(extract_fit_parsnip(lm_fit3))
775775
icept3 <- format(round(coeffs |>
776776
filter(term == "(Intercept)") |>
777777
pull(estimate)),

source/viz.Rmd

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ library(magick)
99
1010
1111
knitr::opts_chunk$set(fig.align = "center")
12+
13+
options(knitr.table.format = ifelse(knitr::is_latex_output(), "latex", "html"))
1214
```
1315

1416
## Overview
@@ -1055,7 +1057,7 @@ We would also like to fine tune this vertical line,
10551057
styling it so that it is dashed and 1 point in thickness.
10561058
A point is a measurement unit commonly used with fonts,
10571059
and 1 point is about 0.353 mm.
1058-
We do this by setting `linetype = "dashed"` and `size = 1`, respectively.
1060+
We do this by setting `linetype = "dashed"` and `linewidth = 1`, respectively.
10591061
There is a similar function, `geom_hline`,
10601062
that is used for plotting horizontal lines.
10611063
Note that
@@ -1065,7 +1067,7 @@ while *horizontal lines* are used to denote quantities on the *vertical axis*.
10651067
```{r 03-data-morley-hist-2, warning=FALSE, fig.height = 2.75, fig.width = 4.5, fig.align = "center", fig.pos = "H", out.extra="", message=FALSE,fig.cap = "Histogram of Michelson's speed of light data with vertical line indicating true speed of light."}
10661068
morley_hist <- ggplot(morley, aes(x = Speed)) +
10671069
geom_histogram() +
1068-
geom_vline(xintercept = 792.458, linetype = "dashed", size = 1)
1070+
geom_vline(xintercept = 792.458, linetype = "dashed", linewidth = 1)
10691071
10701072
morley_hist
10711073
```
@@ -1094,7 +1096,7 @@ when they are colored by another categorical variable).
10941096
```{r 03-data-morley-hist-3, warning=FALSE, message=FALSE, fig.height = 2.75, fig.width = 4.5, fig.align = "center", fig.pos = "H", out.extra="", fig.cap = "Histogram of Michelson's speed of light data where an attempt is made to color the bars by experiment."}
10951097
morley_hist <- ggplot(morley, aes(x = Speed, fill = Expt)) +
10961098
geom_histogram(alpha = 0.5, position = "identity") +
1097-
geom_vline(xintercept = 792.458, linetype = "dashed", size = 1.0)
1099+
geom_vline(xintercept = 792.458, linetype = "dashed", linewidth = 1.0)
10981100
10991101
morley_hist
11001102
```
@@ -1119,7 +1121,7 @@ and the color will be mapped discretely.
11191121
```{r 03-data-morley-hist-with-factor, warning=FALSE, message=FALSE, fig.height = 2.75, fig.width = 5, fig.pos = "H", out.extra="", fig.align = "center", fig.cap = "Histogram of Michelson's speed of light data colored by experiment as factor."}
11201122
morley_hist <- ggplot(morley, aes(x = Speed, fill = as_factor(Expt))) +
11211123
geom_histogram(alpha = 0.5, position = "identity") +
1122-
geom_vline(xintercept = 792.458, linetype = "dashed", size = 1.0)
1124+
geom_vline(xintercept = 792.458, linetype = "dashed", linewidth = 1.0)
11231125
11241126
morley_hist
11251127
```
@@ -1158,7 +1160,7 @@ in the context of the data frame.
11581160
morley_hist <- ggplot(morley, aes(x = Speed, fill = as_factor(Expt))) +
11591161
geom_histogram() +
11601162
facet_grid(rows = vars(Expt)) +
1161-
geom_vline(xintercept = 792.458, linetype = "dashed", size = 1.0)
1163+
geom_vline(xintercept = 792.458, linetype = "dashed", linewidth = 1.0)
11621164
11631165
morley_hist
11641166
```
@@ -1189,7 +1191,7 @@ morley_hist <- ggplot(morley_rel,
11891191
fill = as_factor(Expt))) +
11901192
geom_histogram() +
11911193
facet_grid(rows = vars(Expt)) +
1192-
geom_vline(xintercept = 0, linetype = "dashed", size = 1.0) +
1194+
geom_vline(xintercept = 0, linetype = "dashed", linewidth = 1.0) +
11931195
labs(x = "Relative Accuracy (%)",
11941196
y = "# Measurements",
11951197
fill = "Experiment ID") +
@@ -1243,7 +1245,7 @@ morley_hist_default <- ggplot(morley_rel,
12431245
fill = as_factor(Expt))) +
12441246
geom_histogram() +
12451247
facet_grid(rows = vars(Expt)) +
1246-
geom_vline(xintercept = 0, linetype = "dashed", size = 1.0) +
1248+
geom_vline(xintercept = 0, linetype = "dashed", linewidth = 1.0) +
12471249
labs(x = "Relative Accuracy (%)",
12481250
y = "# Measurements",
12491251
fill = "Experiment ID") +
@@ -1256,7 +1258,7 @@ morley_hist_big <- ggplot(morley_rel,
12561258
fill = as_factor(Expt))) +
12571259
geom_histogram(binwidth = 0.1) +
12581260
facet_grid(rows = vars(Expt)) +
1259-
geom_vline(xintercept = 0, linetype = "dashed", size = 1.0) +
1261+
geom_vline(xintercept = 0, linetype = "dashed", linewidth = 1.0) +
12601262
labs(x = "Relative Accuracy (%)",
12611263
y = "# Measurements",
12621264
fill = "Experiment ID") +
@@ -1269,7 +1271,7 @@ morley_hist_med <- ggplot(morley_rel,
12691271
fill = as_factor(Expt))) +
12701272
geom_histogram(binwidth = 0.01) +
12711273
facet_grid(rows = vars(Expt)) +
1272-
geom_vline(xintercept = 0, linetype = "dashed", size = 1.0) +
1274+
geom_vline(xintercept = 0, linetype = "dashed", linewidth = 1.0) +
12731275
labs(x = "Relative Accuracy (%)",
12741276
y = "# Measurements",
12751277
fill = "Experiment ID") +
@@ -1282,7 +1284,7 @@ morley_hist_small <- ggplot(morley_rel,
12821284
fill = as_factor(Expt))) +
12831285
geom_histogram(binwidth = 0.001) +
12841286
facet_grid(rows = vars(Expt)) +
1285-
geom_vline(xintercept = 0, linetype = "dashed", size = 1.0) +
1287+
geom_vline(xintercept = 0, linetype = "dashed", linewidth = 1.0) +
12861288
labs(x = "Relative Accuracy (%)",
12871289
y = "# Measurements",
12881290
fill = "Experiment ID") +
@@ -1491,7 +1493,7 @@ file_sizes <- tibble(`Image type` = c("Raster",
14911493
/ 1000000, 2), "MB"),
14921494
paste(round(file.info("img/viz/faithful_plot.svg")["size"]
14931495
/ 1000000, 2), "MB")))
1494-
kable(file_sizes,
1496+
kable(file_sizes, booktabs = TRUE,
14951497
caption = "File sizes of the scatter plot of the Old Faithful data set when saved as different file formats.") |>
14961498
kable_styling(latex_options = "hold_position")
14971499
```

source/wrangling.Rmd

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,16 +1374,20 @@ region_lang |>
13741374
> also return `NA`s when we apply them to columns that
13751375
> contain `NA`s in the data frame. \index{missing data}
13761376
>
1377-
> To avoid this, again we need to add the argument `na.rm = TRUE`,
1378-
> but in this case we need to use it a little bit differently.
1379-
> In this case, we need to add a `,` and then `na.rm = TRUE`,
1380-
> after specifying the function we want `summarize` + `across` to apply,
1381-
> as illustrated below:
1377+
> To resolve this issue, again we need to add the argument `na.rm = TRUE`.
1378+
> But in this case we need to use it a little bit differently:
1379+
> we write a `~`, and then call the summary function
1380+
> with the first argument `.x` and the second argument `na.rm = TRUE`.
1381+
> For example, for the previous example with the `max` function, we would write
13821382
>
13831383
> ``` {r}
13841384
> region_lang_na |>
1385-
> summarize(across(mother_tongue:lang_known, max, na.rm = TRUE))
1385+
> summarize(across(mother_tongue:lang_known, ~ max(.x, na.rm = TRUE)))
13861386
> ```
1387+
> The meaning of this unusual syntax is a bit beyond the scope of this book,
1388+
> but interested readers can look up *anonymous functions* in the `purrr`
1389+
> package from `tidyverse`.
1390+
13871391
13881392
#### `map` for calculating summary statistics on many columns {-}
13891393

0 commit comments

Comments
 (0)