#### Working with experimental data - mixed effects models

This course is part of the International Max Planck Research School on Adapting Behavior in a Fundamentally Uncertain World. The target group are students who, due to the interdisciplinary nature of the IMPRS school, do not have any background in statistics
Schedule:
Lecture (daily): 10.-14.8. (for details, see the calendar)
Motivation
Many, if not all of the students in our program will work with experiments. In most experiments we observe the same unit (participant in the experiment, matching group in the experiment) repeatedly. We say, the data is “grouped”. Mixed-effect models are a versatile tool to analyse grouped data.
Topics:
• Introduction
• Revision: Linear models
• Linear fixed effects
• Balanced and unbalanced data
• Linear random effects
• Linear mixed effects
• Nonlinear mixed effects
• Nested factors
• Hypothesis tests
• Correlation structures
• Sample size estimation
Handout
If you want to prepare for the lecture (or revise), you can have look at the handout.
You will find still a lot of mistakes but you might get the idea ;-)
Literature
• Jose C. Pinheiro and Douglas M. Bates, Mixed Effects Models in S and S-Plus. Springer, 2002.
• Julian J. Faraway, Extending the Linear Model with R. Chapman & Hall, 2006.
• John K. Kruschke , Doing Bayesian Data Analysis: A Tutorial with R, JAGS, and Stan. Academic Press, 2nd Edition, 2014.<
• A. C. Davison, D. V. Hinkley, Bootstrap Methods and their Application, Cambridge University Press, 1997
• Bradley Efron and Robert J. Tibshirani, An Introduction to the Bootstrap, Chapman & Hall, 1994
Software
For our practical examples (during the entire course) we will use the software environment R. I think that it is helpful to coordinate on one environment and R has the advantage of being free and rather powerful.

For the Bayesian parts we will use JAGS. It helps if you have installed R and JAGS on your computer when we start the course.

• Documentation for R is provided via the built in help system but also through the R Homepage. Useful are
• The R Guide, Jason Owen (Easy to read, explains R with the help of examples from basic statistics)
• Simple R, John Verzani (Explains R with the help of examples from basic statistics)
• Einführung in R, Günther Sawitzki (In German. Rather compact introduction.)
• Econometrics in R, Grant V. Farnsworth (The introduction to R is rather compact and pragmatic.)
• An Introduction to R, W. N. Venables und D. M. Smith (The focus is more on R as a programming language)
• The R language definition (Concentrates only on R as a programming language.)
• On the JAGS Homepage you go to the files pages, then to Manuals, to find the JAGS user manual.
• We will use the following packages: `boot, car, Ecdat, ellipse, foreign, geepack, Hmisc, lattice, latticeExtra, lme4, lmtest, MASS, nlme, nnet, SASmixed, survival`. If, e.g., the command `library(Ecdat)` generates an error message (`Error in library(Ecdat): There is no package called 'Ecdat'`), you have to install the package.
Installing packages with Microsoft Windows:
Start `Rgui.exe` and install packages from the menu `Packages / Install Packages`).
Installing packages from advanced operating systems:
From within R use the command `install.packages("Ecdat")`, e.g., to install the package `Ecdat`
• In the lecture we will use RStudio as a front end.
• It will help if you can bring your own portable computer to the classes and exercises. You should have an up-to-date version of R already installed. You should also have the following libraries already installed: `car, Ecdat, Hmisc, nlme, lme4`. You should have set up your computer such that you can work with R.
• You might also find make and Emacs useful tools.
Handout + Exam
I have put a preliminary version of the handout on the net. The data we use for the examples is attached to the handout. Use `pdftk` or your PDF viewer to extract the data. If you use Adobe Acrobat and you have difficulties saving attachments from PDF files look here.

Please solve the exam between 15:00 on 14th August until 15:00 on 15th August. You will need the data in R format or in Stata format to solve the exam.

For those who are interested in the commands I used in today's exercise:

```with(exe2,{
print(length(unique(player)))
print(table(player,treatment))
print(table(treatment))
})
exe2 <- within(exe2,t<-as.factor(treatment))
lmer(y ~ t -1 + period + (1|player),data=exe2)
```
And here are some commands that you could have used in the exam. Let us start with a colorful boxplot (not really part of the exam):
```with(exam,boxplot(effort ~ Treatment,col=levels(as.factor(Treatment))))
```
Here is the serious stuff:
```summary(lm (effort ~ Treatment - 1,data=exam))
```
If we are interested in the red treatment, we would rather say
```summary(lm (effort ~ (Treatment=="red"),data=exam))
```
In the lecture I forgot to mention that `geeglm` needs ordered data (and since I gave you unordered data, in your estimates you got too many clusters):
```exam <- exam[with(exam,order(matchingGroup,playerid)),]
summary(r.cluster <- geeglm (effort ~ Treatment - 1,
id=matchingGroup,data=exam))

(lmer.0 <- lmer(effort ~ Treatment - 1 + (1|matchingGroup) +
(1|playerid),data=exam))
```
When I asked for a random effect also for treatments (and not only for the intercept) some of you then dropped the fixed effect for `Treatment` entirely. That was not the idea. What I had in mind was the following:
```(lmer.T <- lmer(effort ~ Treatment - 1 + (1+Treatment|matchingGroup) +
(1+Treatment|playerid),data=exam))

anova(lmer.T,lmer.0)
```
Here you could explain that `anova` is anticonservative and that one should better use a permutation test.
```hausman(lm(effort ~ Treatment*matchingGroup +
Treatment*playerid,data=exam),lmer.T)
```