igiari21
igiari21

Reputation: 29

Linear Contrasts and Anova in R

I'm trying to understand this question (my teacher is on vacation) and I would appreciate some help.

Using the “contr.sum” contrasts option, conduct a two-way analysis of variance (ANOVA) that includes Time and Area as main effects and an interaction between the two main effects.

I'm not really sure how to use contrasts to do ANOVA. My answers seem to be separate. I have created a model like so

modelCO1 = aov(CO~Time+Area+(Time*Area), data = WorkplaceCO)

But this has nothing to do with linear contrasts. Whenever I try to use this code

modelCO1$contrasts$Time

I get an output that just says

"contr.sum"

Which doesn't really tell me anything. Alternatively, I have done this

options(contrasts=c("contr.sum", "contr.poly"))

contrasts(WorkplaceCO$Area)
contrasts(WorkplaceCO$Time)

Which gave me an output of

> contrasts(WorkplaceCO$Area)
           [,1]
Nonsmoking    1
Smoking      -1
> contrasts(WorkplaceCO$Time)
        [,1] [,2] [,3] [,4] [,5]
7:00am     1    0    0    0    0
10:00am    0    1    0    0    0
11:00am    0    0    1    0    0
1:20pm     0    0    0    1    0
4:20pm     0    0    0    0    1
7:00pm    -1   -1   -1   -1   -1

But again, where does ANOVA fit in? Much obliged.

Upvotes: 2

Views: 989

Answers (1)

drammock
drammock

Reputation: 2543

Setting contrasts needs to be done before you fit the model. So if you run options(contrasts=c("contr.sum", "contr.poly")) before the call to aov() then you will get the model you want. Note that the options() call changes the defaults for future calls to contrasts(); if you don't want that you can set contrasts on a factor without changing the defaults like this:

contrasts(WorkplaceCO$Area) <- contr.sum
contrasts(WorkplaceCO$Time) <- contr.sum

Whichever way you do it, it needs to be done before aov().

Upvotes: 3

Related Questions