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

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

% Uncomment next 2 lines instead of the first for article-style handout:
%   \documentclass[12pt]{article}
%   \usepackage{beamerarticle}
% \usefonttheme{serif} % Looks like Computer Modern for non-math text -- nice!
\setbeamertemplate{navigation symbols}{} % Supress navigation symbols at bottom
% \usetheme{Berlin} % Displays sections on top
% \usetheme{Warsaw} % Displays sections on top
\usetheme{Frankfurt}  % Displays sections on top: Fairly thin but swallows some material at bottom of crowded slides
% \usetheme{AnnArbor}  % CambridgeUS
\usepackage[english]{babel}
\usepackage{graphicx}%
\usepackage{graphpap} % Graph paper for pictures.
\setbeamertemplate{footline}[frame number] 

\mode<presentation>
% \mode<handout>{\setbeamercolor{background canvas}{bg=black!5}}


\title{Within Cases ANOVA Part One\footnote{See last slide for copyright information.}}
\subtitle{STA441 Spring 2016} % (optional)
\date{} % To suppress date


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

%\begin{frame}
%\frametitle{Overview}
%\tableofcontents
%\end{frame}

\section{Overview}

\begin{frame}
\frametitle{Within Cases} 
Example: A random sample of judges each tastes 4 wines and rates the flavour. Explanatory variable is type of wine. \pause
\begin{itemize}
     \item A case contributes a response variable value for more than one value of a categorical explanatory variable \pause --- usually all of them. \pause
     \item It is natural to expect data from the same case to be correlated -- \emph{not} independent. \pause
     \item For example, the same subject appears in several treatment conditions. \pause
     \item Hearing study: How does pitch affect our ability to hear faint sounds? The same subjects will hear a variety of different pitch and volume levels (in a random order). They press a key when they think they hear something.
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Student's Sleep Study (\emph{Biometrika}, 1908)}
\framesubtitle{First Published Example of a $t$-test} \pause

  \begin{itemize}
    \item Patients take two sleeping medicines several days apart. \pause
    \item Half get $A$ first, half get $B$ first. \pause
    \item Reported extra hours of sleep are recorded (difference from baseline). \pause
    \item It's natural to subtract, and test whether the mean \emph{difference} equals zero. \pause
    \item That's what Gossett did. \pause
    \item But some might do an independent $t$-test with $n_1=n_2$. \pause
    \item It's wrong, but is it harmful?
  \end{itemize}
\end{frame}

\begin{frame}
\frametitle{Matched pairs, testing $H_0: \mu_1=\mu_2$} {Independent \emph{v.s.} Matched  $t$-test} \pause
\begin{itemize} 
     \item If population covariance between the two measurements is positive, Type I error probability of both tests is 0.05, but matched $t$-test has better power. \pause
        \begin{itemize}
            \item Each case serves as its own control. \pause
            \item Many unknown influences are removed by subtraction. \pause
            \item This makes the analysis more precise. \pause
        \end{itemize}
     \item If population covariance between measurements is \emph{negative}, independent $t$-test has Type I error probability greater than 0.05.  \pause
        \begin{itemize}
            \item Matched $t$-test still has the correct Type I error probability. \pause
             \item Negative covariance is unlikely to happen in most real situations.
        \end{itemize}
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Within-cases Terminology}
You may hear terms like \vspace{5mm} \pause
\begin{itemize}
     \item \textbf{Longitudinal}: \pause The same variables are measured repeatedly over time. \pause Usually there are lots of variables, including categorical ones, and large samples. \pause If there's an experimental treatment, itÕs usually once at the beginning, like a surgery. \pause Longitudinal studies basically track what happens over time. \pause

     \item \textbf{Repeated measures}: \pause Usually, the same subjects experience two or more experimental treatments. \pause Usually quantitative response variables, and often small samples.

\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Archery Example: Bow and Arrow} 
\framesubtitle{Two within-cases factors} \pause
\begin{itemize}
     \item Cases are archers. There are $n$ archers. \pause
     \item Test two bows, three arrow types. \pause
     \item Warmup, then each archer takes 10 shots with each Bow-Arrow combination --- 60 shots. \pause
     \item In a different random order for each archer, of course. \pause
     \item $Y_{i,1}, \ldots, Y_{i,6}$ are mean distances from arrow tip to centre of target, for $i=1, \ldots, n$. \pause
     \item Each $Y_{i,j}$ is based on 10 shots. \pause
     \item $E(Y_{i,j})=\mu_j$ for  $j=1,\ldots,6$.
