jeff-hughes / reghelper

R package with regression helper functions
5 stars 5 forks source link

"Subscript out of bounds" in simple_slopes, when lmer object was fitted with contr.sum. #17

Closed format0 closed 1 year ago

format0 commented 1 year ago

Hi, I noticed that simple_slopes produces a "subscript out of bounds" error when running it on an lmer object that was fitted after setting the global contrasts variable to "contr.sum" rather than "contr.treatment". The same applies when the object was fitted using the mixed() command from the "apex" package, that automatically fits the model with the contr.sum attribute.

I belief the reason is that contr.sum does not specify column names (e.g., the name of the factor levels). See below for the difference between contr.treatment and contr.sum:

z <- factor(LETTERS[1:3])
contr.treatment(z)

> B C
> A 0 0
> B 1 0
> C 0 1

contr.sum(z)
>   [,1] [,2]
> A    1    0
> B    0    1
> C   -1   -1

Given that the column name is empty, dummy_names <- paste0(test_var_name, colnames(contr)) does not generate the correct name to look up the coefficients, leading to the subscripts out of bounds error.