\documentclass[12pt]{article}
\usepackage{amssymb,amsmath,amsfonts}
\usepackage[dvips]{graphicx}
\addtolength{\hoffset}{-1.05cm}
\addtolength{\textwidth}{2.5cm}
\addtolength{\textheight}{2.5cm}

\author{Jeremy Morris}
\title{Monthly Rainfall / Water Discharge of Cervik A \\ \textit{Project II}}

\begin{document}
\maketitle

For this project, we will be using a simple linear model for our data.  We will define the linear model as
\begin{equation}\label{linmod}
Y_{i} = \beta_{i}X_{i} + \alpha_{i}
\end{equation}
For each of $i=5$ months in the data set, the values for $Y_{i}$ will be the observed water discharges for the $i^{th}$ month.  Similarly, the $X_{i}$ will be the observed rainfalls.  What we hope to do is to find estimators for the slope and intercept of each line, $\hat{\beta}$ and $\hat{\alpha}$.  This will be done using the usual linear least squares formula discussed in class.  All calculations are done using the various functions and methods available in the \texttt{R} programming language.

\section{Plots for each month}\label{fit}
Figure (\ref{omodels}) shows a model fit to the data for each month given in the original data.
\begin{figure}[ht]
\includegraphics[scale=1]{originalmodels.eps}
\caption{Fitted Lines}\label{omodels}
\end{figure}
There are a few exceptional outliers.  For example, the water discharge for October 1971 affects the model terribly.  This outlier will be removed for the rest of the analysis.  A comparison plot can be seen in Figure (\ref{octcorr}).  Here, we see a plot of the original data along with a plot of the data with the outlier removed.  The line appears to fit better without the outlier.
\begin{figure}[ht]
\begin{center}
\includegraphics[scale=0.5]{octcorr.eps}
\end{center}
\caption{October data with and without outlier}\label{octcorr}
\end{figure}
The model for June does not appear to match the data as well as for the other months.  The data for 1971 will be removed, this point seems to be the worst outlier.  The effect of removing this data point can be seen in Figure (\ref{junecorr}).
\begin{figure}[ht]
\begin{center}
\includegraphics[scale=0.5]{junecorr.eps}
\end{center}
\caption{June data with and without outlier}\label{junecorr}
\end{figure}

\section{How well do the models fit?}\label{good}
Often $R^{2}$ is used as a measure of how well the model fits the data.  The popular interpretation is that $R^{2}$ is the fraction of variance expliained by the model.  The $R^{2}$ coefficient is calculated using the formula
\begin{equation}\label{rsquare}
R^{2} = \frac{\sum (\hat{Y}_{i} - \bar{Y})^{2}}{\sum (Y_{i} - \bar{Y})^{2}}
\end{equation}
The interpritation of $R^{2}$ follows from this formula.  The equation is the ratio of the fitted variance with the observed variance in the dependant variable.

We can also use an $F$ test to determine whether the model is significant or not.  In this case we want to test
\begin{equation}
H_{0} : \beta_{1} = 0 \hspace*{1cm} H_{a} : \beta_{1} \neq 0\label{sighyp}
\end{equation}
We will calculate the test statistic
\begin{equation}
F = \frac{(n-2)R^{2}}{1 - R^{2}} \label{fstat}
\end{equation}
And reject $H_{0}$ when $F > F^{\alpha}_{1,n-1}$.  From our text, we note that the rejection of $H_{0}$ means that there is a significant regression and the $x$ values cannot be ignored.  It does not mean that the model is very adequate for prediction purposes.  A working rule suggests that the $F$ statistic be at least four or five times $F^{\alpha}_{1,n-1}$ to provide an adequate model for predicion. 

%\clearpage

Table (\ref{rsquared}) displays the $R^{2}$, $F$ and $F^{\alpha}_{df_{1},df_{2}}$ values along with the values for the degrees of freedom ($df_{1},df_{2}$) with $\alpha=0.05$.
\begin{table}[!h]
\begin{center}
\begin{tabular}{l|lllll}
Month & $R^{2}$ value & $F$ & $F^{\alpha}_{df_{1},df_{2}}$ $df_{1}$ & $df_{2}$\\ \hline
\input{rsquared.tex}
\end{tabular}
\end{center}
\caption{Hypothesis tests for each model by month}\label{rsquared}
\end{table}
This table seems to indicate that the monthly models fit the data adequately.  We see that the model for June fits the worst, which is to be expected since a look at the plot also indicates a bad fit.  We also see that the model for the months of July and August seem to fit the best.  This could be because those months are the most stable being in the middle of the summer.  The other data occur during seasonal changes, which may explain the presence of more variability.

\section{Normally Distributed Errors}\label{normerr}
Table (\ref{nerr}) shows the $p$-values for the Shapiro-Wilks and Cr\'amer-von Mises tests.  Both tests work well in this case, one is parameter invariant and the other is based on the ECDF, therefore we do not have to normalize the data.
\begin{table}[!h]
\begin{center}
\begin{tabular}{l|ll}
Month & Shapiro-Wilks & Cramer-von Mises \\ \hline
\input{npvals.tex}
\end{tabular}
\end{center}
\caption{Distribution of Residuals}\label{nerr}
\end{table}
We see that if we use the null hypothesis that the residuals are normally distributed, we cannot reject for any of the months at the $\alpha=0.05$ level.  Therefore we say that we are $95\%$ sure that the residuals are normally distributed for all months.

\section{Fitting a model to all the data}
\subsection{A different model for each month}
If there is a different model for each month, we should see a significant difference in the values for the $\alpha_{i}$ and $\beta_{i}$.  The values for $\alpha$ and $\beta$ are displayed in Table (\ref{cftbl}), and seem to indicate that the slope for each month may be the same, but the intercepts are different for each month.  This would imply that the water discharge is the same relative to the rainfall recieved, but the amount of rainfall is different from month to month.  Meaning that we cannot create one model to fit all the data, and have the model remain significantly accurate.
\begin{table}[!h]
\begin{center}
\begin{tabular}{l|ll}
Month & $\alpha$ & $\beta$ \\ \hline
\input{cf.tex}
\end{tabular}
\end{center}
\caption{Coefficients of each model}\label{cftbl}
\end{table}

To make this more precise, we can combine all our data into one model and test the hypothesis that the slopes for each month are the same. We can construct the equation
\begin{equation}\label{Hslope}
Y = X\gamma
\end{equation}
where (\ref{Hslope}) is given by
\begin{equation}\label{parlines}
\left( \begin{array}{c}
	y_{11} \\
	y_{12} \\
	\vdots \\
	y_{1n_{1}} \\
	y_{21} \\
	\vdots \\
	y_{2n_{2}} \\ \hline
	\cdots \\ \hline
	y_{K1} \\
	\vdots \\
	y_{Kn_{K}}
	\end{array} \right)
=
\left( \begin{array}{llll}
	x_{11} & 0 & \cdots & 0 \\
	\vdots & 0 & \cdots & 0 \\
	x_{1n_{1}} & 0 & \cdots & 0 \\
	0 & x_{21} & \cdots & 0 \\
	0 & \vdots & \cdots & 0 \\
	0 & x_{2n_{2}} & \cdots & 0 \\ \hline
	\cdots & \cdots & \cdots & \cdots \\ \hline
	0 & 0 & \cdots & x_{K1} \\
	0 & 0 & \cdots & \vdots \\
	0 & 0 & \cdots & x_{Kn_{K}} \\
	\end{array} \right) \\
\left( \begin{array}{c}
	\beta_{1} \\
	\beta_{2} \\
	\vdots \\
	\beta_{K}
	\end{array} \right)
\end{equation}
Where there are $n_{i}$ observations for each month and $K$ months.  We need to calculate the $F$ statistic
\begin{equation}\label{Ftest}
F = \frac{(RSS_{H_{0}} - RSS_{H_{a}}) / (K-1)}{RSS_{H_{a}}/(N - 2K)}
\end{equation}
where $N = \sum_{i=1}^{K} n_{i}$.  When we calculate (\ref{Hslope}) under $H_{0}$, we set $\beta_{1} = \beta_{2} = \cdots = \beta_{K} = 1$ and use the observed $\beta_{i}$ under $H_{a}$.  Then we reject when $F > F^{\alpha}_{2K,N-2K}$ for $\alpha=0.05$.  By the results in Table (\ref{lines}) we cannot reject $H_{0}$.  This implies that the slopes of the lines are the same.
\begin{table}[!h]
\begin{center}
\begin{tabular}{ll}
$F$ & $F^{\alpha}_{2K,N-2K}$ \\ \hline
0.043&1.923
\end{tabular}
\end{center}
\caption{$F$ test for parallel lines}\label{lines}
\end{table}

As mentioned earlier, we do not necessarily need to test if the intercepts are the same a simple examination of the $\alpha$ values tells us that they will not be.  This leads us to the conclusion that one model does not exist for all of the data.

\subsection{Modifications to fit a model}
To fix the problem of having different models for each month, we could modify the model such that it takes into account the differing amount of rain recieved each month.  This means that we use the fitted $\beta$ value which is the same for each month and scale the model based on a chosen $\alpha$ value.

\section{Can the Model Be Improved?}
To improve the model for predicting water discharges, all we have to do is think about the situation a little bit.  It can easily be argued that the water discharge during the later part of a month will be based on the amount of rain during the month.  But, what about the discharge during the beginning of the month?  This will mostly be determined by the rainfall and water discharge during the preceding month.  In this case our model is altered from (\ref{linmod}) to
\begin{equation}
Y_{i} = \varphi_{1}X_{i} + \varphi_{2}X_{i-1} + \varphi_{3} Y_{i-} + \varphi_{4}
\end{equation}

If this modification improves our models, then we should see an increase in both the $R^{2}$ coefficient and the ratio of $F$ values for each model.
\begin{table}[!h]
\begin{center}
\begin{tabular}{l|llllll}
Month & $R^{2}_{\{I\}}$ & $R^{2}_{\{O\}}$ & $\frac{F_{\{I\}}}{F^{\alpha}}$ & $\frac{F_{\{O\}}}{F^{\alpha}}$ \\ \hline
\input{cmpr.tex}
\end{tabular}
\end{center}
\caption{Comparisons for imporoving the model}\label{cmpr}
\end{table}
Table (\ref{cmpr}) displays this information.  It is interesting that in every case we see an increase in the $R^{2}$ coefficient, but not in the $F$ statistic.  However, the suggested ratio of higher than four or five is maintained, so that we can still say that these models adequately fit the data.

\end{document}
