How do I set up orthogonal contrasts in R?

492 Views Asked by At

I have a data set consisting of several Types and two treatments (example below). I am using the lm() function in R to analyze it and emmeans for post hoc tests.

m <- lm(Y ~ Type*Treatment)
Anova(m, type = 3)

enter image description here

How do I set up orthogonal contrasts in R? If I run

emmeans(m, ~Type*Treatment)

it compares everything. I am interested in comparing different Treatments within Type and different Types within Treatments but not different Treatments and different Types. In other words, I would like to compare Treated A vs. Untreated A, or Treated A vs. Treated B, but not Treated A vs. untreated B.

I am sorry if this is covered under another question. I have searched the forums and haven't found anything that I can apply to this situation. Thank you.

1

There are 1 best solutions below

0
jan-glx On
emmeans(m, list(~ Type | Treatment, ~ Treatment | Type))

To figure out whether theses test are really what you want to test, I'd kindly ask you to ask this question with more background on stats.stackexchange.com .

Reproducible example:

library(emmeans)
pigs.lm <- lm(log(conc) ~ source * factor(percent), data = pigs)
pigs.emm.s <- emmeans(pigs.lm,  list(~ source | percent, ~ percent | source))
pairs(pigs.emm.s)
#> $`emmeans of source | percent`
#> percent =  9:
#>  contrast    estimate     SE df t.ratio p.value
#>  fish - soy   -0.2932 0.1061 17  -2.763  0.0338
#>  fish - skim  -0.3153 0.1061 17  -2.971  0.0222
#>  soy - skim   -0.0221 0.0949 17  -0.233  0.9706
#> 
#> percent = 12:
#>  contrast    estimate     SE df t.ratio p.value
#>  fish - soy   -0.2495 0.0949 17  -2.629  0.0442
#>  fish - skim  -0.3347 0.0949 17  -3.526  0.0069
#>  soy - skim   -0.0852 0.0949 17  -0.898  0.6489
#> 
#> percent = 15:
#>  contrast    estimate     SE df t.ratio p.value
#>  fish - soy   -0.2348 0.1061 17  -2.212  0.0976
#>  fish - skim  -0.4700 0.1163 17  -4.043  0.0023
#>  soy - skim   -0.2352 0.1061 17  -2.216  0.0969
#> 
#> percent = 18:
#>  contrast    estimate     SE df t.ratio p.value
#>  fish - soy   -0.2836 0.1342 17  -2.112  0.1169
#>  fish - skim  -0.6157 0.1342 17  -4.587  0.0007
#>  soy - skim   -0.3321 0.1644 17  -2.020  0.1376
#> 
#> Results are given on the log (not the response) scale. 
#> P value adjustment: tukey method for comparing a family of 3 estimates 
#> 
#> $`emmeans of percent | source`
#> source = fish:
#>  contrast estimate     SE df t.ratio p.value
#>  9 - 12    -0.1849 0.1061 17  -1.742  0.3339
#>  9 - 15    -0.1894 0.1163 17  -1.629  0.3897
#>  9 - 18    -0.2300 0.1061 17  -2.168  0.1723
#>  12 - 15   -0.0045 0.1061 17  -0.042  1.0000
#>  12 - 18   -0.0452 0.0949 17  -0.476  0.9634
#>  15 - 18   -0.0407 0.1061 17  -0.383  0.9802
#> 
#> source = soy:
#>  contrast estimate     SE df t.ratio p.value
#>  9 - 12    -0.1412 0.0949 17  -1.487  0.4662
#>  9 - 15    -0.1310 0.0949 17  -1.380  0.5281
#>  9 - 18    -0.2204 0.1342 17  -1.642  0.3829
#>  12 - 15    0.0102 0.0949 17   0.108  0.9995
#>  12 - 18   -0.0792 0.1342 17  -0.590  0.9336
#>  15 - 18   -0.0895 0.1342 17  -0.666  0.9082
#> 
#> source = skim:
#>  contrast estimate     SE df t.ratio p.value
#>  9 - 12    -0.2043 0.0949 17  -2.152  0.1767
#>  9 - 15    -0.3441 0.1061 17  -3.242  0.0225
#>  9 - 18    -0.5305 0.1342 17  -3.952  0.0051
#>  12 - 15   -0.1398 0.1061 17  -1.317  0.5651
#>  12 - 18   -0.3262 0.1342 17  -2.430  0.1090
#>  15 - 18   -0.1864 0.1424 17  -1.309  0.5699
#> 
#> Results are given on the log (not the response) scale. 
#> P value adjustment: tukey method for comparing a family of 4 estimates

Created on 2021-10-07 by the reprex package (v2.0.1)