\documentstyle[bj,txfonts]{article}

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

\begin{document}

\begin{center}
\Large
36-617: Applied Linear Models\\
Fall 2021\\
HW10 -- Due Wed Nov 17, 11:59pm
\end{center}

\begin{itemize}

\item Please turn the homework in to Gradescope using the appropriate link in our course webspace at canvas.cmu.edu, 
under Assignments. 

\item No new reading.

\item No quiz on Mon Nov 22.

\item Please get started on this assignment right away.  Problem \#2 may take a while, for example (it's the technical appendix for Project 02!).

\end{itemize}


\section*{Exercises}

\begin{enumerate}

\item {\em Continuing with the Project 01 data\ldots} The file {\tt cdi.dat} in the Canvas folder for this hw is taken from Kutner et al. (2005)\comment{Kutner, M.H., Nachsheim, C.J., Neter, J. \& Li, W. (2005) {\em Applied Linear Statistical Models, Fifth Edition}.  NY: McGraw-Hill/Irwin.}: It provides selected county demographic information (CDI) for 440 of the most populous counties in the United States. Each line of the data set has an identification number with a county name and state abbreviation and provides information on 14 variables for a single county. Counties with missing data were deleted from the data set. The information generally pertains to the years 1990 and 1992.  The definitions of the variables are given in Table~\ref{t:variables} on p.~\pageref{t:variables} below.  
%
For this exercise we only consider a couple of these variables.
%
\begin{table}[b]
\caption{Variable definitions for CDI data from Kutner et al. (2005).  {\em Original source:}  Geospatial and Statistical Data Center, University of Virginia.}\label{t:variables}
\begin{center}
\sf\small
\let\mc\multicolumn
\begin{tabular}{rp{1.5in}p{4in}}
\hline
Variable\\ Number &  \mc{1}{c}{Variable Name} & \mc{1}{c}{Description} \\
\hline
1 & Identification number             &  1--440\\
2	&	County														&	County name\\
3	&	State															&	Two-letter state abbreviation\\
4	&	Land area													&	Land area (square miles)\\
5	&	Total population									&	Estimated 1990 population\\
6	&	Percent of population aged 18--34 & Percent of 1990 CDI population aged 18--34\\
7	&	Percent of population 65 or older	&	Percent of 1990 CDI population aged 65 or old\\
8	&	Number of active physicians				&	Number of professionally active nonfederal physicians during 1990\\
9	& Number of hospital beds						&	Total number of beds, cribs, and bassinets during 1990\\
10&	Total serious crimes							&	Total number of serious crimes in 1990, including murder, rape, robbery, aggravated assault, burglary, larceny-theft, and motor vehicle theft, as reported by law enforcement agencies\\
11&	Percent high school graduates			&	Percent of adult population (persons 25 years old or older) who completed 12 or more years of school\\
12&	Percent bachelor's degrees				&	Percent of adult population (persons 25 years old or older) with bachelor's degree\\
13&	Percent below poverty level				&	Percent of 1990 CDI population with income below poverty level\\
14&	Percent unemployment							&	Percent of 1990 CDI population that is unemployed\\
15&	Per capita income 								&	Per-capita income (i.e. average income per person) of 1990 CDI population (in dollars)\\
16&	Total personal income							&	Total personal income of 1990 CDI population (in millions of dollars)\\
17&	Geographic region									&	Geographic region classification used by the US Bureau of the Census, NE (northeast region of the US), NC (north-central region of the US), S (southern region of the US), and W (Western region of the US)\\
\hline
\end{tabular}
\end{center}
\end{table}


Use the function {\tt rescale()} from {\tt library(arm)} to rescale {\tt pct.hs.grad} and {\tt per.capita.income} as follows:
\begin{verbatim}
cdi$pci <- rescale(2*cdi$per.cap.income)
cdi$phg <- rescale(2*cdi$pct.hs.grad)
\end{verbatim}
(This rescales the variables to have mean 0 and standard deviation 1; see {\tt help(rescale)}.  The rescaling is necessary because {\tt lmer()} is a somewhat fragile function, and it can fail to converge correctly if the variables are on vastly different scales.)


\begin{enumerate}\itemsep=0pt

\item Make a facet plot\footnote{Hint: see sample code in the file {\tt 22 - mlm residuals.r}.} like that on slide 7, lecture 22, using state as the grouping variable, with the following elements:
\begin{itemize}\itemsep=0pt
\item Scatter plot of {\tt x=phg} and {\tt y=pci} within each facet (state)
\item The fitted regression line for the completely pooled regression ${\tt pci}_i = \beta_0 + \beta_1 {\tt phg}_i + \epsilon_i$, ignoring states, plotted in each facet (state)
\item The fitted regression lines for the completely unpooled regression ${\tt pci}_i = \beta_{0j[i]} + \beta_{1j[i]} {\tt phg}_i + \epsilon_i$ where $j[i]$ is the state that observation $i$ is in, and where $\beta_{0j}$ and $\beta_{1j}$ are fixed effects, plotted in each facet.
\item The fitted regression lines for the multilevel model
\begin{eqnarray*}
 {\tt pci}_i & = & \alpha_{0j[i]} + \alpha_{1j[i]} {\tt phg}_i + \epsilon_i, ~~ 
 										\epsilon_i \stackrel{iid}{\sim} N(0,\sigma^2) \\
 \alpha_{0j} & = & \beta_0 + \eta_{0j}, ~~ 
 										\eta_{0j} \stackrel{iid}{\sim} N(0,\tau_0^2) \\
 \alpha_{1j} & = & \beta_1 + \eta_{1j}, ~~ 
 										\eta_{1j} \stackrel{iid}{\sim} N(0,\tau_1^2) 
\end{eqnarray*}
and where the correlation between $\eta_{0j}$ and $\eta_{1j}$ is zero\footnote{Hint: to force the correlation to be zero, rather than specifying the random effects as {\tt (1 + phg | state)}, you should specify \\ {\tt (1 | state) + (0 + phg | state)}.}.
\end{itemize}

\item Make a new facet plot with state as the grouping variable again, with {\tt x} equal to the conditional $\hat y$'s, and {\tt y} equal to the conditional residuals\footnote{Hint: {\tt source("residual-functions.r")}, and use the appropriate functions defined in that file.}, within each facet (state).  

\end{enumerate}
Submit your code, and the two plots.  You do not need to write any commentary.


\item Make and submit sections of the technical appendix for your project 02 paper, using the hints and suggestions under {\bf Suggestions for Answering the Research Questions} in the {\tt project-02.pdf} assignment file:
\begin{enumerate}
\item Section of your technical appendix for Research Question \#1.
\item Section of your technical appendix for Research Question \#2.
\item Section of your technical appendix for Research Question \#3.
\item Section of your technical appendix for Research Question \#4.
\end{enumerate}

\end{enumerate}


\end{document}
