GreenBlueBar.gif GreenBlueBar.gif

Examples Using Repeated Measures in R

The following is bare code. I think that you will be able to extend it to similar analyses.

### Various Repeated Measures Designs

### The following uses aov() instread of lm(). This creates better printouts

###One Within Subject Variable   No Between Subject Variable

data1 <- read.table("http://www.uvm.edu/~dhowell/fundamentals9/DataFiles/Tab18-1.dat"
, header = TRUE)
data1$person <- factor(1:25)
attach(data1)
data1Long <- reshape(data = data1, varying = 1:5, v.names = "depression",
 timevar = "time", idvar = "person", 
                      ids = "subj", direction = "long")
attach(data1Long)
time <- factor(time)
person <- factor(person)
model1 <- aov(depression ~ time + Error(person))
summary(model1)


### Now one between and one within
### Using King data
data2 <- read.table("http://www.uvm.edu/~dhowell/methods8/DataFiles/Tab14-4.dat",
 header = TRUE)
data2$person <- factor(1:24)
data2$Group <- factor(data2$Group)
data2Long <- reshape(data = data2, varying = 2:7, v.names = "outcome", timevar
                   = "time", idvar = "person", ids = "person", direction = "long")
attach(data2Long)
time <- factor(time)
my.aov <- aov(outcome ~ (Group*time) + Error(person/(time)))

###  THIS WORKS TOO  --  SO DOES THE NEXT

############## This was modified from one written by Joshua Wiley, in the Psychology 
###  Department at UCLA.
### Handles 2 Between and 1 Within
### Howell Table 14.7 ###
### Repeated Measures ANOVA with 2 Between and 1 Within variables
### Read in data, convert to 'long' format, and factor()

dat <- read.table("http://www.uvm.edu/~dhowell/methods7/DataFiles/Tab14-7.dat",
                  header = TRUE)
head(dat)                   
dat$subject <- factor(1:40)
datLong <- reshape(data = dat, varying = 4:7, v.names = "outcome", timevar
                   = "time", idvar = "subject", ids = "subj", direction = "long")

datLong$Condition <- factor(datLong$Condition, levels = 1:2, labels = c("BST", "Control"))
datLong$Sex <- factor(datLong$Sex, levels = 1:2, labels = c("Male","Female"))
datLong$time <- factor(datLong$time)
str(datLong)
attach(datLong)
# Actual formula and calculation
my.aov <- aov(outcome ~ (Condition*Sex*time) + Error(subject/(time)),data = datLong)

# Present the summary table (ANOVA source table)
summary(my.aov)


# Two within subject variables, one between
# Data from Bouton & Schwartzentruber (1985)
# Methods8, p. 486
rm(list = ls())
dataBouton <- read.table("http://www.uvm.edu/~dhowell/methods8/DataFiles/Tab14-11.dat",
 header = T)
dataBouton$subject <- factor(c(1:24))
attach(dataBouton)
## I should use "reshape," but I can't get it to work. This is pretty easy.

Phase <- factor(rep(1:2, each = 24, times = 4))
Cycle <- factor(rep(1:4, each = 48))
Group = factor(rep(1:3, each = 8,times = 8))
dv <- c(C1P1, C1P2, C2P1, C2P2, C3P1, C3P2, C4P1, C4P2)
Subj <- factor(rep(1:24, times = 8))

cat("Means and sd by Group \n")
tapply(dv, Group, mean); tapply(dv, Group, sd)
cat("\n Means and sd by Cycle\n")
tapply(dv, Cycle, mean); tapply(dv, Cycle, sd)
cat("\n Means and sd by Phase\n")
tapply(dv, Phase, mean); tapply(dv, Phase, sd)

options(contrasts = c("contr.sum","contr.poly"))
model1 <- aov(dv ~ (Group*Cycle*Phase) + Error(Subj/(Cycle*Phase)), contrasts = contr.sum)
print(summary(model1))
print(coefficients(model1))     # This will give mean deviations)

library(ez)  # Alternative approach using ezANOVA

df <- data.frame(cbind(Subj, dv, Group, Phase, Cycle))
df$Group <- factor(df$Group)
df$Phase <- factor(df$Phase)
df$Cycle <- factor(df$Cycle)
df$Subj <- factor(df$Subj)
modelez <- ezANOVA(data <- df, dv = dv, wid = Subj, within = .(Phase, Cycle), between = 
Group, type = 3 )

print(modelez)    #ges = Generalized Eta Square, GGE - Greenhouse and Geisser, HFe = 
Huynh * Feldt)
# I get slightly different values for the Greenhouse & Geisser and the
#Huynh & Feldt correction
GreenBlueBar.gif GreenBlueBar.gif dch: