%\documentclass{article}
%\usepackage{beamerarticle}
\documentclass[serif,ignorenonframetext]{beamer}
\usepackage[spanish]{babel}
\newcommand{\E}[1]{{\rm E }\left[#1\right]}
\newcommand{\vecy}{\skew{-2}\vec{y}}
\newcommand{\uno}{\skew{-2}\vec1}
\newcommand{\sigue}{\mathbin{\equiv}}
\newcommand{\gaus}[2]{\mathcal N\left(#1,#2\right)}
\newcommand{\gausmv}[3]{\mathcal N_{#1}\left(#2,#3\right)}
\newcommand{\por}{\,}
\renewcommand{\t}[1]{{#1}^t} % traspuesta
\newcommand{\T}[1]{\t{\left({#1}\right)}} % traspuesta con paréntesis
\DeclareMathOperator{\tr}{tr}
\DeclareMathOperator{\rango}{rango}
\DeclareMathOperator{\Cov}{Cov}
\DeclareMathOperator{\Var}{Var}
\title{ANOVA}
\subtitle{análisis de varianza}
\author[Carleos, Corral]{Carlos Carleos, Norberto Corral}
\usepackage{Sweave}
\begin{document}
%% hay que ponerlos tras begin{document} según el manual de Sweave
\DefineVerbatimEnvironment{Sinput}{Verbatim}{formatcom=\color{blue}}
\setkeys{Gin}{width=\textwidth}
\begin{frame}
  \titlepage
\end{frame}

\section<article>{Planteamiento}
\begin{frame}
  \frametitle<presentation>{Planteamiento}
  \begin{itemize}
  \item Sean $q$ poblaciones $x_i$ (\(i=1,\dots,q\))
    \begin{itemize}
    \item niveles o modalidades de un ``factor''
    \end{itemize}
  \item \(x_i=\mu_i+\epsilon\sigue\gaus{\mu_i}{\sigma}\)
    \quad con \(\epsilon\sigue\gaus{0}{\sigma}=\gausmv{1}{0}{\sigma^2}\)
  \item De cada $x_i$ se conoce una muestra aleatoria simple\\
    de tamaño $n_i$: \(x_{i1},\dots,x_{in_i}\)
  \item Tamaño muestral total: \(n=\sum_{i=1}^q n_i\)
  \item Otra parametrización: \(\mu_i=\mu+\alpha_i\)\quad con
    \(\mu=\frac{n_i\por\mu_i}{n}\)
  \item \(\sum_{i=1}^q n_i\por\alpha_i=0\)
  \item Si \(\forall\,i,\;n_i=\frac{n}{q}\) el
    modelo se dice \emph{equilibrado} o \emph{balanceado}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle<presentation>{Planteamiento}
  Hipótesis previas
  \[
    \forall\,i=1,\dots,q\quad
    \vec x_i = (x_{i1},\dots,x_{in_i})^t \sigue
    \gausmv{n_i}{\mu_i\uno_{n_i}}{\sigma^2\por I_{n_i}}
  \]
  \begin{itemize}
  \item independencia (muestras aleatorias simples)
  \item gausianidad
  \item homoscedasticidad (igualdad de varianzas)
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle<presentation>{Planteamiento}
  Hipótesis del análisis de varianza (\textsc{anova})
  \begin{eqnarray*}
    H_0\quad&\equiv&\quad\forall\,i,j,\;\;\mu_i=\mu_j
    \quad\equiv\quad\forall\,i,\;\;\alpha_i=0\\
    H_1\quad&\equiv&\quad\exists\,i,j,\;\;\mu_i\not=\mu_j
    \quad\equiv\quad\exists\,i,\;\;\alpha_i\not=0
  \end{eqnarray*}
  \begin{itemize}
  \item $H_0$: el factor no influye en la respuesta
  \item $H_1$: el factor sí influye en la respuesta
  \end{itemize}
\end{frame}
\section<article>{Repaso de álgebra lineal}
\begin{frame}
  \frametitle<presentation>{Repaso de álgebra lineal}
  \begin{itemize}
  \item Matriz simétrica $A=[a_{ij}]_{i,j}$ si $a_{ij}=a_{ji}\equiv A=\t A$
    \begin{itemize}
    \item todos sus autovalores $(\lambda_i)_i$ son reales; autovectores: $(\vec u_i)_i$
    \item $A=U\por\Lambda\por\t U$ con $\Lambda=\text{diag}(\dots,\lambda_i,\dots)$ y $U=[\dots,\vec u_i\dots]$
    \end{itemize}
  \item Matriz idempotente: $A\por A = A$
    \begin{itemize}
    \item (autovalor $A$) $\in$ $\{0,1\}$ pues si $\vec x\neq 0$ es autovector
      no nulo
      $$
%      A\por \vec x=\lambda\por\vec x\implies
      \lambda\por\vec x=A\por\vec x=A\por A\por\vec x=A\por\lambda\por\vec x
      = \lambda\por A\por\vec x = \lambda^2\vec x
      $$
      $$
      \implies\qquad\lambda=0\quad\cup\quad
      \biggl\{\vec x=\lambda\vec x\implies\lambda=1\biggr\}
      $$
    \end{itemize}\bigskip
  \item Traza: $A=[a_{ij}]_{i,j}\implies \tr A=\sum_{i} a_{ii}$
    \begin{itemize}
    \item $a = \tr[a]$
    \item $\tr(A\por B) = \tr(B\por A)$
    \item $\tr(A\por B\por C) = \tr(B\por C\por A) = \tr(C\por A\por B)$
    \item $\E{\tr A}=\tr\E{A}$
    \end{itemize}
  \end{itemize}
\end{frame}

\section<article>{Formas cuadráticas}

\begin{frame}
  \frametitle<presentation>{Matriz de varianzas y covarianzas}
  $$
  \Var(\vec x)=\Cov(\vec x)=\Cov(\vec x,\vec x)=
  \E{(\vec x-\E{\vec x})\T{\vec x-\E{\vec x}}}
  $$
  $$
  {}=\E{
    \begin{pmatrix}
      (x_1-\E{x_1})\\\vdots\\(x_q-\E{x_q})
    \end{pmatrix}\por
    \begin{bmatrix}
      (x_1-\E{x_1})&\dots&(x_q-\E{x_q})
    \end{bmatrix}
  }={}
  $$
  $$
  {}=
  \begin{bmatrix}
    \E{(x_1-\E{x_1})^2}&\dots&\E{(x_1-\E{x_1})(x_q-\E{x_q})}\\
    \vdots&\ddots&\vdots\\
    \E{(x_1-\E{x_1})(x_q-\E{x_q})}&\dots&\E{(x_q-\E{x_q})^2}
  \end{bmatrix}
  $$
  \begin{itemize}
  \item intradiagonal: $\E{(x_i-\E{x_i})^2}=\Var(x_i)$
  \item extradiagonal: $\E{(x_i-\E{x_i})(x_j-\E{x_j})}=\Cov(x_i,x_j)$
  \end{itemize}
\end{frame}


\begin{frame}
  \frametitle<presentation>{Matriz de varianzas y covarianzas}
  $$
  \Var(\vec x)=\Cov(\vec x)=\Cov(\vec x,\vec x)=
  \E{(\vec x-\E{\vec x})\T{\vec x-\E{\vec x}}}={}
  $$
  $$
  {}=  \E{\vec x\por\t{\vec x}}-
  \E{\vec x\por\t{\E{\vec x}}}-
  \E{\E{\vec x}\por\t{\vec x}}+
  \E{\E{\vec x}\t{\E{\vec x}}}={}
  $$
  $$
  {}=  \E{\vec x\por\t{\vec x}}-
  \E{\vec x}\por\t{\E{\vec x}}-
  \E{\vec x}\por\E{\t{\vec x}}+
  \E{\vec x}\por\t{\E{\vec x}}={}
  $$
  $$
  {}=\E{\vec x\por\t{\vec x}}-
  \E{\vec x}\por\t{\E{\vec x}}
  $$
\end{frame}

\begin{frame}
  \frametitle<presentation>{Formas cuadráticas}
  Dados una matriz $A$ \((n,n)\) simétrica e idempotente con \(\rango(A)=r\)
  y un vector aleatorio $\vec x\equiv N_n(\vec \mu,\sigma^2\,I)$,
  se verifican las siguientes propiedades:
  \begin{itemize}
  \item $A=U\,\Lambda\,U^t=(U_1\;U_2)\,
    \begin{pmatrix}
      I_r&\\&0
    \end{pmatrix}
    \,
    \begin{pmatrix}
      U_1^t\\U_2^t
    \end{pmatrix}=U_1\,U_1^t$
    \\con $U_1$ vectores propios asociados al valor propio $1$
    \\con $U_2$ vectores propios asociados al valor propio $0$
    \\y $U^t \, U = I_n$
  \item \(\vec x^t\,A\,\vec x=\vec x^t\,U_1\,U_1^t\,\vec x\)
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle<presentation>{Formas cuadráticas}
  \begin{itemize}
  \item esperanza
  \begin{eqnarray*}
    \E{\vec x^t\,A\,\vec x}
    &=&\E{\vec x^t\,U_1\,U_1^t\,\vec x} = \E{\tr\left\{\vec x^t\,U_1\,U_1^t\,\vec x\right\}}\\
    &=&\E{\tr\left\{U_1\,U_1^t\,\vec x\,\vec x^t\right\}}
        =\tr\left\{U_1\,U_1^t\,\E{\vec x\,\vec x^t}\right\}\\
    &=&\tr\left\{U_1\,U_1^t\,\left(\Cov(\vec x)+\E{\vec x}\,\E{\vec x^t}\right)\right\}\\
    &=&\tr\left\{U_1\,U_1^t\,\left(\sigma^2\,I+\E{\vec x}\,\E{\vec x^t}\right)\right\}\\
    &=&\tr\left\{\sigma^2\,U_1\,U_1^t\right\}+
        \tr\left\{U_1\,U_1^t\,\E{\vec x}\,\E{\vec x^t}\right\}\\
    &=&\sigma^2\,\tr(U_1\,U_1^t)+
        \tr\left\{\E{\vec x^t}\,U_1\,U_1^t\,\E{\vec x}\right\}\\
    &=&\sigma^2\,\rango(A)+
        \E{\vec x^t}\,A\,\E{\vec x}
  \end{eqnarray*}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle<presentation>{Formas cuadráticas}
  \begin{itemize}
    \item
      Si $A\,\vec\mu = \vec0$ entonces $\vec x^t\,A\,\vec x\equiv\sigma^2\,\chi^2_r$.

      \[ \vec x^t\,A\,\vec x= \vec x^t\,U_1\,U_1^t\,\vec x = \vecy_1^t\,\vecy_1 ,\qquad \mbox{con  } \vecy_1 = U_1^t\,\vec x \]
     
      \begin{equation*}
        \E{\vecy_1} = U_1^t\,\E{\vec x} = 
        U_1^t\,\E{\vec x}=\vec\gamma
      \end{equation*}
      \[ U_1\,\vec\gamma= U_1\,U_1^t\,\E{\vec x}  = A\,\E{\vec x} =\vec 0 \]
      y, por ser $U_1$ una matriz de rango completo, se tiene que $\vec\gamma=\vec0$.

      \[\Cov(\vecy_1)=U_1^t\,\Cov(\vec x)\,U_1 = \sigma^2\,U_1^t\,U_1=\sigma^2\,I_r\]
      En consecuencia, \(\vecy_1 = U_1^t\,N(\vec\mu,\sigma^2\,I)\equiv N_r(\vec0,
      \sigma^2\,I_r)\)
      \[\vecy_1^t\,\vecy_1 = \sum_{j=1}^r y_{1j}^2
      =\sum_{j=1}^r \sigma^2 \left(\frac{y_{1j}}{\sigma} \right)^2 = \sigma^2 \sum_{j=1}^r N^2(0,1) \equiv \sigma^2 \chi^2_r\]
    \end{itemize}
  \end{frame}

\section<article>{Análisis de varianza}
\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  Sea el vector columna \(n\times1\):\qquad
  \(
    \vec x =
    \begin{pmatrix}
      \vec x_1 \\ \vec x_2 \\ \vdots \\ \vec x_q 
    \end{pmatrix}
    \)
    
    yuxtaposición de los vectores \(n_i\times1\):\qquad
    \(
    \vec x_i =
    \begin{pmatrix}
       x_{i1} \\ x_{i2} \\ \vdots \\  x_{in_i}
    \end{pmatrix}    
    \)
    
    para  \(i=1,\dots,q\)
\end{frame}

\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  \[ \vec1_n =
    \begin{pmatrix}
      1 \\ \vdots \\ 1
    \end{pmatrix}_{n\times1}
  \qquad
   J = \frac1n\,\uno_n\uno_n^t =
    \begin{pmatrix}
      \frac1n & \frac1n & \dots & \frac1n \\[1ex]
      \frac1n & \frac1n & \dots & \frac1n \\
      \vdots  & \vdots  & \ddots & \vdots \\
      \frac1n & \frac1n & \dots & \frac1n
    \end{pmatrix}
  \]
  
  \bigskip
  $J$ es simétrica e idempotente
  
  \bigskip
  al multiplicar, genera un vector columna con la media:

  \bigskip
  $$
   J\,\vec x = \frac1n\,\uno_n\,\uno_n^t \,\vec x =
   \uno_n\,\frac1n\,\uno_n^t\,\vec x
   = \bar x \, \uno_n
  $$
\end{frame}

\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  \[
    H = I_n - J =     \begin{pmatrix}
      1-\frac1n & -\frac1n & \dots & -\frac1n \\[1ex]
      -\frac1n & 1-\frac1n & \dots & -\frac1n \\
      \vdots  & \vdots  & \ddots & \vdots \\
      -\frac1n & -\frac1n & \dots & 1-\frac1n
    \end{pmatrix}
  \]

  \bigskip
  $H$ es simétrica e idempotente
\end{frame}

\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  $H$ es la matriz de centrado

  \bigskip
  al multiplicar, sustrae la media:

  \bigskip
  \begin{eqnarray*}
    H\,\vec x &=& \left(I_n-\frac1n\,\uno_n\,\uno_n^t\right)\,\vec x =
                  \vec x - \uno_n\,\frac1n\,\uno_n^t\,\vec x\\
              &=& \vec x - \uno_n\,\bar x =
    \begin{pmatrix}
      \vec x_1-\bar x\,\uno_{n_1} \\
      \vec x_2-\bar x\,\uno_{n_2} \\
      \vdots \\
      \vec x_q-\bar x\,\uno_{n_q} \\
    \end{pmatrix}
  \end{eqnarray*}
\end{frame}

\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  \begin{eqnarray*}
    \vec x^t\,H\,\vec x
    &=& \vec x^t\,H\,H\,\vec x =  \vec x^t\,H^t\,H\,\vec x \\
    &=& \left[(\vec x_1-\bar x\,\uno_{n_1})^t, \dots, (\vec x_q-\bar x\,\uno_{n_q})^t\right]\,
    \begin{pmatrix}
      \vec x_1-\bar x\,\uno_{n_1} \\
      \vec x_2-\bar x\,\uno_{n_2} \\
      \vdots \\
      \vec x_q-\bar x\,\uno_{n_q} \\
    \end{pmatrix}\\
    &=& \sum_{i=1}^q (\vec x_i-\bar x\,\uno_{n_i})^t\,(\vec x_i-\bar x\,\uno_{n_i}) \\
    &=& \sum_{i=1}^q \sum_{j=1}^{n_i} (x_{ij}-\bar x)^2 = \text{SCT}
  \end{eqnarray*}
\end{frame}

\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  \[
    H = I - J = I - D + D - J
  \]
  con
  \[ D =
    \begin{pmatrix}
      J_1 &     &  \\ 
      & J_2 &  \\
      &     & \ddots & \\
      & & & J_q
    \end{pmatrix}
  \]
  con \(J_i = \frac1{n_i}\,\uno_{n_i}\,\uno_{n_i}^t\)
\bigskip
  \begin{itemize}
  \item \(I-D\) es simétrica e idempotente
  \item \(\tr (I-D)=\tr I-\tr D=n-q=\rango(I-D)\)
  \item \(D-J\) es simétrica e idempotente
  \item \(\tr (D-J)=\tr D-\tr J=q-1=\rango(D-J)\)
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  \begin{multline*}
    D\,\vec x=
    \begin{pmatrix}
      J_1 &     &  \\ 
      & J_2 &  \\
      &     & \ddots & \\
      & & & J_q
    \end{pmatrix}
    \,
    \begin{pmatrix}
      \vec x_1 \\ \vec x_2 \\ \vdots \\ \vec x_q
    \end{pmatrix}
    \, =
    \begin{pmatrix}
      J_1\,\vec x_1 \\ J_2\,\vec x_2 \\ \vdots \\ J_q\,\vec x_q
    \end{pmatrix}
    =
    \begin{pmatrix}
      \bar x_1\,\uno_{n_1} \\
      \bar x_2\,\uno_{n_2} \\ \vdots \\ \bar x_q\,\uno_{n_q}
    \end{pmatrix}
  \end{multline*}
\end{frame}

\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  \begin{equation*}
    (I-D)\,\vec x = \vec x - D\,\vec x =
    \begin{pmatrix}
      \vec x_1 - \bar x_1 \, \uno_{n_1} \\  
      \vec x_2 - \bar x_2 \, \uno_{n_2} \\
      \vdots \\    
      \vec x_q - \bar x_q \, \uno_{n_q}  
    \end{pmatrix}
  \end{equation*}
\end{frame}

\begin{frame}
  \frametitle<presentation>{Análisis de varianza}

  \begin{multline*}
    (D-J)\,\vec x=D\,\vec x-J\,\vec x = 
    \begin{pmatrix}
      J_1 &     &  \\ 
      & J_2 &  \\
      &     & \ddots & \\
      & & & J_q
    \end{pmatrix}
  \,
  \begin{pmatrix}
    \vec x_1 \\ \vec x_2 \\ \vdots \\ \vec x_q
  \end{pmatrix}
  -J\,\vec x
  \\[2ex]
  =
  \begin{pmatrix}
    J_1\,\vec x_1 \\ J_2\,\vec x_2 \\ \vdots \\ J_q\,\vec x_q
  \end{pmatrix}
  - \bar x\,\uno_n =
  \begin{pmatrix}
    \bar x_1\,\uno_{n_1} \\    \bar x_2\,\uno_{n_2} \\ \vdots \\ \bar x_q\,\uno_{n_q}
  \end{pmatrix}
  -\bar x\,\uno_n=
  \begin{pmatrix}
    (\bar x_1-\bar x)\,\uno_{n_1} \\    (\bar x_2-\bar x)\,\uno_{n_2} \\
    \vdots \\    (\bar x_q-\bar x)\,\uno_{n_q} 
  \end{pmatrix}
\end{multline*}
\end{frame}

\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  \begin{eqnarray*}
    \text{SCT}&=&\vec x^t\,H\,\vec x= \vec x^t\,(I-J)\,\vec x= \vec x^t\,(I-D+D-J)\,\vec x\\
       &=&\vec x\,(I-D)\,\vec x+\vec x\,(D-J)\,\vec x\\
       &=&\sum_{i=1}^q\sum_{j=1}^{n_i}(x_{ij}-\bar x_{i})^2+
           \sum_{i=1}^qn_i\,(\bar x_{i}-\bar x)^2\\
           &=&\text{SCE}+\text{SCF}
  \end{eqnarray*}
  ya que\mode<presentation>{...}
\end{frame}

\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  \begin{multline*}
    \vec x^t\,(I-D)\,\vec x=
    \\[2ex]
  =\vec x^t\,\left[
    \begin{pmatrix}
      I_{n_1} &     &  \\ 
      & I_{n_2} &  \\
      &     & \ddots & \\
      & & & I_{n_q}
    \end{pmatrix}
    -
    \begin{pmatrix}
      J_1 &     &  \\ 
      & J_2 &  \\
      &     & \ddots & \\
      & & & J_q
    \end{pmatrix}
  \right]\,\vec x
  \\[2ex]
  = (\vec x_1^t,\dots,\vec x_q^t)\,
  \begin{pmatrix}
    I_{n_1}-J_1 &     &  \\ 
    & I_{n_2}-J_2 &  \\
    &     & \ddots & \\
    & & & I_{n_q}-J_q
  \end{pmatrix}
  \,
  \begin{pmatrix}
    \vec x_1 \\ \vec x_2 \\ \vdots \\ \vec x_q
  \end{pmatrix}
  \\[2ex]
  = \sum_{i=1}^q\vec x_i^t\,(I_{n_i}-J_i)\,\vec x_i
  = \sum_{i=1}^q\sum_{i=1}^{n_i}(x_{ij}-\bar x_{i})^2=\text{SCE}
\end{multline*}
\end{frame}

\begin{frame}
  \frametitle<presentation>{Análisis de varianza}

Por ser \(D-J\) simétrica e idempotente se tiene que
 \begin{multline*}
   \vec x^t\,(D-J)\,\vec x=  \vec x^t\,(D-J)^t\,(D-J)\,\vec x\\
   =\left[(\bar x_1-\bar x)\,\uno_{n_1}^t,\dots,(\bar x_q-\bar x)\,\uno_{n_q}^t\right]\,
   \begin{pmatrix}
     (\bar x_1-\bar x)\,\uno_{n_1} \\ \vdots \\ (\bar x_q-\bar x)\,\uno_{n_q}
   \end{pmatrix}\\
   =\sum_{i=1}^q n_i\,(\bar x_{i}-\bar x)^2=\text{SCF}
 \end{multline*}
\end{frame}

\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  \begin{eqnarray*}
    \E{\text{SCE}}
    &=& \E{\vec x^t\,(I-D)\,\vec x} = \E{\tr(\vec x^t\,(I-D)\vec x} \\
    &=& \E{\tr([I-D]\,\vec x\,\vec x^t)}=\tr\left\{\E{(I-D)\,\vec x\,\vec x^t}\right\} \\
    &=& \tr\left\{(I-D)\,\E{\vec x\,\vec x^t}\right\} \\
    &=& \tr\left\{(I-D)\,\left(\Cov(\vec x)+\E{\vec x}\,\E{\vec x^t}\right)\right\}\\
    &=& \tr\left\{(I-D)\,\left(\sigma^2\,I+\E{\vec x}\,\E{\vec x^t}\right)\right\}\\
    &=& \sigma^2\,\tr(I-D)+ \tr\left\{(I-D)\,\E{\vec x}\,\E{\vec x^t}\right\}\\
    &=& \sigma^2\,(n-q)+ \tr\left\{\E{\vec x^t}\,(I-D)\,\E{\vec x}\right\}\\
    &=& \sigma^2\,(n-q)+ \E{\vec x^t}\,(I-D)\,\E{\vec x}
  \end{eqnarray*}
\end{frame}

\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  \[
    \E{\vec x} =
    \begin{pmatrix}
      \E{\vec x_1} \\      \E{\vec x_2} \\ \vdots \\ \E{\vec x_q} 
    \end{pmatrix} =
    \begin{pmatrix}
      \mu_1\,\uno_{n_1}\\ \mu_2\,\uno_{n_2}\\ \vdots \\ \mu_q\,\uno_{n_q}
    \end{pmatrix}
  \]
  \begin{eqnarray*}
    (I-D)\,\E{\vec x}
    &=& 
  \begin{pmatrix}
    I_{n_1}-J_1 &     &  \\ 
    & I_{n_2}-J_2 &  \\
    &     & \ddots & \\
    & & & I_{n_q}-J_q
  \end{pmatrix}
          \,
              \begin{pmatrix}
      \mu_1\,\uno_{n_1}\\ \mu_2\,\uno_{n_2}\\ \vdots \\ \mu_q\,\uno_{n_q}
    \end{pmatrix}
    \\
    &=& \begin{pmatrix}\vdots\\(I_{n_i}-J_i)\por\mu_i\por\uno_{n_i}\\\vdots
    \end{pmatrix}
    \begin{pmatrix}\vdots\\H_i\por\mu_i\por\uno_{n_i}\\\vdots
    \end{pmatrix}
    =\vec0
  \end{eqnarray*}
  \[
    \implies\E{\vec x^t}\,(I-D)\,\E{\vec x}=\E{\vec x^t}\,\vec0=0
    \implies
    \E{\text{SCE}} = \sigma^2\,(n-q)
  \]
\end{frame}

\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  \begin{eqnarray*}
    \E{\text{SCF}}
    &=&\E{\vec x^t\,(D-J)\vec x} = \E{\tr\left\{\vec x^t\,(D-J)\,\vec x\right\}}\\
    &=&\E{\tr\left\{(D-J)\,\vec x\,\vec x^t\right\}}
        =\tr\left\{(D-J)\,\E{\vec x\,\vec x^t}\right\}\\
    &=&\tr\left\{(D-J)\,\left(\Cov(\vec x)+\E{\vec x}\,\E{\vec x^t}\right)\right\}\\
    &=&\tr\left\{(D-J)\,\left(\sigma^2\,I+\E{\vec x}\,\E{\vec x^t}\right)\right\}\\
    &=&\tr\left\{\sigma^2\,(D-J)\right\}+
        \tr\left\{(D-J)\,\E{\vec x}\,\E{\vec x^t}\right\}\\
    &=&\sigma^2\,\tr(D-J)+
        \tr\left\{\E{\vec x^t}\,(D-J)\,\E{\vec x}\right\}\\
    &=&\sigma^2\,(q-1)+
        \E{\vec x^t}\,(D-J)\,\E{\vec x}
  \end{eqnarray*}
\end{frame}

\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  \[
    \E{\vec x} =
    \begin{pmatrix}
      \E{\vec x_1} \\ \vdots \\ \E{\vec x_q}
    \end{pmatrix} =
    \begin{pmatrix}
      \mu\,\uno_{n_1}+\alpha_1\,\uno_{n_1} \\ \vdots \\ \mu\,\uno_{n_q}+\alpha_q\,\uno_{n_q} 
    \end{pmatrix}
    = \mu\,\uno_{n}+
    \begin{pmatrix}
      \alpha_1\,\uno_{n_1} \\ \vdots \\ \alpha_q\,\uno_{n_q} 
    \end{pmatrix}
\]
\end{frame}


\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  \begin{multline*}
    (D-J)\,\E{\vec x}
    = \mu\,(D-J)\,\uno + (D-J)\,
    \begin{pmatrix}
      \alpha_1\,\uno_{n_1} \\ \vdots \\ \alpha_q\,\uno_{n_q} 
    \end{pmatrix}\\
    = \mu\,(D-J)\,\uno + D\,
    \begin{pmatrix}
      \alpha_1\,\uno_{n_1} \\ \vdots \\ \alpha_q\,\uno_{n_q} 
    \end{pmatrix}
    - J\,
    \begin{pmatrix}
      \alpha_1\,\uno_{n_1} \\ \vdots \\ \alpha_q\,\uno_{n_q} 
    \end{pmatrix}
    \\
    = D\,
    \begin{pmatrix}
      \alpha_1\,\uno_{n_1} \\ \vdots \\ \alpha_q\,\uno_{n_q} 
    \end{pmatrix}=
    \begin{pmatrix}
      J_1 &     &  \\ 
      & J_2 &  \\
      &     & \ddots & \\
      & & & J_q
    \end{pmatrix}
            \,
    \begin{pmatrix}
      \alpha_1\,\uno_{n_1} \\ \vdots \\ \alpha_q\,\uno_{n_q} 
    \end{pmatrix}=
    \begin{pmatrix}
      \alpha_1\,\uno_{n_1} \\ \vdots \\ \alpha_q\,\uno_{n_q} 
    \end{pmatrix}
  \end{multline*}
  puesto que...
  \end{frame}

  \begin{frame}
    \frametitle<presentation>{Análisis de varianza}
    \begin{itemize}
    \item
      \((D-J)\,\uno=\vec0\)
    \item
     \(J\,
        \begin{pmatrix}
      \alpha_1\,\uno_{n_1} \\ \vdots \\ \alpha_q\,\uno_{n_q} 
    \end{pmatrix}
    =
    \frac1n\,\uno\,\uno^t\,
    \begin{pmatrix}
      \alpha_1\,\uno_{n_1} \\ \vdots \\ \alpha_q\,\uno_{n_q} 
    \end{pmatrix}
    =
    \frac1n\,\uno\,\sum n_i\,\alpha_i=\vec0
  \)
\end{itemize}
Por tanto,
\begin{eqnarray*}
  \E{\vec x^t}\,(D-J)\,\E{\vec x}
  &=& \left(\alpha_1\,\uno_{n_1}^t,\dots,\alpha_q\,\uno_{n_q}^t\right)\,
    \begin{pmatrix}
      \alpha_1\,\uno_{n_1} \\ \vdots \\ \alpha_q\,\uno_{n_q} 
    \end{pmatrix}\\
  &=& \sum_{i=1}^q\alpha_i^2\uno_{n_i}^t\,\uno_{n_i}=\sum_{i=1}^q n_i\,\alpha_i^2
\end{eqnarray*}
y se obtiene
\[ \E{\text{SCF}} = \sigma^2\,(q-1)+\sum n_i\,\alpha_i^2 \]
  \end{frame}

\subsection<article>{Distribución de la SCE}
\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  \mode<presentation>{Distribución de la SCE:\bigskip}

  Suponiendo que \(\vec x_i \equiv
  N_{n_i}(\mu_i\,\uno,\sigma^2\,I_{n_i})\) para \(i=1,\dots,q\)\\
  y que las \(\vec x_i\) son independientes, se verifica que
  \[
    \text{SCE}=\vec x^t\,(I-D)\,\vec x=\sigma^2\,\chi^2_{n-q}
  \]
  Para demostrar esta propiedad, bastará ver que \(\E{(I-D)\,\vec x}=\vec0\).
\end{frame}

\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  \begin{eqnarray*}
    \E{(I-D)\,\vec x}
    &=& (I-D)\,\E{\vec x} = \E{\vec x}-D\,\E{\vec x} \\
    &=&
        \begin{pmatrix}
          \mu_1\,\uno_{n_1} \\ \vdots \\ \mu_q\,\uno_{n_q}
        \end{pmatrix}
    -
        \begin{pmatrix}
          J_1 & & \\ &\ddots& \\ &&J_q
        \end{pmatrix}
                                    \,
        \begin{pmatrix}
          \mu_1\,\uno_{n_1} \\ \vdots \\ \mu_q\,\uno_{n_q}
        \end{pmatrix} \\
    &=&
        \begin{pmatrix}
          \mu_1\,\uno_{n_1} \\ \vdots \\ \mu_q\,\uno_{n_q}
        \end{pmatrix}
    -
        \begin{pmatrix}
          \mu_1\,J_1\,\uno_{n_1} \\ \vdots \\ \mu_q\,J_q\,\uno_{n_q}
        \end{pmatrix} \\
    &=& \vec0
  \end{eqnarray*}
\end{frame}
\subsection<article>{Distribución de la SCF bajo $H_0$}
\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  \mode<presentation>{Distribución de la SCF bajo $H_0$:}
  \[
    \text{SCF}=\vec x^t\,(D-J)\,\vec x \stackrel{H_0}{\equiv}
    \sigma^2\,\chi^2_{q-1}
  \]
  Dado que \(D-J\) es idempotente, bastará comprobar que
  \(\E{(D-J)\,\vec x}=\vec0\).
  \begin{equation*}
    \E{(D-J)\,\vec x}
  =(D-J)\,\E{\vec x}=D\,\E{\vec x}-J\,\E{\vec x}=\vec0
  \end{equation*}
  ya que bajo $H_0\equiv\mu_1=\dots=\mu_q$ y
  $\E{\vec x}=\mu\,\uno$, con lo cual
  \[
    D\,\mu\,\uno = \mu\,
    \begin{pmatrix}
      J_1 & & \\ &\ddots& \\ &&J_q
    \end{pmatrix}\,
    \begin{pmatrix}
      \uno_{n_1} \\ \vdots \\ \uno_{n_q}
    \end{pmatrix}
    =\mu\,
    \begin{pmatrix}
      J_1\,\uno_{n_1} \\ \vdots \\ J_q\,\uno_{n_q}
    \end{pmatrix}
    =\mu\,\uno
  \]
  \[J\,\E{\vec x} = J\,\mu\uno = \mu\,J\,\uno = \mu\,\uno\]
\end{frame}
\section<article>{Estadístico del contraste}
\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  \[\text{CME}=\frac{\text{SCE}}{n-q}\]
  Verifica que \(\E{\text{CME}}=\sigma^2\) y por tanto
  es un estimador insesgado de la varianza de los residuos.
  Además, \[\frac{\text{CME}\,(n-q)}{\sigma^2}\equiv\chi^2_{n-q}\]
\end{frame}

\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  \[\text{CMF}=\frac{\text{SCF}}{q-1}\]
  \[\E{\text{CMF}} =
    \sigma^2+\frac{\sum_{i=1}^q n_i\,\alpha_i^2}{q-1}\]
  Bajo $H_0\equiv \alpha_i=0\,\forall\,i$, se cumple que
  \(\E{\text{CMF}\mid H_0} =\sigma^2\).  Además
  \[\frac{\text{CMF}\,(q-1)}{\sigma^2}
    \stackrel{H_0}{\equiv}\chi^2_{q-1}\]
\end{frame}

\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  CMF y CME son independientes:
  \begin{itemize}
    \newcommand{\esgaus}{\sigue\gaus{\cdot}{\cdot}}
  \item \(\vec x\esgaus\) \(\implies\)
    \(
    \begin{cases}
      (I-D)\vec x\esgaus&\\
      (D-J)\vec x\esgaus&
    \end{cases}
    \)
  \item \(\Cov[(I-D)\vec x, (D-J)\vec x]\) = \((I-D)\Var(\vec x)(D-J)\)
    = \((I-D)\sigma^2I(D-J)\) = \(\sigma^2(I-D)(D-J)\)
    = \(\sigma^2(D-J-DD+DJ)\) = \(\sigma^2(D-J-D+J)\) = \(\mathbf 0\)
  \item \((I-D)\vec x\) independiente de \((D-J)\vec x\)
  \item \(\t{\vec x}(I-D)\vec x\) independiente de \(\t{\vec x}(D-J)\vec x\)
  \item SCE independiente de SCF
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  El cociente
  \[\frac{\text{CMF}}{\text{CME}} =
    \frac{\text{CMF}/\sigma^2}{\text{CME}/\sigma^2}
  \]
  tiende a tomar valores cercanos a uno bajo $H_0$, y más grandes
  bajo \(H_1\).

  Por otra parte, CMF y CME son independientes, luego
  \[
    \frac{\text{CMF}}{\text{CME}} \stackrel{H_0}{\equiv}
    \frac{\frac{\chi^2_{q-1}}{q-1}}{\frac{\chi^2_{n-q}}{n-q}} = F_{q-1,n-q}
  \]
  En consecuencia, la región crítica del contraste viene dada por la
  expresión
  \[
    \text{R.C.} =
    \left\{
      \frac{\text{CMF}}{\text{CME}} > k
    \right\}
    \text{ con } P[\text{R.C.}\mid H_0] = \alpha
  \]
\end{frame}

\begin{frame}
  \frametitle<presentation>{Análisis de varianza}
  Tabla \textsc{anova}
  \bigskip
  \begin{center}
    \begin{tabular}{lcccc}
      Fuente de variación & S.C. & g.l. & C.M. & $F$ \\
      \hline\\[-1.5ex]
      Entre / Factor & SCF & $q-1$ & CMF & $\text{CMF}\over\text{CME}$\\
      Dentro / Error  & SCE & $n-q$ & CME & \\
      \hline
      Total & SCT & $n-1$ & & 
     \end{tabular}
  \end{center}
\end{frame}

\section<article>{Ejemplo}
\begin{frame}[fragile]
  \frametitle<presentation>{Ejemplo}
  Considérense los datos siguientes incluidos en R:
\begin{Schunk}
\begin{Sinput}
> aves <- data.frame (peso = chickwts$weight,
                      come = factor(chickwts$feed, 
                                    labels = 
                   c("caseína","fabona","linaza",
                     "har.hueso","soja","girasol")))
> summary (aves)
\end{Sinput}
\begin{Soutput}
      peso              come   
 Min.   :108.0   caseína  :12  
 1st Qu.:204.5   fabona   :10  
 Median :258.0   linaza   :12  
 Mean   :261.3   har.hueso:11  
 3rd Qu.:323.5   soja     :14  
 Max.   :423.0   girasol  :12  
\end{Soutput}
\end{Schunk}
\end{frame}

\begin{frame}
  \frametitle<presentation>{Ejemplo}
  \begin{itemize}
  \item 
  Se repartió aleatoriamente en seis grupos una remesa de pollos recién nacidos.
  \item
  Cada grupo recibió un complemento alimenticio distinto.
  \item
  Se registró el peso en gramos tras seis semanas.
  \bigskip
  \item ¿Influye el complemento en el peso?
\end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle<presentation>{Ejemplo}
\begin{Schunk}
\begin{Sinput}
> options (digits = 3)
> RcmdrMisc::numSummary (aves$peso, groups=aves$come)
\end{Sinput}
\begin{Soutput}
          mean   sd  IQR  0% 25% 50% 75% 100% data:n
caseína    324 64.4 93.5 216 277 342 371  404     12
fabona     160 38.6 39.2 108 137 152 176  227     10
linaza     219 52.2 79.8 141 178 221 258  309     12
har.hueso  277 64.9 70.5 153 250 263 320  380     11
soja       246 54.1 63.2 158 207 248 270  329     14
girasol    329 48.8 27.5 226 313 328 340  423     12
\end{Soutput}
\end{Schunk}
\end{frame}

\begin{frame}[fragile]
  \frametitle<presentation>{Ejemplo}
\begin{Schunk}
\begin{Sinput}
> boxplot (peso ~ come, aves)
\end{Sinput}
\end{Schunk}
\includegraphics{anova-004}
\end{frame}

\begin{frame}[fragile]
  \frametitle<presentation>{Ejemplo}
\begin{Schunk}
\begin{Sinput}
> boxplot (peso ~ come, aves, notch=TRUE)
\end{Sinput}
\end{Schunk}
\includegraphics{anova-005}
\end{frame}

\begin{frame}[fragile]
  \frametitle<presentation>{Ejemplo}
\begin{Schunk}
\begin{Sinput}
> bartlett.test (peso ~ come, aves)
\end{Sinput}
\begin{Soutput}
	Bartlett test of homogeneity of variances

data:  peso by come
Bartlett's K-squared = 3, df = 5, p-value = 0.7
\end{Soutput}
\begin{Sinput}
> car::leveneTest (peso ~ come, aves)
\end{Sinput}
\begin{Soutput}
Levene's Test for Homogeneity of Variance (center = median)
      Df F value Pr(>F)
group  5    0.75   0.59
      65               
\end{Soutput}
\end{Schunk}
\end{frame}

\begin{frame}[fragile]
  \frametitle<presentation>{Ejemplo}
\begin{Schunk}
\begin{Sinput}
> options (width = 60)
> summary (aov (peso ~ come, aves))
\end{Sinput}
\begin{Soutput}
            Df Sum Sq Mean Sq F value  Pr(>F)    
come         5 231129   46226    15.4 5.9e-10 ***
Residuals   65 195556    3009                    
---
Signif. codes:  
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
\end{Soutput}
\end{Schunk}
\end{frame}

\section<article>{Contrastes a posteriori}
\begin{frame}
  \frametitle<presentation> {Contrastes a posteriori}
  \[
    H_0^{\text{ANOVA}}\colon \mu_1=\dots=\mu_q
    \qquad\equiv\qquad\bigcap_{\substack{1\le i<j\le q}}
    H_0^{ij}\colon\mu_i=\mu_j
  \]
  ¿Qué \(H_0^{ij}\) se rechazan?
\end{frame}
\subsection<article>{Criterio de Bonferroni}
\begin{frame}
  \frametitle<presentation> {Contrastes a posteriori}
  \mode<presentation>{criterio de Bonferroni\bigskip}
  \begin{itemize}
  \item hay \(\frac{q\,(q-1)}{2}\) parejas de grupos\bigskip
  \item contrastar \(H_0^{ij}\) a nivel 
    \(\alpha^*=\frac{2\alpha}{q\,(q-1)}\)
    \begin{multline*} 
      P\left[\text{rechazar alguna }H_0^{ij}\mid H_0^{\text{ANOVA}}\right]
      \leq \\ \sum_{\substack{1\le i<j\le q}}
      P\left[\text{rechazar }H_0^{ij}\mid H_0^{ij}\right]
      = \frac{q\,(q-1)}2\,\alpha^* = \alpha
    \end{multline*}
  \item estadígrafo de contraste
    \[
      \frac {\bar X_{i} - \bar X_{j}} 
      {\sqrt{\bigl(\frac1{n_i}+\frac1{n_j}\bigr)\text{CME}}}
      \stackrel{H_0^{ij}}{\equiv} t_{n-q}
    \]
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle<presentation> {Contrastes a posteriori}
  En el ejemplo
\begin{Schunk}
\begin{Sinput}
> q <- length (levels (aves$come))
> q 
\end{Sinput}
\begin{Soutput}
[1] 6
\end{Soutput}
\begin{Sinput}
> q * (q-1) / 2
\end{Sinput}
\begin{Soutput}
[1] 15
\end{Soutput}
\end{Schunk}
\end{frame}

\begin{frame}[fragile]
  \frametitle<presentation> {Contrastes a posteriori}
\begin{Schunk}
\begin{Sinput}
> pairwise.t.test (aves$peso, aves$come, "none")
\end{Sinput}
\begin{Soutput}
	Pairwise comparisons using t tests with pooled SD 

data:  aves$peso and aves$come 

          caseína fabona linaza har.hueso soja 
fabona    2e-09   -      -      -         -    
linaza    1e-05   0.02   -      -         -    
har.hueso 0.05    7e-06  0.01   -         -    
soja      7e-04   3e-04  0.20   0.17      -    
girasol   0.81    8e-10  6e-06  0.03      3e-04

P value adjustment method: none 
\end{Soutput}
\end{Schunk}
\end{frame}

\begin{frame}[fragile]
    \frametitle<presentation> {Contrastes a posteriori}
\begin{Schunk}
\begin{Sinput}
> pairwise.t.test (aves$peso, aves$come, "bonferroni")
\end{Sinput}
\begin{Soutput}
	Pairwise comparisons using t tests with pooled SD 

data:  aves$peso and aves$come 

          caseína fabona linaza har.hueso soja 
fabona    3e-08   -      -      -         -    
linaza    2e-04   0.228  -      -         -    
har.hueso 0.684   1e-04  0.202  -         -    
soja      0.010   0.005  1.000  1.000     -    
girasol   1.000   1e-08  9e-05  0.397     0.004

P value adjustment method: bonferroni 
\end{Soutput}
\end{Schunk}
\end{frame}
\subsection<article>{Criterio de Tukey}
\begin{frame}
    \frametitle<presentation> {Contrastes a posteriori}
    \mode<presentation>{criterio de Tukey\bigskip}
    \begin{itemize}
    \item basado en la distribución del \emph{rango estudentizado}
      \begin{itemize}
      \item \(Y_1,\dots,Y_q \sigue N(0,1)\) independientes
      \item \(Z\sigue\chi^2_r\) independiente de las \(Y_1,\dots,Y_q\)
      \item entonces \(\frac{Y_{(q)}-Y_{(1)}}{\sqrt{Z/r}}\sigue Q_{q,r}\)
        \quad\texttt{ptukey(,q,r)} en R
      \end{itemize}
    \item en \textsc{anova} para contrastar $H_0^{ij}$ se calcula el P-valor
      \[
        P\left[
          Q_{q,n-q}
          >
          \frac{|\bar X_{i}-\bar X_{j}|}{\sqrt{\text{CME}\,
              \bigl(\frac1{n_i}+\frac1{n_j}\bigr)}}
        \right]
      \]
    \end{itemize}
\end{frame}

\begin{frame}[fragile]
    \frametitle<presentation> {Contrastes a posteriori}
\begin{Schunk}
\begin{Sinput}
> r <- nrow (aves) - q              # q=6 r=65
> distro <- replicate (1e5,
  {
      medias <- rnorm (q)
      numerador <- diff (range (medias))
      denominador <- sqrt (rchisq (1, r) / r)
      numerador / denominador
  })
> alfas <- c (0.01, 0.025, 0.05, 0.1, 0.5,
              0.9, 0.95, 0.975, 0.99)
> rbind (sim = quantile (distro, alfas),
         num = qtukey (alfas, q, r))
\end{Sinput}
\begin{Soutput}
       1% 2.5%   5%  10%  50%  90%  95% 97.5%  99%
sim 0.861 1.06 1.25 1.48 2.49 3.76 4.16  4.54 5.00
num 0.862 1.06 1.24 1.48 2.49 3.75 4.15  4.52 4.97
\end{Soutput}
\end{Schunk}
\end{frame}

\begin{frame}[fragile]
    \frametitle<presentation> {Contrastes a posteriori}
\begin{Schunk}
\begin{Sinput}
> a <- aov (peso ~ come, aves)
> TukeyHSD (a)
\end{Sinput}
\end{Schunk}
\end{frame}

\scriptsize
\begin{frame}[fragile]
    \frametitle<presentation> {Contrastes a posteriori}
\begin{Schunk}
\begin{Soutput}
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = peso ~ come, data = aves)

$come
                     diff     lwr   upr p adj
fabona-caseína    -163.38 -232.35 -94.4 0.000
linaza-caseína    -104.83 -170.59 -39.1 0.000
har.hueso-caseína  -46.67 -113.91  20.6 0.332
soja-caseína       -77.15 -140.52 -13.8 0.008
girasol-caseína      5.33  -60.42  71.1 1.000
linaza-fabona       58.55  -10.41 127.5 0.141
har.hueso-fabona   116.71   46.34 187.1 0.000
soja-fabona         86.23   19.54 152.9 0.004
girasol-fabona     168.72   99.75 237.7 0.000
har.hueso-linaza    58.16   -9.07 125.4 0.128
soja-linaza         27.68  -35.68  91.0 0.793
girasol-linaza     110.17   44.41 175.9 0.000
soja-har.hueso     -30.48  -95.38  34.4 0.739
girasol-har.hueso   52.01  -15.22 119.2 0.221
girasol-soja        82.49   19.13 145.9 0.004
\end{Soutput}
\end{Schunk}
\end{frame}
\normalsize

\subsection<article>{Incoherencias}
Es posible que se produzcan resultados contradictorios, es decir, 
que el \textsc{anova} rechace su \(H_0\) mientras que ningún contraste 
a posteriori por parejas dé resultado significativo, o viceversa.
Veamos un par de ejemplos.
\begin{frame}[fragile]
    \frametitle<presentation> {Contrastes a posteriori}
\begin{Schunk}
\begin{Sinput}
> set.seed (122)
\end{Sinput}
\end{Schunk}
\begin{Schunk}
\begin{Sinput}
> mu <- c (0, 0, 0.2)
> q <- length(mu)
> m <- 100
> g <- factor (rep (1:q, each=m))
> x <- unlist (lapply (mu, 
                       function (mui) rnorm (m, mui)))
> a <- aov (x ~ g)
> summary (a)
\end{Sinput}
\begin{Soutput}
             Df Sum Sq Mean Sq F value Pr(>F)  
g             2      9    4.29     3.8  0.024 *
Residuals   297    336    1.13                 
---
Signif. codes:  
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
\end{Soutput}
\end{Schunk}
\end{frame}
\begin{frame}[fragile]
    \frametitle<presentation> {Contrastes a posteriori}
\begin{Schunk}
\begin{Sinput}
> options (digits = 7)
> pairwise.t.test (x, g, "bonf")
\end{Sinput}
\begin{Soutput}
	Pairwise comparisons using t tests with pooled SD 

data:  x and g 

  1     2    
2 1.000 -    
3 0.056 0.050

P value adjustment method: bonferroni 
\end{Soutput}
\end{Schunk}
\end{frame}

\begin{frame}[fragile]
    \frametitle<presentation> {Contrastes a posteriori}
\begin{Schunk}
\begin{Sinput}
> boxplot (x ~ g)
\end{Sinput}
\end{Schunk}
\includegraphics{anova-017}
\end{frame}
\begin{frame}[fragile]
    \frametitle<presentation> {Contrastes a posteriori}
\begin{Schunk}
\begin{Sinput}
> boxplot (x ~ g, notch=TRUE)
\end{Sinput}
\end{Schunk}
\includegraphics{anova-018}
\end{frame}


\begin{frame}[fragile]
    \frametitle<presentation> {Contrastes a posteriori}
\begin{Schunk}
\begin{Sinput}
> set.seed (614)
> mu <- c (0, 0, 0.2)
> q <- length(mu)
> m <- 100
> g <- factor (rep (1:q, each=m))
> x <- unlist (lapply (mu, 
                       function (mui) rnorm (m, mui)))
> a <- aov (x ~ g)
> summary (a)
\end{Sinput}
\begin{Soutput}
             Df Sum Sq Mean Sq F value Pr(>F)  
g             2   5.99   2.995   2.955 0.0536 .
Residuals   297 301.09   1.014                 
---
Signif. codes:  
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
\end{Soutput}
\end{Schunk}
\end{frame}
\begin{frame}[fragile]
    \frametitle<presentation> {Contrastes a posteriori}
\begin{Schunk}
\begin{Sinput}
> pairwise.t.test (x, g, "bonf")
\end{Sinput}
\begin{Soutput}
	Pairwise comparisons using t tests with pooled SD 

data:  x and g 

  1     2    
2 0.546 -    
3 0.048 0.831

P value adjustment method: bonferroni 
\end{Soutput}
\end{Schunk}
\end{frame}
\begin{frame}[fragile]
    \frametitle<presentation> {Contrastes a posteriori}
\begin{Schunk}
\begin{Sinput}
> boxplot (x ~ g)
\end{Sinput}
\end{Schunk}
\includegraphics{anova-021}
\end{frame}
\begin{frame}[fragile]
    \frametitle<presentation> {Contrastes a posteriori}
\begin{Schunk}
\begin{Sinput}
> boxplot (x ~ g, notch=TRUE)
\end{Sinput}
\end{Schunk}
\includegraphics{anova-022}
\end{frame}

\begin{frame}[fragile]
  \frametitle<presentation> {Contrastes a posteriori}
\begin{Schunk}
\begin{Sinput}
> alfa <- 0.05 ; mu <- c (0, 0, 0.2) ; q <- length(mu)
> m <- 100 ; g <- factor (rep (1:q, each=m))
> table (data.frame (t (replicate (10000, {
      x <- unlist (lapply (mu,
              function (mui) rnorm (m, mui)))
      a <- aov (x ~ g)
      H1anova <- summary(a) [[1]] ["g", "Pr(>F)"] < alfa
      H1bonfe <- any (na.omit (c (pairwise.t.test 
                                  (x,g,"bonf")$p.value))
                      < alfa)
      c (H1anova=H1anova, H1bonfe=H1bonfe)
  }))))
\end{Sinput}
\begin{Soutput}
       H1bonfe
H1anova FALSE TRUE
  FALSE  7099   37
  TRUE    308 2556
\end{Soutput}
\end{Schunk}
\end{frame}

\subsection<article>{Apéndice: cómo hallar la semillas}
Para encontrar muestras que produzcan los resultados anómalos,
primero guardamos toda la distribución simulada:
\begin{frame}[fragile]
  \frametitle<presentation> {Contrastes a posteriori}
\begin{Schunk}
\begin{Sinput}
> rechazos <- sapply (1:1000, function(semilla){
      set.seed (semilla)
      x <- unlist (lapply (mu,
              function (mui) rnorm (m, mui)))
      a <- aov (x ~ g)
      H1anova <- summary(a) [[1]] ["g", "Pr(>F)"] < alfa
      H1bonfe <- any (na.omit (c (pairwise.t.test 
                                  (x,g,"bonf")$p.value))
                      < alfa)
      c (H1anova=H1anova, H1bonfe=H1bonfe)
  })
\end{Sinput}
\end{Schunk}
\end{frame}
A continuación, buscamos qué columnas contienen TRUE-FALSE o FALSE-TRUE:
\begin{frame}[fragile]
  \frametitle<presentation> {Contrastes a posteriori}
\begin{Schunk}
\begin{Sinput}
> options (width = 55)
> (semillas <- which (xor (rechazos[1,], rechazos[2,])))
\end{Sinput}
\begin{Soutput}
 [1] 122 129 153 218 226 246 267 268 343 369 371 377
[13] 383 397 414 437 457 513 610 614 619 646 654 658
[25] 670 677 706 741 744 762 777 779 793 895 906 930
[37] 973
\end{Soutput}
\begin{Sinput}
> rechazos [, semillas]
\end{Sinput}
\begin{Soutput}
         [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]
H1anova  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
H1bonfe FALSE FALSE FALSE FALSE FALSE FALSE FALSE
         [,8]  [,9] [,10] [,11] [,12] [,13] [,14]
H1anova  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
H1bonfe FALSE FALSE FALSE FALSE FALSE FALSE FALSE
        [,15] [,16] [,17] [,18] [,19] [,20] [,21]
H1anova  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE
H1bonfe FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
        [,22] [,23] [,24] [,25] [,26] [,27] [,28]
H1anova  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
H1bonfe FALSE FALSE FALSE FALSE FALSE FALSE FALSE
        [,29] [,30] [,31] [,32] [,33] [,34] [,35]
H1anova  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
H1bonfe FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
        [,36] [,37]
H1anova  TRUE  TRUE
H1bonfe FALSE FALSE
\end{Soutput}
\end{Schunk}
\end{frame}

\end{document}
