% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{article}
\usepackage{amsmath,amssymb}
\usepackage{lmodern}
\usepackage{iftex}
\ifPDFTeX
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
  \usepackage{unicode-math}
  \defaultfontfeatures{Scale=MatchLowercase}
  \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
  \usepackage[]{microtype}
  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
  \IfFileExists{parskip.sty}{%
    \usepackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
  \KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\hypersetup{
  pdftitle={Homework 03 Solutions},
  hidelinks,
  pdfcreator={LaTeX via pandoc}}
\urlstyle{same} % disable monospaced font for URLs
\usepackage[margin=1in]{geometry}
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{hyperref}
\usepackage{xcolor}
\DeclareMathOperator{\Cov}{Cov}
\DeclareMathOperator{\Corr}{Corr}
\DeclareMathOperator{\Var}{Var}
\DeclareMathOperator{\upmodels}{\perp \!\!\! \perp}
\ifLuaTeX
  \usepackage{selnolig}  % disable illegal ligatures
\fi

\title{Homework 03 Solutions}
\author{}
\date{\vspace{-2.5em}2022-02-16}

\begin{document}
\maketitle

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(arm)  }\DocumentationTok{\#\# includes lme4, MASS, Matrix}
\FunctionTok{library}\NormalTok{(ggplot2); }\FunctionTok{theme\_set}\NormalTok{(}\FunctionTok{theme\_bw}\NormalTok{())}
\FunctionTok{library}\NormalTok{(gridExtra) }\DocumentationTok{\#\# to arrange ggplots...}
\FunctionTok{library}\NormalTok{(HLMdiag)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Warning: package 'HLMdiag' was built under R version 4.1.2
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(cAIC4)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Warning: package 'cAIC4' was built under R version 4.1.2
\end{verbatim}

\def\red#1{\textcolor{red}{#1}}
\def\blue#1{\textcolor{blue}{#1}}
\let\bar\overline

\hypertarget{problem-1.}{%
\section{Problem 1.}\label{problem-1.}}

Return to the CD4 data from HW02, and consider the model

\begin{verbatim}
lmer.1 <- lmer(sqrt.CD4PCT ~ 1 + VISIT * treatmnt + (1 + VISIT | newpid),
               data=cd4)
\end{verbatim}

Bring together all the residuals (except random effects residuals) that
we have talked about, using library(HLMdiag):

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{cd4 }\OtherTok{\textless{}{-}} \FunctionTok{read.csv}\NormalTok{(}\StringTok{"allvar.csv"}\NormalTok{,}\AttributeTok{header=}\NormalTok{T)}

\DocumentationTok{\#\# It\textquotesingle{}s worth doing a little exploration of the data here,}
\DocumentationTok{\#\# especially to look at missing values.}

\FunctionTok{apply}\NormalTok{(cd4,}\DecValTok{2}\NormalTok{,}\ControlFlowTok{function}\NormalTok{(x) }\FunctionTok{mean}\NormalTok{(}\FunctionTok{is.na}\NormalTok{(x)))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##       VISIT      newpid       VDATE      CD4PCT         arv      visage 
## 0.000000000 0.000000000 0.000000000 0.142743222 0.103668262 0.109250399 
##    treatmnt      CD4CNT     baseage 
## 0.000000000 0.146730463 0.007177033
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\DocumentationTok{\#\# we only need the variables newpid, VISIT, visage, CD4PCT and treatmnt}
\DocumentationTok{\#\# so let\textquotesingle{}s get rid of some (but not all) missing data problems}
\DocumentationTok{\#\# by deleting the other variables}

\NormalTok{cd4 }\OtherTok{\textless{}{-}} \FunctionTok{with}\NormalTok{(cd4,}\FunctionTok{data.frame}\NormalTok{(}\AttributeTok{newpid=}\NormalTok{newpid,}
                           \AttributeTok{VISIT=}\NormalTok{VISIT, }
                           \AttributeTok{visage=}\NormalTok{visage, }
                           \AttributeTok{CD4PCT=}\NormalTok{CD4PCT, }
                           \AttributeTok{treatmnt=}\NormalTok{treatmnt))}

\DocumentationTok{\#\# and since we still have some missing data, we will just }
\DocumentationTok{\#\# delete the rows that continue to have NA\textquotesingle{}s (not a great}
\DocumentationTok{\#\# practice in general, but good enough for this exercise...)}
\NormalTok{cd4 }\OtherTok{\textless{}{-}}\NormalTok{ cd4[}\SpecialCharTok{!}\FunctionTok{apply}\NormalTok{(cd4,}\DecValTok{1}\NormalTok{,}\ControlFlowTok{function}\NormalTok{(x) }\FunctionTok{any}\NormalTok{(}\FunctionTok{is.na}\NormalTok{(x))),]}

\DocumentationTok{\#\# set up the sqrt of the response variable...}
\NormalTok{cd4}\SpecialCharTok{$}\NormalTok{sqrt.CD4PCT }\OtherTok{\textless{}{-}} \FunctionTok{sqrt}\NormalTok{(cd4}\SpecialCharTok{$}\NormalTok{CD4PCT)}

\DocumentationTok{\#\# and for coloring ggplot elements it will better if treatmnt is a factor...}
\NormalTok{cd4}\SpecialCharTok{$}\NormalTok{treatmnt }\OtherTok{\textless{}{-}} \FunctionTok{as.factor}\NormalTok{(cd4}\SpecialCharTok{$}\NormalTok{treatmnt)}

\DocumentationTok{\#\# select the first 12 kids...}
\NormalTok{first}\FloatTok{.12} \OtherTok{\textless{}{-}}\NormalTok{ (cd4}\SpecialCharTok{$}\NormalTok{newpid }\SpecialCharTok{\textless{}=} \DecValTok{12}\NormalTok{)}

\DocumentationTok{\#\# Fit the base model for this exercise...}
\FunctionTok{display}\NormalTok{(lmer}\FloatTok{.1} \OtherTok{\textless{}{-}} \FunctionTok{lmer}\NormalTok{(sqrt.CD4PCT }\SpecialCharTok{\textasciitilde{}} \DecValTok{1} \SpecialCharTok{+}\NormalTok{ VISIT }\SpecialCharTok{*}\NormalTok{ treatmnt }\SpecialCharTok{+}\NormalTok{ (}\DecValTok{1} \SpecialCharTok{+}\NormalTok{ VISIT }\SpecialCharTok{|}\NormalTok{ newpid),}
               \AttributeTok{data=}\NormalTok{cd4))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## lmer(formula = sqrt.CD4PCT ~ 1 + VISIT * treatmnt + (1 + VISIT | 
##     newpid), data = cd4)
##                 coef.est coef.se
## (Intercept)      4.71     0.13  
## VISIT           -0.03     0.01  
## treatmnt2        0.14     0.19  
## VISIT:treatmnt2  0.01     0.01  
## 
## Error terms:
##  Groups   Name        Std.Dev. Corr  
##  newpid   (Intercept) 1.40           
##           VISIT       0.05     -0.10 
##  Residual             0.72           
## ---
## number of obs: 1075, groups: newpid, 251
## AIC = 3149.4, DIC = 3093
## deviance = 3113.2
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{r}\FloatTok{.1} \OtherTok{\textless{}{-}} \FunctionTok{hlm\_resid}\NormalTok{(lmer}\FloatTok{.1}\NormalTok{,}\AttributeTok{level=}\DecValTok{1}\NormalTok{,}\AttributeTok{include.ls=}\NormalTok{F)}
\NormalTok{r}\FloatTok{.1}\NormalTok{s }\OtherTok{\textless{}{-}} \FunctionTok{hlm\_resid}\NormalTok{(lmer}\FloatTok{.1}\NormalTok{,}\AttributeTok{level=}\DecValTok{1}\NormalTok{,}\AttributeTok{include.ls=}\NormalTok{F,}\AttributeTok{standardize=}\NormalTok{T)}
\NormalTok{r}\FloatTok{.2} \OtherTok{\textless{}{-}} \FunctionTok{hlm\_resid}\NormalTok{(lmer}\FloatTok{.1}\NormalTok{,}\AttributeTok{level=}\StringTok{"newpid"}\NormalTok{,}\AttributeTok{include.ls=}\NormalTok{F)}
\NormalTok{r}\FloatTok{.2}\NormalTok{s }\OtherTok{\textless{}{-}} \FunctionTok{hlm\_resid}\NormalTok{(lmer}\FloatTok{.1}\NormalTok{,}\AttributeTok{level=}\StringTok{"newpid"}\NormalTok{,}\AttributeTok{include.ls=}\NormalTok{F,}\AttributeTok{standardize=}\NormalTok{T)}
\FunctionTok{names}\NormalTok{(r}\FloatTok{.1}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] "id"          "sqrt.CD4PCT" "VISIT"       "treatmnt"    "newpid"     
## [6] ".resid"      ".fitted"     ".mar.resid"  ".mar.fitted"
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{names}\NormalTok{(r}\FloatTok{.1}\NormalTok{s)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] "id"              "sqrt.CD4PCT"     "VISIT"           "treatmnt"       
## [5] "newpid"          ".std.resid"      ".fitted"         ".chol.mar.resid"
## [9] ".mar.fitted"
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{names}\NormalTok{(r}\FloatTok{.2}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] "newpid"           ".ranef.intercept" ".ranef.visit"
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{names}\NormalTok{(r}\FloatTok{.2}\NormalTok{s)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] "newpid"               ".std.ranef.intercept" ".std.ranef.visit"
\end{verbatim}

\hypertarget{problem-1a.}{%
\subsection{Problem 1(a).}\label{problem-1a.}}

Make a facets plot of the marginal residuals, as a function of the
marginal fitted values (use \texttt{scales="free\_x"} if needed to make
the plot legible). Explain in a sentence or two why a facets plot is not
very useful for assessing model fit for this problem, whether we look at
the first 12 children, or all 251 children).

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{ggplot}\NormalTok{(r}\FloatTok{.1}\NormalTok{[first}\FloatTok{.12}\NormalTok{,],}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{.mar.fitted,}\AttributeTok{y=}\NormalTok{.mar.resid)) }\SpecialCharTok{+}
  \FunctionTok{facet\_wrap}\NormalTok{( }\SpecialCharTok{\textasciitilde{}}\NormalTok{ newpid,}\AttributeTok{scales=}\StringTok{"free\_x"}\NormalTok{) }\SpecialCharTok{+}
  \FunctionTok{geom\_point}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{color=}\NormalTok{treatmnt)) }\SpecialCharTok{+}
  \FunctionTok{geom\_abline}\NormalTok{(}\AttributeTok{intercept=}\DecValTok{0}\NormalTok{,}\AttributeTok{slope=}\DecValTok{0}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw03-solutions_files/figure-latex/unnamed-chunk-8-1.pdf}

\it
There are very few data points per child, and so the residual plot within facet for each child contains almost no information about fit. (Having said that, there are some hints of misfit, to the extent that several individual children's residuals are entirely above, or entirely below, the zero line.)
\rm

\hypertarget{problem-1b}{%
\subsection{Problem 1(b)}\label{problem-1b}}

Make an ungrouped (that is, no facets) scatter plot of marginal
residuals as a function of marginal fitted values, using the full data
set (not just the first 12 children). Color the points for
\texttt{treatmnt=1} kids and treatmnt=2 kids with different colors.
Overlay a smooth fit (\texttt{geom\_smooth} is the easist to use here).

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{ggplot}\NormalTok{(r}\FloatTok{.1}\NormalTok{,}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{.mar.fitted,}\AttributeTok{y=}\NormalTok{.mar.resid)) }\SpecialCharTok{+}
  \FunctionTok{geom\_point}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{color=}\NormalTok{treatmnt)) }\SpecialCharTok{+}
  \FunctionTok{geom\_smooth}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw03-solutions_files/figure-latex/unnamed-chunk-9-1.pdf}

Explain, in a couple of sentences (optionally with some math):

\begin{itemize}
\item What is causing the dominant structure in this plot, and why that dominant structure is essentially irrelevant for checking the relationship between \texttt{sqrt.CD4PCT} and \texttt{VISIT};

\it

The dominant structure in the data is the way the residuals are grouped horizontally into 14 groups---7 groups for \texttt{treatmnt=1} and 7 for \texttt{treatmnt=2}.  They are caused by the fact that our main predictor variable is \texttt{VISIT}, which only takes on 7 values. The spacing of the groups is not the same between \texttt{treatmnt=1} and  \texttt{treatmnt=2} because the model contains a \texttt{treatmnt} $\times$ \texttt{VISIT} interaction, so that the slope on \texttt{VISIT} is different for children in the \texttt{treatmnt=1} and  \texttt{treatmnt=2} groups.  

This does not affect our assessment of the fit of the model, since to check the model fit we are interested in vertical patterns (trends, curves, outliers, changing variance, etc.) in the residuals, not horizontal (grouping) patterns.

\rm

and  
\item What in this plot makes you happy or unhappy about having a linear relationship between  \texttt{sqrt.CD4PCT} and \texttt{VISIT} in the model.

\it

The plot actually looks pretty wonderful.  The variance of the residuals looks pretty much constant, as a function of the fitted values, and the trend modeled with the smooth really does look like a horizontal line at 0.

\rm
\end{itemize}

\hypertarget{problem-1c}{%
\subsection{Problem 1(c)}\label{problem-1c}}

Make an ungrouped (that is, no facets) scatter plot of conditional
residuals as a function of conditional fitted values, using the full
data set (not just the first 12 children). Color the points for
\texttt{treatmnt=1} kids and \texttt{treatmnt=2} kids with different
colors. Overlay a smooth fit (\texttt{geom\_smooth} is the easist to use
here).

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{ggplot}\NormalTok{(r}\FloatTok{.1}\NormalTok{,}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{.fitted,}\AttributeTok{y=}\NormalTok{.resid)) }\SpecialCharTok{+}
  \FunctionTok{geom\_point}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{color=}\NormalTok{treatmnt)) }\SpecialCharTok{+}
  \FunctionTok{geom\_smooth}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw03-solutions_files/figure-latex/unnamed-chunk-10-1.pdf}

Explain, in a couple of sentences (optionally with some math):

\begin{itemize}
\item Why the dominant structure in the marginal residuals is not also present in this plot of conditional residuals

\it

The marginal residuals plot shows
\rm
\begin{eqnarray*}
\mbox{.mar.resid}_i & = & \mbox{sqrt.CD4PCT}_i - \hat\beta_0 - \hat\beta_1\cdot\mbox{VISIT}_i - 
\hat\beta_3\cdot\mbox{treatmnt}_{j[i]} - \hat\beta_4\cdot\mbox{VISIT}_i\cdot\mbox{treatmnt}_{j[i]}\\
& \mbox{vs.}& \\
\mbox{.mar.fitted}_i & = & \hat\beta_0 + \hat\beta_1\cdot\mbox{VISIT}_i + 
\hat\beta_3\cdot\mbox{treatmnt}_{j[i]} + \hat\beta_4\cdot\mbox{VISIT}_i\cdot\mbox{treatmnt}_{j[i]}
\end{eqnarray*}
\it
and because \texttt{VISIT} and \texttt{treatmnt} are both discrete, the fitted values will only take on a few discrete values, causing the horizontal grouping in the marginal plot.

The conditional residuals plot shows
\rm
\begin{eqnarray*}
\mbox{.resid}_i & = & \mbox{sqrt.CD4PCT}_i - \hat\beta_0 - \hat\beta_1\cdot\mbox{VISIT}_i - 
\hat\beta_3\cdot\mbox{treatmnt}_{j[i]} - \hat\beta_4\cdot\mbox{VISIT}_i\cdot\mbox{treatmnt}_{j[i]}\\
&& - \eta_{0j[i]} - \eta_{1j[i]}\cdot\mbox{VISIT}_i \\
& \mbox{vs.}  & \\
\mbox{.fitted}_i & = & \hat\beta_0 + \hat\beta_1\cdot\mbox{VISIT}_i +
\hat\beta_3\cdot\mbox{treatmnt}_{j[i]} + \hat\beta_4\cdot\mbox{VISIT}_i\cdot\mbox{treatmnt}_{j[i]}\\
&& + \eta_{0j[i]} + \eta_{1j[i]}\cdot\mbox{VISIT}_i
\end{eqnarray*}
\it
The additional continuous (because the $\eta$'s are continuous) terms $\eta_{0j[i]} + \eta_{1j[i]}\cdot\mbox{VISIT}_i$ essentially "jitter" the fitted values so they no longer show the horizonal grouping structure of the marginal residuals.


\rm

\item What might be causing the trend you see in this plot to be different from the trend in the plot of the marginal residuals.

\it

The trend shows that when the conditional fitted value $\hat y_{cond}$ is larger, it tends to underpredict $y=\mbox{sqrt.CD4PCT}$ (positive residual), and when $\hat y_{cond}$ is smaller, it tends to overpredict $y$ (negative residual).

The only difference between the two plots is the presence of the $\eta_{0j[i]} + \eta_{1j[i]}\cdot\mbox{VISIT}_i$ terms.

In ordinary regression,we know that residuals and fitted values will be uncorrelated: that is why we don't see overall increasing or overall decreasing trends in the marginal residual plots (as far as the math is concerned, marginal residuals and marginal fitted values behave exactly like ordinary regression).  The fact that conditional residuals and conditional fitted values are correlated is due to the values we use for the $\eta$'s: $\hat\eta=E[\eta|\mbox{the data}]$, and "the data" includes $y$.  Since, therefore, the conditional fitted values depend on $y$, it's not surprising that the residuals and fitted values are correlated.

The plots below explore the association between the $\hat\eta$'s and $y$.  We can see that, although there's not much association between $\hat\eta_{1j}$ and $y$, there is quite a strong association between $\hat\eta_{0j}$ and $y$. 

\rm

\end{itemize}

\vspace*{1em}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{y.grouped }\OtherTok{\textless{}{-}} \FunctionTok{with}\NormalTok{(cd4,}\FunctionTok{sapply}\NormalTok{(}\FunctionTok{split}\NormalTok{(sqrt.CD4PCT,newpid),mean))}
\NormalTok{tx.grouped }\OtherTok{\textless{}{-}} \FunctionTok{with}\NormalTok{(cd4,}\FunctionTok{sapply}\NormalTok{(}\FunctionTok{split}\NormalTok{(treatmnt,newpid),}\ControlFlowTok{function}\NormalTok{(x) x[}\DecValTok{1}\NormalTok{]))}

\NormalTok{g1 }\OtherTok{\textless{}{-}} \FunctionTok{ggplot}\NormalTok{(r}\FloatTok{.2}\NormalTok{,}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{y.grouped,}\AttributeTok{y=}\NormalTok{.ranef.intercept)) }\SpecialCharTok{+}
  \FunctionTok{geom\_point}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{color=}\NormalTok{tx.grouped)) }\SpecialCharTok{+}
  \FunctionTok{xlab}\NormalTok{(}\StringTok{"mean(sqrt.CD4PCT) within child"}\NormalTok{) }\SpecialCharTok{+} 
  \FunctionTok{ylab}\NormalTok{(}\StringTok{"eta0"}\NormalTok{) }\SpecialCharTok{+}
  \FunctionTok{theme}\NormalTok{(}\AttributeTok{legend.position=}\StringTok{"none"}\NormalTok{)}

\NormalTok{g2 }\OtherTok{\textless{}{-}} \FunctionTok{ggplot}\NormalTok{(r}\FloatTok{.2}\NormalTok{,}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{y.grouped,}\AttributeTok{y=}\NormalTok{.ranef.visit)) }\SpecialCharTok{+}
  \FunctionTok{geom\_point}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{color=}\NormalTok{tx.grouped)) }\SpecialCharTok{+}
  \FunctionTok{xlab}\NormalTok{(}\StringTok{"mean(sqrt.CD4PCT) within child"}\NormalTok{) }\SpecialCharTok{+} 
  \FunctionTok{ylab}\NormalTok{(}\StringTok{"eta1"}\NormalTok{) }\SpecialCharTok{+}
  \FunctionTok{theme}\NormalTok{(}\AttributeTok{legend.position=}\StringTok{"none"}\NormalTok{)}

\FunctionTok{grid.arrange}\NormalTok{(g1,g2,}\AttributeTok{ncol=}\DecValTok{2}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw03-solutions_files/figure-latex/unnamed-chunk-11-1.pdf}

\clearpage

\hypertarget{problem-1d}{%
\subsection{Problem 1(d)}\label{problem-1d}}

Use standardized residuals and standardized random effects estimates to
assess the normality of \(\epsilon_i\), \(\eta_{0j}\) and \(\eta_{1j}\)
in the fitted model, and to check for any outliers. Include qq plots for
each, and accompany each plot with a sentence or two describing what is
good or bad in that plot.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{g1 }\OtherTok{\textless{}{-}} \FunctionTok{ggplot}\NormalTok{(r}\FloatTok{.1}\NormalTok{s,}\FunctionTok{aes}\NormalTok{(}\AttributeTok{sample=}\NormalTok{.std.resid)) }\SpecialCharTok{+} 
  \FunctionTok{geom\_qq}\NormalTok{() }\SpecialCharTok{+}
  \FunctionTok{geom\_qq\_line}\NormalTok{() }\SpecialCharTok{+}
  \FunctionTok{xlim}\NormalTok{(}\SpecialCharTok{{-}}\FloatTok{3.5}\NormalTok{,}\FloatTok{3.5}\NormalTok{) }\SpecialCharTok{+} 
  \FunctionTok{ylim}\NormalTok{(}\SpecialCharTok{{-}}\DecValTok{8}\NormalTok{,}\DecValTok{8}\NormalTok{) }\SpecialCharTok{+}
  \FunctionTok{xlab}\NormalTok{(}\StringTok{"Normal Quantiles"}\NormalTok{) }\SpecialCharTok{+} 
  \FunctionTok{ylab}\NormalTok{(}\StringTok{"Data Quantiles"}\NormalTok{) }\SpecialCharTok{+}
  \FunctionTok{ggtitle}\NormalTok{(}\StringTok{"Standardized conditional residuals"}\NormalTok{)}

\NormalTok{g2 }\OtherTok{\textless{}{-}} \FunctionTok{ggplot}\NormalTok{(r}\FloatTok{.1}\NormalTok{s,}\FunctionTok{aes}\NormalTok{(}\AttributeTok{sample=}\NormalTok{.chol.mar.resid)) }\SpecialCharTok{+} 
  \FunctionTok{geom\_qq}\NormalTok{() }\SpecialCharTok{+}
  \FunctionTok{geom\_qq\_line}\NormalTok{() }\SpecialCharTok{+}
  \FunctionTok{xlim}\NormalTok{(}\SpecialCharTok{{-}}\FloatTok{3.5}\NormalTok{,}\FloatTok{3.5}\NormalTok{) }\SpecialCharTok{+} 
  \FunctionTok{ylim}\NormalTok{(}\SpecialCharTok{{-}}\DecValTok{8}\NormalTok{,}\DecValTok{8}\NormalTok{) }\SpecialCharTok{+}
  \FunctionTok{xlab}\NormalTok{(}\StringTok{"Normal Quantiles"}\NormalTok{) }\SpecialCharTok{+} 
  \FunctionTok{ylab}\NormalTok{(}\StringTok{"Data Quantiles"}\NormalTok{) }\SpecialCharTok{+}
  \FunctionTok{ggtitle}\NormalTok{(}\StringTok{"Cholesky marginal residuals"}\NormalTok{)}

\NormalTok{g3 }\OtherTok{\textless{}{-}} \FunctionTok{ggplot}\NormalTok{(r}\FloatTok{.2}\NormalTok{s,}\FunctionTok{aes}\NormalTok{(}\AttributeTok{sample=}\NormalTok{.std.ranef.intercept)) }\SpecialCharTok{+} 
  \FunctionTok{geom\_qq}\NormalTok{() }\SpecialCharTok{+}
  \FunctionTok{geom\_qq\_line}\NormalTok{() }\SpecialCharTok{+}
  \FunctionTok{xlim}\NormalTok{(}\SpecialCharTok{{-}}\FloatTok{3.5}\NormalTok{,}\FloatTok{3.5}\NormalTok{) }\SpecialCharTok{+} 
  \FunctionTok{ylim}\NormalTok{(}\SpecialCharTok{{-}}\DecValTok{8}\NormalTok{,}\DecValTok{8}\NormalTok{) }\SpecialCharTok{+}
  \FunctionTok{xlab}\NormalTok{(}\StringTok{"Normal Quantiles"}\NormalTok{) }\SpecialCharTok{+} 
  \FunctionTok{ylab}\NormalTok{(}\StringTok{"Data Quantiles"}\NormalTok{) }\SpecialCharTok{+}
  \FunctionTok{ggtitle}\NormalTok{(}\StringTok{"Standardized eta0"}\NormalTok{)}

\NormalTok{g4 }\OtherTok{\textless{}{-}} \FunctionTok{ggplot}\NormalTok{(r}\FloatTok{.2}\NormalTok{s,}\FunctionTok{aes}\NormalTok{(}\AttributeTok{sample=}\NormalTok{.std.ranef.visit)) }\SpecialCharTok{+} 
  \FunctionTok{geom\_qq}\NormalTok{() }\SpecialCharTok{+}
  \FunctionTok{geom\_qq\_line}\NormalTok{() }\SpecialCharTok{+}
  \FunctionTok{xlim}\NormalTok{(}\SpecialCharTok{{-}}\FloatTok{3.5}\NormalTok{,}\FloatTok{3.5}\NormalTok{) }\SpecialCharTok{+} 
  \FunctionTok{ylim}\NormalTok{(}\SpecialCharTok{{-}}\DecValTok{8}\NormalTok{,}\DecValTok{8}\NormalTok{) }\SpecialCharTok{+}
  \FunctionTok{xlab}\NormalTok{(}\StringTok{"Normal Quantiles"}\NormalTok{) }\SpecialCharTok{+} 
  \FunctionTok{ylab}\NormalTok{(}\StringTok{"Data Quantiles"}\NormalTok{) }\SpecialCharTok{+}
  \FunctionTok{ggtitle}\NormalTok{(}\StringTok{"Standardized eta1"}\NormalTok{)}

\FunctionTok{grid.arrange}\NormalTok{(g1,g2,g3,g4,}\AttributeTok{ncol=}\DecValTok{2}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Warning: Removed 1 rows containing missing values (geom_point).
\end{verbatim}

\includegraphics{hw03-solutions_files/figure-latex/unnamed-chunk-12-1.pdf}
\it

The ``middle'' part of all four plots seem to follow the normal
distribution fairly well, but the tails fail in various ways:

\begin{itemize}

\item The standardized conditional and marginal residuals both have longer tails than the Normal distribution.  There seem to be two low outliers and two high outliers among these residuals.

\item The Cholesky marginal residual seem a little more extreme that the standardized conditional residuals, which make sense since $X\beta + Z\eta$ should do a better job predicting $y$ than $X\beta$ alone.   There seem to be two low outliers and two high outliers among these residuals.  It would be useful to know if these are the same data points as in the first QQ plot.

\item The standardized $\hat\eta_0$'s have a long left tail and slighltly short right tail.  In fact the left tail for $\eta_0$ is the most extreme of all four plots.  There don't appear to be any clear outliers among the $\hat\eta_0$'s.

\item The distribution of $\hat\eta_1$ is closest to Normal, with a right tail that is only a little longer than the Normal, and a left tail that is the least extreme of all four plots.  There may be one low outlier among the $\hat\eta_1$'s.

\end{itemize}

\rm

\hypertarget{problem-2.}{%
\subsection{Problem 2.}\label{problem-2.}}

Continuing with the CD4 data\ldots

\hypertarget{problem-2a}{%
\subsection{Problem 2(a)}\label{problem-2a}}

Make a table giving values of AIC, BIC, DIC, and cAIC (you compared two
of these on the last assignment,using just AIC, BIC and DIC):

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{lmer}\FloatTok{.1} \OtherTok{\textless{}{-}} \FunctionTok{lmer}\NormalTok{(sqrt.CD4PCT }\SpecialCharTok{\textasciitilde{}} \DecValTok{1} \SpecialCharTok{+}\NormalTok{ visage }\SpecialCharTok{+}\NormalTok{            (}\DecValTok{1}\SpecialCharTok{+}\NormalTok{visage}\SpecialCharTok{|}\NormalTok{newpid),}\AttributeTok{data=}\NormalTok{cd4,}\AttributeTok{REML=}\NormalTok{F)}
\NormalTok{lmer}\FloatTok{.2} \OtherTok{\textless{}{-}} \FunctionTok{lmer}\NormalTok{(sqrt.CD4PCT }\SpecialCharTok{\textasciitilde{}} \DecValTok{1} \SpecialCharTok{+}\NormalTok{ visage }\SpecialCharTok{+}\NormalTok{ treatmnt }\SpecialCharTok{+}\NormalTok{ (}\DecValTok{1}\SpecialCharTok{+}\NormalTok{visage}\SpecialCharTok{|}\NormalTok{newpid),}\AttributeTok{data=}\NormalTok{cd4,}\AttributeTok{REML=}\NormalTok{F)}
\NormalTok{lmer}\FloatTok{.3} \OtherTok{\textless{}{-}} \FunctionTok{lmer}\NormalTok{(sqrt.CD4PCT }\SpecialCharTok{\textasciitilde{}} \DecValTok{1} \SpecialCharTok{+}\NormalTok{ visage }\SpecialCharTok{*}\NormalTok{ treatmnt }\SpecialCharTok{+}\NormalTok{ (}\DecValTok{1}\SpecialCharTok{+}\NormalTok{visage}\SpecialCharTok{|}\NormalTok{newpid),}\AttributeTok{data=}\NormalTok{cd4,}\AttributeTok{REML=}\NormalTok{F)}
\NormalTok{lmer}\FloatTok{.4} \OtherTok{\textless{}{-}} \FunctionTok{lmer}\NormalTok{(sqrt.CD4PCT }\SpecialCharTok{\textasciitilde{}} \DecValTok{1} \SpecialCharTok{+}\NormalTok{ VISIT  }\SpecialCharTok{+}\NormalTok{            (}\DecValTok{1}\SpecialCharTok{+}\NormalTok{VISIT}\SpecialCharTok{|}\NormalTok{newpid),}\AttributeTok{data=}\NormalTok{cd4,}\AttributeTok{REML=}\NormalTok{F)}
\NormalTok{lmer}\FloatTok{.5} \OtherTok{\textless{}{-}} \FunctionTok{lmer}\NormalTok{(sqrt.CD4PCT }\SpecialCharTok{\textasciitilde{}} \DecValTok{1} \SpecialCharTok{+}\NormalTok{ VISIT  }\SpecialCharTok{+}\NormalTok{ treatmnt }\SpecialCharTok{+}\NormalTok{ (}\DecValTok{1}\SpecialCharTok{+}\NormalTok{VISIT}\SpecialCharTok{|}\NormalTok{newpid),}\AttributeTok{data=}\NormalTok{cd4,}\AttributeTok{REML=}\NormalTok{F)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, :
## Model failed to converge with max|grad| = 0.00584617 (tol = 0.002, component 1)
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{lmer}\FloatTok{.6} \OtherTok{\textless{}{-}} \FunctionTok{lmer}\NormalTok{(sqrt.CD4PCT }\SpecialCharTok{\textasciitilde{}} \DecValTok{1} \SpecialCharTok{+}\NormalTok{ VISIT  }\SpecialCharTok{*}\NormalTok{ treatmnt }\SpecialCharTok{+}\NormalTok{ (}\DecValTok{1}\SpecialCharTok{+}\NormalTok{VISIT}\SpecialCharTok{|}\NormalTok{newpid),}\AttributeTok{data=}\NormalTok{cd4,}\AttributeTok{REML=}\NormalTok{F)}

\NormalTok{DIC }\OtherTok{\textless{}{-}} \ControlFlowTok{function}\NormalTok{(M) \{}
         \ControlFlowTok{if}\NormalTok{(}\FunctionTok{class}\NormalTok{(M)}\SpecialCharTok{==}\StringTok{"lm"}\NormalTok{) \{ x }\OtherTok{\textless{}{-}} \FunctionTok{AIC}\NormalTok{(M) \}}
         \ControlFlowTok{else}\NormalTok{ \{ x }\OtherTok{\textless{}{-}} \FunctionTok{unname}\NormalTok{(}\FunctionTok{extractDIC}\NormalTok{(M)) \}}
         \FunctionTok{return}\NormalTok{(x)}
\NormalTok{       \}}
  
\NormalTok{CAIC }\OtherTok{\textless{}{-}} \ControlFlowTok{function}\NormalTok{(M) \{}\FunctionTok{cAIC}\NormalTok{(M)}\SpecialCharTok{$}\NormalTok{caic\}}

\NormalTok{IC }\OtherTok{\textless{}{-}} \ControlFlowTok{function}\NormalTok{(M) \{}
\NormalTok{    x }\OtherTok{\textless{}{-}}\FunctionTok{c}\NormalTok{(}\AttributeTok{AIC=}\FunctionTok{AIC}\NormalTok{(M),}\AttributeTok{BIC=}\FunctionTok{BIC}\NormalTok{(M),}\AttributeTok{DIC=}\FunctionTok{DIC}\NormalTok{(M),}\AttributeTok{cAIC=}\FunctionTok{CAIC}\NormalTok{(M))}
    \FunctionTok{return}\NormalTok{(x)}
\NormalTok{\}}

\NormalTok{IC.table }\OtherTok{\textless{}{-}} \ControlFlowTok{function}\NormalTok{(...) \{}
\NormalTok{    Mlist }\OtherTok{\textless{}{-}} \FunctionTok{list}\NormalTok{(...)}
\NormalTok{    x }\OtherTok{\textless{}{-}} \FunctionTok{suppressWarnings}\NormalTok{(}\FunctionTok{lapply}\NormalTok{(Mlist,IC))}
\NormalTok{    x }\OtherTok{\textless{}{-}} \FunctionTok{data.frame}\NormalTok{(}\FunctionTok{matrix}\NormalTok{(}\FunctionTok{unlist}\NormalTok{(x),}\AttributeTok{ncol=}\DecValTok{4}\NormalTok{,}\AttributeTok{byrow=}\NormalTok{T))}
    \FunctionTok{names}\NormalTok{(x) }\OtherTok{\textless{}{-}} \FunctionTok{c}\NormalTok{(}\StringTok{"AIC"}\NormalTok{,}\StringTok{"BIC"}\NormalTok{,}\StringTok{"DIC"}\NormalTok{,}\StringTok{"cAIC"}\NormalTok{)}
\NormalTok{    models }\OtherTok{\textless{}{-}} \FunctionTok{sapply}\NormalTok{(Mlist,formula)}
\NormalTok{    models }\OtherTok{\textless{}{-}} \FunctionTok{abbreviate}\NormalTok{(}\FunctionTok{substr}\NormalTok{(models,}\DecValTok{14}\NormalTok{,}\DecValTok{200}\NormalTok{),}\DecValTok{30}\NormalTok{)}
    \FunctionTok{rownames}\NormalTok{(x) }\OtherTok{\textless{}{-}}\NormalTok{ models}
    \FunctionTok{return}\NormalTok{(x)}
\NormalTok{\}}

\FunctionTok{IC.table}\NormalTok{(lmer}\FloatTok{.1}\NormalTok{,lmer}\FloatTok{.2}\NormalTok{,lmer}\FloatTok{.3}\NormalTok{,lmer}\FloatTok{.4}\NormalTok{,lmer}\FloatTok{.5}\NormalTok{,lmer}\FloatTok{.6}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##                                     AIC      BIC      DIC     cAIC
## 1+visage+(1+visage|newpid)     3142.447 3172.327 3130.447 2697.868
## 1+visag+treatmnt+(1+visg|nwpd) 3142.755 3177.616 3128.755 2698.827
## 1+visag*treatmnt+(1+visg|nwpd) 3144.755 3184.595 3128.755 2699.562
## 1+VISIT+(1+VISIT|newpid)       3126.563 3156.443 3114.563 2642.798
## 1+VISIT+tretmnt+(1+VISIT|nwpd) 3127.625 3162.486 3113.625 2643.307
## 1+VISIT*tretmnt+(1+VISIT|nwpd) 3129.215 3169.056 3113.215 2644.733
\end{verbatim}

Comment briefly on any similarities or differences in how the different
criteria choose fixed effects.

\it

Here's an overall sum of the best (minimizing the criterion) models for
each criterion, with second and third place winners as well:

\begin{description}

\item[AIC:] Best model is lmer.4, with lmer.5 a close second.

\item[BIC:] Best model is lmer.4, with lmer.5 a substantially less close second.

\item[DIC:] Best model is lmer.6, with lmer.4 and lmer.5 almost indistinguishably close.

\item[cAIC:] Best model is lmer.4, with lmer.5 and lmer.5 almost indistinguishably close.

\end{description}

All the criteria prefer having \texttt{VISIT} as a predictor rather than
\texttt{visage}. (This kind of surprises me because visage has more
physical meaning, but so be it\ldots) Consistent with our findings on
earlier hw that \text{treatmnt} did not seem like an important
predictor, \texttt{lmer.4} (without \texttt{treatmnt} in the model) is
first or second with all the criteria, with \texttt{lmer.5} (main effect
for \texttt{treatmt}, but no interaction) a strong second or third with
all the criteria.

\rm

\hypertarget{problem-2b}{%
\subsection{Problem 2(b)}\label{problem-2b}}

Make a table giving values of AIC, BIC, DIC, and cAIC for the following
models:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{lm}\FloatTok{.7} \OtherTok{\textless{}{-}} \FunctionTok{lm}\NormalTok{(sqrt.CD4PCT }\SpecialCharTok{\textasciitilde{}} \DecValTok{1} \SpecialCharTok{+}\NormalTok{ VISIT }\SpecialCharTok{+}\NormalTok{ treatmnt,}\AttributeTok{data=}\NormalTok{cd4)}
\NormalTok{lmer}\FloatTok{.8} \OtherTok{\textless{}{-}} \FunctionTok{lmer}\NormalTok{(sqrt.CD4PCT }\SpecialCharTok{\textasciitilde{}} \DecValTok{1} \SpecialCharTok{+}\NormalTok{ VISIT }\SpecialCharTok{+}\NormalTok{ treatmnt }\SpecialCharTok{+}\NormalTok{ (}\DecValTok{1}\SpecialCharTok{|}\NormalTok{newpid),}\AttributeTok{data=}\NormalTok{cd4,}\AttributeTok{REML=}\NormalTok{F)}
\NormalTok{lmer}\FloatTok{.9} \OtherTok{\textless{}{-}} \FunctionTok{lmer}\NormalTok{(sqrt.CD4PCT }\SpecialCharTok{\textasciitilde{}} \DecValTok{1} \SpecialCharTok{+}\NormalTok{ VISIT }\SpecialCharTok{+}\NormalTok{ treatmnt }\SpecialCharTok{+}\NormalTok{ (}\DecValTok{0}\SpecialCharTok{+}\NormalTok{VISIT}\SpecialCharTok{|}\NormalTok{newpid),}\AttributeTok{data=}\NormalTok{cd4,}\AttributeTok{REML=}\NormalTok{F)}
\NormalTok{lmer}\FloatTok{.10} \OtherTok{\textless{}{-}} \FunctionTok{lmer}\NormalTok{(sqrt.CD4PCT }\SpecialCharTok{\textasciitilde{}} \DecValTok{1} \SpecialCharTok{+}\NormalTok{ VISIT }\SpecialCharTok{+}\NormalTok{ treatmnt }\SpecialCharTok{+}\NormalTok{ (}\DecValTok{1}\SpecialCharTok{+}\NormalTok{VISIT}\SpecialCharTok{|}\NormalTok{newpid),}\AttributeTok{data=}\NormalTok{cd4,}\AttributeTok{REML=}\NormalTok{F)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, :
## Model failed to converge with max|grad| = 0.00584617 (tol = 0.002, component 1)
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{IC.table}\NormalTok{(lm}\FloatTok{.7}\NormalTok{,lmer}\FloatTok{.8}\NormalTok{,lmer}\FloatTok{.9}\NormalTok{,lmer}\FloatTok{.10}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##                                     AIC      BIC      DIC     cAIC
## 1 + VISIT + treatmnt           4009.810 4029.730 4009.810 4009.810
## 1+VISIT+treatmnt+(1|newpid)    3153.879 3178.779 3143.879 2728.147
## 1+VISIT+tretmnt+(0+VISIT|nwpd) 3688.619 3713.520 3678.619 3490.226
## 1+VISIT+tretmnt+(1+VISIT|nwpd) 3127.625 3162.486 3113.625 2643.307
\end{verbatim}

You'll fit the first model with lm(), and the others with lmer().
Comment briefly on any similarities or differences in how the different
criteria choose random effects. (Note that only BIC and cAIC have a
strong theoretical justification here).

\it

There's a convergence warning for one of the models, which you can (and
should) pursue using the methods in the R notes from lecture.

All four information criteria strongly favor at least a random intercept
(\verb|lm.7| and \verb|lmer.9| are very strongly disfavored). Among the
remaining two models, the model with random slope and random intercept
is strongly favored by all the criteria.

\rm

\hypertarget{problem-2c}{%
\subsection{Problem 2(c)}\label{problem-2c}}

Repeat part (b) but with the interaction \verb|VISIT * treatmnt| in each
model instead of just the main effects \verb|VISIT + treatmnt|.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{lm}\FloatTok{.11} \OtherTok{\textless{}{-}} \FunctionTok{lm}\NormalTok{(sqrt.CD4PCT }\SpecialCharTok{\textasciitilde{}} \DecValTok{1} \SpecialCharTok{+}\NormalTok{ VISIT }\SpecialCharTok{*}\NormalTok{ treatmnt,}\AttributeTok{data=}\NormalTok{cd4)}
\NormalTok{lmer}\FloatTok{.12} \OtherTok{\textless{}{-}} \FunctionTok{lmer}\NormalTok{(sqrt.CD4PCT }\SpecialCharTok{\textasciitilde{}} \DecValTok{1} \SpecialCharTok{+}\NormalTok{ VISIT }\SpecialCharTok{*}\NormalTok{ treatmnt }\SpecialCharTok{+}\NormalTok{ (}\DecValTok{1}\SpecialCharTok{|}\NormalTok{newpid),}\AttributeTok{data=}\NormalTok{cd4,}\AttributeTok{REML=}\NormalTok{F)}
\NormalTok{lmer}\FloatTok{.13} \OtherTok{\textless{}{-}} \FunctionTok{lmer}\NormalTok{(sqrt.CD4PCT }\SpecialCharTok{\textasciitilde{}} \DecValTok{1} \SpecialCharTok{+}\NormalTok{ VISIT }\SpecialCharTok{*}\NormalTok{ treatmnt }\SpecialCharTok{+}\NormalTok{ (}\DecValTok{0}\SpecialCharTok{+}\NormalTok{VISIT}\SpecialCharTok{|}\NormalTok{newpid),}\AttributeTok{data=}\NormalTok{cd4,}\AttributeTok{REML=}\NormalTok{F)}
\NormalTok{lmer}\FloatTok{.14} \OtherTok{\textless{}{-}} \FunctionTok{lmer}\NormalTok{(sqrt.CD4PCT }\SpecialCharTok{\textasciitilde{}} \DecValTok{1} \SpecialCharTok{+}\NormalTok{ VISIT }\SpecialCharTok{*}\NormalTok{ treatmnt }\SpecialCharTok{+}\NormalTok{ (}\DecValTok{1}\SpecialCharTok{+}\NormalTok{VISIT}\SpecialCharTok{|}\NormalTok{newpid),}\AttributeTok{data=}\NormalTok{cd4,}\AttributeTok{REML=}\NormalTok{F)}

\FunctionTok{IC.table}\NormalTok{(lm}\FloatTok{.11}\NormalTok{,lmer}\FloatTok{.12}\NormalTok{,lmer}\FloatTok{.13}\NormalTok{,lmer}\FloatTok{.14}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##                                     AIC      BIC      DIC     cAIC
## 1 + VISIT * treatmnt           4010.343 4035.244 4010.343 4010.343
## 1+VISIT*treatmnt+(1|newpid)    3155.181 3185.062 3143.181 2729.893
## 1+VISIT*tretmnt+(0+VISIT|nwpd) 3690.150 3720.030 3678.150 3491.763
## 1+VISIT*tretmnt+(1+VISIT|nwpd) 3129.215 3169.056 3113.215 2644.733
\end{verbatim}

\it

The story here is essentially the same as in part (b): The model with
random slope and random intercept is strongly favored by all four
criteria.

Not part of the required answer here, but, comparing parts (b) and (c)
it seems like the cross-level interaction is not needed.

\rm

\end{document}
