\documentclass[]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
\else % if luatex or xelatex
  \ifxetex
    \usepackage{mathspec}
  \else
    \usepackage{fontspec}
  \fi
  \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\usepackage[margin=1in]{geometry}
\usepackage{hyperref}
\hypersetup{unicode=true,
            pdftitle={Homework 02 Solutions},
            pdfborder={0 0 0},
            breaklinks=true}
\urlstyle{same}  % don't use monospace font for urls
\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{\KeywordTok}[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{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\usepackage{graphicx,grffile}
\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}
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
}
\setlength{\emergencystretch}{3em}  % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{0}
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi

%%% Use protect on footnotes to avoid problems with footnotes in titles
\let\rmarkdownfootnote\footnote%
\def\footnote{\protect\rmarkdownfootnote}

%%% Change title format to be more compact
\usepackage{titling}

% Create subtitle command for use in maketitle
\newcommand{\subtitle}[1]{
  \posttitle{
    \begin{center}\large#1\end{center}
    }
}

\setlength{\droptitle}{-2em}

  \title{Homework 02 Solutions}
    \pretitle{\vspace{\droptitle}\centering\huge}
  \posttitle{\par}
    \author{}
    \preauthor{}\postauthor{}
      \predate{\centering\large\emph}
  \postdate{\par}
    \date{9/14/2018}

\usepackage{amsmath}
\usepackage{amsthm}

\begin{document}
\maketitle

Note: Question 4 is not graded. See the separate solution file for
Question 4.

\section{1. (Sheather 2.8.6)}\label{sheather-2.8.6}

\subsection{(a)}\label{a}

\begin{proof}
\begin{align*}
(y_i - \hat{y}_i) &= (y_i - (\hat{\beta}_0 + \hat{\beta}_1 x_i)) && 
                  \text{since } \hat{y}_i = \hat{\beta}_0 + \hat{\beta}_1 x_i \\
                  &= (y_i - (\bar{y} - \hat{\beta}_1\bar{x} + \hat{\beta}_1 x_i)) &&
                  \text{since } \hat{\beta}_0 = \bar{y} - \hat{\beta}_1\bar{x}\\
                  &= (y_i - \bar{y}) - \hat{\beta}_1(x_i - \bar{x})
\end{align*}
\end{proof}

\subsection{(b)}\label{b}

\begin{proof}
\begin{align*}
(\hat{y}_i - \bar{y}) &= ((\hat{\beta}_0 + \hat{\beta}_1 x_i) - \bar{y}) \\
                  &= ((\bar{y} - \hat{\beta}_1\bar{x} + \hat{\beta}_1 x_i) - \bar{y}) \\
                  &= \hat{\beta}_1(x_i - \bar{x})
\end{align*}
\end{proof}

\subsection{(c)}\label{c}

\begin{proof}
Substituting the equalities from parts (a) and (b) above, we have:
\begin{align*}
  \sum_{i=1}^n (y_i - \hat{y}_i)(\hat{y}_i - \bar{y}) &= 
  \sum_{i=1}^n [(y_i - \bar{y}) - \hat{\beta}_1(x_i - \bar{x})]\hat{\beta}_1(x_i - \bar{x}) \\
  &= \hat{\beta}_1 \left[\sum_{i=1}^n (y_i-\bar{y})(x_i-\bar{x}) - \hat{\beta}_1 \sum_{i=1}^n (x_i - \bar{x})^2 \right] \\
  &= \hat{\beta}_1 \left[SXY - \frac{SXY}{SXX}SXX\right] \\
  &= \hat{\beta}_1 [SXY - SXY] \\
  &= 0
\end{align*}
\end{proof}

\section{2. (Gelman \& Hill 2.6)}\label{gelman-hill-2.6}

