% \documentclass[serif]{beamer} % Serif for Computer Modern math font.
\documentclass[serif, handout]{beamer} % Handout to ignore pause statements

\hypersetup{colorlinks,linkcolor=,urlcolor=red}


\usefonttheme{serif} % Looks like Computer Modern for non-math text -- nice!
\setbeamertemplate{navigation symbols}{} % Suppress navigation symbols
\usetheme{AnnArbor}  % CambridgeUS
% \usetheme{Frankfurt}  % Displays section titles on top: Fairly thin but still swallows some material at bottom of crowded slides
% \usetheme{Berlin} % Displays sections on top
% \usetheme{Berkeley}

\usepackage[english]{babel}
\usepackage{amsmath} % for binom
% \usepackage{graphicx} % To include pdf files!
% \definecolor{links}{HTML}{2A1B81}
% \definecolor{links}{red}
\setbeamertemplate{footline}[frame number] 

\mode<presentation>

\title{Mixed models for the analysis of repeated measurements\footnote{ This slide show is an open-source document. See last slide for copyright information.}}
\subtitle{Jerry Brunner, Department of Statistical Sciences}
\date{} % To suppress date

\begin{document}

\begin{frame}
  \titlepage
\end{frame}

\begin{frame}
\frametitle{The Role  of Statistics in the Discourse of Science} \pause
%\framesubtitle{} 
\begin{itemize}
    \item The Hidden Advisor study (Garfinkel, 1967). \pause
    \item Skeptic in a box.
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{The skeptic says} \pause
%\framesubtitle{} 
\begin{itemize}
    \item Maybe this is coincidence.  \pause
    \item Under the following reasonable circumstances, what are the chances of getting results like these (or even stronger) if the treatment actually had no effect?
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{For example}
%\framesubtitle{} 
\begin{itemize}
    \item You claim that it takes longer for Spanish-speaking children to learn English. \pause
    \item You have data in which the mean length of time required for a set of Russian-speaking children to reach a certain level of proficiency was shorter than the mean for a set of Spanish-speaking children. \pause
    \item Well, what if these were independent random samples from normal populations with exactly the same means, but possibly different variances? \pause
    \item What are the chances of observing a difference as large as or larger than the one you observed? \pause
    \item If the probability is small enough, perhaps coincidence can be ruled out. \pause           
    \item And we can discuss this.
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{They always say}
%\framesubtitle{} 
\begin{itemize}
    \item It's good to think about the statistical analysis as you are designing a study. \pause
    \item They're right.  \pause
    \item But it can be taken too far.
\end{itemize}
\end{frame}


\begin{frame}
\frametitle{Statistics should not be a Procrustean Bed}
%\framesubtitle{Payback's a bitch} 
\begin{center}
\includegraphics[width=3in]{procrustes}
\end{center}
% Procrustes was a son of Poseidon. Theseus killed him by fitting him to his own bed. 
\end{frame}

\begin{frame}
\frametitle{Mixed models for the analysis of repeated measurements}  \pause
%\framesubtitle{} 
\begin{itemize}
    \item First, repeated measurements.  \pause
    \item Are we really shorter in the evening? \pause
    \item Measure a set of people in the morning and the evening. \pause
    \item Well, what if these measurements were independent random samples from normal populations with exactly the same means \ldots \pause
    \item Wait, hold it! 
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{The main benefits of repeated measurement analysis}
\framesubtitle{From a statistical point of view}   \pause
\begin{itemize}
    \item Increased statistical power \pause for a given sample size.\pause
    \item Each participant serves as her own control.\pause
    \item Person-to-person variation cancels out. \pause
    \item Signal is more detectable against a lower level of background noise. 
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Random effects vs. fixed effects} \pause
%\framesubtitle{} 
A random factor is one in which the values of the factor are a random sample from a populations of values. \pause
  \begin{itemize}
    \item Randomly select 20 fast food outlets, survey customers in each about quality of the fries. Outlet is a random effects factor with 20 values.  \pause Amount of salt could be a fixed effects factor. \pause
    \item Randomly select 10 schools, test students at each school. School is a random effects factor with 10 values. \pause
    \item Randomly select 15 naturopathic medicines for arthritis (there are quite a few), and then randomly assign arthritis patients to try them. Drug is a random effects factor. \pause
    \item Randomly select 15 lakes. In each lake, measure how clear the water is at 20 randomly chosen points. Lake is a random effects factor.
  \end{itemize}
