custom_factor: Simplify to custom factor

View source: R/custom_factor.R

custom_factorR Documentation

Simplify to custom factor

Description

Simplifies simple factor to custom factor, which usually means that some of the original levels are merged together. Must be used within factor_modify function. This is usually done with categorical variables where order of levels doesn't matter.

Usage

custom_factor(x, mapping)

Arguments

x

Unquoted symbol. Predictor to be simplified. Must be present in the modeling dataset.

mapping

Integer vector. Mapping to use for simplification. Length must be equal to length of unique levels of the predictor.

Value

Original vector with updated attributes.

See Also

factor_modify

Examples

require(dplyr) # for the pipe operator
data('sev_train')

setup <- setup(
  data_train = sev_train,
  target = 'sev',
  weight = 'numclaims',
  family = 'gamma',
  keep_cols = c('pol_nbr', 'exposure', 'premium')
)

modeling <- setup %>%
  factor_add(pol_yr) %>%
  factor_add(area) %>%
  model_fit() %>%
  model_save('model1') %>%
  factor_modify(area = custom_factor(area, mapping = c(1, 2, 3, 1, 4, 4))) %>%
  model_fit()


realgabon/insuRglm documentation built on Jan. 2, 2023, 2:51 a.m.