Latin Square ANOVA

Navigation:  Resources > R Code samples >

Latin Square ANOVA

Previous pageReturn to chapter overviewNext page

The following R code analyzes the yield dataset for a 5x5 Latin Square design by creating the yield dataset as a data frame, y, and then fitting a linear model using the lm() function to the data. This analyzes the yield in terms of the row, column and treatment effects and produces a summary table. The anova() statement performs the ANOVA, which we then display in table form (note that R does not display the totals row). The two matrix() functions display the design matrix and the data matrix in 5x5 format, as illustrated.

R Code

## setup framework for rows, columns and treatments

rows<- c(rep("r1",1), rep("r2",1), rep("r3",1), rep("r4",1), rep("r5",1))

cols<- c(rep("cA",5), rep("cB",5), rep("cC",5), rep("cD",5), rep("cE",5))

treatments<- c("A","C","D","E","B", "D","B","C","A","E", "E","A","B","C","D", "B","E","A","D","C", "C","D","E","B","A")

matrix(y$treatments, 5,5)

## assign data to variable "yield"

yield<- c(7.4,11.8,10.1,8.8,11.8,8.9,6.5,17.9,10.1,8.8,5.8,8.7,9.0,15.7,14.3,12.0,7.6,8.5,11.1,18.4,14.3,7.9,7.1,7.4,10.1)

matrix(y$yield, 5,5)

y<- data.frame(cols,rows,treatments, yield)

## create a 2x2 plot array

par(mfrow=c(2,2))

## generate box plots of yield versus the 3 factors

plot(yield ~ rows+cols+treatments,y)

## apply a linear model

yfit <- lm(yield ~ rows+cols+treatments, y)

## compute the analysis of variance table

anova(yfit)

 

Matrix outputs:

 

    [,1] [,2] [,3] [,4] [,5]

[1,] "A"  "D"  "E"  "B"  "C"

[2,] "C"  "B"  "A"  "E"  "D"

[3,] "D"  "C"  "B"  "A"  "E"

[4,] "E"  "A"  "C"  "D"  "B"

[5,] "B"  "E"  "D"  "C"  "A"

 

    [,1] [,2] [,3] [,4] [,5]

[1,]  7.4  8.9  5.8 12.0 14.3

[2,] 11.8  6.5  8.7  7.6  7.9

[3,] 10.1 17.9  9.0  8.5  7.1

[4,]  8.8 10.1 15.7 11.1  7.4

[5,] 11.8  8.8 14.3 18.4 10.1

Plot output

aovplot2