\end{frame}

\begin{frame}
\frametitle{Mixed models}
%\framesubtitle{} 
{\LARGE
\begin{center}
Mixed models have both fixed and random effects. 
\end{center} 
} % End size
\end{frame}

\begin{frame}
\frametitle{Random shocks}
\framesubtitle{A nice simple example}  \pause
  \begin{itemize}
    \item Randomly select 5 farms. \pause
    \item Randomly select 10 cows from each farm. \pause
    \item Randomly select 5 to have free access to pasture, and 5 to be kept in the barn. \pause
    \item Record the amount of milk from each cow. \pause
    \item Farm is a random factor, access to pasture is fixed. \pause
  \end{itemize}
  
The idea is that ``Farm" is a kind of random shock that pushes all the amounts of milk in a particular farm up or down by the same amount.  \pause

It's considered random because the farms were randomly selected.
\end{frame}

\begin{frame}
\frametitle{Classical $F$-tests} 
\framesubtitle{For mixed models and pure random effects models}   \pause
\begin{itemize}
    \item Data are normal, including the unobservable random shocks. \pause
    \item Design is balanced, meaning equal or proportional sample sizes. \pause
    \item Then exact $F$-tests are usually possible. \pause
    \item Tests for random effects are asking whether the variance of the (normal) random shock is zero. \pause
    \item Balance matters. \pause
    \item Normality matters.
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Repeated measures}
  \begin{itemize}
    \item An individual is tested under more than one condition, and contributes a response in each treatment condition. \pause
    \item \textbf{One can view ``subject" as just another random effects factor, because subjects supposedly were randomly sampled.} \pause
    \item Subject would be nested within sex, but might cross stimulus intensity. \pause
    \item Sex and simulus intensity are fixed effects. \pause
    \item There might be other random effects in the model, like item. \pause
    \item This is the classical way to analyze repeated measures. \pause
    \item It's not the only way. \pause
    \item It implies equal correlations between measurements from the same individuals. \pause This is unrealistic for some data sets. \pause
    \item But it generalizes naturally to models for categorical responses.
  \end{itemize}
\end{frame}

\begin{frame}
\frametitle{Random intercepts} \pause
\framesubtitle{This is all multiple regression under the surface} 
\begin{center}
\includegraphics[width=3in]{randomintercepts}
\end{center}
\end{frame}

\begin{frame}
\frametitle{Dichotic listening study} \pause
%\framesubtitle{} 
 
\begin{itemize}
    \item Left-handed and right-handed subjects push a key when they hear their names over background noise. \pause
    \item They are wearing stereo headphones.  \pause
    \item Signal comes in the left ear, the right ear, or both. \pause
    \item 50 trials in each condition, in random order. \pause
    \item Dependent variable is median reaction time in milliseconds. \pause
    \item Conceptually, there are two factors: \pause handedness and ear. \pause
    \item Repeated measures on ear. \pause
    \item Technically there are three factors: \pause handedness, ear and subject. \pause
    \item Subject is a random effect \pause that crosses ear and is nested within handedness.
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Treatment Means}
%\framesubtitle{} 

\begin{center}
\begin{tabular}{c|c|c|c||c} %\hline
      & \multicolumn{3}{c||}{\textbf{Presentation}} & \\ \hline
\textbf{Handedness} & Both ears & Left ear & Right ear  & \\ \hline
Left       & 317.90 & 326.60 & 332.80 & 325.77 \\ \hline
Right      & 315.10 & 324.65 & 320.85 & 320.20 \\ \hline\hline
           & 316.50 & 325.62 & 326.82 & 322.98 \\ 
