# # Exercise 18.4 # # 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 # Version 1, 22/08/2015 # Set working directory - use setwd() function or from Session menu in RStudio # e.g. setwd("d:/stats4biol/data) # Set up packages to be used later - available from CRAN library(ggplot2) # for plotting # Read data transect <- read.table("transect.dat",sep="",na.strings="*", header=T) transect$fDist <- as.factor(transect$fDist) summary(transect) # Get log-transform of counts as in previous analysis transect$logCount <- log10(transect$Count) # Plot data qplot(y=Count, x=Distance, data=transect) # SLR model with lack of fit from Example 12.2 transect.lof <- lm(logCount~Distance+fDist, data=transect) anova(transect.lof) # GLM with Poisson errors and log link transect.glm <- glm(Count~Distance+fDist, family=poisson(), data=transect) # Check for over-dispersion - none present ResDev <- summary(transect.glm)$deviance; ResDev ResDF <- summary(transect.glm)$df.residual; ResDF p <- 1-pchisq(ResDev, df=ResDF); p # Check residual plots plot(transect.glm) # Check whether LOF required - yes anova(transect.glm, test="Chisq") # Compare fitted values from the two factor-based models fitGLM <- fitted(transect.glm) fitREGlog <- fitted(transect.lof) fitREG <- 10^fitREGlog aggregate(Count~fDist, data=transect, FUN=mean) aggregate(fitGLM~transect$fDist, FUN=mean) aggregate(fitREG~transect$fDist, FUN=mean) # Set up plot plot.obs <- ggplot(data=transect, aes(y=Count, x=Distance)) # Plot data and fitted means from the two models plot.obs + geom_point() + geom_point(aes(y=fitREG, data=transect.lof, colour="red", size=3) + geom_point(aes(y=fitglm), data=transect.glm, colour="blue", size=3) + ggtitle("Data with fitted means from lm and glm") # End of file