File: Common_Patterns.R

package info (click to toggle)
r-cran-rsample 1.2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,932 kB
  • sloc: sh: 13; makefile: 2
file content (98 lines) | stat: -rw-r--r-- 3,375 bytes parent folder | download
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
## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = rlang::is_installed("modeldata")
)

## ----setup--------------------------------------------------------------------
library(rsample)

## ----message=FALSE------------------------------------------------------------
library(dplyr)

## -----------------------------------------------------------------------------
data(ames, package = "modeldata")
head(ames, 2)

## -----------------------------------------------------------------------------
data(Chicago, package = "modeldata")
head(Chicago, 2)

## -----------------------------------------------------------------------------
head(Orange, 2)

## -----------------------------------------------------------------------------
set.seed(123)

## -----------------------------------------------------------------------------
initial_split(ames)

## -----------------------------------------------------------------------------
initial_split(ames, prop = 0.8)

## -----------------------------------------------------------------------------
resample <- initial_split(ames, prop = 0.6)

head(training(resample), 2)
head(testing(resample), 2)

## -----------------------------------------------------------------------------
vfold_cv(ames, v = 2)

## -----------------------------------------------------------------------------
vfold_cv(ames, v = 2, repeats = 2)

## -----------------------------------------------------------------------------
mc_cv(ames, prop = 0.8, times = 2)

## -----------------------------------------------------------------------------
bootstraps(ames, times = 2)

## -----------------------------------------------------------------------------
three_way_split <- initial_validation_split(ames, prop = c(0.6, 0.2))
three_way_split

## -----------------------------------------------------------------------------
validation_set(three_way_split)

## -----------------------------------------------------------------------------
vfold_cv(ames, v = 2, strata = Sale_Price)

## -----------------------------------------------------------------------------
vfold_cv(ames, v = 2, strata = Sale_Price, breaks = 100)

## -----------------------------------------------------------------------------
resample <- group_initial_split(Orange, group = Tree)

unique(training(resample)$Tree)
unique(testing(resample)$Tree)

## -----------------------------------------------------------------------------
set.seed(1)
group_bootstraps(ames, Neighborhood, times = 2)

## -----------------------------------------------------------------------------
group_vfold_cv(ames, Neighborhood, balance = "observations", v = 2)

## -----------------------------------------------------------------------------
initial_time_split(Chicago)

initial_validation_time_split(Chicago)

## -----------------------------------------------------------------------------
sliding_window(Chicago) %>%
  head(2)

## -----------------------------------------------------------------------------
sliding_index(Chicago, date) %>%
  head(2)

## -----------------------------------------------------------------------------
sliding_period(Chicago, date, "year") %>%
  head(2)

## -----------------------------------------------------------------------------
rolling_origin(Chicago) %>%
  head(2)