File: Roles.R

package info (click to toggle)
r-cran-recipes 0.1.15%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,496 kB
  • sloc: sh: 37; makefile: 2
file content (85 lines) | stat: -rw-r--r-- 2,689 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
## ----ex_setup, include=FALSE--------------------------------------------------
knitr::opts_chunk$set(
  message = FALSE,
  digits = 3,
  collapse = TRUE,
  comment = "#>"
  )
options(digits = 3)
library(recipes)

## ----formula-roles------------------------------------------------------------
library(recipes)

recipe(Species ~ ., data = iris) %>% summary()

recipe( ~ Species, data = iris) %>% summary()

recipe(Sepal.Length + Sepal.Width ~ ., data = iris) %>% summary()

## ----formula-update-----------------------------------------------------------
library(modeldata)
data(biomass)

recipe(HHV ~ ., data = biomass) %>% 
  update_role(dataset, new_role = "dataset split variable") %>% 
  update_role(sample, new_role = "sample ID") %>% 
  summary()

## ----formula-rm---------------------------------------------------------------
recipe(HHV ~ ., data = biomass) %>% 
  remove_role(sample, old_role = "predictor") %>% 
  summary()

## ----formula-rm-fail, error=TRUE----------------------------------------------
recipe(HHV ~ ., data = biomass) %>% 
  update_role(sample, new_role = NA_character_)

## ----formula-add--------------------------------------------------------------
multi_role <- recipe(HHV ~ ., data = biomass) %>% 
  update_role(dataset, new_role = "dataset split variable") %>% 
  update_role(sample, new_role = "sample ID") %>% 
  # Roles below from https://wordcounter.net/random-word-generator
  add_role(sample, new_role = "jellyfish") 

multi_role %>% 
  summary()

## -----------------------------------------------------------------------------
multi_role %>%
  update_role(sample, new_role = "flounder", old_role = "jellyfish") %>%
  summary()

## -----------------------------------------------------------------------------
multi_role %>% 
  add_role(HHV, new_role = "nocenter") %>% 
  step_center(all_predictors(), -has_role("nocenter")) %>% 
  prep(training = biomass, retain = TRUE) %>% 
  juice() %>% 
  head()

## ----x-none-------------------------------------------------------------------
recipe(biomass) %>% 
  summary()

## ----x-none-updated-----------------------------------------------------------
recipe(biomass) %>% 
  update_role(contains("gen"), new_role = "lunchroom") %>% 
  update_role(sample, HHV, new_role = "snail") %>% 
  summary()

## ----dummy--------------------------------------------------------------------
recipe( ~ ., data = iris) %>% 
  step_dummy(Species) %>% 
  prep() %>% 
  juice(all_predictors()) %>% 
  dplyr::select(starts_with("Species")) %>% 
  names()

# or something else
recipe( ~ ., data = iris) %>% 
  step_dummy(Species, role = "trousers") %>% 
  prep() %>% 
  juice(has_role("trousers")) %>% 
  names()