# # Exercise 12.6 # # 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 V. Buchanan-Wollaston (PRESTA), University of Warwick # Version 1, 15/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 senescence <- read.table("senescence.dat",sep="",header=T) summary(senescence) # Plot CATMA3A13560 against Day qplot(y=CATMA3A13560, x=Day, data=senescence) # Fit SLR model senescence.slr <- lm(CATMA3A13560~Day, data=senescence) # Use ANOVA to check for strength of relationship anova(senescence.slr) # Summarise using adjusted R-squared summary(senescence.slr)$adj.r.squared # Plot residuals plot(senescence.slr, ask=FALSE) # Get fitted values and plot with data fv <- fitted(senescence.slr) # Set up basic plot using model object plot.obs <- ggplot(data=senescence.slr, aes(y=CATMA3A13560, x=Day) ) # Plot observations and fitted line plot.obs + geom_point() + geom_line(aes(y=fitted(senescence.slr))) + ggtitle("Observed data with fitted SLR") # SLR model with lack of fit # Create factor copy of variate Day senescence$fDay <- as.factor(senescence$Day) # Fit model with lack of fit senescence.lof <- lm(CATMA3A13560~Day+fDay, data=senescence) # ANOVA table indicates lack of fit present anova(senescence.lof) # Plot fitted values from the two models with data to interpret lack of fit senescence.fit <- senescence senescence.fit$fit.lof <- fitted(senescence.lof) senescence.fit$fit.slr <- fitted(senescence.slr) # Set up plot plot.obs <- ggplot(data=senescence.fit, aes(y=CATMA3A13560, x=Day)) # Plot data, fitted line and fitted means plot.obs + geom_point() + geom_line(aes(y=fit.slr)) + geom_point(aes(y=fit.lof), colour="red", size=3) + ggtitle("Data with fitted means and SLR") # End of file # Note: Exercises 13.1 and 17.2 use different variates from the same data set.