Skip to content

Instantly share code, notes, and snippets.

@marutter
Created April 8, 2011 02:16
Show Gist options
  • Save marutter/909156 to your computer and use it in GitHub Desktop.
Save marutter/909156 to your computer and use it in GitHub Desktop.
# Classic repeated measures example
# Finding the results of a wine tasting
# 3 wines, 22 judges. The judges are the repeated measure
data <- read.csv("wine_taste.csv")
attach(data)
interaction.plot(Wine,Taster,Taste)
#
# Naive assumptions, no repeated measures
#
res <- lm(Taste~Wine)
anova(res)
#
# Mixed effects model
# The judges results are correlated
#
library(lme4)
res.m <- lmer(Taste~Wine+(1|Taster))
summary(res.m)
# Correlatin
.0645/(.0645+.0075)
# ANOVA
anova(res.m)
1-pf(6.2883,2,(66-1)-(22-1)-(3-1))
#
# Note the correction of the df
#
# What are the estimates of each wine
#
res.m2 <- lmer(Taste~Wine-1+(1|Taster))
summary(res.m2)
anova(res.m2) # BAD test
#
# Balanced so find the least significant difference (lsd) value
#
# Error #2: When comparing two levels,use variance of residual only
#
1/sqrt(2)*qtukey(.95,3,42)*sqrt((0.0074513)*(1/22+1/22))
5.54318-5.45909 # Difference
5.54318-5.53409 # No Difference
#
# Example 2: Repeated Measures Two Factor ANOVA
#
data <- read.csv("shoe_sales.csv")
attach(data)
# Correlated by test market, of which there are ten
# Two advertising campaings
# Three time periods
# Two weeks prior
# Two weeks during campaign
# Two weeks after
#
data
campf <- factor(campaign)
timef <- factor(time.period)
res <- lm(sales~campf*timef)
anova(res)
city <- campf:factor(market)
res.m <- lmer(sales~campf*timef+(1|city))
summary(res.m)
anova(res.m)
# Is the interaction significant
1-pf(.5468,2,(30-1)-(10-1)-(2-1)-(3-1)-2)
# Is the time period significant
1-pf(93.6861,2,15)
# Is the campaign significant
1-pf(.6788,1,15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment