% 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 04 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}
\DeclareMathOperator{\Cov}{Cov}
\DeclareMathOperator{\Corr}{Corr}
\ifLuaTeX
  \usepackage{selnolig}  % disable illegal ligatures
\fi

\title{Homework 04 Solutions}
\author{}
\date{\vspace{-2.5em}9/27/2021}

\begin{document}
\maketitle

\hypertarget{section}{%
\section{1.}\label{section}}

\hypertarget{a}{%
\subsection{(1a)}\label{a}}

We will use several facts from lecture. First, let
\(W \sim N(\mu, \Sigma)\) be an arbitrary multivariate normal random
vector and \(A\) a matrix whose second dimension is the same as the
length of \(W\), so that \(AW\) is defined. Then we have \begin{align}
  & AW \sim N(A\mu, A\Sigma A^T) \label{eq:linear_normal}
\end{align} Additionally, we have from lecture that \begin{align}
               & HX\beta = X\beta \label{eq:idempotent1} \\
  & (I - H)(I - H)^T = (I - H)(I - H) = (I - H) \label{eq:idempotent2} \\
                 & y \sim N(X\beta, \sigma^2 I)  \label{eq:y_dist}
\end{align} Let \(\hat{y}\) be the \(n \times 1\) column vector of
fitted values. \begin{align*}
  \hat{e} &= y - \hat{y} \\
          &= y - Hy \\
          &= (I - H)y \\
          &\sim N((I - H)X\beta, (I-H)\sigma^2 I (I-H)^T) \tag{by facts \eqref{eq:linear_normal} and \eqref{eq:y_dist}} \\
          &= N(0, \sigma^2 (I - H)(I - H)^T) \tag{by fact \eqref{eq:idempotent1}} \\
          &= N(0, \sigma^2 (I - H)) \tag{by fact \eqref{eq:idempotent2}}
\end{align*} \qed

\hypertarget{b}{%
\subsection{(1b)}\label{b}}

\hypertarget{i}{%
\subsubsection{(i)}\label{i}}

In the intercept-only model, the design matrix \(X\) is an \(n\times 1\)
matrix in which each entry is 1. This can be used to derive the hat
matrix \(H_1\): \begin{align*}
  X^TX &= [n]_{1\times 1} \tag{the $1\times 1$ matrix whose only entry is $n$} \\
  \implies (X^TX)^{-1} &= [1/n]_{1\times 1} \tag{the $1\times 1$ matrix whose only entry is $1/n$} \\
  \implies X(X^TX)^{-1}X^T &= \frac{1}{n}XX^T \\
                           &= \frac{1}{n} \begin{bmatrix}
                           1 & \ldots & 1 \\
                           \vdots & \ddots & \vdots \\
                           1 & \ldots & 1 \end{bmatrix}_{n\times n} \\
                           &= H_1 \tag{by the definition of the hat matrix}
\end{align*} For any \(i\) in \(1, 2, \ldots, n\), we derive
\(\hat{y}_i\) by multiplying the \(i\)th row of \(H_1\) by the vector
\(y\). In other words, for the intercept-only model,
\(\hat{y}_i = \frac{1}{n}\sum_{i=1}^n y_i = \bar{y}\) for all \(i\).
\qed

\hypertarget{ii}{%
\subsubsection{(ii)}\label{ii}}

To avoid using \(\bar{y}\) to denote both a scalar and a vector, let's
immediately use the fact that \(H_1y\) is equal to the \(n\times 1\)
vector
\(\begin{bmatrix} \bar{y} & \bar{y} & \ldots & \bar{y}\end{bmatrix}^T\),
as shown in the previous problem. Rewriting the covariance in vector
form, per the problem suggestion, we have: \begin{align*}
  \mathop{\mathrm{Cov}}(y, \hat{y}) &= \frac1n {\sum}_{i=1}^n (y_i - \bar y)(\hat y_i - \bar y) \\
                   &= \frac{1}{n}(y - H_1y)^T(\hat{y} - H_1y) \\
                   &= \frac{1}{n}(y - H_1y)^T(Hy - H_1y) \\
                   &= \frac{1}{n}[(I - H_1)y]^T(H - H_1)y \\
                   &= \frac{1}{n}y^T(I - H_1)^T(H - H_1)y \tag{since $(AB)^T = B^T A^T$} \\
                   &= \frac{1}{n}y^T(H - H_1 - H_1^TH + H_1^TH_1)y \\
                   &= \frac{1}{n}y^T(H - H_1H)y \tag{since $H_1^T = H_1$ and $H_1H_1 = H_1$} \\
                   &= \frac{1}{n}y^T(H - H_1)y \tag{since $H_1H = HH_1 = H_1$}
\end{align*} \qed

\hypertarget{iii}{%
\subsubsection{(iii)}\label{iii}}

Recall that the sample correlation is the standardized sample
covariance, defined as \begin{align*}
  \\widehat{\mathop{\mathrm{Corr}}}(y, \hat{y}) &= \frac{\hat{\mathop{\mathrm{Cov}}}(y, \hat{y})}{\sqrt{ \hat{\sigma}_y^2 \hat{\sigma}_{\hat{y}}^2 }} \\
  &= \frac{\sum_{i=1}^n (y_i - \bar{y})(\hat{y}_i - \bar{y})} {\sqrt{\sum_{i=1}^n (y_i - \bar{y})^2 \sum_{i=1}^n (\hat{y}_i - \bar{y})^2}} \\
  &= \frac{(y-H_1y)^T(\hat{y}-H_1y)}{\sqrt{(y-H_1y)^T(y-H_1y)(\hat{y}-H_1y)^T(\hat{y}-H_1y)}} \\
  &= \frac{y^T(H-H_1)y}{\sqrt{y^T(I-H_1)^T(I-H_1)y y^T(H-H_1)^T(H-H_1)y}} \tag{using the previous problem} \\
  &= \frac{\sqrt{y^T(H-H_1)y}}{\sqrt{y^T(I-H_1)^T(I-H_1)y}} \\
  &= \frac{\sqrt{y^T(H-H_1)y}}{\sqrt{y^T(I-H_1)y}} \tag{using a version of fact \eqref{eq:idempotent2}} \\
  &= \sqrt{\frac{SS_{reg}}{SST}} \\
  &= \sqrt{R^2} \tag{by the definition of $R^2$}
\end{align*} And therefore,
\(R^2 = \widehat{\mathop{\mathrm{Corr}}}(y, \hat{y})^2\). \qed

\hypertarget{c}{%
\subsection{(1c)}\label{c}}

The sample correlation is 0 iff the sample covariance is 0, so let's
just concern ourselves with the sample covariance and not worry about
the denominator in the correlation.

Recall that the residuals sum to 0 by construction, which also means
that their sample mean is 0. Hence: \begin{align*}
  \hat{\mathop{\mathrm{Cov}}}(\hat{e}, \hat{y}) &= \frac{1}{n}\sum_{i=1}^n (\hat{e_i} - 0)(\hat{y_i} - \bar{y}) \\
  &= \frac{1}{n}(\hat{e})^T(\hat{y} - \bar{y}) \\
  &= \frac{1}{n}(y - \hat{y})^T(\hat{y} - \bar{y}) \\
  &= \frac{1}{n}[(I-H)y]^T(H-H_1)y \\
  &= \frac{1}{n}y^T(I-H)(H-H_1)y \tag{since $(I-H)^T = (I-H)$} \\
  &= \frac{1}{n}y^T(H-H_1 - HH + HH_1)y \\
  &= \frac{1}{n}y^T(H - H_1 - H + H_1)y \tag{since $HH_1 = H_1$} \\
  &= 0
