\documentclass[12pt]{amsart}
\usepackage{pdflscape}
\pagestyle{empty}
\thispagestyle{empty}
\theoremstyle{plain}    
\newtheorem*{definition}{Definition}
\newtheorem*{example}{Example}
\newtheorem*{exercise}{Exercise}
\newtheorem*{conjecture}{Conjecture}
\newtheorem*{theorem}{Theorem}
\newtheorem*{lemma}{Lemma}
\newtheorem*{remark}{Remark}
\begin{document} 
\title{Computational Assignment 3}
\section{computational assignment 3}
\subsection{Question 1 (Octave)}
\par\vspace{1 cm}
Download and install the GNU Octave linear algebra software using the instructions posted in the \textit{Notes and Handouts} section of the MTH396 web page.
\par\vspace{1 cm}
Download the data files \texttt{computational\_assignment3\_octave\_x.csv} and \texttt{computational\_assignment3\_octave\_y.csv} from the links in the assignments section of the MTH396 web page.
\par\vspace{1 cm}
Start Octave and load the $Y$ vector using the command:
\par\vspace{1 cm}
\texttt{Y=dlmread("computational\_assignment3\_octave\_y.csv",',')}
\par\vspace{1 cm}
This represents miles per gallon data from the 2009 EPA data base.  
\par\vspace{1 cm}
Now load the $X$ matrix with the command:
\par\vspace{1 cm}
\texttt{X=dlmread("computational\_assignment3\_octave\_x.csv",',')}
\par\vspace{1 cm}
The first column of the $X$ matrix is all ones and the second column represents the engine horsepower.
\par\vspace{1 cm}
The purpose of this exercise is to use Octave to carry out the matrix operations used to perform a simple linear regression.
\par\vspace{1 cm}
The estimates of the parameters are given by $(X'X)^{-1}X'Y$, which in Octave is coded as:
\par\vspace{1 cm}
\texttt{beta=inverse(X'*X)*X'*Y}
\par\vspace{1 cm}
The vector of errors or residuals is obtained as $(Y-X\beta)'(Y-X\beta)$, 
\par\vspace{1 cm}
\texttt{e=Y-X*beta}
\par\vspace{1 cm}
The dataset has 781 elements, so $Y$, $X\beta$, and $e$ are in $\mathbb{R}^{781}$.  $X$ has two linearly independent columns, so the dimension of the column space  has dimension $2$.  The nullspace has dimension $781-2=779$.
\par\vspace{1 cm}
An estimate of $\sigma^2_e$, the variance of each error term, is given by $e'e$ divided by the dimension of the column space.  Calculate this estimate.
\subsection{Question 2 (R)}
We will perform a simple linear regression, that is, a model of the form $Y=X\beta+e$ where (in the row view),
\[
Y_i=\beta_1+\beta_2\cdot X_i+e_i
\]
\par\vspace{1 cm}
R is set up to automatically generate the appropriate $X$ matrix from a specification of the model, so all we have to do is get the data into R and run the linear model procedure.
\par\vspace{1 cm}
Because R can read data from a URL, the easiest way to get the data into R is just to read the \texttt{.csv} file from the link on a web page.  The syntax for this command is: 
\par\vspace{1 cm}\noindent
\texttt{epa<-read.table("URL",sep=",",fill=TRUE,header=TRUE)}
\par\vspace{1 cm}
To read the EPA data into R, copy and paste this into the R command line, then replace \texttt{URL} with:
\par\vspace{1 cm}\noindent
\texttt{http://www.sandgquinn.org/stonehill/MA225/notes/09tstcar.csv}
\par\vspace{1 cm}
This will read the entire 2009 EPA test data matrix into a data frame in R.  To display the strucure of the result, 
type:
\par\vspace{1 cm}
\texttt{str(epa)}
The columns we are interested in are \texttt{etw}, which contains the vehicle weight, and \texttt{mpg}, which contains the mileage rating.  However, first we need to restrict the data to only cars and only highway mileage.  To do this, we will create a new dataframe that is a subset of our original one using the command:
\par\vspace{1 cm}
\texttt{epaCH<-subset(epa, C.H=="H" \& car.truck=="C")}
\par\vspace{1 cm}
Now we make the column names of the data frame \texttt{epaCH} visible using
\par\vspace{1 cm}
\texttt{attach(epaCH)}
\par\vspace{1 cm}
You can verify that the subset and attach worked by typing
\par\vspace{1 cm}
\texttt{mpg}
\par\vspace{1 cm}
There should be 781 values in the \texttt{mpg} vector.
\par\vspace{1 cm}
Now we have R perform the regression. Since R is designed to do this sort of thing, all we have to do is type:
\par\vspace{1 cm}\noindent
\texttt{summary(lm(mpg $\sim$ etw))} 
\par\vspace{1 cm}
In the "Coefficients:" section of the summary, the estimated values of beta are in the "Estimate" column.  How do these compare with the values we obtained from Octave using $(X'X)^{-1}X'Y$?
\par\vspace{1 cm}
Notice the "Residual standard error:" entry.  How does the value compare to the square root of the value of $e'e/779$ from Octave?
\par\vspace{1 cm}
How does the degrees of freedom value relate to the geometric interpretation of the linear model?   
\end{document}



