\documentstyle[bj,txfonts]{article}

\frenchspacing
\def\vtrim{\vspace*{-0.8em}}

\begin{document}

\begin{center}
\Large
36-463/663: Multilevel \& Hierarchical Models\\
Spring 2022\\
HW02 -- Due Tue Feb 8, 11:59pm 
\end{center}

\begin{itemize}

\item Please turn the homework in, as a single pdf, online in GradeScope using the link provided on the HW01 assignment page on canvas.cmu.edu, 
under Assignments.  Upload \ul{\em one} file per person.  

\item Reading:
\begin{itemize}
\item See the subfolder ``reading'' in the week04 folder on Canvas. 
\end{itemize}

\item  There are 4 exercises below; some have ``parts''.

\end{itemize}


\section*{Exercises}

\begin{enumerate}% \itemsep=0pt\parsep=0pt\parskip=3pt

\item In lecture 06 (crossed random effects, cross-level interactions), six different models were fitted to the London Schools data, but only the {\tt lmer()} model specification was given for each model.  For each of these six models, write out the two-level MLM formulation of the model. You can use mathematical variable names ($y_i$, $x_{3i}$, etc.) or English variable names (LRT$_i$, etc.), as long as it is clear which variables in the data frame are in each of your six MLM models. Be sure to state all distribution assumptions, and be sure that the indexing ($i$, $j$, $j[i]$ and so forth) is clear. 

\item Return to the model on slide 10 of lecture 05 (random slopes, correlation and centering).  Use the AIC.ml, BIC.ml, and DIC.lm functions defined at the end of lecture 06 (crossed random effects, cross-level interactions) as needed for the following quesitons.
\begin{enumerate}

\item Write the multilevel model that has a cross-level interaction between log.uranium and floor.  Interpret this model: what does it say about how log.uranium and floor influence log.radon in each house, and how is it different from the model without the cross-level interaction?

\item Which model fits the data better, with or without the cross-level interaction between floor and log.uranium?

\item Construct a new Level 2 variable nj that is equal to the number of houses sampled in each county.  Like log.uranium, this variable will have the same value for each house in the county.  Fit two models like the one on slide 10 of lecture 05, but with nj replacing log.uranium: one model without a cross-level interaction between nj and floor, and one model with the cross-level interaction.  Of the four models (with nj or with log.uranium, and with or without the cross-level interaction) which one(s) fit the data best?  Accompany your results with a short paragraph interpreting them. 

\end{enumerate}



\item Although it is usual to keep the correlation between random effects in a multilevel model, there are times when we'd like to formally establish whether the data support having the correlation in the model or not.  One way to do that is with a likelihood ratio test.  Recall that the likelihood ratio test for testing model $M_0$ vs model $M_1$, where $M_0$ is obtained from $M_1$ by linear constraint(s), computes the log-likelihood ratio test statistic 
\[
	   -2[\log(likelihood_{M_0}) - \log(likelihood_{M_1})]
\]
where the likelihoods are evaluated at the maximum likelihood values of the parameters (e.g. as fitted by {\tt lmer()}), and compares the value with a chi-squared distribution with degrees of freedom equal to the number of linear constraints.  A statistically significant result rejects $M_0$ in favor of $M_1$ and conversely a nonsignificant result suggests that there is not enough evidence in the data to reject $M_0$.   {\em \hfill (continued on next page\ldots)} 

For the model on slide 10 of lecture 05 (random slopes, correlation and centering), use a likelihood ratio test to determine whether there is sufficient evidence in the data to keep the correlation $\rho$ between $\eta_{0j}$ and $\eta_{1j}$ in the model.  {\em (Note: For reasons that we will talk about next week, you need to set {\tt REML=FALSE} in {\tt lmer()} when you fit $M_0$ and $M_1$ for the likelihood ratio test.  You will also find the {\tt logLik()} function in R useful for this exercise.)}  Be sure to report the number of degrees of freedom of the test, the value of the test statistic, and the criterion you are using to determine statistical significance. 


\item The file allvars.csv contains CD4 percentages (CD4PCT) for a set of 254 young children with HIV who were measured several times over a period of two years (A CD4 count measures the number of CD4 cells in your blood. It's used to check the immune system function in people with HIV\footnote{https://medlineplus.gov/lab-tests/cd4-lymphocyte-count/}). The dataset also includes the ages of the children at each measurement. This is an example of {\em growth curve data} in which the Level 1 observations are CD4PCT's at each visit, and the Level 2 ``group'' is a child.  How the CD4PCT's change over time for each child is the primary question of interest.  Because of skewing, you should replace CD4PCT with sqrt(CD4PCT) for all of the following questions.

\begin{enumerate}

\item For the first 12 children only ({\tt newpid $\leq$ 12}), make a facet plot of CD4PCT vs VISIT number. Use different plotting point colors for children who have treatmnt=1 and treatmnt=0\footnote{Presumably, ``treatmnt'' stands for some sort of treatment to improve childrens' HIV status.}.  Do the same for CD4PCT vs. visage\footnote{If you are using {\tt ggplot()}, you may wish to set {\tt scales=''free\_x''} in the {\tt facet\_wrap()} function.} (the age at the child on each visit).  Which is a better measure of time?

\item Build a multilevel model with random slopes and intercepts for all the children, using a measure of time (VISIT or visage, whichever you answered in part (a)) as a Level 1 predictor, and treatmnt as a Level 2 predictor.  Report the estimated fixed effects and variances from the model, and add the fitted regression lines to your plot from part (a).

\item Try to expand the model by adding a (cross-level) interaction between treatmnt and your measure of time.  Comment on changes in the estimated fixed effects and variances for this model, vs. the model in part (b).  Add the fitted regression lines from this model to your previous facets plot.  Does treatmnt account for an interesting amount of the variation in slopes (or intercepts) among children in the study? 

\end{enumerate}



\end{enumerate}

\end{document}
