Projpred, brms, and categorical variables

Hi,

I am trying to use the varsel function in projpred to perform predictive variable selection for my brms model.

I keep getting stuck on an error which refers to level of a categorical variable being missing. I thought perhaps it was not appropriate to use varsel with categorical predictors - but I can’t find any specific mention of that in the documentation (although I note none of the examples use categorical predictors).

I created a small example to show the error I am getting.
Is this a problem with the software or is it incorrect to use this function on a model with categorical predictors?

library(brms)
library(projpred)

d = data.frame(y = rnorm(100),
               x = c(rnorm(50), rnorm(50, 1)),
               group = rep(c("A", "B"), each = 50)
              )

fit = brm(y ~ x + group, data = d)

projpred::varsel(fit)

The error I have is:

Error in eval(predvars, data, env) : object 'groupA' not found

I am using MacOS Catlina 10.15.7 & brms 2.15.9

Which projpred version? tagging also @AlejandroCatalina

Thanks for your reply. Here is a list of package / program versions:

projpred: 2.0.2
brms: 2.15.9
R: 4.1.0
Rstudio: 1.4.1717 (probably doesn’t matter)
MacOS Catlina 10.15.7

I think this might have to do with how we build the contrasts. I’ll take a look, thanks!

Apparently, the problem here is that the variable “group” is not technically a factor. So if you create the variable as group = factor(rep(....)) the issue disappears. I will add an error or automatically convert character variables to factors!

2 Likes

So obvious now that you point it out!

Thanks.