Skip to content

Instantly share code, notes, and snippets.

@brshallo
Created February 27, 2019 15:59
Show Gist options
  • Select an option

  • Save brshallo/45a0b282da5d6017ac03819339eb2dd7 to your computer and use it in GitHub Desktop.

Select an option

Save brshallo/45a0b282da5d6017ac03819339eb2dd7 to your computer and use it in GitHub Desktop.
Check if recipes are working with deviance coding the way I would expect. Seems that they are (thought I'd seen some funny behavior within a project, but perhaps an illusion or other error).
library(dplyr)
library(tibble)
library(recipes)
library(car)

param <- getOption("contrasts")
go_deviance <- param
# traditional `contr.sum` does not name levels, so use function from `car` package
go_deviance["unordered"] <- "contr.Sum"
options(contrasts = go_deviance)


tib_example <- tibble(fct_example = factor(c("a", "b", "c", "d")),
                      chr_example = c("al", "ben", "cam", "cam"),
                      target_example = rnorm(4))

recipe(target_example ~fct_example + chr_example, data = tib_example) %>% 
  step_dummy(all_nominal()) %>% 
  # step_interact(~ contains("fct"):contains("chr")) %>% # how to set-up an interaction
  prep(retain = TRUE) %>% 
  juice() %>% 
  glimpse()
#> Observations: 4
#> Variables: 6
#> $ target_example       <dbl> 0.3598693, 0.1322287, 1.7492702, -0.4346045
#> $ fct_example_X.S.a.   <dbl> 1, 0, 0, -1
#> $ fct_example_X.S.b.   <dbl> 0, 1, 0, -1
#> $ fct_example_X.S.c.   <dbl> 0, 0, 1, -1
#> $ chr_example_X.S.al.  <dbl> 1, 0, -1, -1
#> $ chr_example_X.S.ben. <dbl> 0, 1, -1, -1

# Looks like it is working correctly... had thought it wasn't handling
# "contr.Sum" correctly, and was doing dummy encoding, but looks to be working

Created on 2019-02-27 by the reprex package (v0.2.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment