[R] Implementation of contrasts
Dennis Fisher
fisher at plessthan.com
Tue Jul 7 16:20:54 CEST 2015
R 3.2.1
OS X
Colleagues
I am trying to understand ANOVA contrasts and I have encountered some puzzling results.
I have data from five studies with four different combinations of treatments. I want to make the following comparisons:
Study 1, 3: A / B vs. C / D (i.e., mean of A and B vs. mean of C and D)
Study 2: A vs. B / C
Study 4: A / B / C vs. D
Study 5: A vs. B / C / D
With some trial-and-error, I got the following code to yield results matching SAS outputs:
AB.CD <- c(1, 1, -1, -1)/2
A.BC <- c(1, -1, -1)/2
ABC.D <- c(1, 1, 1, -1)/2
A.BCD <- c(1, -1, -1, -1)/2
if (STUDY %in% c(1,3))) contrasts(DATA$TRT) <- AB.CD
if (STUDY == 2) contrasts(DATA$TRT) <- A.BC
if (STUDY == 4) contrasts(DATA$TRT) <- ABC.D
if (STUDY == 5) contrasts(DATA$TRT) <- A.BCD
AB.CD makes sense to me — take one-half of each of A and B compare to negative one-half of C and D (the contrasts add to zero).
However, I don’t understand how the other contrasts are written (i.e., they don’t add to zero). For example, I tried:
A.BC <- c(2, -1, -1) / 2
ABC.D <- c(1, 1, 1, -3)/3
without success (they yielded results markedly different from SAS)
I have searched the web extensively but the explanations of contrasts in R are not particularly understandable. Can anyone help me understand the specifics of this situation? Thanks in advance.
Dennis
Dennis Fisher MD
P < (The "P Less Than" Company)
Phone: 1-866-PLessThan (1-866-753-7784)
Fax: 1-866-PLessThan (1-866-753-7784)
www.PLessThan.com
More information about the R-help
mailing list