forked from laser-institute/supervised-machine-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode-along.R
39 lines (29 loc) · 1.01 KB
/
code-along.R
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
library(tidyverse)
library(tidymodels)
starwars
view(starwars)
set.seed(20230718)
starwars_recoded <- starwars %>% # built-in data available just by typing
mutate(species_human = ifelse(species == "Human", 1, 0)) %>%
mutate(species_human =as.factor(species_human))# recoding species to create a 0-1 outcome
starwars_recoded %>%
count(species_human, sort = TRUE) # how many humans are there?
train_test_split <- initial_split(starwars_recoded, prop = .80)
data_train <- training(train_test_split)
data_test <- testing(train_test_split)
# predicting humans based on the independent effects of height and mass
my_rec <- recipe(species_human ~ height + mass, data = data_train)
# specify model
my_mod <-
logistic_reg() %>%
set_engine("glm") %>%
set_mode("classification")
# specify workflow
my_wf <-
workflow() %>%
add_model(my_mod) %>%
add_recipe(my_rec)
fitted_model <- fit(my_wf, data = data_train)
final_fit <- last_fit(fitted_model, train_test_split)
final_fit %>%
collect_metrics()