Hi David, You attach the dataset, which creates a copy of it. You set the contrasts on the copy, but in your models, you reference the dataset explicitly. You should set the contrasts directly in the data (and for your sake and your students, would encourage not using attach() ).
contrasts(Eysenck$Condition) <- contr.sum Setting the global option works because it applies to everything R does, so even the dataset where contrasts were not set. Cheers, Josh On Mon, Dec 2, 2013 at 5:41 PM, David C. Howell <david.how...@uvm.edu>wrote: > I have been having trouble understanding the difference between > "contrasts(Group) <- contr.sum" and options(contrasts = > c("contr.sum","contr.poly"). They both seem to say that they have done what > I want, but only the latter works. > > The reason why the question arises is tha,t using Fox's Anova, it is > important to use sum contrasts. So I wrote the following code, with the > result commented in. > > > # Start R from scratch in case old contrasts are left over --They aren't > #removed with rm() > # Note that the data are balanced, so almost any solution SHOULD give the > same > #result. > Eysenck <- read.table("http://www.uvm.edu/~dhowell/methods7/ > DataFiles/Tab13-2.dat", > header = TRUE) > Eysenck$subj <- factor(1:100) > Eysenck$Condition <- factor(Eysenck$Condition, levels = 1:5, labels = > c("Counting", > "Rhyming", "Adjective", "Imagery","Intention")) > Eysenck$Age <- factor(Eysenck$Age, levels = 1:2, labels = c("Old","Young")) > attach(Eysenck) > result <- anova(aov(Recall~Condition*Age, data = Eysenck)) > print(result) # This is the correct result > #Analysis of Variance Table > # > # Response: Recall > # Df Sum Sq Mean Sq F value Pr(>F) > # Condition 4 1514.94 378.73 47.1911 < 2.2e-16 *** > # Age 1 240.25 240.25 29.9356 3.981e-07 > *** > # Condition:Age 4 190.30 47.57 5.9279 0.0002793 *** > # Residuals 90 722.30 8.03 > getOption("contrasts") > # unordered ordered > # "contr.treatment" "contr.poly" > #Note that these are the default treatment contrasts--bad, bad, but OK > here. > > # Leave the contrasts alone for now > library(car) > resultsCar1 <- lm(Recall~Age*Condition, data = Eysenck ) > type2 <- Anova(resultsCar1, type = "II") #This is OK, but the next is > very wrong > print(type2) > #Anova Table (Type II tests) > #type2 <- Anova(resultsCar1, type = "III") > #Response: Recallprint(type2) > # Sum Sq Df F value Pr(>F) > #Age 240.25 1 29.9356 3.981e-07 *** > #Condition 1514.94 4 47.1911 < 2.2e-16 *** > #Age:Condition 190.30 4 5.9279 0.0002793 *** > #Residuals 722.30 90 > > resultsCar2 <- lm(Recall~Age*Condition, data = Eysenck ) > type3 <- Anova(resultsCar1, type = "III") > print(type3) # This is still wrong --Fox says I need sum contrasts > #Anova Table (Type III tests) > # contrasts(Condition) <- contr.sum > # Response: Recallcontrasts(Age) <- contr.sum > # Sum Sq Df F value Pr(>F) > # (Intercept) 490.00 1 61.0550 9.85e-12 *** > # Age 1.25 1 0.1558 0.6940313 > # Condition 351.52 4 10.9500 2.80e-07 *** # Hmmm! why do > we still have treatment contrasts > # Age:Condition 190.30 4 5.9279 0.0002793 ***## No LUCK!! > # Residuals 722.30 90 > getOption("contrasts") > # unordered ordered > #"contr.treatment" "contr.poly" > contrasts(Condition) <- contr.sum > contrasts(Age) <- contr.sum > contrasts(Condition); contrasts(Age) > #Yup, we see sum contrasts! > # [,1] [,2] [,3] [,4] > #Counting 1 0 0 0 > #Rhyming 0 1 0 0 > #Adjective 0 0 1 0 > #Imagery 0 0 0 1 > #Intention -1 -1 -1 -1 > # [,1] > #Old 1 > #Young -1 > # BUT!! > resultsCar3 <- lm(Recall~Age*Condition, data = Eysenck ) > type3 <- Anova(resultsCar3, type = "III") > print(type3) > #Anova Table (Type III tests) > # > #Response: Recall > # Sum Sq Df F value Pr(>F) > #(Intercept) 490.00 1 61.0550 9.85e-12 *** > #Age 1.25 1 0.1558 0.6940313 > #Condition 351.52 4 10.9500 2.80e-07 *** > #Age:Condition 190.30 4 5.9279 0.0002793 *** > #Residuals 722.30 90 > > ##Damn! We are still wrong even though the above shows sum contrasts > > ## So we do it another way > options(contrasts = c("contr.sum","contr.poly")) > getOption("contrasts") > #[1] "contr.sum" "contr.poly" > resutsCar4 <- lm(Recall~Age*Condition, data = Eysenck ) > type4 <- Anova(resultsCar4, type = "III") > #print(type4) # Now we're back where we should be > #Anova Table (Type III tests) > # > #Response: Recall > # Sum Sq Df F value Pr(>F) > #(Intercept) 13479.2 1 1679.5361 < 2.2e-16 *** > #Age 240.2 1 29.9356 3.981e-07 *** > #Condition 1514.9 4 47.1911 < 2.2e-16 *** > #Age:Condition 190.3 4 5.9279 0.0002793 *** > #Residuals 722.3 90 > ## Now it works just fine. > ##So what is the difference between setting contrasts individuall and > setting > ##them through the options? > > I get similar results if I use drop1, but perhaps that is what Fox did > also. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://joshuawiley.com/ Senior Analyst - Elkhart Group Ltd. http://elkhartgroup.com [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.