% NOTE -- ONLY EDIT THE .Rnw FILE!!!  The .tex file is
% likely to be overwritten.

\SweaveOpts{engine=R,eps=FALSE,pdf=TRUE,width=10,height=6.5,strip.white=all}
\SweaveOpts{prefix=TRUE,prefix.string=figs/item,include=TRUE}
\setkeys{Gin}{width=\textwidth}
<<preliminaries,echo=FALSE,results=hide>>=
options(width=65,show.signif.stars=FALSE)
library(lattice)
library(Matrix)
library(lme4)
#lattice.options(default.theme = standard.theme(color = FALSE))
@ 

\begin{frame}
  \frametitle{Item Response Models}
  \begin{itemize}
  \item Models for binary (or ordered categorical) data that are
    cross-classified according to subject and item are sometimes
    called \Emph{Item Response} or \Emph{IRT} (Item Response Theory)
    models.
  \item There is a long history of models for such data with many
    contributors.  Only recently have statisticians become aware of
    this literature and considered how such models could be framed in
    the context of GLMMs.
  \item Even when approaching IRT models as GLMMs they were not
    expressed as GLMMs with crossed random effects, because of
    software limitations.
  \item Because \code{glmer} can fit GLMMs with crossed random
    effects, we can approach such models as GLMMs with random effects
    for subject and item.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Data from a study of verbal aggression}
  \begin{itemize}
  \item Results on a study of verbal aggression, used as an example
    through the book \Emph{Expanatory Item Response Models}, edited by
    De Boeck and Wilson (Springer, 2004) are available as the data set
    \code{VerbAgg}, in the ``long'' format.
  \item The items correspond to scenarios for which the subject
    was asked if they would curse, scold or shout.
  \item The scenarios are classified according to the behavior mode
    (want versus do) and according to the situation (self-to-blame
    versus other-to-blame).
  \item The subjects are classified by sex. Each subject's score on a
    separately administered anger index (STAXI) is given.
  \item The response was recorded on a three-level ordinal scale
    (``no'', ``perhaps'' and ``yes''). We will consider a dichotomous
    version, \code{r2}.
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Structure of VerbAgg data}
  \begin{itemize}
  \item We also check that the item-level covariates and the
    person-level covariates are consistently defined.
  \end{itemize}
<<VerbAggstr>>=
str(VerbAgg)
stopifnot(nrow(unique(subset(VerbAgg, select = c(item, btype, situ, mode))))
          == 24,
          nrow(unique(subset(VerbAgg, select = c(id, Anger, Gender))))
          == 316)
@ 
\end{frame}

\begin{frame}[fragile]
  \frametitle{Influence of item-level covariates}
  \begin{itemize}
  \item We can check the proportions of responses for combinations of
    item-level covariates
  \end{itemize}
<<VerbAggTbl>>=
round(100 * ftable(prop.table(xtabs(~ mode + situ + resp, VerbAgg), 1:2)), 1)
@
\end{frame}


\begin{frame}[fragile]
  \frametitle{Influence of person-level covariates}
<<VerbAggxy,fig=TRUE,echo=FALSE>>=
print(xyplot(resp ~ Anger, VerbAgg, type = c("g", "smooth"),
             groups = Gender, auto.key =
             list(columns = 2, points = FALSE, lines = TRUE),
             xlab = "Anger Index (STAXI)", ylab = NULL,
             aspect = 1.4))
@     
\end{frame}

\begin{frame}[fragile]
  \frametitle{Initial model fit}
<<VerbAggm1,echo=FALSE>>=
print(fm1 <- glmer(r2 ~ Anger * Gender + situ + btype + mode + (1|id) + (1|item), VerbAgg, binomial), corr = FALSE)
@     
\end{frame}

\begin{frame}[fragile]
  \frametitle{Removing non-significant gender effects}
<<VerbAggm2,echo=FALSE>>=
print(fm2 <- glmer(r2 ~ Anger + situ + btype + mode + (1|id) + (1|item), VerbAgg, binomial), corr = FALSE)
@     
\end{frame}


\begin{frame}[fragile]
  \frametitle{Allowing situational/behavior random effects by person} 
<<VerbAggm3,echo=FALSE>>=
print(fm3 <- glmer(r2 ~ Anger + situ + btype + mode + (1|id:btype) + (1|id:situ) + (1|id:mode) + (1|id) + (1|item), VerbAgg, binomial), corr = FALSE)
@
\end{frame}

\begin{frame}[fragile]
  \frametitle{Item-specific random effects}
<<itemREplot3,fig=TRUE,echo=FALSE>>=
print(dotplot(ranef(fm3, post = TRUE), strip = FALSE)$item)
@               
\end{frame}

\begin{frame}[fragile]
  \frametitle{Person-specific random effects - Intercept}
<<idREplot3,fig=TRUE,echo=FALSE,height=7.8>>=
print(dotplot(ranef(fm3, post = TRUE), scales = list(y = list(draw=FALSE)), strip = FALSE)$id)
@               
\end{frame}

\begin{frame}[fragile]
  \frametitle{Correlated random effects by person} 
<<VerbAggm4,echo=FALSE>>=
print(fm4 <- glmer(r2 ~ Anger + situ + btype + mode + (1+situ+btype+mode|id) + (1|item), VerbAgg, binomial), corr = FALSE)
@
\end{frame}

