1.
#annual data
Annual <- scan("http://www.calpoly.edu/~gbdurham/econ522/precip/annual_precip.csv")
Annual <- ts(Annual)
#monthly data
monthly <- scan("http://www.calpoly.edu/~gbdurham/econ522/precip/monthly_precip.csv")
monthly <- ts(monthly, frequency = 12, start = c(0,6))
2.
plot.ts(Annual)

3.
plot.ts(Annual)
s = ksmooth(1:145,Annual,bandwidth = 5)
lines(smooth(s$y),col = 3)

4.
res = Annual - s$y
par(mfrow=c(2,1))
plot(res)
hist(res)

par(mfrow=c(1,1))
acf(res)

#The residuals seem normal
4.
lamda <- 0.5
z = ((Annual^lamda)-1)/lamda
zSmooth<- ksmooth(1:145,z,bandwidth = 5)
Z_Res <- z - zSmooth$y
plot(Z_Res)

hist(Z_Res)

#Residuals at Lamda = 0.5 seem to be normal
5.
Monthly <- ts(monthly, frequency = 12, start = c(0,6))
monthplot(Monthly)

MonthSmooth<- ksmooth(1:145,Monthly,bandwidth = 5)
Monthly_Res <- Monthly - MonthSmooth$y
plot(Monthly_Res)

hist(Monthly_Res)

#does not seem to appear normal before transformation
lamda <- 0.5
z = ((Monthly^lamda)-1)/lamda
zSmooth<- ksmooth(1:145,Monthly,bandwidth = 5)
Z_Res <- z - zSmooth$y
plot(Z_Res)

hist(Z_Res)

#Seems to be normal after Box Cox Transformation
6.
Monthlyfactor <- factor(cycle(z))
Dummy1<- model.matrix(z~ Monthlyfactor)
Dummy1<- Dummy1[,-1]
auto.arima(z, xreg = Dummy1)
## Series: z
## ARIMA(1,0,0)(2,0,2)[12] with non-zero mean
##
## Coefficients:
## Warning in sqrt(diag(x$var.coef)): NaNs produced
## ar1 sar1 sar2 sma1 sma2 intercept Monthlyfactor2
## 0.0879 -0.2526 0.4896 0.2630 -0.4521 1.7626 -0.3676
## s.e. 0.0141 NaN NaN 0.0087 0.0154 0.1144 0.1546
## Monthlyfactor3 Monthlyfactor4 Monthlyfactor5 Monthlyfactor6
## -1.6098 -2.8217 -3.4596 -3.6535
## s.e. 0.1612 0.1618 0.1618 0.1618
## Monthlyfactor7 Monthlyfactor8 Monthlyfactor9 Monthlyfactor10
## -3.6335 -3.2443 -2.3462 -1.3704
## s.e. 0.1618 0.1618 0.1618 0.1618
## Monthlyfactor11 Monthlyfactor12
## -0.1761 0.1197
## s.e. 0.1612 0.1546
##
## sigma^2 estimated as 1.671: log likelihood=-2915.77
## AIC=5867.54 AICc=5867.93 BIC=5965.84
ARTrend<- arima(z, order = c(1,0,0), include.mean = FALSE, xreg = Dummy1)
ARTrend2<- arima(z, order = c(2,0,1), include.mean = FALSE, xreg = Dummy1)
## Warning in arima(z, order = c(2, 0, 1), include.mean = FALSE, xreg =
## Dummy1): possible convergence problem: optim gave code = 1
#AIC on ARMA (2,1) had the Lowest AIC of 5881.75
tsdiag(ARTrend)

tsdiag(ARTrend2)
