# mean of repeated measures...spot the (matlab) error

I have a population of 114 subjects. For each subject I have repeated measurements (1 each day) of the variable assessed (activity level). The number of measures varies for each subject (number of assessed days) and the median number of measures for each subject is 4.

Can I simply take the average of the measures to continue with my statistical analysis of the population, i.e. each subject will be represented by the average measure across the assessed days. Should I perform a statistical test before doing that? which one?

Could you please suggest the workflow? My purpose is to find whether there are differences in the variable measured between group of subjects (lets say first 57 subjects are groupA and last 57 subjects are group B).

My attempt (results are obtained using MATLAB, but should be interpretable):

• I fit a repeated model of the type 't1-t5 ~ Group' meaning that measurements t1-t5 are the responses and the groups is the predictor variable

Time = [1 2 3 4 5]' %array contaning the day of assessment

rm = fitrm(table,'t1-t5 ~ Group','WithinDesign',Time) %table is a matlab table contaning the data

the coefficients of the models are

                   t1         t2         t3         t4        t5
_______    _______    _______    ______    _______

(Intercept)       425.64     412.64     425.64    414.05     443.64
Group_A          -52.182    -12.636    -23.182     -47.5    -45.455

• I check Mauchly's test for sphericity

tbl = mauchly(rm) % if small use the correction when compute p value in ranova

tbl =

   W       ChiStat    DF    pValue
_______    _______    __    _______

0.84303    3.1446     9     0.95828


p value is > than 0.05 so I do not have to correct the repeated ANOVA

• I run repeated ANOVA

ranovatbl = ranova(rm)

ranovatbl =

                      SumSq       DF    MeanSq       F       pValue     pValueGG    pValueHF    pValueLB
__________    __    ______    _______    _______    ________    ________    ________

(Intercept):Time         13611     4    3402.6    0.98232    0.42203    0.41863     0.42203     0.33347
Group:Time               26256     4    6564.1      1.895    0.11938    0.12457     0.11938     0.18386
Error(Time)         2.7711e+05    80    3463.9


It tells me that there are no effect of days in the activity level, and there are no differences between groups.

• I compute the marginal means

M = margmean(rm,{'Group'})

 Group      Mean     StdErr    Lower     Upper
_______    ______    ______    ______    ______

A          388.13    18.68     349.16    427.09
B          460.51    18.68     421.54    499.47

• It is not a good idea to use the mean for each day, because these means are computed with a different number of terms. Probably you should look at mixed effect models, see stats.stackexchange.com/questions/166434/…
– user83346
Aug 28, 2015 at 9:57
• Thanks. Is repeated ANOVA OK? Should I test any assumption before using it? Thanks a lot Aug 28, 2015 at 10:01
• Well I don't know mathlab and without data it is not easy to tell how to proceed in detail but it sounds as if you want to estimate something like $y=\beta_1 + \beta_2 G + \epsilon$ where $y$ is the number of steps, $G$ the group. The problem you have is that your var-covar matrix of $\epsilon$ is not diagonal because of the repeated measures, so you have to use Generalised Least Squares (GLS) to estimate it, using several var-covar patterns (unstructured, compound symmetry, ...) and decide which one fits best (with likelihood ratio test). (see the reference in the answer on the patients)
– user83346
Aug 28, 2015 at 10:16
• Hi, according to the ANOVA for repeated measurements (see question) I think that I can say that there is not significant effect of the repeated measurements (time) on the measured variables and on the groups...maybe then I can take the mean right? Aug 28, 2015 at 11:35
• Sorry but without data it is hard to tell whether this is 'right' or 'wrong' or whether one 'can do' this, I don't know which assumptions have been made (e.g. for the var-covar structure) but as far as I am concerned you are free to 'take' whatever you want :-)
– user83346
Aug 28, 2015 at 11:53

I imported your dataset, converted it to long form, and ran a mixed model in R. I used the packages lme4 and lmerTest:

M1 <- lmer(outcome ~ group + time + (1|ID), data=cv.long)


As you can see, I included time as a linear independent variable and ID (person ID) as a random constant. This gives an estimate of 25.266 for belonging to group B, and std. error 12.328. P = 0.0427, so this model supports a slight difference between the groups.

I loaded the data to SPSS, converted the variables that had NaN's to numeric variables, and saved the data to a .sav file. I also changed group A to 1 and group B to 2. I then applied the following code in R:

library(foreign)
library(lme4)
library(lmerTest)
cv.data <- read.spss("/Users/jonasberge/Dropbox/R/crossvalidated_data.sav",  to.data.frame = T)
cv.long <- reshape(cv.data,
varying = c("t1", "t2", "t3", "t4", "t5"),
v.names = "outcome",
timevar = "time",
times = c(1, 2, 3, 4, 5),
direction = "long")
M1 <- lmer(outcome ~ Group + time + (1|id), data=cv.long)
summary(M1)


This gives the following output:

Linear mixed model fit by REML
t-tests use  Satterthwaite approximations to degrees of freedom ['lmerMod']
Formula: outcome ~ Group + time + (1 | id)
Data: cv.long

REML criterion at convergence: 5376.7

Scaled residuals:
Min      1Q  Median      3Q     Max
-3.3032 -0.5652  0.0447  0.5502  4.6394

Random effects:
Groups   Name        Variance Std.Dev.
id       (Intercept) 3213     56.69
Residual             4482     66.95
Number of obs: 466, groups:  ID, 114

Fixed effects:
Estimate Std. Error      df t value Pr(>|t|)
(Intercept)  401.707     11.078 252.600  36.263   <2e-16 ***
Group2        25.266     12.328 112.200   2.049   0.0427 *
time           1.421      2.610 361.300   0.545   0.5864
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Correlation of Fixed Effects:
(Intr) group2
Group2 -0.572
time   -0.613  0.019