\end{align*} \qed

\hypertarget{sheather-ch-5-pp.-146147-1-with-extra-questions.}{%
\section{2. (Sheather, Ch 5, pp.~146--147, \#1, with extra
questions.)}\label{sheather-ch-5-pp.-146147-1-with-extra-questions.}}

\hypertarget{a-develop-model-to-predict-late-from-bill.}{%
\subsection{(2a) Develop model to predict LATE from
BILL.}\label{a-develop-model-to-predict-late-from-bill.}}

Let's start with a little EDA\ldots

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{overdue }\OtherTok{\textless{}{-}} \FunctionTok{read.table}\NormalTok{(}\StringTok{"overdue.txt"}\NormalTok{,}\AttributeTok{header=}\NormalTok{T)}
\FunctionTok{str}\NormalTok{(overdue)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 'data.frame':    96 obs. of  2 variables:
##  $ LATE: int  16 47 22 47 47 21 44 27 19 48 ...
##  $ BILL: int  79 264 97 289 288 100 250 140 97 299 ...
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{plot}\NormalTok{(LATE }\SpecialCharTok{\textasciitilde{}}\NormalTok{ BILL, }\AttributeTok{data=}\NormalTok{overdue)}
\end{Highlighting}
\end{Shaded}

\begin{center}\includegraphics{hw04_solutions_files/figure-latex/2a scatter i-1} \end{center}

From the scatter plot, it looks like regression is going to fail,
because LATE is not a single-valued function of BILL. Indeed, if we do a
simple regression of LATE on BILL, we get

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{round}\NormalTok{(}\FunctionTok{summary}\NormalTok{(}\FunctionTok{lm}\NormalTok{(LATE }\SpecialCharTok{\textasciitilde{}}\NormalTok{ BILL,}\AttributeTok{data=}\NormalTok{overdue))}\SpecialCharTok{$}\NormalTok{coefficients,}\DecValTok{2}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)    51.98       5.96    8.72     0.00
## BILL           -0.01       0.03   -0.40     0.69
\end{verbatim}

and the coefficient on BILL is clearly not significantly different from
zero.

Looking back at the problem statement, we also know that the first 48
observations are for residential accounts, and the last 48 are for
commercial accounts. Maybe this helps explain the structure we see
above?

So, let's create a dummy variable for ``residential'', and colored the
points according to this dummy variable.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{overdue}\SpecialCharTok{$}\NormalTok{residential }\OtherTok{\textless{}{-}} \FunctionTok{c}\NormalTok{(}\FunctionTok{rep}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{48}\NormalTok{),}\FunctionTok{rep}\NormalTok{(}\DecValTok{0}\NormalTok{,}\DecValTok{48}\NormalTok{))}
\FunctionTok{plot}\NormalTok{(LATE }\SpecialCharTok{\textasciitilde{}}\NormalTok{ BILL, }\AttributeTok{data=}\NormalTok{overdue,}\AttributeTok{col=}\NormalTok{residential }\SpecialCharTok{+} \DecValTok{1}\NormalTok{)}
\FunctionTok{legend}\NormalTok{(}\DecValTok{250}\NormalTok{,}\DecValTok{30}\NormalTok{,}\AttributeTok{legend=}\FunctionTok{c}\NormalTok{(}\StringTok{"Residential"}\NormalTok{,}\StringTok{"Commercial"}\NormalTok{),}\AttributeTok{pch=}\DecValTok{1}\NormalTok{,}\AttributeTok{col=}\FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{,}\DecValTok{1}\NormalTok{),}\AttributeTok{cex=}\FloatTok{0.5}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{center}\includegraphics{hw04_solutions_files/figure-latex/2a scatter ii-1} \end{center}

Much better: Now we clearly see that the two ``branches'' in the scatter
plot correspond to the two types of account, with different slopes and
intercepts for each type of account. This suggests we should fit an
ANCOVA model with interactions:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{lm}\FloatTok{.2}\NormalTok{a }\OtherTok{\textless{}{-}} \FunctionTok{lm}\NormalTok{(LATE }\SpecialCharTok{\textasciitilde{}}\NormalTok{ BILL}\SpecialCharTok{*}\NormalTok{residential, }\AttributeTok{data=}\NormalTok{overdue)}
\FunctionTok{print}\NormalTok{(coefs }\OtherTok{\textless{}{-}} \FunctionTok{coef}\NormalTok{(lm}\FloatTok{.2}\NormalTok{a))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##      (Intercept)             BILL      residential BILL:residential 
##      101.7581844       -0.1909615      -99.5485607        0.3566445
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{plot}\NormalTok{(LATE }\SpecialCharTok{\textasciitilde{}}\NormalTok{ BILL, }\AttributeTok{data=}\NormalTok{overdue,}\AttributeTok{col=}\NormalTok{residential }\SpecialCharTok{+} \DecValTok{1}\NormalTok{)}
\FunctionTok{abline}\NormalTok{(}\AttributeTok{a=}\NormalTok{coefs[}\DecValTok{1}\NormalTok{], }\AttributeTok{b=}\NormalTok{coefs[}\DecValTok{2}\NormalTok{],}\AttributeTok{col=}\DecValTok{1}\NormalTok{)}
\FunctionTok{abline}\NormalTok{(}\AttributeTok{a=}\NormalTok{coefs[}\DecValTok{1}\NormalTok{]}\SpecialCharTok{+}\NormalTok{coefs[}\DecValTok{3}\NormalTok{], }\AttributeTok{b=}\NormalTok{coefs[}\DecValTok{2}\NormalTok{]}\SpecialCharTok{+}\NormalTok{coefs[}\DecValTok{4}\NormalTok{],}\AttributeTok{col=}\DecValTok{2}\NormalTok{)}
\FunctionTok{legend}\NormalTok{(}\DecValTok{250}\NormalTok{,}\DecValTok{30}\NormalTok{,}\AttributeTok{legend=}\FunctionTok{c}\NormalTok{(}\StringTok{"Residential"}\NormalTok{,}\StringTok{"Commercial"}\NormalTok{),}\AttributeTok{pch=}\DecValTok{1}\NormalTok{,}\AttributeTok{col=}\FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{,}\DecValTok{1}\NormalTok{),}\AttributeTok{cex=}\FloatTok{0.5}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{center}\includegraphics{hw04_solutions_files/figure-latex/2a scatter iii-1} \end{center}

The new scatter plot suggests that this may be pretty good model for the
data.

\hypertarget{b-summary-residual-diagnostics.}{%
\subsection{(2b) Summary \& Residual
Diagnostics.}\label{b-summary-residual-diagnostics.}}

Here is the model summary and the set of four standard diagnostic plots:

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{summary}\NormalTok{(lm}\FloatTok{.2}\NormalTok{a)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = LATE ~ BILL * residential, data = overdue)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -12.1211  -2.2163   0.0974   1.9556   8.6995 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      101.758184   1.198504   84.90   <2e-16 ***
## BILL              -0.190961   0.006285  -30.38   <2e-16 ***
## residential      -99.548561   1.694940  -58.73   <2e-16 ***
## BILL:residential   0.356644   0.008888   40.12   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.371 on 92 degrees of freedom
## Multiple R-squared:  0.9803, Adjusted R-squared:  0.9796 
## F-statistic:  1524 on 3 and 92 DF,  p-value: < 2.2e-16
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{par}\NormalTok{(}\AttributeTok{mfrow=}\FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{,}\DecValTok{2}\NormalTok{))}
\FunctionTok{plot}\NormalTok{(lm}\FloatTok{.2}\NormalTok{a)}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw04_solutions_files/figure-latex/2b summary-1.pdf}
This seems like a pretty good model. The regression coefficients are all
strongly significantly different from zero, and the \(R^2=0.98\).
Looking at the residual diagnostic plots, we see that the residuals are
centered at zero and show no vertical patterns; the QQ plot suggests
that the residuals are approximately normal with just 3 or 4 outliers,
the Scale-Location plot is consistent with constant variance, and there
aer no points with Cook's distance above 0.5. The points with high
leverage are generally not outliers, and the points that are outliers
have modest leverage.

