8.4.1 Comparing two variances
Before we can carry out a test to compare two sample means (see below), we need to test whether the sample
variances are significantly different (see p. 356). The test could not be simpler. It is called Fisher’s F test
after the famous statistician and geneticist R.A. Fisher, who worked at Rothamsted in south-east England
qf(0.975,9,9)
f.test.data <- read.table("c:\\temp\\f.test.data.txt",header = T)
attach(f.test.data)
names(f.test.data)
var(gardenB)
var(gardenC)
F.ratio <- var(gardenC)/var(gardenB)
F.ratio
2*(1-pf(F.ratio,9,9))
var.test(gardenB,gardenC)
refs <- read.table("c:\\temp\\refuge.txt",header=T)
attach(refs)
names(refs)
tapply(B,T,var)
bartlett.test(B[-31],T[-31])
fligner.test(B[-31],T[-31])
model <- lm(B~T)
plot(model)
ozone <- read.table("c:\\temp\\gardens.txt",header=T)
attach(ozone)
names(ozone)
y <- c(gardenA,gardenB,gardenC)
garden <- factor(rep(c("A","B","C"),c(10,10,10)))
var.test(gardenB,gardenC)
fligner.test(y~garden)
The reason for the difference is that Fisher and Bartlett are sensitive to outliers, whereas Fligner–Killeen is
not (i
There are two classical tests for comparing two sample means:
Student’s t test when the samples are independent, the variances constant, and the errors are normally
distributed;
Wilcoxon’s rank-sum test when the samples are independent but the errors are not normally distributed
(e.g. they are ranks or scores or some sort).
What to do when these assumptions are violated (e.g. when the variances are different) is discussed later on
qt(0.975,18)
t.test.data <- read.table("c:\\temp\\t.test.data.txt",header=T)
attach(t.test.data)
par(mfrow=c(1,1))
names(t.test.data)
ozone <- c(gardenA,gardenB)
label <- factor(c(rep("A",10),rep("B",10)))
boxplot(ozone~label,notch=T,xlab="Garden",ylab="Ozone")
s2A <- var(gardenA)
s2B <- var(gardenB)
(mean(gardenA)-mean(gardenB))/sqrt(s2A/10+s2B/10)
2*pt(-3.872983,18)
t.test(gardenA,gardenB)
ozone <- c(gardenA,gardenB)
ozone
label <- c(rep("A",10),rep("B",10))
label
combined.ranks <- rank(ozone)
combined.ranks
tapply(combined.ranks,label,sum)
wilcox.test(gardenA,gardenB)