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