\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 01 Solutions},
            pdfauthor={Alan Mishler},
            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 01 Solutions}
    \pretitle{\vspace{\droptitle}\centering\huge}
  \posttitle{\par}
    \author{Alan Mishler}
    \preauthor{\centering\large\emph}
  \postauthor{\par}
      \predate{\centering\large\emph}
  \postdate{\par}
    \date{8/30/2018}


\begin{document}
\maketitle

\section{Chapter 1 Exercises}\label{chapter-1-exercises}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{load}\NormalTok{(}\StringTok{"../usingR.RData"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\subsection{Exercise 1}\label{exercise-1}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{plot}\NormalTok{(distance }\OperatorTok{~}\StringTok{ }\NormalTok{stretch, }\DataTypeTok{data =}\NormalTok{ elasticband)}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw01_solutions_files/figure-latex/1_1-1.pdf}

\subsection{Exercise 2}\label{exercise-2}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# i.}
\NormalTok{snow <-}\StringTok{ }\KeywordTok{data.frame}\NormalTok{(}\DataTypeTok{year =} \DecValTok{1970}\OperatorTok{:}\DecValTok{1979}\NormalTok{,}
                   \DataTypeTok{snow.cover =} \KeywordTok{c}\NormalTok{(}\FloatTok{6.5}\NormalTok{, }\DecValTok{12}\NormalTok{, }\FloatTok{14.9}\NormalTok{, }\DecValTok{10}\NormalTok{, }\FloatTok{10.7}\NormalTok{, }\FloatTok{7.9}\NormalTok{, }\FloatTok{21.9}\NormalTok{, }\FloatTok{12.5}\NormalTok{, }\FloatTok{14.5}\NormalTok{, }\FloatTok{9.2}\NormalTok{))}

\CommentTok{# ii.}
\KeywordTok{plot}\NormalTok{(snow.cover }\OperatorTok{~}\StringTok{ }\NormalTok{year, }\DataTypeTok{data =}\NormalTok{ snow)}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw01_solutions_files/figure-latex/1_2-1.pdf}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# iii. }
\KeywordTok{hist}\NormalTok{(snow}\OperatorTok{$}\NormalTok{snow.cover, }\DataTypeTok{xlab =} \StringTok{"Snow cover"}\NormalTok{, }\DataTypeTok{main =} \StringTok{"Histogram of snow cover"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw01_solutions_files/figure-latex/1_2-2.pdf}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# iv.}
\KeywordTok{plot}\NormalTok{(}\KeywordTok{log}\NormalTok{(snow.cover) }\OperatorTok{~}\StringTok{ }\NormalTok{year, }\DataTypeTok{data =}\NormalTok{ snow)}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw01_solutions_files/figure-latex/1_2-3.pdf}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{hist}\NormalTok{(}\KeywordTok{log}\NormalTok{(snow}\OperatorTok{$}\NormalTok{snow.cover), }\DataTypeTok{xlab =} \StringTok{"Log of snow cover"}\NormalTok{, }\DataTypeTok{main =} \StringTok{"Histogram of snow cover"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw01_solutions_files/figure-latex/1_2-4.pdf}

\subsection{Exercise 3}\label{exercise-3}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{orings <-}\StringTok{ }\KeywordTok{data.frame}\NormalTok{(}\DataTypeTok{temperature =} \KeywordTok{c}\NormalTok{(}\DecValTok{53}\NormalTok{, }\DecValTok{57}\NormalTok{, }\DecValTok{63}\NormalTok{, }\DecValTok{70}\NormalTok{, }\DecValTok{70}\NormalTok{, }\DecValTok{75}\NormalTok{),}
                     \DataTypeTok{erosion =} \KeywordTok{c}\NormalTok{(}\DecValTok{3}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{0}\NormalTok{),}
                     \DataTypeTok{blowby =} \KeywordTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{, }\DecValTok{0}\NormalTok{, }\DecValTok{0}\NormalTok{, }\DecValTok{0}\NormalTok{, }\DecValTok{0}\NormalTok{, }\DecValTok{2}\NormalTok{),}
                     \DataTypeTok{total =} \KeywordTok{c}\NormalTok{(}\DecValTok{5}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{1}\NormalTok{))}

\KeywordTok{plot}\NormalTok{(total }\OperatorTok{~}\StringTok{ }\NormalTok{temperature, }\DataTypeTok{data =}\NormalTok{ orings)}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw01_solutions_files/figure-latex/1_3-1.pdf}

\section{Chapter 2 Exercises}\label{chapter-2-exercises}

