# Manually setting reference levels in glm of categorical variables

I have a data set with variables that have 3 or more levels for example pen size: small, medium, large. I know that if you don't set a reference level, r will just pick one and then compare the other levels back to that one (reference level chosen = small, so medium to small and large to small). But if I wanted to know the relationships of all the variables (small against med/large, medium against small/large, large against small/med), I would have to set the reference, which I could do like this:

DF<- within(DF, PenSize<- relevel(PenSize, ref = "Small"))


When I do this, for the most part, the outputs from the GLM are the same when comparing two levels against each other (e.g. small-medium and medium-small) but sometimes I would get different values between those levels! So, in example, I would get a significant p-value for small compared to medium but then a non-significant p-value for medium compared to small. The only reason why I can guess this is happening is that one of the levels has a very small sample size, where the other levels had bigger sample sizes. If this is the case, which reference variable should I be using for my values?

## migrated from stackoverflow.comFeb 24 at 10:27

This question came from our site for professional and enthusiast programmers.

• Please include a reproducible example. It seems like you may be looking for post-hoc comparisons as provided by the emmeans package. – Nakx Feb 21 at 1:06

I think you're looking for contrasts. Check out ?contrasts in R.