# # Exercise 5.4 - follows on from Exercise 4.3 # # Statistical Methods in Biology: Design and Analysis of Experiments and Regression # by S.J. Welham, S.A. Gezan, S.J. Clark & A. Mead (2014) # Chapman & Hall/CRC Press, Boca Raton, Florida. ISBN: 978-1-4398-0878-8 # Data from J. Baverstock, Rothamsted Research # Version 1, 26/09/2014 # Set working directory - use setwd() function or from Session menu in RStudio # e.g. setwd("d:/stats4biol/data) # read data fungus <- read.table("fungus.dat",sep="",header=T) summary(fungus) # Bartlett test for homogeneity of variances b.test <- bartlett.test(Nymphs~Fungus, data=fungus); b.test # Rest of file verifies calculation of test statistic # Get properties of design N <- length(fungus$Nymphs); N t <- length(levels(fungus$Fungus)); t # Get replication and variance for each treatment group t.count <- aggregate(Nymphs~Fungus, data=fungus, FUN=length); t.count t.var <- aggregate(Nymphs~Fungus, data=fungus, FUN=var); t.var # Get pooled estimate of variance pooled <- sum((t.count$Nymphs-1)*t.var$Nymphs)/sum(t.count$Nymphs-1); pooled # Calculate scalar for Bartlett statistic c <- (sum(1/(t.count$Nymphs-1))-1/(N-t))/(3*(t-1)); c # Calculate test statistic X2 <- ((N-t)*log(pooled)-sum((t.count$Nymphs-1)*log(t.var$Nymphs)))/(1+c); X2 # Get 95th percentile of Chisquared distribution on 2 df qchisq(0.95,2) # Get observed significance level 1-pchisq(X2,2) # End of file