\subsection{Exercise 1}\label{exercise-1-1}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# 2.1(a)}
\NormalTok{answer <-}\StringTok{ }\DecValTok{0}
\ControlFlowTok{for}\NormalTok{ (j }\ControlFlowTok{in} \DecValTok{3}\OperatorTok{:}\DecValTok{5}\NormalTok{) \{answer <-}\StringTok{ }\NormalTok{j }\OperatorTok{+}\StringTok{ }\NormalTok{answer\}}
\KeywordTok{print}\NormalTok{(answer)}
\end{Highlighting}
\end{Shaded}

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

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# 2.1(b)}
\NormalTok{answer <-}\StringTok{ }\DecValTok{10}
\ControlFlowTok{for}\NormalTok{ (j }\ControlFlowTok{in} \DecValTok{3}\OperatorTok{:}\DecValTok{5}\NormalTok{) \{answer <-}\StringTok{ }\NormalTok{j }\OperatorTok{+}\StringTok{ }\NormalTok{answer\}}
\KeywordTok{print}\NormalTok{(answer)}
\end{Highlighting}
\end{Shaded}

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

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# 2.1(c)}
\NormalTok{answer <-}\StringTok{ }\DecValTok{10}
\ControlFlowTok{for}\NormalTok{ (j }\ControlFlowTok{in} \DecValTok{3}\OperatorTok{:}\DecValTok{5}\NormalTok{) \{answer <-}\StringTok{ }\NormalTok{j}\OperatorTok{*}\NormalTok{answer\}}
\KeywordTok{print}\NormalTok{(answer)}
\end{Highlighting}
\end{Shaded}

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

\subsection{Exercise 2}\label{exercise-2-1}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{?prod}
\KeywordTok{prod}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\DecValTok{10}\NormalTok{, }\DecValTok{3}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{5}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

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

\subsection{Exercise 5}\label{exercise-5}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{radii <-}\StringTok{ }\DecValTok{3}\OperatorTok{:}\DecValTok{20}
\NormalTok{spheres <-}\StringTok{ }\KeywordTok{data.frame}\NormalTok{(}\DataTypeTok{radius =}\NormalTok{ radii, }\DataTypeTok{volume =} \DecValTok{4}\OperatorTok{*}\NormalTok{pi}\OperatorTok{*}\NormalTok{radii}\OperatorTok{^}\DecValTok{3}\OperatorTok{/}\DecValTok{3}\NormalTok{)}
\NormalTok{spheres}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##    radius     volume
## 1       3   113.0973
## 2       4   268.0826
## 3       5   523.5988
## 4       6   904.7787
## 5       7  1436.7550
## 6       8  2144.6606
## 7       9  3053.6281
## 8      10  4188.7902
## 9      11  5575.2798
## 10     12  7238.2295
## 11     13  9202.7721
## 12     14 11494.0403
## 13     15 14137.1669
## 14     16 17157.2847
## 15     17 20579.5263
## 16     18 24429.0245
## 17     19 28730.9120
## 18     20 33510.3216
\end{verbatim}

When you type \texttt{pi} in R and hit \texttt{Enter}, R prints the
value of \(\pi\) to some number of digits.

