User defined contrast codes contrast missing from output in R

Viewed 53

I want to create three contrast codes to include in a regression/ANOVA analysis in R.

I have tried two different approaches and although the code appears in the contrast matrix it doesn't appear in my regression/ANOVA output

This is one approach I have used to create the contrasts and assign them to the condition of interest:

c1 <- c(1, 0, -1)
c2 <- c(1, -2, 1)
c3 <- c(2, -1, -1)

mat <- cbind(c1,c2,c3)
contrasts(df$condition) <- mat

I then use this in the regression analysis:

summary(lm(Diff ~ condition, df))

And here are the coefficients from my output:

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  42.6679     0.4249 100.425   <2e-16 ***
conditionc1   1.0108     0.5169   1.955   0.0524 .  
conditionc2   0.7829     0.3024   2.589   0.0106 *  

What I was expecting was coefficients for c1, c2, and c3 and I cannot understand why I can't generate this.

Here are the data I'm working with:

structure(list(condition = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L), .Label = c("prompt_explicit", "prompt_gen", "prompt_less"
), contrasts = structure(c(1, 0, -1, 1, -2, 1), .Dim = 3:2, .Dimnames = list(
    c("prompt_explicit", "prompt_gen", "prompt_less"), c("c1", 
    "c2"))), class = "factor"), Diff = c(44L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 60L, 44L, 50L, 40L, 40L, 40L, 60L, 
40L, 44L, 40L, 40L, 52L, 50L, 50L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 50L, 40L, 60L, 42L, 42L, 40L, 48L, 40L, 60L, 40L, 
40L, 40L, 44L, 50L, 50L, 60L, 42L, 60L, 50L, 40L, 40L, 40L, 56L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 60L, 50L, 40L, 44L, 40L, 60L, 40L, 42L, 
50L, 50L, 42L, 46L, 40L, 40L, 50L, 40L, 40L, 40L, 44L, 42L, 40L, 
40L, 40L, 40L, 40L, 40L, 46L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 42L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 42L, 40L, 48L, 46L, 
50L, 44L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 50L, 52L, 
40L, 40L, 40L, 40L)), row.names = c(NA, -151L), class = "data.frame")

Is there something I am missing here?

2 Answers

It's definitely normal situation for factor with three levels. If you want c1, c2, c3 then you may use contrasts(df$condition, 3) <- mat but it after regression, it will return NA's for c3.

You have 3 levels for your factors and it should be 2 levels of contrast, because the linear model will include an intercept. Since you specified three, only the first two are used.

I am guessing you want to test the specific hypothesis you have, so you can fit the model first:

contrasts(df$condition) = NULL
fit = lm(Diff ~ condition, df)

Then provide the contrast matrix you have to test:

library(multcomp)
summary(glht(fit,linfct=t(mat)))

 Simultaneous Tests for General Linear Hypotheses

Fit: lm(formula = Diff ~ condition, data = df)

Linear Hypotheses:
        Estimate Std. Error t value Pr(>|t|)    
c1 == 0   46.483      1.625   28.61   <2e-16 ***
c2 == 0   49.159      2.205   22.29   <2e-16 ***
c3 == 0   94.304      3.079   30.62   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Adjusted p values reported -- single-step method)
Related