# UW-Madison STAT 850 - Example of “Treatment Contrasts” Used by R in Estimating Anova Coefficients (8 pages)

Previewing pages*1, 2, 3*of 8 page document

**View the full content.**## Example of “Treatment Contrasts” Used by R in Estimating Anova Coefficients

Previewing pages *1, 2, 3*
of
actual document.

**View the full content.**View Full Document

## Example of “Treatment Contrasts” Used by R in Estimating Anova Coefficients

0 0 202 views

**Unformatted text preview: **

Statistics 850 Spring 2005 Example of treatment contrasts used by R in estimating ANOVA coefficients The first example shows a simple numerical design matrix in R no factors for the groups 1 a b ab resp rnorm 4 0 1 data data frame resp resp A c 0 1 0 1 B c 0 0 1 1 data resp A B 1 0 3112628 0 0 2 0 7630832 1 0 3 1 7276357 0 1 4 0 5787976 1 1 lm1 out lm resp A B data data Note that the columns of the design matrix have zeros where our effect contrasts had 1 s The results fitted values and tests are equivalent although this design matrix does not have orthogonal columns and the contrast coefficients do not sum to zero as can be seen from X T X X model matrix lm1 out X Intercept A B A B 1 1 0 0 0 2 1 1 0 0 3 1 0 1 0 4 1 1 1 1 X t X 1 2 3 1 1 1 1 2 1 2 1 3 1 1 2 4 1 2 2 coef coef 4 1 2 2 4 solve t X X t X data resp 1 Intercept 0 3112628 A 1 0743461 B 2 0388985 A B 2 2231841 lm1 out coef Intercept A B 0 3112628 1 0743461 2 0388985 A B 2 2231841 Page 1 Statistics 850 Spring 2005 An equivalent example with factors rather than numeric design data data frame resp resp A c f m f m B c f f m m data resp A B 1 0 3112628 f f 2 0 7630832 m f 3 1 7276357 f m 4 0 5787976 m m R drops the first group Since f comes before m in the alphabet the contrast for f is dropped contrasts data A m f 0 m 1 contrasts data B m f 0 m 1 lm2 out lm resp A B data data X as num model matrix lm2 out X 1 2 3 4 1 1 0 0 0 2 1 1 0 0 3 1 0 1 0 4 1 1 1 1 Note that the coefficients calculated using the model matrix and those from the R fit match and match the previous example coef coef solve t X X t X data resp 1 1 0 3112628 2 1 0743461 3 2 0388985 4 2 2231841 lm2 out coef Intercept Am 0 3112628 1 0743461 lm1 out coef Intercept A 0 3112628 1 0743461 Bm 2 0388985 Am Bm 2 2231841 B 2 0388985 A B 2 2231841 Page 2 Statistics 850 Spring 2005 Example of a 3x2 factorial A has levels low med and high and B has levels low and high resp data rnorm 6 0 1 data frame resp resp A c low med high low med high B c low low low

View Full Document