make_dummies: Initiate the making of dummy variables

Description Usage Arguments Value Author(s) Examples

View source: R/features.R

Description

Initiate the making of dummy variables

Usage

1
make_dummies(traindata, testdata)

Arguments

traindata

data.table or data.frame.

testdata

data.table or data.frame. New data that has the same feature names, types, and levels as traindata.

Value

A list that contains the following entries:

feature_list

List. Output from check_features

train_dummies

A data.frame containing all of the factors in traindata as one-hot encoded variables.

test_dummies

A data.frame containing all of the factors in testdata as one-hot encoded variables.

traindata_new

Original traindata with correct column ordering and factor levels. To be passed to shapr.

testdata_new

Original testdata with correct column ordering and factor levels. To be passed to explain.

Author(s)

Annabelle Redelmeier, Martin Jullum

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
if (requireNamespace("MASS", quietly = TRUE)) {
  data("Boston", package = "MASS")
  x_var <- c("lstat", "rm", "dis", "indus")
  y_var <- "medv"
  x_train <- as.data.frame(Boston[401:411, x_var])
  y_train <- Boston[401:408, y_var]
  x_test <- as.data.frame(Boston[1:4, x_var])

  # convert to factors for illustational purpose
  x_train$rm <- factor(round(x_train$rm))
  x_test$rm <- factor(round(x_test$rm), levels = levels(x_train$rm))

  dummylist <- make_dummies(traindata = x_train, testdata = x_test)
}

shapr documentation built on Jan. 28, 2021, 5:06 p.m.