\subsection{(a)}\label{a-1}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{var1 <-}\StringTok{ }\KeywordTok{rnorm}\NormalTok{(}\DecValTok{1000}\NormalTok{, }\DecValTok{0}\NormalTok{, }\DecValTok{1}\NormalTok{)}
\NormalTok{var2 <-}\StringTok{ }\KeywordTok{rnorm}\NormalTok{(}\DecValTok{1000}\NormalTok{, }\DecValTok{0}\NormalTok{, }\DecValTok{1}\NormalTok{)}
\NormalTok{model <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(var2 }\OperatorTok{~}\StringTok{ }\NormalTok{var1)}
\KeywordTok{summary}\NormalTok{(model)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = var2 ~ var1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.7075 -0.6497 -0.0185  0.6384  3.5440 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  0.02605    0.03062   0.851    0.395
## var1         0.01784    0.03014   0.592    0.554
## 
## Residual standard error: 0.9676 on 998 degrees of freedom
## Multiple R-squared:  0.0003509,  Adjusted R-squared:  -0.0006507 
## F-statistic: 0.3503 on 1 and 998 DF,  p-value: 0.5541
\end{verbatim}

The slope is not significantly different from 0, as we can see from the
p-value in the \texttt{var1} row,
\texttt{Pr(\textgreater{}\textbar{}t\textbar{})} column in the model
summary.

\subsection{(b)}\label{b-1}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(arm)  }\CommentTok{# Needed for the se.coef() function.}
\NormalTok{z.scores <-}\StringTok{ }\KeywordTok{rep}\NormalTok{(}\OtherTok{NA}\NormalTok{, }\DecValTok{100}\NormalTok{)}
\ControlFlowTok{for}\NormalTok{ (k }\ControlFlowTok{in} \DecValTok{1}\OperatorTok{:}\DecValTok{100}\NormalTok{) \{}
\NormalTok{  var1 <-}\StringTok{ }\KeywordTok{rnorm}\NormalTok{(}\DecValTok{1000}\NormalTok{, }\DecValTok{0}\NormalTok{, }\DecValTok{1}\NormalTok{)}
\NormalTok{  var2 <-}\StringTok{ }\KeywordTok{rnorm}\NormalTok{(}\DecValTok{1000}\NormalTok{, }\DecValTok{0}\NormalTok{, }\DecValTok{1}\NormalTok{)}
\NormalTok{  fit <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(var2 }\OperatorTok{~}\StringTok{ }\NormalTok{var1)}
\NormalTok{  z.scores[k] <-}\StringTok{ }\KeywordTok{coef}\NormalTok{(fit)[}\DecValTok{2}\NormalTok{]}\OperatorTok{/}\KeywordTok{se.coef}\NormalTok{(fit)[}\DecValTok{2}\NormalTok{]}
\NormalTok{\}}

\NormalTok{type1_errors <-}\StringTok{ }\KeywordTok{sum}\NormalTok{(}\KeywordTok{abs}\NormalTok{(z.scores) }\OperatorTok{>}\StringTok{ }\DecValTok{2}\NormalTok{)}
\KeywordTok{print}\NormalTok{(type1_errors)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] 5
\end{verbatim}

There were 5 z-scores that were significant.

\subsection{(c)}\label{c-1}

The rule of thumb to consider a z-score significant if the absolute
value exceeds 2 is roughly equivalent to setting \(\alpha = 0.05\),
since 95\% of the standard normal distribution falls within 1.96
standard deviations of the mean of 0. Therefore, we should expect the
coefficients to be significant 5 out of 100 times on average,
corresponding to a Type 1 error rate of 0.05.

\section{3. (Sheather 2.8.7)}\label{sheather-2.8.7}

The regression line is defined as \(\mu(x) = E(Y|X = x)\). In other
words, it's the mean value of \(Y\) at a given value of the covariates
\(X\). The confidence interval is for the regression line itself; that
is, it's a confidence interval around an estimated mean. It's entirely
possible for the data points themselves to fall outside the 95\%
confidence interval for a mean.

To gain some intuition, imagine that you have data from a normal
distribution and you want to estimate the mean of that distribution. The
more data you gather, the narrower the confidence interval around your
estimate will be. The underlying distribution hasn't changed, however,
so the more data you gather, the more of the data will fall outside the
confidence interval for the mean.

\section{5. (Sheather 3.4.8)}\label{sheather-3.4.8}

\subsection{Part 1}\label{part-1}

\subsubsection{(a)}\label{a-2}

Given the problem constraints, the only flexibility we have is in
specifying the intercept. We could (1) fix the intercept to some value,
or (2) estimate the intercept via least squares.

Option (1) typically means setting the intercept to 0, which can be nice
for interpretability in cases where the value (0, 0) is meaningful and
where a different intercept would not make sense. For example, suppose
we were asked to regress the number of words typed in a paper against
the number of hours of work put in. Zero hours of work would necessarily
produce zero words, so in that case there is a good reason to set the
intercept to 0.

Here, however, the size of a diamond is presumably bounded away from 0.
The notion of a zero-carat diamond doesn't make sense, and no one will
manufacture a ring with a diamond that's so small as to be invisible to
the naked eye. Indeed, the sizes of diamonds in the data range from 0.12
to 0.35 carats. If this is the range we're interested in modeling, then
we may as well give the model the additional flexibility of a non-zero
intercept, since the y-value at the intercept will not be meaningful
regardless.

Hence, we take option (2) and estimate a model of the form

\begin{align*}
  \hat{y}_i = \hat{\beta}_0 + \hat{\beta}_1 x_i
\end{align*}

Although the problem dicatates that we use a linear model, you should
always visualize the data before specifying a model to help you decide
whether that model is appropriate. Since we only have two variables,
let's simply look at a scatter plot of Price against Size (Figure
\ref{fig:scatter1}).

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{diamonds <-}\StringTok{ }\KeywordTok{read.table}\NormalTok{(}\StringTok{"diamonds.txt"}\NormalTok{, }\DataTypeTok{header =} \OtherTok{TRUE}\NormalTok{)}
\KeywordTok{plot}\NormalTok{(Price }\OperatorTok{~}\StringTok{ }\NormalTok{Size, }\DataTypeTok{data =}\NormalTok{ diamonds)}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{hw02_solutions_files/figure-latex/5a_scatter-1.pdf}
\caption{Scatter plot of diamond Price against Size\label{fig:scatter1}}
\end{figure}

