# [R] repeated measures regression

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Thu May 24 07:08:28 CEST 2007

```> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of John Christie
> Sent: Wednesday, May 23, 2007 6:09 PM
> To: Bert Gunter
> Cc: R-help at stat.math.ethz.ch
> Subject: Re: [R] repeated measures regression
>
> Hmmm, been away and got this...  I appreciate the effort but there
> wasn't anything, in principle, in MASS on this I didn't already
> know.  My question is just more about the functioning of the lm
> command and deriving these values.  I understand that its the wrong
> approach for repeated measures design and lme is more appropriate.
> But, I wanted to examine / compare.  So, my question still stands.
> How does one get something like the subject x effect
> interaction term
> from lm?

If you just want to piece together a repeated measures analysis from lm(), it is certainly possible.  Say you have 10 subjects, each measured under 2 conditions.  You could do something like this:

#create sample data
y <- sample(1:10, 20, replace=TRUE) #response variable
subj <- as.factor(c(1:10, 1:10)) #subject id
condition <- as.factor(c(rep(1,10),rep(2,10))) #experimental condition
test.dat <- as.data.frame(cbind(y, subj, condition))
test.dat

#model response as a function of subj and condition
test.lm <- lm(y ~ as.factor(subj) + as.factor(condition), data=test.dat)
summary(test.lm)
anova(test.lm)

If you look at the anova() output, you will have sums of squares for subj, condition and residuals.  For this simple and balanced example, Residuals is the subj x condition interaction which can be used as the error term for testing the condition effect.  But as has been pointed out, there are better and easier ways to analyze repeated measures, especially as the designs get more complex.