\hypertarget{c-under-60-days-or-your-money-back}{%
\subsection{(2c) ``Under 60 days or your money
back?''}\label{c-under-60-days-or-your-money-back}}

To think about the ``Under 60 days or your money back!'' slogan, let's
look at the final scatter plot with the fitted regression model again,
but with horizontal lines at 60, 90 and 100 days overlaid:

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{plot}\NormalTok{(LATE }\SpecialCharTok{\textasciitilde{}}\NormalTok{ BILL, }\AttributeTok{data=}\NormalTok{overdue,}\AttributeTok{col=}\NormalTok{residential }\SpecialCharTok{+} \DecValTok{1}\NormalTok{)}
\FunctionTok{abline}\NormalTok{(}\AttributeTok{a=}\NormalTok{coefs[}\DecValTok{1}\NormalTok{], }\AttributeTok{b=}\NormalTok{coefs[}\DecValTok{2}\NormalTok{],}\AttributeTok{col=}\DecValTok{1}\NormalTok{)}
\FunctionTok{abline}\NormalTok{(}\AttributeTok{a=}\NormalTok{coefs[}\DecValTok{1}\NormalTok{]}\SpecialCharTok{+}\NormalTok{coefs[}\DecValTok{3}\NormalTok{], }\AttributeTok{b=}\NormalTok{coefs[}\DecValTok{2}\NormalTok{]}\SpecialCharTok{+}\NormalTok{coefs[}\DecValTok{4}\NormalTok{],}\AttributeTok{col=}\DecValTok{2}\NormalTok{)}
\FunctionTok{abline}\NormalTok{(}\AttributeTok{h=}\DecValTok{60}\NormalTok{,}\AttributeTok{col=}\StringTok{"Blue"}\NormalTok{)}
\FunctionTok{abline}\NormalTok{(}\AttributeTok{h=}\DecValTok{90}\NormalTok{,}\AttributeTok{col=}\StringTok{"Yellow"}\NormalTok{)}
\FunctionTok{abline}\NormalTok{(}\AttributeTok{h=}\DecValTok{100}\NormalTok{,}\AttributeTok{col=}\StringTok{"Green"}\NormalTok{)}
\FunctionTok{legend}\NormalTok{(}\DecValTok{250}\NormalTok{,}\DecValTok{30}\NormalTok{,}\AttributeTok{legend=}\FunctionTok{c}\NormalTok{(}\StringTok{"Residential"}\NormalTok{,}\StringTok{"Commercial"}\NormalTok{),}\AttributeTok{pch=}\DecValTok{1}\NormalTok{,}\AttributeTok{col=}\FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{,}\DecValTok{1}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw04_solutions_files/figure-latex/2c scatter iv-1.pdf}
It looks like 60 days is enough for residential accounts (even if we
extrapolate a little to the right of the data using the regression
line), but we need up to 100 days for commercial accounts.

If we want the slogan to work for almost all collections, perhaps we
should recommend

\begin{itemize}
\item "Under 90 days or your money back!"   $~~~$or perhaps
\item "Under 100 days or your money back!"
\end{itemize}

since 90 days is enough for most collections, even commercial ones, and
100 days is enough for viturally all of them.

On the other hand, if we can restrict the advertising to Residential
accounts only, then ``Under 60 days'' seems just fine: Based on this
plot, we would expect virtually all Residential account to be collected
in 60 days or less.

