How to set contrasts correctly in R

I was asked to see if there is a linear trend in 3 data groups (5 points each) using ANOVA and linear contrasts. The 3 groups represent the data collected in 2010, 2011and 2012. I want to use R for this procedure, and I tried both of the following:

contrasts(data$groups, how.many=1) <- contr.poly(3)
contrasts(data$groups)  <- contr.poly(3)

Both methods seem to work fine, but give slightly different answers in terms of their p-values. I have no idea what is right, and it is really difficult to find help for this on the Internet. I would like to help figure out what is causing the different answers. I am not sure if this is due to the separation of the sums of squares or something else.

+3
source share
2 answers

, .

, , x, y .

x <- y <- gl(3, 2)
# [1] 1 1 2 2 3 3
# Levels: 1 2 3

, .. (.L) (.Q). 3 : 3 - 1 th .

contrasts(x) <- contr.poly(3)
# [1] 1 1 2 2 3 3
# attr(,"contrasts")
#              .L         .Q
# 1 -7.071068e-01  0.4082483
# 2 -7.850462e-17 -0.8164966
# 3  7.071068e-01  0.4082483
# Levels: 1 2 3

, (.. ). how.many = 1. , 1.

contrasts(y, how.many = 1) <- contr.poly(3)
# [1] 1 1 2 2 3 3
# attr(,"contrasts")
#              .L
# 1 -7.071068e-01
# 2 -7.850462e-17
# 3  7.071068e-01
# Levels: 1 2 3

, .

+3

, , . , , , , : ( , ).

( "nlevels - 1" ) , () . ( , .

, , mtcars () . , , ( , ).

df = mtcars # copy the dataset
df$gear = as.ordered(df$gear) # make an ordered factor

, :

contrasts(df$gear) <- contr.poly(nlevels(df$gear))

.

m1 = lm(mpg ~ gear, data = df);
summary.lm(m1)
#             Estimate Std. Error t value Pr(>|t|)    
# (Intercept)  20.6733     0.9284  22.267  < 2e-16 ***
# gear.L        3.7288     1.7191   2.169  0.03842 *  
# gear.Q       -4.7275     1.4888  -3.175  0.00353 ** 
# 
# Multiple R-squared:  0.4292,  Adjusted R-squared:  0.3898 
# F-statistic:  10.9 on 2 and 29 DF,  p-value: 0.0002948

, F (2,29) = 10,9 p = 0,38 3,7 /.

" " .

contrasts(df$gear, how.many = 1) <- contr.poly(nlevels(df$gear))
m1 = lm(mpg ~ gear, data = df)
summary.lm(m1)
# Coefficients:
#             Estimate Std. Error t value Pr(>|t|)    
# (Intercept)   21.317      1.034  20.612   <2e-16 ***
# gear.L         5.548      1.850   2.999   0.0054 ** 
# Multiple R-squared:  0.2307,  Adjusted R-squared:  0.205 
# F-statistic: 8.995 on 1 and 30 DF,  p-value: 0.005401

(5,5 ) p <.05 - ? , : 23% ( 43%)! , :

plot(mpg ~ gear, data = df) # view the relationship

(mixing) gear to gallon ratios

, , ( ) , . (, , -1).

, : , , .

- , 4 , 3, : -)

+1

All Articles