\documentstyle[bj,txfonts]{article}

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

\begin{document}

\begin{center}
\Large
36-617: Applied Linear Models\\
Fall 2022\\
HW07 -- Due Wed Nov 9, 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 For this week, you should finish Sheather 10.1 and Gelman \& Hill, Ch 12.
\item For next week, please finish Gelman \& Hill Ch 13 and read G\&H Ch 14.
\end{itemize}

\item  There are 3 exercises below; each one has several ``parts''.

\end{itemize}


\section*{Exercises}

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

\item % Sheather, Ch 5, p. 147, \#2.
      % data set modified so that there is enough between-county 
      % variation for some multilevel modeling
{\bf Data analysis.}\label{p:houston} On July 23, 2006, the Houston Chronicle published an article entitled ``Reading:
First-grade standard too tough for many''. The article claimed in part that ``more
students (across Texas) are having to repeat first grade. Experts attribute the
increase partially to an increase in poverty.'' The article presents data for each of
61 Texas school districts on
\begin{equation}
\left.
\begin{array}{rcl}
y & = & \mbox{Percentage of students repeating first grade}\\
x & = & \mbox{Percentage of low-income students}
\end{array}
\right\}
\label{e:x-and-y}
\end{equation}
for both 2004--2005 and 1994--1995. The data in the file {\tt houston.txt} is derived from this study, and contains the following variables:
\begin{verbatim}
District                 - An individual school district               
Pct.Repeating.1st.Grade  - The percent of students repeating first grade in the district
Pct.Low.income.students  - The percent of low-income students in the district
Year                     - The year of the observation: 1994 or 2004
County                   - The county that the school district is in
\end{verbatim}
Fit ordinary linear models to this data with {\tt lm()} to determine whether:
\begin{enumerate}
\item An increase in the percentage of low income students is associated with an
increase in the percentage of students repeating first grade?
\item There has been an increase in the percentage of students repeating first grade
between 1994--1995 and 2004–-2005?
\item Any association between the percentage of students repeating first grade and the
percentage of low-income students differs between 1994--1995 and 2004--2005?
\end{enumerate}
Use appropriate numerical and/or graphical tools 
% that you remember from earlier course(s) on linear regression
to assess fit, compare models, determine whether predictors are significant, etc.

\comment{

\item {\bf Notation.} It is important to be able to translate between the ``multilevel'' model formulation and the ``variance components'' formulation.  In each part below, perform the indicated translation.

\begin{enumerate}

\item Translate to variance components form:

\item Translate to variance components form:

\item Translate to multilevel form:

\end{enumerate}

}

\clearpage

\item In the data for problem~\ref{p:houston}, it is plausible that there would be some variation among Counties in percentage of students repeating first grade.  We will use a multilevel model to explore the variation across counties, {\em using the 2004 data \ul{\bf only}}.

\begin{enumerate}

\item Extract from the file {\tt houston.txt} a data frame containing just the data from 2004.  Report the following for the extracted data frame:
\begin{itemize}
\item Number of groups, $J$.  (Each County is a group.)
\item For each group $j$, number of observations $n_j$.
\item Total number of observations, $n$. (Each District is an observation.)
\end{itemize}
Use only data from this data frame in the rest of this problem.

\item Use {\tt lmer()} from {\tt library(lme4)} to fit the multilevel model
\begin{equation}
        \left.
        \begin{array}{rcl}
	y_i & = & \alpha_{j[i]} + \epsilon_i, 
		~\epsilon_i \stackrel{iid}{\sim} N(0,\sigma^2)\\
            \alpha_j & = & \beta_0 + \eta_j, 
		~\eta_j \stackrel{iid}{\sim} N(0,\tau^2)
        \end{array}
        \right\}
        \label{e:rand-int}
\end{equation}
where $i$ indexes Districts, $j$ indexes Counties, and $y_i$ is the percentage of students repeating first grade in the $i^{th}$ District.  Provide: a {\tt summary()} of your model, and report the following quantities from your summary:
\begin{itemize}
\item The value of $\hat\sigma^2$;
\item The value of $\hat\tau^2$;
\item The value of $\hat\beta_0$.
\end{itemize}

