# How do I set up an unbalanced repeated measures analysis in R?

### Context:

I am analysing some impact assessment data (measuring invertebrate richness in response to pollution), but they are unbalanced - there are not data for every site at sampling occasion, and there were more datapoints recorded after the impact than before the impact.

I am a new user to R, and have gathered through reading on this site and others that the standard anova packages aov() and ezAnova() can't deal with unbalanced designs. I assume I should instead be using a package like lme4.

However, I am not sure how to structure my data, or program the analysis. One of the problems is that I'm not sure how to incorporate sampling dates as the repeated measures aspect of my design.

My data has 5 columns Site code, Date, BeforeAfter, ControlImpact, Richness.

### Questions:

• How should I set up my data for conducting repeated measures analysis with unbalanced data in R?
• Should I use lme4 or some other package?

I believe that your scenario is generally described as one of missing data, not as an unbalanced design, which is usually reserved for cases of unequal numbers of observations between independent groups. ezANOVA() from the ez package can handle unbalanced designs, but cannot handle missing data. lmer() fromthe lme4 package can handle missing data. You can either use lmer() directly, as in:

my_lmer = lmer(
formula = richness ~ (1|site) + BeforeAfter*ControlImpact
, data = my_data_frame
, family = gaussian #or change to whatever model of error is appropriate
)
anova(my_lmer)


Or you could check out the ezMixed function from the ez package, which wraps lmer and produces likelihood ratios (a superior metric of evidence):

my_mix = ezMixed(
data = my_data_frame
, dv = .(richness)
, random = .(site)
, fixed = .(BeforeAfter,ControlImpact)
, family = gaussian
)
print(my_mix\$summary)


Finally, be careful with Date as a predictor; it sounds like it may be confounded with BeforeAfter.