# Contrasts to ANOVA in R

I need to fit orthogonal contrasts to the following model. See the following example data:

   rep <- c(rep( 1, 15), rep(2, 15))
parent1 <- c(1, 2, 3, 4, 5, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 1, 2, 3, 4, 5, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4)
parent2 <- c(1, 2, 3, 4, 5, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5,1, 2, 3, 4, 5, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5)
yld <- c(10, 12, 13, 14, 15, 12, 13, 14, 15, 13, 14, 15, 14, 15, 15,11, 13, 12, 13, 15, 14, 12, 13, 15, 12, 15, 15, 13, 15, 16)
mydf <- data.frame(rep, parent1, parent2, yld)
mydf$parent1 <- as.factor(mydf$parent1)
mydf$parent2 <- as.factor(mydf$parent2)


Parent1 and Parent2 are factors.

    mode11 <- lm(yld ~ rep + parent1:parent2, data = mydf)
anova(mode11)


I need to fit contrasts, I suppose the order of the treatments for the coefficients for the interaction is:

Parent1    1, 2, 3, 4, 5, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4
Parent2    1, 2, 3, 4, 5, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5

# contrasts for Parent1:Parent2 interaction
mycontrast <- c(-4, -4, -4, -4, 4,  2, 2, 2, 2, 2,  2, 2, 2, 2,2)


I want to apply above orthogonal contrasts (mycontrasts). I am used to SAS to this short of analysis however not in R.
I have two questions:

(a) How can I properly apply contrast to above model?

(b) I am concerned about the order of the treatments while writing coefficients. See my assumed orders: are they correct? I assume this should be either alphabetical or ordered in file.

Edits: The following my own progress to answer the question

>     mode11$coefficients # to see all coefficients > mode11$coefficients[1:5] # just first five to save space here
(Intercept)               rep parent11:parent21 parent1**2**:parent2**1** parent1**3**:parent2**1**
1.500000e+01      1.087998e-15     -4.500000e+00                NA                NA


Should orthogonal contrast ordered in the way the comparisions are ordered here - 1-2, 2-1, 3-1 and so on. As the design is unbalanced you can see some combinations are missing, so should we provide a value like 0 for them.

  contrasts(parent1:parent2) <- cbind(c(-4, -4, -4, -4, 4,  2, 2, 2, 2, 2,


2, 2, 2, 2,2,-4, -4, -4, -4, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2,2 )) Error in contrasts<-(*tmp*, value = c(-4, -4, -4, -4, 4, 2, 2, 2, : contrasts apply only to factors In addition: Warning messages: 1: In *tmp*:parent2 : numerical expression has 30 elements: only the first used 2: In *tmp*:parent2 : numerical expression has 30 elements: only the first used

Look at ?contrasts and ?lm. The default contrasts in R are different than those of SAS, but the difference is explained and you can specify the alternate form. (Am I correct in thinking you wanted to compare output with SAS?) I didn't keep your ":" in the formula since I thought it would be clearer to include a full interaction "*".

 model1 <- lm(yld ~ rep + parent1*parent2, data = mydf,
contrasts = list(parent1="contr.SAS", parent2="contr.SAS"))
model1  # note I did not retain your mode-eleven spelling.
#---------------------
Coefficients:
(Intercept)                rep           parent11           parent12
1.500e+01          2.079e-15          6.872e-17          4.262e-16
parent13           parent14           parent21           parent22
-7.692e-16          5.000e-01         -4.500e+00         -2.500e+00
parent23           parent24  parent11:parent21  parent12:parent21
-2.500e+00         -2.000e+00                 NA                 NA
parent13:parent21  parent14:parent21  parent11:parent22  parent12:parent22
NA                 NA          5.000e-01                 NA
parent13:parent22  parent14:parent22  parent11:parent23  parent12:parent23
NA                 NA         -9.873e-16          2.040e-16
parent13:parent23  parent14:parent23  parent11:parent24  parent12:parent24
NA                 NA          5.000e-01          1.500e+00
parent13:parent24  parent14:parent24
5.000e-01                 NA


Also note that some of your contrast coefficients are effectively 0, for instance: "rep", parents(11,12,13), parent11:parent23 and parent12:parent23.

Have a look on An R companion to "Experimental Design" by Vikneswaran. See chapter 5 in particular on "Orthogonality, Orthogonal Decomposition, and their Role in Modern Experimental Design".

• thank you for the suggestion, I am trying to find my answer within the manual
– John
Nov 6 '11 at 12:53
• the word "contrast" doesn't even appear in that PDF ... Nov 6 '11 at 14:13
• @Ben Bolker: Chapter 5 Orthogonality, Orthogonal Decomposition, and their Role in Modern Experimental Design is all about Contrasts. Nov 6 '11 at 16:11
• sorry. Don't know why the search didn't find it. Nov 6 '11 at 16:57
• @Myaseen208, do you mind expanding upon the link? Links go bad, and one would not even know what you are referencing if that happened. Nov 6 '11 at 20:35