tsostarics / contrastable

Contrast Coding Utilities
https://tsostarics.github.io/contrastable/
Other
0 stars 0 forks source link

Better error message when model data is not provided #22

Closed tsostarics closed 5 months ago

tsostarics commented 8 months ago

If you forget to pipe in the model data like so

mtcars
set_contrasts(cyl ~ sum_code + 1, gear ~ sum_code)

The error message is

Error in enlist_contrasts(model_data, !!!formulas, verbose = verbose) : 
  Columns not found in model_data: gear

because the first formula is treated as model_data. Similarly for when only one formula is provided:

> set_contrasts(cyl ~ sum_code + 1)
Error in enlist_contrasts(model_data, !!!formulas, verbose = verbose) : 
  No contrast formulas provided

There should be a type check on model_data to ensure it's not a formula, with an error message telling the user to check that they provided model_data correctly.

tsostarics commented 5 months ago

Implemented as follows:

When an object of class formula is detected:

> enlist_contrasts(cyl ~ 2)
Error in enlist_contrasts(cyl ~ 2) : 
  Formula passed to model_data, did you forget to pass a data frame?

When any other object that does not inherit data.frame is detected:

Error in enlist_contrasts(matrix(c(1, 1, 1, 1), nrow = 2)) : 
  model_data should inherit class data.frame.
Instead found class(es): matrix, array