Fitting nested terms in SAS and R: This lecture has code for a nested experiment. The data is from the text "Design of Experiments: Statistical Principles of Research Design and Analysis" by Robert O. Kuehl (second edition) Glucose was measured in a laboratory on three different days, with two separate runs per day. Thus runs are nested within days. SAS In SAS, to specify a nested term use parentheses. In the code below we see that run is nested within day by writing "run(day)" proc glm data = table77 ; class day run ; model glucose = day run(day) ; random day run(day) / test ; lsmeans day / e = run(day) stderr ; run ; proc mixed data = table77 ; class day run ; model glucose = ; random day run(day) ; run ; R R has more than one way to specify a nested term, depending on what package is used. Listed below are two ways to use with the aov function: summary(aov(glucose ~ day + Error(day/run), data = table77)) summary(aov(glucose ~ day + run %in% day, data = table77)) Either way calculates the sums of squares for run within day correctly, but they do not form the correct F ratio. Also, the two notations above are not often recognized by other packages. Another way is more reliable, but less transparent. This way is based on the identity that: SSB(A) = SSB + SSA*B (In other words, the SS for a nested effect equals what SSB + SSA*B would have been had the factors been crossed instead of nested) Thus one way to calculate nested SS is to pretend the effect is crossed, calculate both SSB and SSA*B and add them: summary(aov(glucose ~ day + run + run:day, data = table77a)) However this is inconvenient and sometimes not possible depending on how the nested effect was coded. Instead we can use the fact that if a model is specified with an interaction but excluding a lower-order term, the software generally includes the term automatically. In other words if the software sees: summary(aov(glucose ~ day + run:day, data = table77a)) It will automatically use SSrun + SSrun:day as the result for SSrun:day. This approach works more widely than just with the aov function, many of my examples use it. Altered data for this example: data <- matrix(scan(),ncol=3,byrow=TRUE) 1 1 42.5 1 1 43.3 1 1 42.9 1 2 42.2 1 2 41.4 1 2 41.8 2 1 48.0 2 1 44.6 2 1 43.7 2 2 42.0 2 2 42.8 2 2 42.8 3 1 41.7 3 1 43.4 3 1 42.5 3 2 40.6 3 2 41.8 3 2 41.8 data table77a <- data.frame(data) colnames(table77a) <- c("day","run","glucose") rm(data) table77a$day <- as.factor(table77a$day) table77a$run <- as.factor(table77a$run) summary(aov(glucose ~ day + run %in% day, data = table77a)) summary(aov(glucose ~ day + run + run:day, data = table77a)) summary(aov(glucose ~ day + run:day, data = table77a))