\end{itemize}
\end{frame}

\begin{frame}{One Between, One Within} \pause
\begin{itemize}
     \item Grapefruit study \pause
     \item Within stores factor: Three price levels \pause
     \item Between-stores factor: Incentive program for produce managers (Yes-No)
\end{itemize}
\end{frame}

\begin{frame}{Monkey Study} \pause
\begin{itemize}
     \item Train monkeys on discrimination tasks, at 16, 12, 8, 4 and 2 weeks prior to treatment. \pause Different task each time, equally difficult (randomize order). \pause
     \item Treatment is to block function of the hippocampus (with drug, not surgery), \pause re-tested. Get 5 scores for each monkey.
\begin{center}
\includegraphics[width=4in]{Timeline}
\end{center} \pause
     \item 11 randomly assigned to treatment, 7 to control \pause
     \item Treatment is between, time elapsed since training is within.
\end{itemize}

\end{frame}



\begin{frame}{Advantages of Within-cases Designs}{If measurement of the response variable does not mess things up too much} \pause
\begin{itemize}
     \item Convenience (sometimes) \pause
     \item Each case serves as its own control. A huge number of extraneous variables are automatically held constant. The result can be a very sensitive analysis. \pause
     \item For some models, you can have lots of measurements on just a few subjects \pause --- if you are willing to make some assumptions.
\end{itemize}
\end{frame}

\begin{frame}{Three Main Approaches} {For normal response variables} \pause
\begin{itemize}
     \item Classical Mixed model
     \item Multivariate
     \item Covariance Structure
\end{itemize}
\end{frame}

\begin{frame}{Classical Mixed Model Approach}
\begin{itemize}
     \item ``Case" (or Subject) is one of the factors. \pause
     \item Case is a \emph{random effects} factor \pause that is \emph{nested} within combinations of the between-cases factors, \pause and \emph{crosses} the within-cases factors. \pause
     \item Uses a mixed model ANOVA. \pause
     \item $F$-tests depend on balanced experimental designs. 
%     \item Can also do it with the covariance structure approach, and don't need balance.
\end{itemize}
\end{frame}

\begin{frame}{Multivariate Approach}
\begin{itemize}
     \item Multivariate methods allow the analysis of multiple response variables at the same time. \pause
     %\item When a case (subject) provides data under more than one set of conditions, it is natural to think of the measurements as multiple response variables.
     \item The humble matched $t$-test has a multivariate version (Hotelling's $t$-squared). \pause
     \item Simultaneously test whether the means of several \emph{differences} equal zero. \pause
     \item Like rating of Wine One minus Wine Two, Wine Two minus Wine Three, and Wine Three minus Wine Four. \pause
     \item When there are also between-subjects factors (like nationality of judge), use multivariate regression methods. \pause
     \item It's very attractive, but applies mostly to the normal case.  \pause
     \item The covariance structure approach is limited to the normal case too, but is more versatile. \pause
     \item More on the covariance structure approach later.
\end{itemize}
\end{frame}

\section{Classical Mixed Model Approach}

\begin{frame}
\frametitle{Classical Mixed Model Approach}
\framesubtitle{Repeating \dots} \pause
\begin{itemize}
     \item Case (or Subject) is one of the factors.
     \item Case is a \emph{random effects} factor, because cases are assumed to be a randdom sample.
     \item Case is \emph{nested} within combinations of the between-cases factors.
     \item Case \emph{crosses} the within-cases factors.
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{No interactions of cases with other factors}
\framesubtitle{A technical issue}  \pause
\begin{itemize}
    \item Cases (subjects) is a random effects factor.  \pause
    \item Models almost never include interactions between cases and other factors. \pause
    \item This may not be realistic. \pause
    \item Why assume it? \pause
    \item Because with all possible interactions, $SSE=0$ and $n-p=0$ (details omitted).
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Pictures of crossing and nesting}
%\framesubtitle{} 
Cases (subjects) is a random effects factor nested within combinations of the between-cases factors and crossing the within-cases factors. \pause
\begin{itemize}
    \item Recall the archery example -- two bow types, three arrow types. \pause
    \item Suppose each archer only used one type of bow and one type of arrow.  \pause
    \item Make a diagram showing the nesting/crossing of cases.
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Both Factors between}
\framesubtitle{Make a diagram showing the nesting/crossing of cases.} 
\begin{itemize}
    \item Each archer only uses one type of bow and one type of arrow.
    \item Both factors are between cases. \pause
    \item Cases are nested within both bow and arrow.