\item Use {\tt library(gglpot2)} (or another R package if you prefer) to make a facet plot of $y$ vs $x$ in equation~(\ref{e:x-and-y}), with a different facet for each County.  Add to the facets
\begin{itemize}
\item A black horizontal line indicating the average of $y$ in the whole data set (ignoring groups)
\item A red horizontal line indicating the average of $y$ in each County
\item A green horizontal line indicating the ``random effect'' estimates $\hat\alpha_j = \hat\eta_j + \hat\beta_0$ for each County, as indicated in equation~(\ref{e:rand-int}). 
\end{itemize}
and submit your plot.  (Does your plot illustrate the ``shrinkage'' phenomenon of MLM's?)
\item Fit the ordinary linear model {\tt lm(y $\sim$ County)}, where the coefficients on the County dummy variables are constrained to sum to zero.  Provide the {\tt summary()} of your model, and decide from the output whether we need to keep the {\tt County} variable in the model (this is also an informal way to decide whether to keep the multilevel model that you fitted in part (a)).


\end{enumerate}

\clearpage

\item {\bf This is a math problem, not a data analysis problem.}
Consider the following multilevel model for data $y_i$, $
i=1,\ldots, n$, arranged into $J$ groups, $j=1,\ldots, J$, where
each group $j$ has $n_j$ observations:
$$
        \left.
        \begin{array}{rcl}
	y_i & = & \alpha_{j[i]} + \epsilon_i, 
		~\epsilon_i \stackrel{iid}{\sim} N(0,\sigma^2)\\
            \alpha_j & = & \beta_0 + \eta_j, 
		~\eta_j \stackrel{iid}{\sim} N(0,\tau^2)
        \end{array}
        \right\}~,
        \tag{(*)}
$$
where the $\epsilon$'s and $\eta$'s are also independent of each other.  In all of the rest of this problem, variances, covariances and correlations are based on theoretical expected values, not on sample quantities.  Thus, for any random variable that appears below (e.g., $U=y_i$, $U=\overline{y}_{j.}$, etc.):
\begin{itemize}
\item $\mu_U = E[U]$, not $\overline{u} = \frac{1}{m} \sum_{k=1}^m u_k$ in some sample
\item $\mbox{Var}\,(U) = E[(U - \mu_U)^2]$, not $s_u^2 = \frac{1}{m-1}\sum_{k=1}^m (u_k - \overline{u})^2$ in some sample
\item $\mbox{Cov}\,(U,V) = E[(U - \mu_U)(V - \mu_V)]$, not  $c_{uv} = \frac{1}{m-1}\sum_{k=1}^m (u_k - \overline{u})(v_k - \overline{v})$ in some sample, and
\item $\mbox{Corr}\,(U,V) = \frac{\mbox{Cov}\,(U,V)}{\sqrt{\mbox{Var}\,(U)\mbox{Var}\,(V)}}$, not $\displaystyle\frac{c_{uv}}{\sqrt{s_u^2s_v^2}}$
\end{itemize}
Prove the following four assertions that were stated in lecture:
\begin{enumerate}\parskip=0pt
\item If $i\neq i'$ and $j[i]\neq j[i']$, then 
  $\mbox{Corr}\,(y_i,y_{i'}) = 0$.
% \item For any $j\neq j'$,   $\mbox{Cov}\,(y_{ij},y_{ij'}) = \tau^2 +
% \sigma^2$. 
\item If $i\neq i'$ but $j[i]=j[i']$, then 
  $\mbox{Corr}\,(y_i,y_{i'}) = 
  \frac{\tau^2}{\tau^2+\sigma^2}$.
\item Let $\bar y_{j.} = \frac{1}{n_j} \sum_{i:j[i]=j} y_i$,
  the average of all observations in group $j$.  Then
 $\mbox{Var}\,(\bar y_{j.}) = \tau^2 + \sigma^2/n_j$
\item Suppose we exactly replicate the experiment generating new data
 $y^*_i$ following the model
$$
        \left.
        \begin{array}{rcl}
	y_i^* & = & \alpha_{j[i]} + \epsilon_i^*, 
		~\epsilon_i^* \stackrel{iid}{\sim} N(0,\sigma^2)\\
            \alpha_j & = & \beta_0 + \eta_j, 
		~\eta_j \stackrel{iid}{\sim} N(0,\tau^2)
        \end{array}
        \right\}~,
        \tag{(**)}
$$
so that the group level $\alpha$'s and $\eta$'s (and $\beta_0$) are
the same between $(*)$ and $(**)$ [the conditions and groups we are measuring
didn't change] but the new set of $\epsilon^*$'s are independent of 
$\eta$'s and $\epsilon$'s [we re-measured, and so we have new sample with new
measurement errors within each group].  Form the group averages 
$\bar y^*_{j.}$, analogous to $\bar y_{j.}$.
Then 
\[
	\mbox{Corr}\,(\bar y_{j.},\bar y^*_{j.}) = 
 	\frac{\tau^2}{\tau^2 + \sigma^2/n_j} ~.
\]
(This is another interpretation of the reliability coefficient 
$\frac{\tau^2}{\tau^2 + \sigma^2/n_j}$.)
\end{enumerate}
{\em In all four parts, be sure to state any assumptions that you need.}
%  that do not seem to be explicit in the model formulation $(*)$.}


\end{enumerate}

\end{document}