% \hline
\end{tabular}\end{center} \pause
Vocabulary \pause
\begin{itemize}
    \item Marginal means
    \item Main effects
    \item Interaction
\end{itemize}

\end{frame}

\begin{frame}
\frametitle{Interaction Plot}
%\framesubtitle{} 
\begin{center}
\includegraphics[width=3.1in]{InteractionPlot}
\end{center}
\end{frame}


\begin{frame}[fragile]
\frametitle{Another example (Baayen, Davidson and Bates , 2008)}
\framesubtitle{Treatment is SOA} \pause
{\scriptsize
\begin{verbatim}
Subject   Item    Treatment   ReactionTime
s1          w1      Long        466 
s1          w2      Long        520 
s1          w3      Long        502 
s1          w1      Short       475 
s1          w2      Short       494 
s1          w3      Short       490 
s2          w1      Long        516 
s2          w2      Long        566 
s2          w3      Long        577 
s2          w1      Short       491 
s2          w2      Short       544 
s2          w3      Short       526 
s3          w1      Long        484 
s3          w2      Long        529 
s3          w3      Long        539 
s3          w1      Short       470 
s3          w2      Short       511 
s3          w3      Short       528
\end{verbatim} 
} % End size
\end{frame}

\begin{frame}
\frametitle{The modern approach}
\framesubtitle{For unbalanced designs}  \pause
\begin{itemize}
    \item Estimate the parameters with Restricted Maximum Likelihood (REML)   \pause
    \item Exact $F$-tests are still out of reach for unbalanced designs.  \pause
    \item There are good approximations for testing the fixed effects.  \pause
    \item Satterthwaite adjustment to $df$.  \pause
    \item So-called $F$ statistics reduce to the classical $F$s when the design is balanced. \pause
    \item Inference for the random effects is a challenge. \pause
    \item Not an issue for repeated measures analysis.
\end{itemize}
\end{frame}



\begin{frame}
\frametitle{References}
%\framesubtitle{} 
\begin{itemize}
    \item[] Garfinkel, H. (1967). \emph{Studies in Ethnomethodology}. Englewood Cliffs, New Jersey: Prentice-Hall. See pp. 79-94
    \item[] 
    \item[] Baayen, R.H., Davidson, D.J.~and Bates, D.M.~(2008). Mixed-effects modeling with crossed random effects for subjects and items. \emph{Journal of Memory and Language}, 59, 390-412.
    
\end{itemize}
\end{frame}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\begin{frame}
\frametitle{Copyright Information}

This slide show was prepared by  \href{http://www.utstat.toronto.edu/~brunner}{Jerry Brunner},
Department of Statistical Sciences, University of Toronto. Except for the picture of Procrustes, which I took from the Internet, it is licensed under a 
\href{http://creativecommons.org/licenses/by-sa/3.0/deed.en_US}
     {Creative Commons Attribution - ShareAlike 3.0 Unported License}. Use any part of it as you like and share the result freely. The \LaTeX~source code is available from 
\href{http://www.utstat.toronto.edu/~brunner/workshops/mixed} {\footnotesize \texttt{http://www.utstat.toronto.edu/$^\sim$brunner/workshops/mixed}}

\end{frame}


\end{document}


# R work for random intercept picture
n = 20; alpha1 = 5; alpha2 = 7; beta = 1; sigma=1
set.seed(9999)

x = runif(n,0,10)
y  = alpha1 + beta*x + rnorm(n,0,sigma)
y2 = alpha2 + beta*x + rnorm(n,0,sigma)

plot(x,y, col = 'red', ylim = c(3,18))
points(x,y2, col = 'blue')
# Add lines to the plot
xx = c(0,10); yy1 = alpha1 + beta*xx; yy2 = alpha2 + beta*xx
lines(xx,yy1,lty=1, col = 'red')
lines(xx,yy2,lty=1, col = 'blue')