\end{itemize} \pause

\vspace{10mm}

\begin{picture}(100,100)(-70,0) \thicklines
%\graphpaper(0,0)(210,100) % Need \usepackage{graphpap}
% Draw the cells
        \put (0,0){\line(1,0){210}}   % Bottom
        \put (0,50){\line(1,0){210}}  % Middle horizontal 
        \put (0,100){\line(1,0){210}} % Top
        \put (0,0){\line(0,1){100}}   % Left
        \put (70,0){\line(0,1){100}}   % One-third 
        \put (140,0){\line(0,1){100}}   % Two-thirds
        \put (210,0){\line(0,1){100}}   % Right
        \put (-30,50){Bow}
        \put(90,110){Arrow}
% Draw the ellipses
    {\color{red}
        \put(35,75){\oval(50,30)}  \put(105,75){\oval(50,30)}   \put(175,75){\oval(50,30)}
        \put(35,25){\oval(50,30)}  \put(105,25){\oval(50,30)}   \put(175,25){\oval(50,30)}
    } % End color
\end{picture}
\end{frame}

\begin{frame}
\frametitle{One factor between and one within}
\framesubtitle{Make a diagram showing the nesting/crossing of cases.}  \pause
\begin{itemize}
    \item Suppose each archer only uses one type of bow, but all 3 types of arrow. \pause
    \item Bow is between cases, arrow is within (repeated measures on arrow). \pause
    \item Cases are nested within bow, but cross arrow. \pause
\end{itemize}

\vspace{10mm}

\begin{picture}(100,100)(-70,0) \thicklines
%\graphpaper(0,0)(210,100) % Need \usepackage{graphpap}
% Draw the cells
        \put (0,0){\line(1,0){210}}   % Bottom
        \put (0,50){\line(1,0){210}}  % Middle horizontal 
        \put (0,100){\line(1,0){210}} % Top
        \put (0,0){\line(0,1){100}}   % Left
        \put (70,0){\line(0,1){100}}   % One-third 
        \put (140,0){\line(0,1){100}}   % Two-thirds
        \put (210,0){\line(0,1){100}}   % Right
        \put (-30,50){Bow}
        \put(90,110){Arrow}
% Draw the ellipses
    {\color{red}
        \put(105,75){\oval(190,30)}
        \put(105,25){\oval(190,30)}
    } % End color
\end{picture}
\end{frame}

\begin{frame}
\frametitle{Another one factor between and one within} \pause
\framesubtitle{Make a diagram showing the nesting/crossing of cases.} 
\begin{itemize}
    \item Suppose each archer uses both types of bow, but only one type of arrow. \pause
    \item Bow is within cases, Arrow is between (repeated measures on Bow). \pause
    \item Cases are nested within Arrow, but cross Bow. \pause
\end{itemize}

\vspace{10mm}

\begin{picture}(100,100)(-70,0) \thicklines
%\graphpaper(0,0)(210,100) % Need \usepackage{graphpap}
% Draw the cells
        \put (0,0){\line(1,0){210}}   % Bottom
        \put (0,50){\line(1,0){210}}  % Middle horizontal 
        \put (0,100){\line(1,0){210}} % Top
        \put (0,0){\line(0,1){100}}   % Left
        \put (70,0){\line(0,1){100}}   % One-third 
        \put (140,0){\line(0,1){100}}   % Two-thirds
        \put (210,0){\line(0,1){100}}   % Right
        \put (-30,50){Bow}
        \put(90,110){Arrow}
