make_dummies: Initiate the making of dummy variables

View source: R/features.R

make_dummiesR Documentation

Initiate the making of dummy variables

Description

Initiate the making of dummy variables

Usage

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

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 May 4, 2023, 5:10 p.m.