debug_contrast_error: Debug "contrasts can be applied only to factors with 2 or...

View source: R/debug_contrast_error.R

debug_contrast_errorR Documentation

Debug "contrasts can be applied only to factors with 2 or more levels" error?

Description

When fitting models using lm and glm, sometimes you can get a mystery error that says that "contrasts can be applied only to factors with 2 or more levels". This function helps trouble shoot this error when it appears by helping to find the problematic errors. Amazing details are found in the StackOverflow where this function comes from. Link to the awesome post

The function produces a warning, if there are no complete cases or no factor variables to summarize.

Note: this function relies on debug_contrast_error

Usage

debug_contrast_error(dat, subset_vec = NULL)

debug_contrast_error2(form, dat, subset_vec = NULL)

Arguments

dat

data frame passed to lm or glm via data argument;

subset_vec

the index vector passed to lm or glm via subset argument.

form

model formula

Value

debug_contrast_errorreturns a list with

n_levels

(a tibble) gives the number of factor levels for all factor variables

levels

(a list) gives levels for all factor variables

debug_contrast_error2 returns a list with

  • model_frame(a tibble) gives the model frame (with "terms" attribute dropped)

  • n_levels(a tibble) gives the number of factor levels for all factor variables

  • levels(a list) gives levels for all factor variables

Examples

## Not run: 
#### Example 1 --------------------------------
dat <- data.frame(y = 1:4,
                  x = c(1:3, NA),
                  f1 = gl(2, 2, labels = letters[1:2]),
                  f2 = c("A", "A", "A", "B"),
                  stringsAsFactors = FALSE)

dat
str(dat)

lm(y ~ x + f1 + f2, dat)

# Good, we see an error. Now debug_contrast_error exposes that f2 ends up with a
# single level.
debug_contrast_error(dat = dat)

#### Example 2 --------------------------------
# An example with a matrix variable x
dat <- data.frame(X = I(rbind(matrix(1:6, 3), NA)),
                  f = c("a", "a", "a", "b"),
                  y = 1:4)

dat
str(dat)

lm(y ~ X + f, data = dat)
#Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
#  contrasts can be applied only to factors with 2 or more levels

debug_contrast_error(dat)

#### Example 3 --------------------------------
# A factor variable with no levels can cause an "contrasts error", too
dat <- data.frame(y = 1:4,
                  x = rep(NA_real_, 4),
                  f1 = gl(2, 2, labels = letters[1:2]),
                  f2 = c("A", "A", "A", "B"),
                  stringsAsFactors = FALSE)
dat
str(dat)

lm(y ~ x + f1 + f2, dat)
#Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
#  contrasts can be applied only to factors with 2 or more levels

debug_contrast_error(dat)

## End(Not run)
#### Example 1 --------------------------------
dat <- data.frame(y = 1:4, x = c(1:3, -1), f = rep(letters[1:2], c(3, 1)))
debug_contrast_error2(y ~ log(x) + f, dat)

# With subset_vec
debug_contrast_error2(y ~ log(x) + f, dat, subset_vec = c(1,3,4))

emilelatour/lamisc documentation built on May 20, 2024, 2:42 a.m.