Skip to content

Instantly share code, notes, and snippets.

@tmastny
Created August 30, 2019 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmastny/15fbace489c28822026261012b92dd57 to your computer and use it in GitHub Desktop.
Save tmastny/15fbace489c28822026261012b92dd57 to your computer and use it in GitHub Desktop.
library(recipes)
library(embed)
library(dplyr)
library(mlbench)
set.seed(124)
data(PimaIndiansDiabetes)
d <- PimaIndiansDiabetes
d <- d %>%
as_tibble() %>%
select(diabetes, everything())
# make factor variables
d <- d %>%
mutate(mass_fct = factor(ifelse(mass > 30, "large", "small"))) %>%
mutate(pregnant_fct = as.factor(pregnant)) %>%
mutate(pressure_fct = factor(case_when(
pressure < 30 ~ "low",
between(pressure, 30, 50) ~ "medium",
pressure > 50 ~ "high"
))) %>%
mutate(triceps_fct = factor(ifelse(triceps > 0, "has", "none"))) %>%
mutate(insulin_fct = factor(insulin)) %>%
mutate(age_fct = factor(age))
# steps in `embed`
embed_rec <- recipe(diabetes ~ ., d) %>%
embed::step_woe(mass_fct, outcome = diabetes) %>%
embed::step_lencode_bayes(pregnant_fct, outcome = vars(diabetes)) %>%
embed::step_lencode_glm(pressure_fct, outcome = vars(diabetes)) %>%
embed::step_lencode_mixed(triceps_fct, outcome = vars(diabetes)) %>%
embed::step_embed(
insulin_fct,
outcome = vars(diabetes),
options = embed_control(epochs = 1)
) %>%
embed::step_umap(age_fct, outcome = vars(diabetes))
embed_rec_prepped <- prep(embed_rec, d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment