# [R] Repeated measures

Chuck Cleland ccleland at optonline.net
Tue Jan 23 12:52:45 CET 2007

```Richard Plant wrote:
> In the two solutions for the repeated measures problem given in the
> original reply below, the F and p values given by aov() with the error
> strata defined by Error() are different from those given by lme().
> However, when one does the problem "by hand" using the standard split
> plot model, the results agree with those of nlme(). The difference
> between the two aov() solutions is in the partitioning of sums of
> squares. Is there a ready explanation for this discrepancy?

The discrepancy in this case is due to a mistake on my part.  The id
variable should be a factor.

tolerance <-

tolerance.long <- reshape(tolerance,
varying = list(c("tol11","tol12","tol13",
"tol14", "tol15")),
v.names = c("tol"), timevar = "time",
times = 11:15, direction = "long")

tolerance.aov <- aov(tol ~ factor(time) * male + Error(factor(id)),
data = tolerance.long)

summary(tolerance.aov)

Error: factor(id)
Df Sum Sq Mean Sq F value Pr(>F)
male       1 0.3599  0.3599  0.6071 0.4488
Residuals 14 8.2990  0.5928

Error: Within
Df Sum Sq Mean Sq F value   Pr(>F)
factor(time)       4 2.8326  0.7081  5.1309 0.001358 **
factor(time):male  4 0.1869  0.0467  0.3386 0.850786
Residuals         56 7.7289  0.1380
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

library(nlme)

tolerance.lme <- lme(tol ~ as.factor(time) * male, random = ~ 1 | id,
data = tolerance.long)

anova(tolerance.lme)
numDF denDF  F-value p-value
(Intercept)              1    56 353.9049  <.0001
as.factor(time)          4    56   5.1309  0.0014
male                     1    14   0.6071  0.4488
as.factor(time):male     4    56   0.3386  0.8508

Could anyone point me to an example where the variable specified
inside of Error() is not a factor?

> Thanks,
> Richard Plant
>
>> tolerance <- tolerance <-
> +
> txt",
>> tolerance.long <- reshape(tolerance,
> +                           varying = list(c("tol11","tol12","tol13",
> +                                            "tol14", "tol15")),
> +                           v.names = c("tol"), timevar = "time",
> +                           times = 11:15, direction = "long")
>> tolerance.aov2 <- aov(tol ~ factor(male) + factor(male):factor(id) +
> factor(time) + factor(time):male, data = tolerance.long)
>> tolerance.sum <- summary(tolerance.aov2)
>> tolerance.sum
>                         Df Sum Sq Mean Sq F value    Pr(>F)
> factor(male)             1 0.3599  0.3599  2.6077  0.111967
> factor(time)             4 2.8326  0.7081  5.1309  0.001358 **
> factor(male):factor(id) 14 8.2990  0.5928  4.2951 4.295e-05 ***
> factor(time):male        4 0.1869  0.0467  0.3386  0.850786
> Residuals               56 7.7289  0.1380
> ---
> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
>> tolerance.list <- tolerance.sum[[1]]
>> tolerance.mat <- as.matrix(tolerance.list[3])
>> tolerance.F.male <- tolerance.mat[1,1]/tolerance.mat[3,1]
>> tolerance.F.male
> [1] 0.607137
>> tolerance.df <- as.matrix(tolerance.list[1])
>> tolerance.p.male <- 1 -
> pf(tolerance.F.male,tolerance.df[1,1],tolerance.df[3,1])
>> tolerance.p.male
> [1] 0.4488394
>> Message: 68
>> Date: Wed, 17 Jan 2007 05:45:01 -0500
>> From: Chuck Cleland <ccleland at optonline.net>
>> Subject: Re: [R] Repeated measures
>> To: Tom Backer Johnsen <backer at psych.uib.no>
>> Cc: r-help at stat.math.ethz.ch
>> Content-Type: text/plain; charset=ISO-8859-1
>>
>> Tom Backer Johnsen wrote:
>>> I am having a hard time understanding how to perform a "repeated
>>> measures" type of ANOVA with R.  When reading the document found
> here:
>>> http://cran.r-project.org/doc/contrib/Lemon-kickstart/kr_repms.html
>>>
>>> I find that there is a reference to a function make.rm () that is
>>> supposed to rearrange a "one row per person" type of frame to a "one
>>> row per observation" type of frame.  But that function does not seem
>>> to be there.  Nor does the help.search suggest anything.  Is that
>>> function buried in some package?
>>   I'm not able to find that function.  Perhaps that document is out of
>> date.
>>
>>> Is there  some simple documentation that might be useful somewhere?
>>> Starting with a really simple problem (one group, two observations)?
>>   Here is an example showing the use of reshape() and analysis via
> aov()
>> and lme() in the nlme package.
>>
>> tolerance <-
>>
> tx
>> t",
>>
>> tolerance.long <- reshape(tolerance,
>>                           varying = list(c("tol11","tol12","tol13",
>>                                            "tol14", "tol15")),
>>                           v.names = c("tol"), timevar = "time",
>>                           times = 11:15, direction = "long")
>>
>> tolerance.aov <- aov(tol ~ as.factor(time) * male + Error(id),
>>                      data = tolerance.long)
>>
>> summary(tolerance.aov)
>>
>> Error: id
>>      Df   Sum Sq  Mean Sq
>> male  1 0.085168 0.085168
>>
>> Error: Within
>>                      Df  Sum Sq Mean Sq F value  Pr(>F)
>> as.factor(time)       4  2.8326  0.7081  3.0538 0.02236 *
>> male                  1  0.3024  0.3024  1.3039 0.25745
>> as.factor(time):male  4  0.1869  0.0467  0.2015 0.93670
>> Residuals            69 16.0002  0.2319
>> ---
>> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
>>
>> library(nlme)
>>
>> tolerance.lme <- lme(tol ~ as.factor(time) * male, random = ~ 1 | id,
>>                      data = tolerance.long)
>>
>> anova(tolerance.lme)
>>                      numDF denDF  F-value p-value
>> (Intercept)              1    56 353.9049  <.0001
>> as.factor(time)          4    56   5.1309  0.0014
>> male                     1    14   0.6071  0.4488
>> as.factor(time):male     4    56   0.3386  0.8508
>>
>>   RSiteSearch("repeated measures") points to other examples,
> functions,
>> and documentation.
>>
>>> Tom
>>>
>>> ______________________________________________
>>> R-help at stat.math.ethz.ch mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>> guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>> --
>> Chuck Cleland, Ph.D.
>> NDRI, Inc.
>> 71 West 23rd Street, 8th floor
>> New York, NY 10010
>> tel: (212) 845-4495 (Tu, Th)
>> tel: (732) 512-0171 (M, W, F)
>> fax: (917) 438-0894
>>
>>
>>
>> ------------------------------
>>
>> _______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.

--
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894

```