% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \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 03 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{longtable,booktabs}
% Correct order of tables after \paragraph or \subparagraph
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
\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}
% 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}{5}

\title{Homework 03 Solutions}
\author{}
\date{\vspace{-2.5em}9/14/2020}

\begin{document}
\maketitle

\hypertarget{problem-1}{%
\section{Problem 1}\label{problem-1}}

\hypertarget{a-eda}{%
\subsection{(a) EDA}\label{a-eda}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{data <-}\StringTok{ }\KeywordTok{read.table}\NormalTok{(}\StringTok{"../cars04.dat"}\NormalTok{,}\DataTypeTok{header=}\NormalTok{T)  }
\NormalTok{num_of_NA_vals <-}\StringTok{ }\KeywordTok{sum}\NormalTok{(}\KeywordTok{is.na}\NormalTok{(data))}
\end{Highlighting}
\end{Shaded}

Our data contains 234 unique vehicles' information and 13 features about each vehicle, and the data is very clean, with 0 missing data values across all vehicles. Table \ref{tab:tab-variables} contains information about all the variables in the dataset.

\begin{longtable}[]{@{}ll@{}}
\caption{\label{tab:tab-variables} Details about all variables in \texttt{car04.dat} data frame}\tabularnewline
\toprule
\begin{minipage}[b]{0.21\columnwidth}\raggedright
Variable name\strut
\end{minipage} & \begin{minipage}[b]{0.73\columnwidth}\raggedright
Description\strut
\end{minipage}\tabularnewline
\midrule
\endfirsthead
\toprule
\begin{minipage}[b]{0.21\columnwidth}\raggedright
Variable name\strut
\end{minipage} & \begin{minipage}[b]{0.73\columnwidth}\raggedright
Description\strut
\end{minipage}\tabularnewline
\midrule
\endhead
\begin{minipage}[t]{0.21\columnwidth}\raggedright
\texttt{Hybrid}\strut
\end{minipage} & \begin{minipage}[t]{0.73\columnwidth}\raggedright
Is the vehicle a hybrid? (Yes = 1, No = 0)\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.21\columnwidth}\raggedright
\texttt{SuggestedRetailPrice}\strut
\end{minipage} & \begin{minipage}[t]{0.73\columnwidth}\raggedright
Suggested retail price, what the manufacturer thinks the vehicle is worth, including adequate profit for the automaker and the dealer (U.S. dollars)\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.21\columnwidth}\raggedright
\texttt{DealerCost}\strut
\end{minipage} & \begin{minipage}[t]{0.73\columnwidth}\raggedright
Price dealer had to pay manufacturer (U.S. dollars)\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.21\columnwidth}\raggedright
\texttt{EngineSize}\strut
\end{minipage} & \begin{minipage}[t]{0.73\columnwidth}\raggedright
Engine Size (liters)\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.21\columnwidth}\raggedright
\texttt{Cylinders}\strut
\end{minipage} & \begin{minipage}[t]{0.73\columnwidth}\raggedright
Number of cylinders\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.21\columnwidth}\raggedright
\texttt{Horsepower}\strut
\end{minipage} & \begin{minipage}[t]{0.73\columnwidth}\raggedright
Gross Hoursepower\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.21\columnwidth}\raggedright
\texttt{CityMPG}\strut
\end{minipage} & \begin{minipage}[t]{0.73\columnwidth}\raggedright
City miles per gallon\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.21\columnwidth}\raggedright
\texttt{HighwayMPG}\strut
\end{minipage} & \begin{minipage}[t]{0.73\columnwidth}\raggedright
Highway miles per gallon\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.21\columnwidth}\raggedright
\texttt{Weight}\strut
\end{minipage} & \begin{minipage}[t]{0.73\columnwidth}\raggedright
Weight of vehicle (pounds)\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.21\columnwidth}\raggedright
\texttt{WheelBase}\strut
\end{minipage} & \begin{minipage}[t]{0.73\columnwidth}\raggedright
Wheelbase - horizontal distance between front and rear wheels (inches)\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.21\columnwidth}\raggedright
\texttt{Length}\strut
\end{minipage} & \begin{minipage}[t]{0.73\columnwidth}\raggedright
Length of vehicle (inches)\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.21\columnwidth}\raggedright
\texttt{Width}\strut
\end{minipage} & \begin{minipage}[t]{0.73\columnwidth}\raggedright
Width of vehicle (inches)\strut
\end{minipage}\tabularnewline
\bottomrule
\end{longtable}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{par}\NormalTok{(}\DataTypeTok{mfrow =} \KeywordTok{c}\NormalTok{(}\DecValTok{3}\NormalTok{,}\DecValTok{4}\NormalTok{))}
\ControlFlowTok{for}\NormalTok{ (c_name }\ControlFlowTok{in} \KeywordTok{colnames}\NormalTok{(data))\{}
  \ControlFlowTok{if}\NormalTok{(}\KeywordTok{length}\NormalTok{(}\KeywordTok{unique}\NormalTok{(data[,c_name])) }\OperatorTok{<=}\StringTok{ }\DecValTok{6}\NormalTok{)\{}
\NormalTok{     c_vals <-}\StringTok{ }\KeywordTok{names}\NormalTok{(}\KeywordTok{table}\NormalTok{(data[,c_name]))}
\NormalTok{     c_counts <-}\StringTok{ }\KeywordTok{table}\NormalTok{(data[,c_name])}
     \KeywordTok{barplot}\NormalTok{(}\DataTypeTok{names.arg =}\NormalTok{ c_vals,}\DataTypeTok{height=}\NormalTok{ c_counts, }\DataTypeTok{main =}\NormalTok{ c_name)}
\NormalTok{  \}}\ControlFlowTok{else}\NormalTok{ \{}
    \KeywordTok{hist}\NormalTok{(data[,c_name], }\DataTypeTok{main =}\NormalTok{ c_name)}
\NormalTok{  \}}
\NormalTok{\}}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{hw03_solutions_files/figure-latex/unnamed-chunk-2-1.pdf}
\caption{\label{fig:unnamed-chunk-2}Univariate Distribution of Variables in \texttt{car04.dat}}
\end{figure}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{## or }
\CommentTok{# summary(data)}
\CommentTok{# table(data$Hybrid)}
\CommentTok{# }
\CommentTok{# par(mfrow=c(3,4))}
\CommentTok{# }
\CommentTok{# for (i in 1:12) \{}
\CommentTok{#     hist(data[,i],main=names(data)[i])}
\CommentTok{# \}}
\CommentTok{# }
\CommentTok{# for (i in 1:12) \{}
\CommentTok{#     boxplot(data[,i],main=names(data)[i])}
\CommentTok{# \}}
\end{Highlighting}
\end{Shaded}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{par}\NormalTok{(}\DataTypeTok{mar =} \KeywordTok{c}\NormalTok{(}\DecValTok{4}\NormalTok{, }\DecValTok{4}\NormalTok{, }\FloatTok{.1}\NormalTok{, }\FloatTok{.1}\NormalTok{))}
\KeywordTok{library}\NormalTok{(psych)}

\KeywordTok{pairs.panels}\NormalTok{(data[,}\DecValTok{2}\OperatorTok{:}\DecValTok{3}\NormalTok{])}
\NormalTok{log_price <-}\StringTok{ }\KeywordTok{log10}\NormalTok{(data[,}\DecValTok{2}\OperatorTok{:}\DecValTok{3}\NormalTok{])}
\KeywordTok{names}\NormalTok{(log_price) <-}\StringTok{ }\KeywordTok{paste0}\NormalTok{(}\StringTok{"log10("}\NormalTok{,}\KeywordTok{names}\NormalTok{(log_price), }\StringTok{")"}\NormalTok{)}
\KeywordTok{pairs.panels}\NormalTok{(log_price)}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\includegraphics[width=0.5\linewidth]{hw03_solutions_files/figure-latex/figures-side-1} \includegraphics[width=0.5\linewidth]{hw03_solutions_files/figure-latex/figures-side-2} \caption{Distributions of Prices and log10(Prices) relative to each other}\label{fig:figures-side}
\end{figure}

\hypertarget{b-model-fitting}{%
\subsection{(b) Model Fitting}\label{b-model-fitting}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{lm}\FloatTok{.1}\NormalTok{ <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(SuggestedRetailPrice }\OperatorTok{~}\StringTok{ }\NormalTok{DealerCost, }\DataTypeTok{data=}\NormalTok{data)}
\end{Highlighting}
\end{Shaded}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{summary}\NormalTok{(lm}\FloatTok{.1}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = SuggestedRetailPrice ~ DealerCost, data = data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1743.52  -262.59    74.92   265.98  2912.72 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -61.904248  81.801381  -0.757     0.45    
## DealerCost    1.088841   0.002638 412.768   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 587 on 232 degrees of freedom
## Multiple R-squared:  0.9986, Adjusted R-squared:  0.9986 
## F-statistic: 1.704e+05 on 1 and 232 DF,  p-value: < 2.2e-16
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{par}\NormalTok{(}\DataTypeTok{mfrow =} \KeywordTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{,}\DecValTok{2}\NormalTok{))}
\KeywordTok{plot}\NormalTok{(lm}\FloatTok{.1}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw03_solutions_files/figure-latex/fig-diagnostics-1.pdf}

In examining the relationship in the Section \ref{a-eda} there was a very linear relationship between the dealer's cost and the suggested retail price. After fitting, the very expensive cars (say with predicted sales cost above \$60,000) seem to underestimated. Additional, there appears to be natural clusters in the residual vs fitted plot and with future examination of some of these clusters (using \texttt{identify}) we find that certain clusters relate to vehicles made by the same brand - (e.g.~lower left 5 points of the plot are all Suzuki cars). The residuals vs normal QQ plot suggests that the residuals have slightly heavier tails (i.e.~more extreme values of residuals than might be expected). The scale-location plot shows us that as the estimated value increase we see an increase in the variability (suggesting heteroskedasticity of the errors)\footnote{This observation can also be gathered for the residual-fitted visualization.} - though it should be acknowledged that there are less observations with high estimated fitted values. Our Cook's distance figure shows that we have the Mercedes-Benz CL600 2dr has a very high impact on the model (having a Cook's distance above 1 and a high leverage score - with the average expected score being 1/234.).

The fitted regression would suggest that the retail price is supposed to be around 8-9\% larger than the dealer cost, and it is natural to see that as the price goes up the difference in predicting the truth also scales (think as a proportion of the predicted price).

\hypertarget{problem-2-box-cox}{%
\section{Problem 2 Box-Cox}\label{problem-2-box-cox}}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(car)}
\end{Highlighting}
\end{Shaded}

\hypertarget{a-transform-exploration}{%
\subsection{(a) Transform Exploration}\label{a-transform-exploration}}

\hypertarget{i.}{%
\subsubsection{i.}\label{i.}}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{boxCox}\NormalTok{(}\KeywordTok{lm}\NormalTok{(DealerCost}\OperatorTok{~}\DecValTok{1}\NormalTok{,}\DataTypeTok{data=}\NormalTok{data))}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw03_solutions_files/figure-latex/fig-boxcox-x-1.pdf}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{powerTransform}\NormalTok{(}\KeywordTok{lm}\NormalTok{(DealerCost}\OperatorTok{~}\DecValTok{1}\NormalTok{,}\DataTypeTok{data=}\NormalTok{data))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Estimated transformation parameter 
##         Y1 
## -0.2576098
\end{verbatim}

Based on the figure above we might transform \texttt{DealerCost} by raising it to a power of \(-.25\).

\hypertarget{ii.}{%
\subsubsection{ii.}\label{ii.}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{data}\OperatorTok{$}\NormalTok{TransCost <-}\StringTok{ }\NormalTok{data}\OperatorTok{$}\NormalTok{DealerCost}\OperatorTok{^}\NormalTok{(}\OperatorTok{-}\NormalTok{.}\DecValTok{25}\NormalTok{)}

\KeywordTok{boxCox}\NormalTok{(}\KeywordTok{lm}\NormalTok{(SuggestedRetailPrice}\OperatorTok{~}\NormalTok{TransCost,}\DataTypeTok{data=}\NormalTok{data))}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw03_solutions_files/figure-latex/fig-boxcox-y-1.pdf}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{powerTransform}\NormalTok{(}\KeywordTok{lm}\NormalTok{(SuggestedRetailPrice}\OperatorTok{~}\NormalTok{TransCost,}\DataTypeTok{data=}\NormalTok{data))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Estimated transformation parameter 
##         Y1 
## -0.2001193
\end{verbatim}

\hypertarget{b-transformed-model}{%
\subsection{(b) Transformed Model}\label{b-transformed-model}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{data}\OperatorTok{$}\NormalTok{srp}\FloatTok{.20}\NormalTok{ <-}\StringTok{ }\NormalTok{data}\OperatorTok{$}\NormalTok{SuggestedRetailPrice}\OperatorTok{^}\NormalTok{(}\OperatorTok{-}\FloatTok{0.20}\NormalTok{)}
\NormalTok{data}\OperatorTok{$}\NormalTok{dc}\FloatTok{.25}\NormalTok{ <-}\StringTok{ }\NormalTok{data}\OperatorTok{$}\NormalTok{DealerCost}\OperatorTok{^}\NormalTok{(}\OperatorTok{-}\FloatTok{0.25}\NormalTok{)}

\KeywordTok{par}\NormalTok{(}\DataTypeTok{mfrow=}\KeywordTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{1}\NormalTok{))}
\KeywordTok{pairs.panels}\NormalTok{(}\KeywordTok{data.frame}\NormalTok{(}\StringTok{`}\DataTypeTok{SuggestedRetailPrice^(-.2)}\StringTok{`}\NormalTok{ =}\StringTok{ }\NormalTok{data}\OperatorTok{$}\NormalTok{srp}\FloatTok{.20}\NormalTok{, }
                        \StringTok{`}\DataTypeTok{DealerCost^(-.25)}\StringTok{`}\NormalTok{ =}\StringTok{ }\NormalTok{data}\OperatorTok{$}\NormalTok{dc}\FloatTok{.25}\NormalTok{,}\DataTypeTok{check.names =}\NormalTok{ F))}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw03_solutions_files/figure-latex/unnamed-chunk-7-1.pdf}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{lm}\FloatTok{.2}\NormalTok{ <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(srp}\FloatTok{.20} \OperatorTok{~}\StringTok{ }\NormalTok{dc}\FloatTok{.25}\NormalTok{, }\DataTypeTok{data =}\NormalTok{ data)}

\KeywordTok{summary}\NormalTok{(lm}\FloatTok{.2}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = srp.20 ~ dc.25, data = data)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -1.143e-03 -2.482e-04 -1.312e-05  2.365e-04  1.796e-03 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.0240895  0.0002663   90.44   <2e-16 ***
## dc.25       1.3279968  0.0032863  404.10   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.0004641 on 232 degrees of freedom
## Multiple R-squared:  0.9986, Adjusted R-squared:  0.9986 
## F-statistic: 1.633e+05 on 1 and 232 DF,  p-value: < 2.2e-16
\end{verbatim}

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

\includegraphics{hw03_solutions_files/figure-latex/unnamed-chunk-7-2.pdf}
Relative to the original data, the transformed \texttt{Suggested\ RetailPrice} and \texttt{DealerCost} have distributions that are much more symmetric. In the residuals vs Fitted plot we still see some outliers\footnote{Looks like 2 Suzuki branded cars} when the fitted value is higher but overall the distribution of residuals looks much better - it has a conditional average residual value around zero and the variability doesn't look too different as we look across the range of fitted values. The residuals now only have a heavy positive tail\footnote{The heavy positive tail on the QQ plot is directly related to those large residuals in the residual vs fitted plot.}. The scale-Location plot confirms out observation from the residual vs Fitted that this new model with the transformations seems to have homoscedastic residuals. The Cook's distance plot also shows less impact from highly influential points, there is a point with leverage at .04 (compared with the average 0.004, but this plot looks better than the original model.

I'm pretty pleased with this new model, most of the linear model assumptions look well met except for a few outliers (which where also identified in our examination of the first model).

\hypertarget{problem-3-log-log}{%
\section{Problem 3: Log-Log}\label{problem-3-log-log}}

Note that we used log-based 10 (this is just a good think to be thinking about when you're transforming and are interested in interpretation).

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{lm}\FloatTok{.3}\NormalTok{ <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(}\KeywordTok{log10}\NormalTok{(SuggestedRetailPrice) }\OperatorTok{~}\StringTok{ }\KeywordTok{log10}\NormalTok{(DealerCost), }\DataTypeTok{data=}\NormalTok{data)}

\NormalTok{log_price <-}\StringTok{ }\KeywordTok{log10}\NormalTok{(data[,}\DecValTok{2}\OperatorTok{:}\DecValTok{3}\NormalTok{])}
\KeywordTok{names}\NormalTok{(log_price) <-}\StringTok{ }\KeywordTok{paste0}\NormalTok{(}\StringTok{"log10("}\NormalTok{,}\KeywordTok{names}\NormalTok{(log_price), }\StringTok{")"}\NormalTok{)}
\KeywordTok{pairs.panels}\NormalTok{(log_price)}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw03_solutions_files/figure-latex/unnamed-chunk-8-1.pdf}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{summary}\NormalTok{(lm}\FloatTok{.3}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = log10(SuggestedRetailPrice) ~ log10(DealerCost), 
##     data = data)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -0.0273260 -0.0037760  0.0002708  0.0046125  0.0211929 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       -0.030166   0.011491  -2.625  0.00924 ** 
## log10(DealerCost)  1.014836   0.002616 387.942  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.008101 on 232 degrees of freedom
## Multiple R-squared:  0.9985, Adjusted R-squared:  0.9985 
## F-statistic: 1.505e+05 on 1 and 232 DF,  p-value: < 2.2e-16
\end{verbatim}

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

\includegraphics{hw03_solutions_files/figure-latex/unnamed-chunk-8-2.pdf}

For our ``Log-Log'' model, there appears to be a slight trend in the average residual as we increase the fitted value, with a slightly positive average residual for the majority of the fitted value values with log(fitted) around 4.2 to 4.6 but negative residual for fitted values on the the extremes. There is also a set of cars with very low residuals with log(fitted) values between 4.2 and 4.6. The normal QQ plot looks decent, with a slightly heavy tail with negative residuals. For the extremely large fitted values we might also be seeming an indication for increased variability of residuals in the Scale-Location plot (although with so few points, this claim is probably premature). There is a group of points with larger leverage than the average, but their Cook's distances aren't that large.

This fit looks much better than the original regression, but there are still some odd groupings in the residual-fitted plot. Overall it looks like a decent model.

\hypertarget{problem-4}{%
\section{Problem 4}\label{problem-4}}

\hypertarget{a-best-model-based-on-assumptions}{%
\subsection{(a) Best Model (based on assumptions)}\label{a-best-model-based-on-assumptions}}

The Box-Cox transformed model seems to best meet the assumptions of the linear regression, specifically in the true linear relationship (examined with \(E[\hat{e}|x] = 0\)). Additionally, the second model seems well meet the assumption of constant noise variance.

\hypertarget{b-complete-the-sentence}{%
\subsection{(b) ``Complete the Sentence''}\label{b-complete-the-sentence}}

\begin{itemize}
\tightlist
\item
  For the model in \#1(b), a \$1 increase in \texttt{DealerCost} results in a \$1.09 increase in the expected value of \texttt{SuggestedRetailPrice}.
\item
  For the model in \#2(b), a 1 unit increase in \((\)\texttt{DealerCost}\()^{-.25}\) results in a 1.33\% increase in the expected value of \((\)\texttt{SuggestedRetailPrice}\()^{-.2}\).
\item
  For the model in \#3, a 1\% increase in \texttt{DealerCost} results in a 1.01\% increase in the expected value of \texttt{SuggestedRetailPrice}.
\end{itemize}

\hypertarget{c-best-model-for-consulting}{%
\subsection{(c) Best Model (for consulting)}\label{c-best-model-for-consulting}}

Giving the difficulting of translating the relationship between \texttt{DealerCost} to \texttt{SuggestedRetailPrice} for the Box-Cox transformed model, I would probably use the Log-Log model for a consulting project. The Log-Log model is still interpretable and more closely meets the OLS assumptions.

\hypertarget{problem-5}{%
\section{Problem 5}\label{problem-5}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{pyth_data <-}\StringTok{ }\KeywordTok{read.table}\NormalTok{(}\StringTok{"../pyth.dat"}\NormalTok{,}\DataTypeTok{header =}\NormalTok{ T)}
\end{Highlighting}
\end{Shaded}

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

\begin{align*}
M1: \texttt{y} = & \beta_0+\beta_1 \texttt{x1}+\varepsilon \\
M2: \texttt{y} = & \beta_0+\beta_1 \texttt{x2}+\varepsilon
\end{align*}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{lm}\FloatTok{.1}\NormalTok{ <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(y}\OperatorTok{~}\NormalTok{x1, }\DataTypeTok{data=}\NormalTok{pyth_data)}
\NormalTok{lm}\FloatTok{.2}\NormalTok{ <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(y}\OperatorTok{~}\NormalTok{x2, }\DataTypeTok{data=}\NormalTok{pyth_data)}
\KeywordTok{summary}\NormalTok{(lm}\FloatTok{.1}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = y ~ x1, data = pyth_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -7.7409 -4.5056  0.7114  4.3739  7.7547 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  10.0633     1.5526   6.481 1.25e-07 ***
## x1            0.6559     0.2499   2.625   0.0124 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.921 on 38 degrees of freedom
##   (20 observations deleted due to missingness)
## Multiple R-squared:  0.1535, Adjusted R-squared:  0.1312 
## F-statistic:  6.89 on 1 and 38 DF,  p-value: 0.01242
\end{verbatim}

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

\begin{verbatim}
## 
## Call:
## lm(formula = y ~ x2, data = pyth_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.1751 -1.2352 -0.1867  1.0899  5.3755 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  3.78532    0.66037   5.732 1.33e-06 ***
## x2           0.83223    0.05017  16.589  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.863 on 38 degrees of freedom
##   (20 observations deleted due to missingness)
## Multiple R-squared:  0.8787, Adjusted R-squared:  0.8755 
## F-statistic: 275.2 on 1 and 38 DF,  p-value: < 2.2e-16
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{plot}\NormalTok{(y}\OperatorTok{~}\NormalTok{x1, }\DataTypeTok{data =}\NormalTok{ pyth_data)}
\KeywordTok{plot}\NormalTok{(y}\OperatorTok{~}\NormalTok{x2, }\DataTypeTok{data =}\NormalTok{ pyth_data)}

\KeywordTok{par}\NormalTok{(}\DataTypeTok{mfrow=}\KeywordTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{,}\DecValTok{2}\NormalTok{))}

\KeywordTok{plot}\NormalTok{(lm}\FloatTok{.1}\NormalTok{)}

\KeywordTok{plot}\NormalTok{(lm}\FloatTok{.2}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics[width=0.5\linewidth]{hw03_solutions_files/figure-latex/unnamed-chunk-11-1} \includegraphics[width=0.5\linewidth]{hw03_solutions_files/figure-latex/unnamed-chunk-11-2} \includegraphics[width=0.5\linewidth]{hw03_solutions_files/figure-latex/unnamed-chunk-11-3} \includegraphics[width=0.5\linewidth]{hw03_solutions_files/figure-latex/unnamed-chunk-11-4}

\(M2\) is better, it reduces more of the variability of \(y-\hat{y}\) by much more than \(M1\).

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

\begin{align*}
M3:  \texttt{y2} = & \beta_0+\beta_1 \texttt{x12}+\varepsilon \\
M4:  \texttt{y2} = & \beta_0+\beta_1 \texttt{x22}+\varepsilon
\end{align*}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{pyth_data}\OperatorTok{$}\NormalTok{y2 <-}\StringTok{ }\NormalTok{pyth_data}\OperatorTok{$}\NormalTok{y}\OperatorTok{^}\DecValTok{2}
\NormalTok{pyth_data}\OperatorTok{$}\NormalTok{x12 <-}\StringTok{ }\NormalTok{pyth_data}\OperatorTok{$}\NormalTok{x1}\OperatorTok{^}\DecValTok{2}
\NormalTok{pyth_data}\OperatorTok{$}\NormalTok{x22 <-}\StringTok{ }\NormalTok{pyth_data}\OperatorTok{$}\NormalTok{x2}\OperatorTok{^}\DecValTok{2}

\CommentTok{## OR}
\CommentTok{# attach(pyth_data)}
\CommentTok{# }
\CommentTok{# y2 <- y^2}
\CommentTok{# x12 <- x1^2}
\CommentTok{# x22 <- x2^2}
\CommentTok{# }
\CommentTok{# detach()}


\NormalTok{lm}\FloatTok{.3}\NormalTok{ <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(y2}\OperatorTok{~}\NormalTok{x12, }\DataTypeTok{data=}\NormalTok{pyth_data)}

\NormalTok{lm}\FloatTok{.4}\NormalTok{ <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(y2}\OperatorTok{~}\NormalTok{x22, }\DataTypeTok{data=}\NormalTok{pyth_data)}

\KeywordTok{summary}\NormalTok{(lm}\FloatTok{.3}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = y2 ~ x12, data = pyth_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -189.324 -125.674    4.988  131.052  214.089 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 161.7831    31.7873   5.090    1e-05 ***
## x12           1.2971     0.6242   2.078   0.0445 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 131.1 on 38 degrees of freedom
##   (20 observations deleted due to missingness)
## Multiple R-squared:  0.102,  Adjusted R-squared:  0.07841 
## F-statistic: 4.318 on 1 and 38 DF,  p-value: 0.04452
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{summary}\NormalTok{(lm}\FloatTok{.4}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = y2 ~ x22, data = pyth_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -41.280 -31.224  -7.463  25.422  59.571 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  35.1583     9.0306   3.893 0.000387 ***
## x22           1.0198     0.0419  24.338  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 33.96 on 38 degrees of freedom
##   (20 observations deleted due to missingness)
## Multiple R-squared:  0.9397, Adjusted R-squared:  0.9381 
## F-statistic: 592.3 on 1 and 38 DF,  p-value: < 2.2e-16
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{par}\NormalTok{(}\DataTypeTok{mfrow=}\KeywordTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{,}\DecValTok{2}\NormalTok{))}

\KeywordTok{plot}\NormalTok{(lm}\FloatTok{.3}\NormalTok{)}

\KeywordTok{plot}\NormalTok{(lm}\FloatTok{.4}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics[width=0.5\linewidth]{hw03_solutions_files/figure-latex/unnamed-chunk-13-1} \includegraphics[width=0.5\linewidth]{hw03_solutions_files/figure-latex/unnamed-chunk-13-2}

\(M4\) is better, it reduces more of the variability of \(y^2-\hat{\texttt{y2}}\) by much more than \(M3\).

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

\begin{align*}
M5: \texttt{y} = & \beta_0+\beta_1 \texttt{x1}+\beta_2\texttt{x2}+\varepsilon \\
M6: \texttt{y2} = & \beta_0+\beta_1 \texttt{x12}+\beta_2\texttt{x22}\varepsilon
\end{align*}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{lm}\FloatTok{.5}\NormalTok{ <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(y }\OperatorTok{~}\StringTok{ }\NormalTok{x1 }\OperatorTok{+}\StringTok{ }\NormalTok{x2, }\DataTypeTok{data=}\NormalTok{pyth_data)}

\NormalTok{lm}\FloatTok{.6}\NormalTok{ <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(y2 }\OperatorTok{~}\StringTok{ }\NormalTok{x12 }\OperatorTok{+}\StringTok{ }\NormalTok{x22, }\DataTypeTok{data=}\NormalTok{pyth_data)}

\KeywordTok{summary}\NormalTok{(lm}\FloatTok{.5}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = y ~ x1 + x2, data = pyth_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.9585 -0.5865 -0.3356  0.3973  2.8548 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.31513    0.38769   3.392  0.00166 ** 
## x1           0.51481    0.04590  11.216 1.84e-13 ***
## x2           0.80692    0.02434  33.148  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9 on 37 degrees of freedom
##   (20 observations deleted due to missingness)
## Multiple R-squared:  0.9724, Adjusted R-squared:  0.9709 
## F-statistic: 652.4 on 2 and 37 DF,  p-value: < 2.2e-16
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{summary}\NormalTok{(lm}\FloatTok{.6}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = y2 ~ x12 + x22, data = pyth_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.26020 -0.05391 -0.00396  0.06367  0.35990 
## 
## Coefficients:
##              Estimate Std. Error  t value Pr(>|t|)    
## (Intercept) 0.0026691  0.0422669    0.063     0.95    
## x12         0.9999672  0.0006419 1557.713   <2e-16 ***
## x22         0.9998685  0.0001663 6011.909   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1344 on 37 degrees of freedom
##   (20 observations deleted due to missingness)
## Multiple R-squared:      1,  Adjusted R-squared:      1 
## F-statistic: 2.013e+07 on 2 and 37 DF,  p-value: < 2.2e-16
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{par}\NormalTok{(}\DataTypeTok{mfrow=}\KeywordTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{,}\DecValTok{2}\NormalTok{))}

\KeywordTok{plot}\NormalTok{(lm}\FloatTok{.5}\NormalTok{)}

\KeywordTok{plot}\NormalTok{(lm}\FloatTok{.6}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics[width=0.5\linewidth]{hw03_solutions_files/figure-latex/unnamed-chunk-15-1} \includegraphics[width=0.5\linewidth]{hw03_solutions_files/figure-latex/unnamed-chunk-15-2}

Both \(M5\) and \(M6\) represent a lot their respectively output, but \(M6\) basically captures all variablity in \(y^2\).

\hypertarget{d}{%
\subsection{(d)}\label{d}}

\begin{align*}
M7: \texttt{y} = & \beta_0+\beta_1 \sqrt{\texttt{x12}+\texttt{x22}}+\varepsilon \\
\end{align*}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{pyth_data}\OperatorTok{$}\NormalTok{x3 <-}\StringTok{ }\KeywordTok{sqrt}\NormalTok{(pyth_data}\OperatorTok{$}\NormalTok{x12 }\OperatorTok{+}\StringTok{ }\NormalTok{pyth_data}\OperatorTok{$}\NormalTok{x22)}

\NormalTok{lm}\FloatTok{.7}\NormalTok{ <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(y }\OperatorTok{~}\StringTok{ }\NormalTok{x3,}\DataTypeTok{data=}\NormalTok{pyth_data)}

\KeywordTok{summary}\NormalTok{(lm}\FloatTok{.7}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = y ~ x3, data = pyth_data)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -0.0083283 -0.0027000 -0.0007907  0.0031643  0.0089809 
## 
## Coefficients:
##              Estimate Std. Error  t value Pr(>|t|)    
## (Intercept) 0.0018422  0.0019159    0.962    0.342    
## x3          0.9998313  0.0001316 7596.431   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.00434 on 38 degrees of freedom
##   (20 observations deleted due to missingness)
## Multiple R-squared:      1,  Adjusted R-squared:      1 
## F-statistic: 5.771e+07 on 1 and 38 DF,  p-value: < 2.2e-16
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{par}\NormalTok{(}\DataTypeTok{mfrow=}\KeywordTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{,}\DecValTok{2}\NormalTok{))}

\KeywordTok{plot}\NormalTok{(lm}\FloatTok{.7}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw03_solutions_files/figure-latex/unnamed-chunk-16-1.pdf}

Based on observations made on \(M6\), \(M7\) seems to capture basically all the variability of \(y\). This relates to the fact that \(M6\) was basically suggesting that \(\texttt{y2} \approx 0 + 1 \texttt{x12} + 1 \texttt{x22}\), which could be re-expressed as

\begin{align*}
y^2 &\approx 0 + 1 \texttt{x1}^2 + 1 \texttt{x2}^2 \\
\Leftrightarrow y &\approx \sqrt{\texttt{x1}^2 + \texttt{x2}^2} \;.\\
\end{align*}

\end{document}
