# Exercise 2.5 # # 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 H.-C. Jing & K. Hammond-Kosack, Rothamsted Research # Version 1, 16/09/2014 " # # 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 triticum <- read.table("triticum.dat",sep="",header=T) summary(triticum) # Plot Weight against Length qplot(y=Weight, x=Length, data=triticum) # Get sample variances for each variable var.Wt <- var(triticum$Weight); var.Wt var.Ln <- var(triticum$Length); var.Ln # Sample covariance cov <- cov(triticum$Weight, triticum$Length); cov # Sample correlation coefficient cor <- cor(triticum$Weight, triticum$Length); cor # Test correlation coefficient cor.test(triticum$Weight, triticum$Length) # Rest of file verifies calculations # Calculate sample covariance DevWt <- triticum$Weight - mean(triticum$Weight) DevLn <- triticum$Length - mean(triticum$Length) # Get number of observations N <- length(DevWt) # Sample covariance sum(DevWt*DevLn)/(N-1) # Sample correlation cov/sqrt(var.Wt*var.Ln) # t-statistic t <- cor*sqrt((N-2)/(1-cor^2)); t # Get critical value # = 97.5th quantile of t distribution with 10 df critical <- qt(0.975,N-2); critical # Get observed significance level # = proportion of t distribution with 10 df > abs(t) or < -abs(t) P <- 2*(1-pt(abs(t),N-2)); P # End of file