\subsection{Exercise 6}\label{exercise-6}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# Identify which columns are factors}
\NormalTok{factor_columns <-}\StringTok{ }\KeywordTok{sapply}\NormalTok{(tinting, is.factor)}
\NormalTok{factor_columns}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##   case     id    age    sex   tint target     it   csoa  agegp 
##  FALSE  FALSE  FALSE   TRUE   TRUE   TRUE  FALSE  FALSE   TRUE
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# Get levels of factor columns}
\KeywordTok{sapply}\NormalTok{(tinting[factor_columns], levels)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## $sex
## [1] "f" "m"
## 
## $tint
## [1] "no" "lo" "hi"
## 
## $target
## [1] "locon" "hicon"
## 
## $agegp
## [1] "Younger" "Older"
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# Check which columns are ordered factors}
\KeywordTok{sapply}\NormalTok{(tinting, is.ordered)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##   case     id    age    sex   tint target     it   csoa  agegp 
##  FALSE  FALSE  FALSE  FALSE   TRUE  FALSE  FALSE  FALSE  FALSE
\end{verbatim}

\section{Chapter 3 Exercises}\label{chapter-3-exercises}

\subsection{Exercise 1}\label{exercise-1-2}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# 3.1(a)}
\KeywordTok{plot}\NormalTok{(mean.height }\OperatorTok{~}\StringTok{ }\NormalTok{year, }\DataTypeTok{data =}\NormalTok{ huron)}

\CommentTok{# 3.1(b)}
\CommentTok{# Note: These labels won't necessarily show up in your saved output, which is fine.}
\KeywordTok{identify}\NormalTok{(huron}\OperatorTok{$}\NormalTok{year, huron}\OperatorTok{$}\NormalTok{mean.height, }\DataTypeTok{labels =}\NormalTok{ huron}\OperatorTok{$}\NormalTok{year)}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw01_solutions_files/figure-latex/3_1-1.pdf}

\begin{verbatim}
## integer(0)
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# 3.1(c)}
\CommentTok{# Note: If you read the docs for lag.plot(), with ?lag.plot or help(lag.plot),}
\CommentTok{# you'll see that you can't change certain graphical parameters, including xlab and ylab.}
\KeywordTok{lag.plot}\NormalTok{(huron}\OperatorTok{$}\NormalTok{mean.height, }\DataTypeTok{do.lines =} \OtherTok{FALSE}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw01_solutions_files/figure-latex/3_1-2.pdf}

\subsection{Exercise 3}\label{exercise-3-1}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# 3.3(a)}
\KeywordTok{hist}\NormalTok{(possum}\OperatorTok{$}\NormalTok{hdlngth, }\DataTypeTok{xlab =} \StringTok{"Head length"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw01_solutions_files/figure-latex/3_3-1.pdf}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# 3.3(b)}
\CommentTok{# Note: the code in the problem is incorrectly given as stem(qqnorm(possum$hdlength))}
\KeywordTok{stem}\NormalTok{(possum}\OperatorTok{$}\NormalTok{hdlngth)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##   The decimal point is at the |
## 
##    82 | 5
##    83 | 
##    84 | 7
##    85 | 1389
##    86 | 057
##    87 | 
##    88 | 24467
##    89 | 23556
##    90 | 001456677
##    91 | 00334455666889
##    92 | 014445568889
##    93 | 112233334556688
##    94 | 0113444578889
##    95 | 113446789
##    96 | 0399
##    97 | 678
##    98 | 056
##    99 | 9
##   100 | 
##   101 | 
##   102 | 5
##   103 | 1
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# 3.3(c)}
\KeywordTok{qqnorm}\NormalTok{(possum}\OperatorTok{$}\NormalTok{hdlngth)}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw01_solutions_files/figure-latex/3_3-2.pdf}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# 3.3(d)}
\KeywordTok{plot}\NormalTok{(}\KeywordTok{density}\NormalTok{(possum}\OperatorTok{$}\NormalTok{hdlngth), }\DataTypeTok{main =} \StringTok{"Estimated density of possum head length"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw01_solutions_files/figure-latex/3_3-3.pdf}

These displays provide slightly different types of information. The stem
and leaf plot is kind of a sideways histogram, with the bin widths fixed
at 1, but it allows you to see all the individual data points. The
histogram provides more flexibility in that bin widths are adjustable,
but it is sensitive to the bin width chosen. The density plot is a
smoothed version of the histogram, which may give a better sense of the
shape of the distribution, but it is sensitive to the parameters of
kernel used to estimate the curve. The normal QQ plot makes it easy to
see deviations from normality, but it is hard to see what the
distribution is if it is not normal. The other displays make it much
easier to see the shape of the distribution.

\subsection{Exercise 5}\label{exercise-5-1}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{par}\NormalTok{(}\DataTypeTok{mfrow =} \KeywordTok{c}\NormalTok{(}\DecValTok{3}\NormalTok{, }\DecValTok{4}\NormalTok{))}
\ControlFlowTok{for}\NormalTok{ (i }\ControlFlowTok{in} \DecValTok{1}\OperatorTok{:}\DecValTok{4}\NormalTok{) \{}
  \KeywordTok{qqnorm}\NormalTok{(}\KeywordTok{rnorm}\NormalTok{(}\DecValTok{10}\NormalTok{))}
\NormalTok{\}}
\ControlFlowTok{for}\NormalTok{ (i }\ControlFlowTok{in} \DecValTok{1}\OperatorTok{:}\DecValTok{4}\NormalTok{) \{}
  \KeywordTok{qqnorm}\NormalTok{(}\KeywordTok{rnorm}\NormalTok{(}\DecValTok{100}\NormalTok{))}
\NormalTok{\}}
\ControlFlowTok{for}\NormalTok{ (i }\ControlFlowTok{in} \DecValTok{1}\OperatorTok{:}\DecValTok{4}\NormalTok{) \{}
  \KeywordTok{qqnorm}\NormalTok{(}\KeywordTok{rnorm}\NormalTok{(}\DecValTok{1000}\NormalTok{))}
\NormalTok{\}}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw01_solutions_files/figure-latex/3_5-1.pdf}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# Typically you'll want to reset the grid after changing it.}
\KeywordTok{par}\NormalTok{(}\DataTypeTok{mfrow =} \KeywordTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{1}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

The larger the sample size, the more representative the sample (i.e.~the
closer to normal), and hence the straighter the line in the QQ plot.


\end{document}
