50% off: Unlimited data and AI learning.
State of Data and AI Literacy Report 2025

sperrorest (version 3.0.5)

remove_missing_levels: remove_missing_levels

Description

Accounts for missing factor levels present only in test data but not in train data by setting values to NA

Usage

remove_missing_levels(fit, test_data)

Value

data.frame with matching factor levels to fitted model

Arguments

fit

fitted model on training data

test_data

data to make predictions for

Examples

Run this code
foo <- data.frame(
  response = rnorm(3),
  predictor = as.factor(c("A", "B", "C"))
)
model <- lm(response ~ predictor, foo)
foo.new <- data.frame(predictor = as.factor(c("A", "B", "C", "D")))
predict(model, newdata = remove_missing_levels(
  fit = model,
  test_data = foo.new
))

Run the code above in your browser using DataLab