Brms replacing factor levels with numbers

Hi all,

I hope this question is appropriate here, apologies if not.

I’m trying to run dyadic multi-membership brms models in R, where one the predictors is a categorical variable, coded as a factor, with multiple levels. Every time I’ve run these kinds of models in the past, the summary(model) output for categorical factors is the reference factor level comparison to to every other factor level; e.g. FactorNameLevelABC - Estimate, CI, etc
This time around, it seems that each factor level is being assigned a number rather than retaining its name So I’m getting FactorName1, FactorName2, FactorName3; meaning I can’t be 100% sure of what factor level is being compared against each of the others. (I’m assuming it is the first factor level encountered is the reference, the second encountered being FactorName1, etc).

I’ve tried renaming the factor levels so they only have letters in their names (no symbols), re-classified them as characters, running the model without the multi-membership term.
I have limited experience running stan/brms in R, so hopefully I’m not overlooking something very basic!

Heres some dummy code to explain the problem

levels(data$categoral_variable)
"apple"  "banana"  "orange"  "berry"

model <-  brm(dissimilarity_matrix ~ continuous_variable + categorical_variable +  (1|mm(IDA,IDB)) + data = data, 
                family= "Gaussian",
                warmup = 10, iter = 40,
                cores = 2, chains = 4, 
                init=0)

summary(model)
Regression Coefficients:
                   Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
Intercept              0.0       0.0     -1.0      0.0   Inf        6       NA           
continous_variable    -0.00      0.00    -0.00     0.00  Inf        6       NA
categorial_variable1   0.00      0.00    -0.00     0.00  Inf        6       NA
categorial_variable2  -0.00      0.00    -0.00     0.00  Inf        6       NA
categorial_variable3   0.00      0.00    -0.00     0.00  Inf        6       NA

whereas what I was expecting was:

summary(model)
Regression Coefficients:
                            Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
Intercept                    0.18      0.95    -1.44     0.88  Inf        6       NA           
continous_variable          -0.00      0.00    -0.00     0.00  Inf        6       NA
categorial_variablebanana    0.00      0.00    -0.00     0.00  Inf        6       NA
categorial_variableorgange  -0.00      0.00    -0.00     0.00  Inf        6       NA
categorial_variableberry     0.00      0.00    -0.00     0.00  Inf        6       NA

Thank you very much in advance!

Right… so turns out I had set the contrasts options to
options(contrasts = c(“contr.sum”,“contr.poly”))
for some other work, and hadn’t restarted R studio… after changing back to
options(contrasts = c(“contr.treatment”,“contr.poly”))
it is now outputting with factor levels as it should again.

I’ll leave this up both as a token of my own silliness and in the hope that it might help some other distracted soul in the future.

3 Likes