Contrasts for lm using a contrast package

I use the contrast package to build contrasts for lm in R With the following code, I am making a contrast between Trt1 and Trt3.

 Y <- c(10, 12, 14, 16, 9, 8) Trt <- gl(n=3, k=2, length=3*2) Data1 <- data.frame(Y, Trt) Data1.lm <- lm(Y~Trt, data = Data1) library(contrast) Contrs1 <- contrast(Data1.lm, a=list(Trt="1"), b=list(Trt="3"), type = "average") print(Contrs1, X=TRUE) 

I would like to make a contrast between the middle (Trt1 and Trt2) and Trt3. I used this code

 Contrs2 <- contrast(Data1.lm, a=list(Trt="1", Trt="2"), b=list(Trt="3"), type = "average") print(Contrs2, X=TRUE) lm model parameter contrast Contrast SE Lower Upper t df Pr(>|t|) 1 6.5 1.224745 2.602315 10.39768 5.31 3 0.0131 Contrast coefficients: (Intercept) Trt2 Trt3 1 0 1 -1 

I see that this is not the desired contrast. I wonder how to get the right contrast with the contrast package in R Any help in this regard would be greatly appreciated. Thanks

PS I know what to use the contrast matrix for the aov function in R , but for this specific task I want to use the contrast package.

+4
source share
1 answer

You should specify the treatment levels included as a vector ( Trt=c("1","2") ), not a list. I figured this out by looking at examples in ?contrast.lm (although admittedly this helps to find out what you are looking for):

 Contrs2 <- contrast(Data1.lm, a=list(Trt=c("1","2")), b=list(Trt="3"), type = "average") print(Contrs2, X=TRUE) ## lm model parameter contrast ## Contrast SE Lower Upper t df Pr(>|t|) ## 1 4.5 1.06066 1.124506 7.875494 4.24 3 0.024 
+6
source

Source: https://habr.com/ru/post/1397113/


All Articles