tsostarics / contrastable

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

Add warning if contrasts passed to `glimpse_contrasts` dont match contrasts on dataframe #24

Closed tsostarics closed 3 months ago

tsostarics commented 3 months ago

consider the following

library(contrastable)

my_data <- mtcars
my_data$cyl <- factor(my_data$cyl)
clist <- list(cyl ~ helmert_code)

glimpse_contrasts(my_data)          # (1) 
glimpse_contrasts(my_data, clist)   # (2)

my_data$carb <- factor(my_data$cyl)    
contrasts(my_data$carb) <- helmert_code(3)
glimpse_contrasts(my_data)          # (3)

(1) will report that the contrasts for cyl uses contr.treatment (default unordered contrasts), as expected (2) will report that the contrasts for cyl uses helmert_code, which adheres to the formula passed to ... but does not correspond to the actual contrasts on the dataframe (3) will report that the contrasts for cyl uses contr.treatment (expected) but will warn that the contrasts for carb are not equal to contr.treatment (because it's a helmert_code matrix)

The case for (2) should warn that the contrasts in the formula are not equal the contrasts on the dataframe, and that set_contrasts should be used.

tsostarics commented 3 months ago

This is implemented and accounts for a few more things:

Consider this as setup, where some matrices dont match, some labels dont match, and some variables aren't factors in the data frame

my_data <- mtcars
clist <- list(cyl ~ helmert_code, 
              carb ~ helmert_code, 
              gear ~ sum_code,
              am ~ treatment_code + 0 | c("diffA"))
my_data$cyl  <- factor(my_data$cyl)
my_data$carb <- factor(my_data$carb)
my_data$am   <- factor(my_data$am)
contrasts(my_data$carb) <- helmert_code(6) 

Output for passing a list of formulas as clist

glimpse_contrasts(my_data, clist)
Warning message:
These vars in `my_data` are not factors:
 - gear
Contrasts for factors in `my_data` don't match matrices in formulas:
 - cyl
Comparison labels for contrasts in `my_data` don't match:
 - carb (expected `<2, <3, <4, <6, <8` but found ``)
 - am   (expected `diffA` but found `1`)
To fix, be sure to run:
my_data <- set_contrasts(my_data, clist)

Also note the "to fix..." line at the bottom even if all the formulas are typed out manually:

glimpse_contrasts(my_data, 
                  cyl ~ helmert_code, 
                  carb ~ helmert_code, 
                  gear ~ sum_code, 
                  am ~ treatment_code + 0 | c("diffA"))
Warning message:
These vars in `my_data` are not factors:
 - gear
Contrasts for factors in `my_data` don't match matrices in formulas:
 - cyl
Comparison labels for contrasts in `my_data` don't match:
 - carb (expected `<2, <3, <4, <6, <8` but found ``)
 - am   (expected `diffA` but found `1`)
To fix, be sure to run:
my_data <- set_contrasts(my_data, 
                         cyl ~ helmert_code,
                         carb ~ helmert_code,
                         gear ~ sum_code,
                         am ~ treatment_code + 0 | c("diffA"))