% Draw the ellipses
    {\color{red}
        \put(35,50){\oval(30,80)}  \put(105,50){\oval(30,80)}   \put(175,50){\oval(30,80)}
    } % End color
\end{picture}
\end{frame}

\begin{frame}
\frametitle{Both factors within}
\framesubtitle{As in the original example}  \pause
\begin{itemize}
    \item Each archer uses both types of bow and all three types of arrow. \pause
    \item Both factors are within cases (repeated measures on both Bow and Arrow). \pause
    \item Cases cross both Bow and Arrow. \pause
\end{itemize}

\vspace{10mm}

\begin{picture}(100,100)(-70,0) \thicklines 
%\graphpaper(0,0)(210,100) % Need \usepackage{graphpap}
% Draw the cells
        \put (0,0){\line(1,0){210}}   % Bottom
        \put (0,50){\line(1,0){210}}  % Middle horizontal 
        \put (0,100){\line(1,0){210}} % Top
        \put (0,0){\line(0,1){100}}   % Left
        \put (70,0){\line(0,1){100}}   % One-third 
        \put (140,0){\line(0,1){100}}   % Two-thirds
        \put (210,0){\line(0,1){100}}   % Right
        \put (-30,50){Bow}
        \put(90,110){Arrow}
% Draw the ellipses
        {\color{red} \put(105,50){\oval(190,55)}  }
\end{picture}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}
\frametitle{One More Example}
\framesubtitle{Without a picture}  \pause
\begin{itemize}
    \item Experienced archers and beginners try both bows and all three arrow types.
    \item Experience is between cases, Bow and Arrow are within. \pause
    \item Cases are nested within experience. \pause
\end{itemize}

\vspace{10mm}
You draw the picture.
\end{frame}


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


\begin{frame}
\frametitle{Copyright Information}

This slide show was prepared by  \href{http://www.utstat.toronto.edu/~brunner}{Jerry Brunner},
Department of Statistics, University of Toronto. 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 the course website:
\href{http://www.utstat.toronto.edu/~brunner/oldclass/441s16} {\small\texttt{http://www.utstat.toronto.edu/$^\sim$brunner/oldclass/441s16}}
\end{frame}

\end{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}
\frametitle{One More Example}
\framesubtitle{Without a picture}  \pause
\begin{itemize}
    \item Experienced archers and beginners try both bows and all three arrow types.
    \item Experience is between cases, Bow and Arrow are within. \pause
    \item Cases are nested within experience. \pause
\end{itemize}

\vspace{10mm}

\newsavebox{twoway}
\savebox{\twoway}(50,25){
\begin{picture}(100,100)(-70,0) \thicklines 
%\graphpaper(0,0)(210,100) % Need \usepackage{graphpap}
% Draw the cells
        \put (0,0){\line(1,0){210}}   % Bottom
        \put (0,50){\line(1,0){210}}  % Middle horizontal 
        \put (0,100){\line(1,0){210}} % Top
        \put (0,0){\line(0,1){100}}   % Left
        \put (70,0){\line(0,1){100}}   % One-third 
        \put (140,0){\line(0,1){100}}   % Two-thirds
        \put (210,0){\line(0,1){100}}   % Right
        \put (-30,50){Bow}
        \put(90,110){Arrow}
% Draw the ellipses
        {\color{red} \put(105,50){\oval(190,55)}  }

\end{picture}
} % End of saved box?
\end{frame}


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


\begin{frame}
\frametitle{Degrees of freedom}
%\framesubtitle{I cut out this detail, explaining why no interaction with subjects} 
\begin{itemize}
    \item First note that while the usual between-cases factorial ANOVA model has all possible interactions, most regression models do not.
    \item Theorem: For any factorial design, a regression model with all the main effects and interactions has a number of $\beta$ coefficients $p$ equal to the number of treatment combinations.    
    \item[]
    \item In the mixed model approach to repeated measures, $n$ is no longer the number of cases.
    \item It's the number of response variable values, like 4 times the number of judges, if each judge rates four wines. 
    \item Suppose cases is a factor, nested within combinations of the between-cases factors, and crossing the within-cases factors.
    \item 
    \item 
    \item 
\end{itemize}
\end{frame}