\hypertarget{sheather-ch-5-p.-147-2.}{%
\section{3. (Sheather, Ch 5, p.~147,
\#2.)}\label{sheather-ch-5-p.-147-2.}}

We begin with a little exploration, and finding a good model.

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

\begin{verbatim}
## 'data.frame':    122 obs. of  5 variables:
##  $ District             : chr  "Alvin" "Alvin" "Angleton" "Angleton" ...
##  $ X.Repeating.1st.Grade: num  4.1 5.8 7.1 6.7 7.3 2.6 8.2 2.3 12.5 0 ...
##  $ X.Low.income.students: num  49.7 41.1 44.2 30.2 49.4 33.7 45.6 29.7 71.7 37.6 ...
##  $ Year                 : int  2004 1994 2004 1994 2004 1994 2004 1994 2004 1994 ...
##  $ County               : chr  "Brazoria" "Brazoria" "Brazoria" "Brazoria" ...
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{cat}\NormalTok{(}\StringTok{"}\SpecialCharTok{\textbackslash{}n}\StringTok{"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{Shaded}
\begin{Highlighting}[]
\ControlFlowTok{for}\NormalTok{ (i }\ControlFlowTok{in} \FunctionTok{names}\NormalTok{(houston)) \{}
  \FunctionTok{cat}\NormalTok{(i,}\StringTok{"}\SpecialCharTok{\textbackslash{}n}\StringTok{"}\NormalTok{)}
  \FunctionTok{print}\NormalTok{(}\FunctionTok{summary}\NormalTok{(houston[,i]))}
  \FunctionTok{cat}\NormalTok{(}\StringTok{"Number of unique values:"}\NormalTok{,}\FunctionTok{length}\NormalTok{(}\FunctionTok{unique}\NormalTok{(houston[,i])),}\StringTok{"}\SpecialCharTok{\textbackslash{}n\textbackslash{}n}\StringTok{"}\NormalTok{)}
\NormalTok{\}}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## District 
##    Length     Class      Mode 
##       122 character character 
## Number of unique values: 61 
## 
## X.Repeating.1st.Grade 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   3.100   5.700   6.076   8.750  18.400 
## Number of unique values: 77 
## 
## X.Low.income.students 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    3.20   27.15   41.35   41.88   53.02   98.10 
## Number of unique values: 111 
## 
## Year 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    1994    1994    1999    1999    2004    2004 
## Number of unique values: 2 
## 
## County 
##    Length     Class      Mode 
##       122 character character 
## Number of unique values: 8
\end{verbatim}

Apparently, there are only two ``year'' values: 1994 and 2004; so we
will convert this to a factor variable, just to make interpretation of
the coefficients easier; and we make a scatterplot matrix (``pairs''
plot) of all of the variables except district. I'm also going to rename
the two continuous variables to something more suggestive, and re-order
the variables, so that the ``pairs'' plot makes a little more sense.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{houston}\SpecialCharTok{$}\NormalTok{Year }\OtherTok{\textless{}{-}} \FunctionTok{as.factor}\NormalTok{(houston}\SpecialCharTok{$}\NormalTok{Year)}
\FunctionTok{names}\NormalTok{(houston)[}\FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{,}\DecValTok{3}\NormalTok{)] }\OtherTok{\textless{}{-}} \FunctionTok{c}\NormalTok{(}\StringTok{"Pct.Repeating.1st.Grade"}\NormalTok{, }\StringTok{"Pct.Low.income.students"}\NormalTok{)}
\NormalTok{houston }\OtherTok{\textless{}{-}}\NormalTok{ houston[,}\FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{3}\NormalTok{,}\DecValTok{2}\NormalTok{,}\DecValTok{4}\NormalTok{,}\DecValTok{5}\NormalTok{)]}
\FunctionTok{ggpairs}\NormalTok{(houston[,}\SpecialCharTok{{-}}\DecValTok{1}\NormalTok{])  }\CommentTok{\# there are too many districts for the plot, so we omit it}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw04_solutions_files/figure-latex/3 ggpairs-1.pdf}
There is some evidence in the ``pairs'' plots for all three of the
hypotheses in parts (a), (b) and (c), but we now fit regression and
ANCOVA models to see which effects are ``significant'' (i.e.~large
enough that they are not likely just due to noise in the data).

\hypertarget{a-low-income-associated-with-repeating-first-grade}{%
\subsection{(3a) Low income associated with repeating first
grade?}\label{a-low-income-associated-with-repeating-first-grade}}

Let's start with a simple regression of repeating first grade on low
income

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{summary}\NormalTok{(lm}\FloatTok{.3}\NormalTok{a }\OtherTok{\textless{}{-}} \FunctionTok{lm}\NormalTok{(Pct.Repeating}\FloatTok{.1}\NormalTok{st.Grade }\SpecialCharTok{\textasciitilde{}}\NormalTok{ Pct.Low.income.students,}\AttributeTok{data=}\NormalTok{houston))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = Pct.Repeating.1st.Grade ~ Pct.Low.income.students, 
##     data = houston)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.9845 -2.5072 -0.4184  1.8505 11.1067 
## 
## Coefficients:
##                         Estimate Std. Error t value Pr(>|t|)    
## (Intercept)              2.91419    0.83836   3.476 0.000709 ***
## Pct.Low.income.students  0.07550    0.01823   4.141 6.47e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.821 on 120 degrees of freedom
## Multiple R-squared:  0.125,  Adjusted R-squared:  0.1177 
## F-statistic: 17.14 on 1 and 120 DF,  p-value: 6.472e-05
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{par}\NormalTok{(}\AttributeTok{mfrow=}\FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{1}\NormalTok{))}
\FunctionTok{plot}\NormalTok{(Pct.Repeating}\FloatTok{.1}\NormalTok{st.Grade }\SpecialCharTok{\textasciitilde{}}\NormalTok{ Pct.Low.income.students,}\AttributeTok{data=}\NormalTok{houston)}
\FunctionTok{abline}\NormalTok{(lm}\FloatTok{.3}\NormalTok{a)}
\end{Highlighting}
\end{Shaded}

\begin{center}\includegraphics{hw04_solutions_files/figure-latex/3a model-1} \end{center}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{par}\NormalTok{(}\AttributeTok{mfrow=}\FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{,}\DecValTok{2}\NormalTok{))}
\FunctionTok{plot}\NormalTok{(lm}\FloatTok{.3}\NormalTok{a)}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw04_solutions_files/figure-latex/3a model diagnostics-1.pdf}
This actually looks like a good regression; the summary shows a very
significant increasing association between percent of students repeating
first grade and percent of students with low (family) incomes: the
coefficient on percent low income students is \(\hat\beta_1 = 0.07550\),
with \(SE(\hat\beta_1)=0.01823\). The effect seems rather small,
however---we expect an increase of only 0.08\% of kids repeating first
grade, for every 1\% increase in kids in poverty.

\hypertarget{b-more-students-repeating-first-grade-in-2004-2005-than-in-1994-1995}{%
\subsection{(3b) More students repeating first grade in 2004-2005 than
in
1994-1995?}\label{b-more-students-repeating-first-grade-in-2004-2005-than-in-1994-1995}}

For this question, we will just regress repeating first grade on year

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{summary}\NormalTok{(lm}\FloatTok{.3}\NormalTok{b }\OtherTok{\textless{}{-}} \FunctionTok{lm}\NormalTok{(Pct.Repeating}\FloatTok{.1}\NormalTok{st.Grade }\SpecialCharTok{\textasciitilde{}}\NormalTok{ Year,}\AttributeTok{data=}\NormalTok{houston))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = Pct.Repeating.1st.Grade ~ Year, data = houston)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.6787 -2.6537 -0.6262  2.5750 12.9262 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   5.4738     0.5172  10.584   <2e-16 ***
## Year2004      1.2049     0.7314   1.647    0.102    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.039 on 120 degrees of freedom
## Multiple R-squared:  0.02212,    Adjusted R-squared:  0.01397 
## F-statistic: 2.714 on 1 and 120 DF,  p-value: 0.1021
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{par}\NormalTok{(}\AttributeTok{mfrow=}\FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{1}\NormalTok{))}
\NormalTok{jy }\OtherTok{\textless{}{-}} \FunctionTok{jitter}\NormalTok{(}\FunctionTok{as.numeric}\NormalTok{(houston}\SpecialCharTok{$}\NormalTok{Year)}\SpecialCharTok{{-}}\DecValTok{1}\NormalTok{)}
\FunctionTok{plot}\NormalTok{(Pct.Repeating}\FloatTok{.1}\NormalTok{st.Grade }\SpecialCharTok{\textasciitilde{}}\NormalTok{ jy,}\AttributeTok{data=}\NormalTok{houston,}\AttributeTok{xlab=}\StringTok{"Year (jittered)"}\NormalTok{)}
\FunctionTok{abline}\NormalTok{(lm}\FloatTok{.3}\NormalTok{b)}
\end{Highlighting}
\end{Shaded}

\begin{center}\includegraphics{hw04_solutions_files/figure-latex/3b model-1} \end{center}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{par}\NormalTok{(}\AttributeTok{mfrow=}\FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{,}\DecValTok{2}\NormalTok{))}
\FunctionTok{plot}\NormalTok{(lm}\FloatTok{.3}\NormalTok{b)}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw04_solutions_files/figure-latex/3b model diagnostics-1.pdf}
Again, this looks like a good regression model, in the sense that the
assumptions underlying linear regression are approximately
satisfied\footnote{Remember, the clustering around different $x$ values is just due to the fact that $x$ is categorical.  The important thing here is that we don't see any interesting patterns within or between those clusters.}.
However, the estimated coefficient on Year, \(\hat\beta_1=1.2\) with
\(SE(\hat\beta_1)=0.73\) is not statistically significantly different
from zero. From this we would conclude that there really is not enough
evidence to say that there are more students repeating first grade in
the 2004 school year than in 1994 school year.

\hypertarget{c-difference-in-the-relationship-of-income-with-repeating-between-the-two-school-years}{%
\subsection{(3c) Difference in the relationship of income with repeating
between the two school
years?}\label{c-difference-in-the-relationship-of-income-with-repeating-between-the-two-school-years}}

Now let's fit the interactive ANCOVA model. This will both help answer
question (3c), and help us to understand whether our answers to
questions (3a) and (3b) need any additional elaboration.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{summary}\NormalTok{(lm}\FloatTok{.3}\NormalTok{c }\OtherTok{\textless{}{-}} \FunctionTok{lm}\NormalTok{(Pct.Repeating}\FloatTok{.1}\NormalTok{st.Grade }\SpecialCharTok{\textasciitilde{}}\NormalTok{ Pct.Low.income.students}\SpecialCharTok{*}\NormalTok{Year,}\AttributeTok{data=}\NormalTok{houston))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = Pct.Repeating.1st.Grade ~ Pct.Low.income.students * 
##     Year, data = houston)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.1606 -2.6121 -0.5576  1.7495 11.6014 
## 
## Coefficients:
##                                  Estimate Std. Error t value Pr(>|t|)   
## (Intercept)                       3.27194    1.22347   2.674  0.00855 **
## Pct.Low.income.students           0.06080    0.03093   1.966  0.05167 . 
## Year2004                         -0.38956    1.76109  -0.221  0.82532   
## Pct.Low.income.students:Year2004  0.01903    0.03949   0.482  0.63066   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.845 on 118 degrees of freedom
## Multiple R-squared:  0.1288, Adjusted R-squared:  0.1066 
## F-statistic: 5.813 on 3 and 118 DF,  p-value: 0.0009689
\end{verbatim}

The summary shows that neither the main effect for Year, nor the
interaction between low income and Year, are significant in the model.
This is illustrated in the scatterplot below: the regression lines for
the two categories (year=1994-1995 and year=2004-2005) are nearly
identical. The residual diagnostic plots that follow suggest that the
modeling assumptions hold up fairly well here.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{par}\NormalTok{(}\AttributeTok{mfrow=}\FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{1}\NormalTok{))}
\FunctionTok{plot}\NormalTok{(Pct.Repeating}\FloatTok{.1}\NormalTok{st.Grade }\SpecialCharTok{\textasciitilde{}}\NormalTok{ Pct.Low.income.students,}\AttributeTok{data=}\NormalTok{houston,}
     \AttributeTok{col=}\FunctionTok{as.numeric}\NormalTok{(houston}\SpecialCharTok{$}\NormalTok{Year))}
\NormalTok{coefs }\OtherTok{\textless{}{-}} \FunctionTok{coefficients}\NormalTok{(lm}\FloatTok{.3}\NormalTok{c)}
\FunctionTok{abline}\NormalTok{(}\AttributeTok{a=}\NormalTok{coefs[}\DecValTok{1}\NormalTok{],}\AttributeTok{b=}\NormalTok{coefs[}\DecValTok{2}\NormalTok{],}\AttributeTok{col=}\DecValTok{1}\NormalTok{)}
\FunctionTok{abline}\NormalTok{(}\AttributeTok{a=}\NormalTok{coefs[}\DecValTok{1}\NormalTok{]}\SpecialCharTok{+}\NormalTok{coefs[}\DecValTok{3}\NormalTok{],}\AttributeTok{b=}\NormalTok{coefs[}\DecValTok{2}\NormalTok{]}\SpecialCharTok{+}\NormalTok{coefs[}\DecValTok{4}\NormalTok{],}\AttributeTok{col=}\DecValTok{2}\NormalTok{)}
\FunctionTok{legend}\NormalTok{(}\DecValTok{78}\NormalTok{,}\DecValTok{18}\NormalTok{,}\AttributeTok{legend=}\FunctionTok{c}\NormalTok{(}\StringTok{"1994{-}1995"}\NormalTok{,}\StringTok{"2004{-}2005"}\NormalTok{),}\AttributeTok{pch=}\DecValTok{1}\NormalTok{,}\AttributeTok{col=}\FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{2}\NormalTok{),}\AttributeTok{cex=}\FloatTok{0.5}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{center}\includegraphics{hw04_solutions_files/figure-latex/3c model graph-1} \end{center}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{par}\NormalTok{(}\AttributeTok{mfrow=}\FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{,}\DecValTok{2}\NormalTok{))}
\FunctionTok{plot}\NormalTok{(lm}\FloatTok{.3}\NormalTok{c)}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw04_solutions_files/figure-latex/3c model diagnostics-1.pdf}
We can also apply an F test using the ANOVA function in R to compare the
full ANCOVA model in part (c) with the models in parts (a) and (b).

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{anova}\NormalTok{(lm}\FloatTok{.3}\NormalTok{a,lm}\FloatTok{.3}\NormalTok{c)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Analysis of Variance Table
## 
## Model 1: Pct.Repeating.1st.Grade ~ Pct.Low.income.students
## Model 2: Pct.Repeating.1st.Grade ~ Pct.Low.income.students * Year
##   Res.Df    RSS Df Sum of Sq      F Pr(>F)
## 1    120 1751.9                           
## 2    118 1744.4  2     7.512 0.2541 0.7761
\end{verbatim}

Since the \(F\) statistic testing \(H_0\):
\({\tt Pct.Repeating.1st.Grade ~\sim~ Pct.Low.income.students}\) vs
\(H_A\):
\({\tt Pct.Repeating.1st.Grade ~\sim~ Pct.Low.income.students * Year}\)
is nonsignificant (\(F=0.2541\), \(p=0.7761\)), we cannot reject the
simpler model in part (a): If we are starting with
\({\tt Pct.Repeating.1st.Grade ~\sim~ }\)
\({\tt Pct.Low.income.students}\), there is really no need to add
\({\tt Year}\) (let alone an interaction with \({\tt Year}\)) to the
model.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{anova}\NormalTok{(lm}\FloatTok{.3}\NormalTok{b,lm}\FloatTok{.3}\NormalTok{c)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Analysis of Variance Table
## 
## Model 1: Pct.Repeating.1st.Grade ~ Year
## Model 2: Pct.Repeating.1st.Grade ~ Pct.Low.income.students * Year
##   Res.Df    RSS Df Sum of Sq      F   Pr(>F)   
## 1    120 1957.9                                
## 2    118 1744.4  2    213.52 7.2221 0.001099 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

Here the \(F\) statistic testing \(H_0\):
\({\tt Pct.Repeating.1st.Grade ~\sim~ Year}\) vs \(H_A\):
\({\tt Pct.Repeating.1st.Grade ~\sim~ }\)
\({\tt Pct.Low.income.students * Year}\) is highly significant
(\(F=7.2221\), \(p=0.001099\)), so we would reject the simpler model in
part (b): starting with \({\tt Pct.Repeating.1st.Grade ~\sim~ Year}\),
we really do get a better model by adding
\({\tt Pct.Low.income.students}\) to the model.

Note that we cannot perform an \(F\) test like
\({\tt anova(lm.3a, lm.3b)}\)
directly\footnote{There are ways to compare non-nested models, e.g. cross-validation, AIC, BIC, etc., but it doesn't work with $F$ tests or likelihood ratio tests.},
since these models are not nested. We can infer, informally, from the
above tests though, that of all three models,
\({\tt Pct.Repeating.1st.Grade ~\sim~ Pct.Low.income.students}\) seems
to be the best. Thus, there is no difference between years in the way
that low income is related to repeating first grade.

\hypertarget{course-evaluations-question-from-gelman-hill}{%
\section{4. (course evaluations question from Gelman \&
Hill)}\label{course-evaluations-question-from-gelman-hill}}

\hypertarget{a-1}{%
\subsection{(4a)}\label{a-1}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{beauty }\OtherTok{\textless{}{-}} \FunctionTok{read.csv}\NormalTok{(}\StringTok{"ProfEvaltnsBeautyPublic.csv"}\NormalTok{)}
\NormalTok{model1 }\OtherTok{\textless{}{-}} \FunctionTok{lm}\NormalTok{(courseevaluation }\SpecialCharTok{\textasciitilde{}}\NormalTok{ btystdave }\SpecialCharTok{+}\NormalTok{ female, }\AttributeTok{data =}\NormalTok{ beauty)}
\NormalTok{model2 }\OtherTok{\textless{}{-}} \FunctionTok{lm}\NormalTok{(courseevaluation }\SpecialCharTok{\textasciitilde{}}\NormalTok{ btystdave}\SpecialCharTok{*}\NormalTok{female, }\AttributeTok{data =}\NormalTok{ beauty)}
\NormalTok{coef1 }\OtherTok{\textless{}{-}} \FunctionTok{coef}\NormalTok{(model1)  }\CommentTok{\# Get model parameters}
\NormalTok{coef2 }\OtherTok{\textless{}{-}} \FunctionTok{coef}\NormalTok{(model2)  }\CommentTok{\# Get model parameters}
\end{Highlighting}
\end{Shaded}

\hypertarget{i-1}{%
\subsubsection{(i)}\label{i-1}}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{par}\NormalTok{(}\AttributeTok{mfrow =} \FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{), }\AttributeTok{oma =} \FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{, }\DecValTok{0}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{0}\NormalTok{), }\AttributeTok{mar =} \FunctionTok{c}\NormalTok{(}\DecValTok{4}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{))}
\CommentTok{\# First model}
\FunctionTok{plot}\NormalTok{(courseevaluation }\SpecialCharTok{\textasciitilde{}}\NormalTok{ btystdave, }\AttributeTok{data =}\NormalTok{ beauty)}
\FunctionTok{abline}\NormalTok{(}\AttributeTok{a =}\NormalTok{ coef1[}\StringTok{"(Intercept)"}\NormalTok{], }\AttributeTok{b =}\NormalTok{ coef1[}\StringTok{"btystdave"}\NormalTok{], }\AttributeTok{col =} \StringTok{"red"}\NormalTok{)  }\CommentTok{\# line for males}
\FunctionTok{abline}\NormalTok{(}\AttributeTok{a =}\NormalTok{ coef1[}\StringTok{"(Intercept)"}\NormalTok{] }\SpecialCharTok{+}\NormalTok{ coef1[}\StringTok{"female"}\NormalTok{], }
       \AttributeTok{b =}\NormalTok{ coef1[}\StringTok{"btystdave"}\NormalTok{], }\AttributeTok{col =} \StringTok{"blue"}\NormalTok{)  }\CommentTok{\# line for females}

\CommentTok{\# Second model}
\FunctionTok{plot}\NormalTok{(courseevaluation }\SpecialCharTok{\textasciitilde{}}\NormalTok{ btystdave, }\AttributeTok{data =}\NormalTok{ beauty)}
\FunctionTok{abline}\NormalTok{(}\AttributeTok{a =}\NormalTok{ coef2[}\StringTok{"(Intercept)"}\NormalTok{], }\AttributeTok{b =}\NormalTok{ coef2[}\StringTok{"btystdave"}\NormalTok{], }\AttributeTok{col =} \StringTok{"red"}\NormalTok{)  }\CommentTok{\# line for males}
\FunctionTok{abline}\NormalTok{(}\AttributeTok{a =}\NormalTok{ coef2[}\StringTok{"(Intercept)"}\NormalTok{] }\SpecialCharTok{+}\NormalTok{ coef2[}\StringTok{"female"}\NormalTok{], }
       \AttributeTok{b =}\NormalTok{ coef2[}\StringTok{"btystdave"}\NormalTok{] }\SpecialCharTok{+}\NormalTok{ coef2[}\StringTok{"btystdave:female"}\NormalTok{], }\AttributeTok{col =} \StringTok{"blue"}\NormalTok{)  }\CommentTok{\# line for females}

\FunctionTok{legend}\NormalTok{(}\FloatTok{0.5}\NormalTok{, }\FloatTok{2.6}\NormalTok{, }\AttributeTok{legend =} \FunctionTok{c}\NormalTok{(}\StringTok{"Males"}\NormalTok{, }\StringTok{"Females"}\NormalTok{), }
       \AttributeTok{col =} \FunctionTok{c}\NormalTok{(}\StringTok{"red"}\NormalTok{, }\StringTok{"blue"}\NormalTok{), }\AttributeTok{lty =} \FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{1}\NormalTok{), }\AttributeTok{cex =} \FloatTok{0.6}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{hw04_solutions_files/figure-latex/4a_i-1.pdf}
\caption{Problem 2(a)i: Course evaluations against beauty ratings, with
no interaction (left) and with an interaction (right).
\label{fig:scatter_plots}}
\end{figure}

The red lines represent the predicted values for males, and the blue
lines represent predicted values for females. In the first plot, the
intercept for the red line is the \texttt{(Intercept)} coefficient,
while the intercept for the blue line is the \texttt{(Intercept)}
coefficient plus the \texttt{female} coefficient. The slope for both
lines is the \texttt{btystdave} coefficient.

In the second plot, the intercept for the red line is the
\texttt{(Intercept)} coefficient. The intercept for the blue line again
is the \texttt{(Intercept)} coefficient plus the \texttt{female}
coefficient. The slope for the red line is the \texttt{btystdave}
coefficient, while the slope for the blue line is the \texttt{btystdave}
coefficient plus the \texttt{btystdave:female} coefficient.

\hypertarget{ii-1}{%
\subsubsection{(ii)}\label{ii-1}}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{par}\NormalTok{(}\AttributeTok{mfrow=}\FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{, }\DecValTok{2}\NormalTok{), }\AttributeTok{mar =} \FunctionTok{c}\NormalTok{(}\DecValTok{4}\NormalTok{, }\FloatTok{4.5}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{1}\NormalTok{))}
\FunctionTok{plot}\NormalTok{(model1)}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{hw04_solutions_files/figure-latex/4a_ii_1-1.pdf}
\caption{Problem 2(a)ii: Diagnostic plots for the first
model\label{fig:diagnostic1}}
\end{figure}

In Figure \ref{fig:diagnostic1}, there don't appear to be any worrying
trends between the fitted values and the residuals. However, the Q-Q
plot shows some non-normality in the residuals, on the right side of the
plot; and the bottom right plot appears to show that the residuals are
left skewed, as there are a good number of standardized residuals with
values below -2. This is apparent also looking back at the scatter plots
in Figure \ref{fig:scatter_plots}.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{par}\NormalTok{(}\AttributeTok{mfrow=}\FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{, }\DecValTok{2}\NormalTok{), }\AttributeTok{mar =} \FunctionTok{c}\NormalTok{(}\DecValTok{4}\NormalTok{, }\FloatTok{4.5}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{1}\NormalTok{))}
\FunctionTok{plot}\NormalTok{(model2)}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{hw04_solutions_files/figure-latex/4a_ii_2-1.pdf}
\caption{Problem 2(a)ii: Diagnostic plots for the second
model\label{fig:diagnostic2}}
\end{figure}

Figure \ref{fig:diagnostic2} for the second model, with the interaction
effect, shows essentially the same results as for the first model. It's
possible that the fit could be improved by transforming the predictor(s)
and/or the outcome.

\hypertarget{iii-1}{%
\subsubsection{(iii)}\label{iii-1}}

\hypertarget{heres-the-summary-for-the-first-model-with-no-interaction}{%
\paragraph{Here's the summary for the first model with no
interaction:}\label{heres-the-summary-for-the-first-model-with-no-interaction}}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{summary}\NormalTok{(model1)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = courseevaluation ~ btystdave + female, data = beauty)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.87196 -0.36913  0.03493  0.39919  1.03237 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  4.09471    0.03328  123.03  < 2e-16 ***
## btystdave    0.14859    0.03195    4.65 4.34e-06 ***
## female      -0.19781    0.05098   -3.88  0.00012 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5373 on 460 degrees of freedom
## Multiple R-squared:  0.0663, Adjusted R-squared:  0.06224 
## F-statistic: 16.33 on 2 and 460 DF,  p-value: 1.407e-07
\end{verbatim}

\hypertarget{heres-the-summary-for-the-second-model-which-has-the-interaction}{%
\paragraph{Here's the summary for the second model, which has the
interaction:}\label{heres-the-summary-for-the-second-model-which-has-the-interaction}}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{summary}\NormalTok{(model2)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = courseevaluation ~ btystdave * female, data = beauty)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.83820 -0.37387  0.04551  0.39876  1.06764 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       4.10364    0.03359 122.158  < 2e-16 ***
## btystdave         0.20027    0.04333   4.622 4.95e-06 ***
## female           -0.20505    0.05103  -4.018 6.85e-05 ***
## btystdave:female -0.11266    0.06398  -1.761   0.0789 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5361 on 459 degrees of freedom
## Multiple R-squared:  0.07256,    Adjusted R-squared:  0.0665 
## F-statistic: 11.97 on 3 and 459 DF,  p-value: 1.471e-07
\end{verbatim}

\hypertarget{heres-a-partial-f-test-computed-with-anovamodel1-model2-where-model1-is-the-model-with-no-interaction}{%
\paragraph{\texorpdfstring{Here's a partial F-test, computed with
\texttt{anova(model1,\ model2)}, where \texttt{model1} is the model with
no
interaction:}{Here's a partial F-test, computed with anova(model1, model2), where model1 is the model with no interaction:}}\label{heres-a-partial-f-test-computed-with-anovamodel1-model2-where-model1-is-the-model-with-no-interaction}}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{anova}\NormalTok{(model1, model2)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Analysis of Variance Table
## 
## Model 1: courseevaluation ~ btystdave + female
## Model 2: courseevaluation ~ btystdave * female
##   Res.Df    RSS Df Sum of Sq     F  Pr(>F)  
## 1    460 132.81                             
## 2    459 131.92  1   0.89124 3.101 0.07891 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

The coefficient estimates for \texttt{btystdave} and \texttt{female} are
quite large in both models with respect to their estimated standard
errors, which is reflected in the fact that their associated p-values
are quite small: the three stars (***) mark that they are less than
0.001. Notice that the estimates differ between the two models. This is
not surprising; in general, adding a new variable to a model will change
the estimates of all the coefficients, unless that new variable is
completely uncorrelated with the previous variables. Since the ``new''
variable here is an interaction between the other two variables, it is
naturally correlated with those variables, so we should expect the
estimates to change.

The adjusted \(R^2\) values for both models are quite small, indicating
that the predictors do not account for much of the total variance. The
addition of the interaction term did not increase the adjusted \(R^2\)
by a non-trivial amount.

The estimate of \texttt{btystdave:female}, the interaction term, is not
significantly different from 0 at the conventional \(\alpha = 0.05\)
level. The actual magnitude of the estimate is also smaller than the
main effect estimates. The partial F-test here constitutes a test of the
null hypothesis \begin{align*}
  H_0: \beta_3 = 0
\end{align*} where \(\beta_3\) is the coefficient associated with
\texttt{btystdave:female}. Hence, the p-value of the partial F-test that
is printed in the \texttt{anova} output is equivalent to the p-value of
the t-test in the output of \texttt{summary(model2)}, namely,
\(p = 0.0789\).

Since we don't have enough evidence to reject \(H_0\), and since the
addition of the interaction term term did not increase the adjusted
\(R^2\), in the interest of parsimony, we may decide based on these
results to exclude the interaction term.

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

We don't have a lot of formal tools available for variable selection at
this stage, so the general approach here is to think in real-world terms
about what variables seem likely to be related to the outcome and see
what happens when they are included in the model. There's no one correct
approach or outcome here. In addition to \texttt{btystdave} and
\texttt{female}, let's consider the variables \texttt{age},
\texttt{minority}, \texttt{nonenglish}, \texttt{tenured}, and
\texttt{onecredit}. First, let's look at a pairs plot with
\texttt{courseevaluation}:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{cols }\OtherTok{\textless{}{-}} \FunctionTok{c}\NormalTok{(}\StringTok{"minority"}\NormalTok{, }\StringTok{"nonenglish"}\NormalTok{, }\StringTok{"tenured"}\NormalTok{, }\StringTok{"onecredit"}\NormalTok{)}
\NormalTok{beauty[, cols] }\OtherTok{\textless{}{-}} \FunctionTok{lapply}\NormalTok{(beauty[, cols], as.factor)}
\FunctionTok{ggpairs}\NormalTok{(beauty[, }\FunctionTok{c}\NormalTok{(}\StringTok{"courseevaluation"}\NormalTok{, }\StringTok{"age"}\NormalTok{, cols)])}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{hw04_solutions_files/figure-latex/4b-1.pdf}
\caption{Pairs plot for candidate predictors in
2(b)\label{fig:2b_pairs}}
\end{figure}

The marginal distributions of each variable appear on the diagonal,
while the top row and leftmost column give different visualizations of
the relationship between the course evaluation scores and the other five
variables. Among the candidate predictors, only \texttt{onecredit} shows
a clear marginal relationship with \texttt{courseevaluation}, but since
these are marginal plots, that doesn't mean the other variables won't be
informative to some degree. There are very few one credit courses
represented here (27 out of 463 rows), but nevertheless, let's try
including it, along with the following sets of variables: \begin{align*}
  &\text{Model m1: onecredit} \\
  &\text{Model m2: onecredit, minority, nonenglish} \\
  &\text{Model m3: onecredit, minority, nonenglish, tenured, age} 
\end{align*} For each model, we'll look at a model summary and
diagnostic plots. We can also run an F-test comparing the model from
part 2(a)ii (aka \texttt{model1}) to the expanded model.

\hypertarget{model-m1}{%
\subsubsection{Model m1}\label{model-m1}}

\texttt{m1\ \textless{}-\ lm(courseevaluation\ \textasciitilde{}\ btystdave\ +\ female\ +\ onecredit)}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{m1 }\OtherTok{\textless{}{-}} \FunctionTok{lm}\NormalTok{(courseevaluation }\SpecialCharTok{\textasciitilde{}}\NormalTok{ btystdave }\SpecialCharTok{+}\NormalTok{ female }\SpecialCharTok{+}\NormalTok{ onecredit, }\AttributeTok{data =}\NormalTok{ beauty)}
\FunctionTok{summary}\NormalTok{(m1)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = courseevaluation ~ btystdave + female + onecredit, 
##     data = beauty)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.82352 -0.34541  0.06084  0.38657  1.08122 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  4.05783    0.03287 123.438  < 2e-16 ***
## btystdave    0.16258    0.03103   5.240 2.45e-07 ***
## female      -0.18832    0.04938  -3.814 0.000155 ***
## onecredit1   0.58513    0.10358   5.649 2.84e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5201 on 459 degrees of freedom
## Multiple R-squared:  0.127,  Adjusted R-squared:  0.1213 
## F-statistic: 22.25 on 3 and 459 DF,  p-value: 1.804e-13
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{par}\NormalTok{(}\AttributeTok{mfrow=}\FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{, }\DecValTok{2}\NormalTok{), }\AttributeTok{mar =} \FunctionTok{c}\NormalTok{(}\DecValTok{4}\NormalTok{, }\FloatTok{4.5}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{1}\NormalTok{))}
\FunctionTok{plot}\NormalTok{(m1)}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{hw04_solutions_files/figure-latex/4b_models1-1.pdf}
\caption{Diagnostic plots for model m1\label{fig:2b_m1}}
\end{figure}

\hypertarget{f-test-for-model-m1-vs.-model1}{%
\paragraph{F-Test for model m1
vs.~model1}\label{f-test-for-model-m1-vs.-model1}}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{anova}\NormalTok{(model1, m1)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Analysis of Variance Table
## 
## Model 1: courseevaluation ~ btystdave + female
## Model 2: courseevaluation ~ btystdave + female + onecredit
##   Res.Df    RSS Df Sum of Sq      F    Pr(>F)    
## 1    460 132.81                                  
## 2    459 124.18  1    8.6326 31.909 2.838e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

\hypertarget{model-m2}{%
\subsubsection{Model m2}\label{model-m2}}

\texttt{m2\ \textless{}-\ lm(courseevaluation\ \textasciitilde{}\ btystdave\ +\ female\ +\ onecredit\ +\ minority\ +\ nonenglish)}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{m2 }\OtherTok{\textless{}{-}} \FunctionTok{lm}\NormalTok{(courseevaluation }\SpecialCharTok{\textasciitilde{}}\NormalTok{ btystdave }\SpecialCharTok{+}\NormalTok{ female }\SpecialCharTok{+}\NormalTok{ onecredit }\SpecialCharTok{+}\NormalTok{ minority }\SpecialCharTok{+}\NormalTok{ nonenglish, }
         \AttributeTok{data =}\NormalTok{ beauty)}
\FunctionTok{summary}\NormalTok{(m2)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = courseevaluation ~ btystdave + female + onecredit + 
##     minority + nonenglish, data = beauty)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.84951 -0.33198  0.04644  0.37907  1.05533 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  4.08668    0.03327 122.833  < 2e-16 ***
## btystdave    0.16604    0.03063   5.422 9.59e-08 ***
## female      -0.17418    0.04911  -3.546 0.000431 ***
## onecredit1   0.64133    0.10632   6.032 3.34e-09 ***
## minority1   -0.16479    0.07569  -2.177 0.029982 *  
## nonenglish1 -0.24801    0.10523  -2.357 0.018859 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.513 on 457 degrees of freedom
## Multiple R-squared:  0.1546, Adjusted R-squared:  0.1453 
## F-statistic: 16.71 on 5 and 457 DF,  p-value: 3.586e-15
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{par}\NormalTok{(}\AttributeTok{mfrow=}\FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{, }\DecValTok{2}\NormalTok{), }\AttributeTok{mar =} \FunctionTok{c}\NormalTok{(}\DecValTok{4}\NormalTok{, }\FloatTok{4.5}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{1}\NormalTok{))}
\FunctionTok{plot}\NormalTok{(m2)}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{hw04_solutions_files/figure-latex/4b_models2-1.pdf}
\caption{Diagnostic plots for model m2\label{fig:2b_m2}}
\end{figure}

\hypertarget{f-test-for-model-m2-vs.-model1}{%
\paragraph{F-Test for model m2
vs.~model1}\label{f-test-for-model-m2-vs.-model1}}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{anova}\NormalTok{(model1, m2)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Analysis of Variance Table
## 
## Model 1: courseevaluation ~ btystdave + female
## Model 2: courseevaluation ~ btystdave + female + onecredit + minority + 
##     nonenglish
##   Res.Df    RSS Df Sum of Sq      F    Pr(>F)    
## 1    460 132.81                                  
## 2    457 120.25  3    12.556 15.906 7.475e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

\hypertarget{model-m3}{%
\subsubsection{Model m3}\label{model-m3}}

\texttt{m3\ \textless{}-\ lm(courseevaluation\ \textasciitilde{}\ btystdave\ +\ female\ +\ onecredit\ +\ minority\ +\ nonenglish)}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{m3 }\OtherTok{\textless{}{-}} \FunctionTok{lm}\NormalTok{(courseevaluation }\SpecialCharTok{\textasciitilde{}}\NormalTok{ btystdave }\SpecialCharTok{+}\NormalTok{ female }\SpecialCharTok{+}\NormalTok{ onecredit }\SpecialCharTok{+}\NormalTok{ minority }\SpecialCharTok{+}\NormalTok{ nonenglish }\SpecialCharTok{+}
\NormalTok{           tenured }\SpecialCharTok{+}\NormalTok{ age, }\AttributeTok{data =}\NormalTok{ beauty)}
\FunctionTok{summary}\NormalTok{(m3)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = courseevaluation ~ btystdave + female + onecredit + 
##     minority + nonenglish + tenured + age, data = beauty)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.8386 -0.3414  0.0501  0.3776  1.0619 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  4.187474   0.138209  30.298  < 2e-16 ***
## btystdave    0.159636   0.032038   4.983 8.92e-07 ***
## female      -0.182937   0.051959  -3.521 0.000474 ***
## onecredit1   0.640845   0.112170   5.713 2.01e-08 ***
## minority1   -0.168522   0.076030  -2.217 0.027151 *  
## nonenglish1 -0.245503   0.106022  -2.316 0.021024 *  
## tenured1     0.005034   0.056134   0.090 0.928583    
## age         -0.002068   0.002807  -0.737 0.461551    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5138 on 455 degrees of freedom
## Multiple R-squared:  0.1556, Adjusted R-squared:  0.1426 
## F-statistic: 11.98 on 7 and 455 DF,  p-value: 4.703e-14
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{par}\NormalTok{(}\AttributeTok{mfrow=}\FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{, }\DecValTok{2}\NormalTok{), }\AttributeTok{mar =} \FunctionTok{c}\NormalTok{(}\DecValTok{4}\NormalTok{, }\FloatTok{4.5}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{1}\NormalTok{))}
\FunctionTok{plot}\NormalTok{(m3)}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{hw04_solutions_files/figure-latex/4b_models3-1.pdf}
\caption{Diagnostic plots for model m3\label{fig:2b_m3}}
\end{figure}

\hypertarget{f-test-for-model-m3-vs.-model1}{%
\paragraph{F-Test for model m3
vs.~model1}\label{f-test-for-model-m3-vs.-model1}}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{anova}\NormalTok{(model1, m3)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Analysis of Variance Table
## 
## Model 1: courseevaluation ~ btystdave + female
## Model 2: courseevaluation ~ btystdave + female + onecredit + minority + 
##     nonenglish + tenured + age
##   Res.Df    RSS Df Sum of Sq     F    Pr(>F)    
## 1    460 132.81                                 
## 2    455 120.10  5    12.706 9.627 9.498e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

The diagnostics for all three models look reasonable, and they look
fairly similar to the diagnostics for \texttt{model1}, the model that we
retained from part 2(a)iii. The F-tests comparing each model to the
baseline \texttt{model1} are all highly significant. Summaries for
models \texttt{m1} and \texttt{m2} each model show highly significant
coefficients. In model \texttt{m3}, however, the tests for
\texttt{tenured} and \texttt{age} are non-significant, and the
coefficient estimates are tiny. Given these tiny estimates, it would be
reasonable to drop these variables from the model and retain model
\texttt{m2}.

If we believe this model, then we can say that course evaluations bear
at least a roughly linear relationship to ratings of beauty, whether a
person is female or male, whether a course is one credit or not, whether
the professor is a minority, and whether the professor is a native
English speaker or not, with the sign and magnitude of the estimated
coefficients indicating the nature and strength of the relationships. It
is tempting to draw causal conclusions here, but we can't really do that
without checking some additional assumptions which are beyond the scope
of the problem. However, these patterns may at least be suggestive of
causal relationships, which we can subsequently go about testing more
rigorously.

\hypertarget{end-of-solutions}{%
\subsection{(end of solutions)}\label{end-of-solutions}}

\end{document}