It looks like there's a more or less linear relationship, so let's fit
the model.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{model5a <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(Price }\OperatorTok{~}\StringTok{ }\NormalTok{Size, }\DataTypeTok{data =}\NormalTok{ diamonds)}
\end{Highlighting}
\end{Shaded}

The coefficients of the model are

\(\hat{\beta}_0\): -258.05

\(\hat{\beta}_1\): 3715.02

Let's add the best fit line to the scatter plot (Figure
\ref{fig:scatter2}).

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{plot}\NormalTok{(Price }\OperatorTok{~}\StringTok{ }\NormalTok{Size, }\DataTypeTok{data =}\NormalTok{ diamonds)}
\KeywordTok{abline}\NormalTok{(}\DataTypeTok{a =} \KeywordTok{coef}\NormalTok{(model5a)[}\DecValTok{1}\NormalTok{], }\DataTypeTok{b =} \KeywordTok{coef}\NormalTok{(model5a)[}\DecValTok{2}\NormalTok{], }\DataTypeTok{col =} \StringTok{"red"}\NormalTok{, }\DataTypeTok{lty =} \StringTok{"dashed"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{hw02_solutions_files/figure-latex/5a_scatter_model-1.pdf}
\caption{Scatter plot of diamond Price against Size with least squares
line\label{fig:scatter2}}
\end{figure}

The model looks reasonable. We'll examine diagnostic plots in Part 2
below.

\subsubsection{(b)}\label{b-2}

One weakness of the model is that by definition, it cannot capture
nonlinearities in the relationship. Additionally, it has only one
predictor; additional predictors could provide additional information.
It also cannot deal with nonconstant variance.

\subsection{Part 2}\label{part-2}

\subsubsection{(a)}\label{a-3}

In order to decide whether and how to modify the model above, let's
examine some diagnostic plots. First, the residuals against the
predictor (Figure \ref{fig:scatter3}).

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{plot}\NormalTok{(model5a}\OperatorTok{$}\NormalTok{residuals }\OperatorTok{~}\StringTok{ }\NormalTok{diamonds}\OperatorTok{$}\NormalTok{Size, }\DataTypeTok{xlab =} \StringTok{"Size"}\NormalTok{, }\DataTypeTok{ylab =} \StringTok{"Residuals"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{hw02_solutions_files/figure-latex/5a_residuals-1.pdf}
\caption{Model 5a residuals against predictor\label{fig:scatter3}}
\end{figure}

There's no clear relationship (e.g.~the residuals don't seem to be
increasing or decreasing with Size), so that's good. Now let's examine
the diagnostic plots that R produces for us automatically (Figure
\ref{fig:diagnostics}).

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{par}\NormalTok{(}\DataTypeTok{mfrow =} \KeywordTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{, }\DecValTok{2}\NormalTok{), }\DataTypeTok{mar =} \KeywordTok{c}\NormalTok{(}\DecValTok{4}\NormalTok{, }\FloatTok{4.5}\NormalTok{, }\FloatTok{2.5}\NormalTok{, }\DecValTok{4}\NormalTok{)) }\CommentTok{# reducing the margins slightly}
\KeywordTok{plot}\NormalTok{(model5a)                                 }\CommentTok{# generating diagnostic plots}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{hw02_solutions_files/figure-latex/5a_diagnostics-1.pdf}
\caption{Diagnostic plots for model 5a\label{fig:diagnostics}}
\end{figure}

Let's discuss each of these in turn:

\begin{itemize}
\tightlist
\item
  The top left plot shows no relationship between the residuals and the
  fitted values (the \(\hat{y}_i\)s), so that's good.
\item
  The top right plot shows that the residuals are approximately normally
  distributed, which is also good.
\item
  The nonparametric red line in the bottom left plot shows a slight
  increasing trend between the fitted values and the standardized
  residuals, but there aren't many data points on the right side of the
  plot, so I wouldn't take this too seriously. (Recall that the variance
  of the residuals \(\hat{e}_i\) is nonconstant,
  \emph{even if the variance of the underlying errors $e_i$ is constant}.
  The standardized residuals have approximately constant variance, so it
  is useful to examine these instead of the raw residuals, particularly
  when points of high leverage exist.)
\item
  The bottom right plot shows that there are a couple outliers in the
  sense defined in Sheather: points whose standardized residual falls
  outside the range {[}-2, 2{]}. R has helpfully labeled these points
  with their row numbers in the data, so we can examine them.
  Additionally, recall the rule of thumb that a point is ``high
  leverage,'' or equivalently a ``leverage point,'' if its leverage
  value \(h_{ii}\) is greater than \(4/n\), where \(n\) is the number of
  data points. In this case, \(4/n = 4/49 \approx 0.082\), so we have a
  couple of leverage points in the data. One of these, point 42, has a
  relatively high Cook's distance; it lies close to the contour line
  that represents a Cook's distance of 0.5.
\end{itemize}

Since R has helpfully labeled points 4, 19, and 42 for us in the bottom
right diagnostic plot, let's examine these on the original graph (Figure
\ref{fig:scatter4}).

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{plot}\NormalTok{(Price }\OperatorTok{~}\StringTok{ }\NormalTok{Size, }\DataTypeTok{data =}\NormalTok{ diamonds)}
\KeywordTok{abline}\NormalTok{(}\DataTypeTok{a =} \KeywordTok{coef}\NormalTok{(model5a)[}\DecValTok{1}\NormalTok{], }\DataTypeTok{b =} \KeywordTok{coef}\NormalTok{(model5a)[}\DecValTok{2}\NormalTok{], }\DataTypeTok{col =} \StringTok{"red"}\NormalTok{, }\DataTypeTok{lty =} \StringTok{"dashed"}\NormalTok{)}
\KeywordTok{points}\NormalTok{(diamonds[}\KeywordTok{c}\NormalTok{(}\DecValTok{4}\NormalTok{, }\DecValTok{19}\NormalTok{, }\DecValTok{42}\NormalTok{), ], }\DataTypeTok{col =} \StringTok{"red"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{hw02_solutions_files/figure-latex/5a_leverage_points-1.pdf}
\caption{Scatter plot of diamond Price against Size with least squares
line and points 4, 19, 42 highlighted\label{fig:scatter4}}
\end{figure}

None of these points is obviously problematic. Let's check one more
diagnostic, the inverse response plot:

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# install.packages(alr3)  # if you don't already have it installed}
\KeywordTok{library}\NormalTok{(alr3)}
\KeywordTok{invisible}\NormalTok{(}\KeywordTok{inverseResponsePlot}\NormalTok{(model5a))}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{hw02_solutions_files/figure-latex/5a_inverse_response-1.pdf}
\caption{Inverse response plot for the diamonds model5a}
\end{figure}

Based on this plot, it looks like the original model, represented by the
light blue line (\(\lambda = 1\)) fits the data well. It doesn't look
like we would do better transforming the response variable, at least not
with a scaled power transformation.

Given these results, it seems reasonable to keep the original model in
5(a)

\subsubsection{(b)}\label{b-3}

Since the model hasn't changed, see 5(a).

\subsection{Part 3}\label{part-3}

The model did not change.


\end{document}
