I am trying to use contrasts in a logistic regression using the lme4 package. I have two between-subject categorical variables (both with 2 levels). The first distinguishes between groups 1 and 2; the second between groups A and B.
I prepared a single column with between-subject categorical variable with 4 levels: 1A, 1B, 2A, and 2B. I would like to see the effect of those variables on 1 binary variable. We have a strong a priori linear hypothesis: i.e. 1A > 1B > 2A > 2B.

# Data frame is d
# Column to predict is BD
# Column with 4 groups is IV with four level (1A, 1B, 2A and 2B).

# Factorize IV column
d$IV <- factor(d$IV, levels = c("1A", "1B" , "2A", "2B"), ordered = TRUE)

I encoded 2 first contrast to test both between-group variables.

# "Group effects" contrast creation
Contrast1 <- c(-0.5, -0.5, 0.5, 0.5) # Test the difference between groups 1 and 2
Contrast2 <- c(-0.5, 0.5, -0.5, 0.5) # Test the difference between groups A and B

I would now like to add a linear contrast to confirm our main hypothesis

# Linear contrast
Contrast3 <- c(-0.6708204, -0.2236068, 0.2236068, 0.6708204)
# I chose these numbers because they were given to me when I simply used the contrasts() function on the
# ordered factor

# Contrast matrices creation
matCont <- matrix(c(Contrast1, Contrast2, Contrast3), ncol = 3)
colnames(matCont) <- c("1vs2", "AvsB", "Linear")
contrasts(d$IV) <- matCont

To run my logistic regression, I use the glm() function:

RegLog <- glm(BD ~ IV, data = d, family = binomial(link = 'logit'))

This command returns expected values for contrasts 1 and 2, but not for the linear contrast (where I only get NAs with the following message Coefficients: (1 not defined because of singularities)).

So I understand this "singularity" error. What poses me problem is that when I use the exact same regression on the standard contrast given by the contrasts() function (linear, quadratic, and cubic), I get a significant linear contrast.
Do these discrepancies between both techniques come from the fact that I mixed different contrasts into 1 analysis (linear and between-group)? Therefore, should I run two separate analyses, one with the between-group (and adding an interaction contrast) and one with the linear, quadratic, and cubic contrasts? I did not do so in an effort to minimize the amount of different analyze (and I do not expect a significant quadratic or cubic contrast at all).

  • 1
    $\begingroup$ There is no real way to treat the grouping variable as ordered (and not necessarily quadratic) without using a Bayesian model. The R brms package respects ordinal predictors. $\endgroup$ Commented Sep 21, 2023 at 11:44

1 Answer 1


Unfortunately your contrast matrix is, as you suspected, rank-deficient. I think your suggestion of two analyses is probably the best way to go forward but you would need to stress when reporting your findings that you are just cutting the cake in slightly different ways, not finding separate things n the two analyses.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.