[R] Type III ANOVA of package car depends on factor level order

From: Xiaoxu LI <lixiaoxu_at_gmail.com>
Date: Tue, 18 Nov 2008 03:27:29 +0800


## Question1: How to define IV with interaction alone, without main effects? ## Question2: Should Type III ANOVA in package car be independent of the factor level order?

## data from http://www.otago.ac.nz/sas/stat/chap30/sect52.htm drug <- c(t(t(rep(1,3)))%*%t(1:4));
disease <- c(t(t(1:3)) %*% t(rep(1,4))); y <- t(matrix(c(

	 42	,44	,36	,13	,19	,22
	,33	,NA	,26	,NA	,33	,21
	,31	,-3	,NA	,25	,25	,24
	,28	,NA	,23	,34	,42	,13
	,NA	,34	,33	,31	,NA	,36
	,3	,26	,28	,32	,4	,16
	,NA	,NA	,1	,29	,NA	,19
	,NA	,11	,9	,7	,1	,-6
	,21	,1	,NA	,9	,3	,NA
	,24	,NA	,9	,22	,-2	,15
	,27	,12	,12	,-5	,16	,15
	,22	,7	,25	,5	,12	,NA
	),nrow=6));

## verify data with
http://www.otago.ac.nz/sas/stat/chap30/sect52.htm (cbind(drug,disease,y));
## make a big table
drug <- as.factor(rep(drug,6));
disease <- as.factor(rep(disease,6));
y <- c(y);
## verify data through type I ANOVA to
http://www.otago.ac.nz/sas/stat/chap30/sect52.htm anova(lm(y~drug*disease));

require(car);
## Type III ANOVA in package car is not Type III ANOVA in SAS Anova(lm(y~drug*disease),type='III');

## Type III ANOVA in package car equates to wishful
## "anova(lm(y~INTERACTION +disease),lm(y~drug*disease))"
## However in R, df of lm(y~drug:disease) is automatically df of
lm(y~drug*disease)
## How to define IV with interaction alone, without main effects?

## Verify type III of package car to be (INTERACTION + disease) vs. (disease*drug)
## However at the 3rd row, replace the maximun levels(drug==4, disease=3) with the minimum levels (==1). rbind(Anova(lm(y~drug*disease),type='III')$'Sum Sq'[2:4]

,	c(
		anova(
			lm(y~ 1
				+	(	I((drug==2)&(disease==2))

+ I((drug==3)&(disease==2))
+ I((drug==4)&(disease==2))
+ I((drug==2)&(disease==3))
+ I((drug==3)&(disease==3))
+ I((drug==4)&(disease==3))
) + ( I(disease==2) + I(disease==3))) ,lm(y~drug*disease))$'Sum of Sq'[2] , anova( lm(y~ 1 + ( I((drug==2)&(disease==2))
+ I((drug==3)&(disease==2))
+ I((drug==4)&(disease==2))
+ I((drug==2)&(disease==3))
+ I((drug==3)&(disease==3))
+ I((drug==4)&(disease==3))
) + ( I(drug==2) + I(drug==3) + I(drug==4))) ,lm(y~drug*disease))$'Sum of Sq'[2] , anova(lm(y~drug+disease),lm(y~drug*disease))$'Sum of Sq'[2] ) , c( anova( lm(y~ 1 + ( I((drug==2)&(disease==2))
+ I((drug==3)&(disease==2))
+ I((drug==1)&(disease==2))
+ I((drug==2)&(disease==1))
+ I((drug==3)&(disease==1))
+ I((drug==1)&(disease==1))
) + ( I(disease==2) + I(disease==1))) ,lm(y~drug*disease))$'Sum of Sq'[2] , anova( lm(y~ 1 + ( I((drug==2)&(disease==2))
+ I((drug==3)&(disease==2))
+ I((drug==1)&(disease==2))
+ I((drug==2)&(disease==1))
+ I((drug==3)&(disease==1))
+ I((drug==1)&(disease==1))
) + ( I(drug==2) + I(drug==3) + I(drug==1))) ,lm(y~drug*disease))$'Sum of Sq'[2] , anova(lm(y~drug+disease),lm(y~drug*disease))$'Sum of Sq'[2] )

)

## I don't know whether the problem is in anova(lm1,lm2) or lm, or Anova

## while type II is independent of level order -- rbind(Anova(lm(y~drug*disease),type='II')$'Sum Sq'[1:3]

,	c(
		anova(
			lm(y~ 1+disease)
			,lm(y~ 1
				+	(	I(drug==2)

+ I(drug==3)
+ I(drug==4)
) + ( I(disease==2) + I(disease==3))) ,lm(y~drug*disease))$'Sum of Sq'[2] , anova( lm(y~ 1+drug) ,lm(y~ 1 + ( I(drug==2)
+ I(drug==3)

+ I(drug==4)
) + ( I(disease==2) + I(disease==3))) ,lm(y~drug*disease))$'Sum of Sq'[2] , anova(lm(y~drug+disease),lm(y~drug*disease))$'Sum of Sq'[2] ) , c( anova( lm(y~ 1+disease) ,lm(y~ 1 + ( I(drug==2)
+ I(drug==3)
+ I(drug==1)
) + ( I(disease==2) + I(disease==1))) ,lm(y~drug*disease))$'Sum of Sq'[2] , anova( lm(y~ 1+drug) ,lm(y~ 1 + ( I(drug==2)
+ I(drug==3)
+ I(drug==1)
) + ( I(disease==2) + I(disease==1))) ,lm(y~drug*disease))$'Sum of Sq'[2] , anova(lm(y~drug+disease),lm(y~drug*disease))$'Sum of Sq'[2] )

)

## LI, Xiaoxu
## School of Humanities & Social Sciences, Shenzhen Graduate School of Peking Univ.

## http://lixiaoxu.lxxm.com/



R-help_at_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. Received on Mon 17 Nov 2008 - 19:32:20 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 17 Nov 2008 - 23:30:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive