Question 1

set.seed(8675309)
x = rnorm(500)
ar2 = arima.sim(list( ar=c(.85,.13), ma = c(-0.8, 0.7)),499)

plot(acf(x,lag.max = 50))

plot(ar2, axes=TRUE, xlab="Time")

ACF<-ARMAacf(ar=c(.85,.13),ma = c(-0.8, 0.7), lag.max=50)
plot(ACF, type="h", xlab="lag")

Question 2

x<- read.csv("/Users/YaseenAbdulridha/Downloads/1_10.csv")
# Create time series object
xt<- ts(x)
# Plot
plot.ts(xt)

#  Fit random walk models with and without drift. Prefer?
NO_DRIFT<- rwf(x, h=499, drift=FALSE, level=c(80,95), fan=FALSE, lambda=NULL)
DRIFT<- rwf(x, h=499, drift=TRUE, level=c(80,95), fan=FALSE, lambda=NULL)
plot(NO_DRIFT$fitted)

plot(DRIFT$fitted)

# PLOT  / ACF
plot(DRIFT$residuals)

DDrift<- DRIFT$residuals[-1,1]
acf(DDrift, lag.max = 50)

#  Create Quarterly DUmmies fit the model

Model<- data.frame(xt)
seq
## function (...) 
## UseMethod("seq")
## <bytecode: 0x7fe421107628>
## <environment: namespace:base>
Quarter<- c()
while(length(Quarter)<498)
  (
for (i in 1:4)
{
Quarter <- c(Quarter,i)
}
)
Model<- cbind(Model, Quarter[-1])
q1<- ifelse(Model[,2] == 1,1,0)
q2<- ifelse(Model[,2] == 2,1,0)
q3<- ifelse(Model[,2] == 3,1,0)
q4<- ifelse(Model[,2] == 4,1,0)


Regression<- lm(xt~ lag(xt,1)+q1+q2+q3)
summary(Regression)
## Warning in summary.lm(Regression): essentially perfect fit: summary may be
## unreliable
## 
## Call:
## lm(formula = xt ~ lag(xt, 1) + q1 + q2 + q3)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -3.444e-14 -1.140e-16  5.300e-17  2.620e-16  2.122e-15 
## 
## Coefficients:
##               Estimate Std. Error    t value Pr(>|t|)    
## (Intercept) -3.817e-15  1.575e-16 -2.424e+01   <2e-16 ***
## lag(xt, 1)   1.000e+00  9.624e-18  1.039e+17   <2e-16 ***
## q1          -1.742e-17  2.033e-16 -8.600e-02    0.932    
## q2          -6.503e-17  2.031e-16 -3.200e-01    0.749    
## q3          -2.834e-16  2.028e-16 -1.397e+00    0.163    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.604e-15 on 494 degrees of freedom
## Multiple R-squared:      1,  Adjusted R-squared:      1 
## F-statistic: 2.707e+33 on 4 and 494 DF,  p-value: < 2.2e-16
# test that q1 is zero/ pvalue/ decision

# P value is very large 
# Use  individual T TEst

#.  test Joint hypothesis that coefficeints 
#Use F test
#use f test to test for joint significance of extra variables 

#  Plot residuals and ACF for residuals
plot(Regression$residuals)

acf(Regression$residuals, lag.max = 50)

Question 3

Y<- read.csv("/Users/YaseenAbdulridha/Downloads/1_11.csv")

#Create Time series object

Yt<- ts(Y)

# Plot Data and Acf
plot.ts(Yt)

acf(Yt, lag.max = 50)

# Lag Plot  
lag1.plot(Yt, max.lag = 6)

# Fit Arma models
fit<- arima(Yt, order=c(1,0,0))
fit_1<- arima(Yt, order=c(2,0,0))
fit_2<- arima(Yt, order=c(3,0,0))
fit_3<- arima(Yt, order=c(4,0,0))
fit_4<- arima(Yt, order=c(5,0,0))
fit_5<- arima(Yt, order=c(6,0,0))


# Which do you prefer? Why?
ACF2<- ARMAacf(ar = fit_3$coef, lag.max = 50)
plot(ACF2, type = "h")

Question 4

# Read data and construct time series
Z<- read.csv("/Users/YaseenAbdulridha/Downloads/1_13.csv")
Zt<- ts(Z)
# Plot
plot.ts(Zt)

#  Kernel smoother
plot(Zt)
lines( ksmooth( time(Zt), Zt, "normal", bandwidth=12), lwd=2, col=3 )
lines( ksmooth( time(Zt), Zt, "normal", bandwidth=20), lwd=2, col=3 )
lines( ksmooth( time(Zt), Zt, "normal", bandwidth=40), lwd=2, col=4 )
lines( ksmooth( time(Zt), Zt, "normal", bandwidth=30), lwd=2, col=5 )

# Lowess Smoother
plot(Zt)
lines(lowess(Zt, f=0.10), lwd=2, col=4)
lines(lowess(Zt, f=0.20), lwd=2, col=2)
lines(lowess(Zt, f=0.15), lwd=2, col=3)

# Periodigram 
x<- periodogram(Zt)

#top Frequency can be found when maximizing one column and looking at the other value for spec/freq
Frequency<- data.frame(x$spec, x$freq)
Frequency
##           x.spec      x.freq
## 1     0.20255824 0.003333333
## 2     0.25452626 0.006666667
## 3     0.26854215 0.010000000
## 4     5.37018603 0.013333333
## 5     0.99040423 0.016666667
## 6   164.33505092 0.020000000
## 7     0.62759237 0.023333333
## 8     0.03598642 0.026666667
## 9     2.44591867 0.030000000
## 10    0.26165310 0.033333333
## 11    0.65127321 0.036666667
## 12    0.07980522 0.040000000
## 13    6.56363554 0.043333333
## 14    0.70915077 0.046666667
## 15    6.01736680 0.050000000
## 16    0.25298745 0.053333333
## 17    0.46450510 0.056666667
## 18    1.26491137 0.060000000
## 19    3.50172438 0.063333333
## 20  117.74043331 0.066666667
## 21    1.61139230 0.070000000
## 22    2.02382388 0.073333333
## 23    0.42356794 0.076666667
## 24    3.34268052 0.080000000
## 25    1.72408596 0.083333333
## 26    0.06779503 0.086666667
## 27    3.63017852 0.090000000
## 28   11.14589629 0.093333333
## 29    0.60438176 0.096666667
## 30    3.71913910 0.100000000
## 31    0.21259289 0.103333333
## 32    0.76574870 0.106666667
## 33    0.49489378 0.110000000
## 34    1.06280948 0.113333333
## 35    1.94527502 0.116666667
## 36    0.47986371 0.120000000
## 37    1.25834316 0.123333333
## 38    0.16977255 0.126666667
## 39    1.11108710 0.130000000
## 40    3.28380718 0.133333333
## 41    0.50672785 0.136666667
## 42    1.00780606 0.140000000
## 43    2.43465741 0.143333333
## 44    0.81215826 0.146666667
## 45    1.37706132 0.150000000
## 46    2.59975056 0.153333333
## 47    1.40968066 0.156666667
## 48    3.07790763 0.160000000
## 49    1.75884484 0.163333333
## 50    1.03283235 0.166666667
## 51    0.63514440 0.170000000
## 52    1.49140891 0.173333333
## 53    0.63101256 0.176666667
## 54    4.06429451 0.180000000
## 55    0.03391974 0.183333333
## 56    1.55927815 0.186666667
## 57    0.04504360 0.190000000
## 58    5.98253920 0.193333333
## 59    3.15387145 0.196666667
## 60    0.17881130 0.200000000
## 61    3.51127032 0.203333333
## 62    1.93446046 0.206666667
## 63    3.56341457 0.210000000
## 64    0.89570982 0.213333333
## 65    0.97135822 0.216666667
## 66    1.89606101 0.220000000
## 67    1.53777254 0.223333333
## 68    0.33044328 0.226666667
## 69    4.94103395 0.230000000
## 70    2.53213370 0.233333333
## 71    0.09987796 0.236666667
## 72    4.12093376 0.240000000
## 73    0.77499529 0.243333333
## 74    0.20244869 0.246666667
## 75    2.25339012 0.250000000
## 76    1.36813265 0.253333333
## 77    1.26078132 0.256666667
## 78    1.04100097 0.260000000
## 79    0.07764672 0.263333333
## 80    0.41189295 0.266666667
## 81    0.22492400 0.270000000
## 82    0.88580828 0.273333333
## 83    0.07159542 0.276666667
## 84    5.82667658 0.280000000
## 85    0.85726516 0.283333333
## 86    0.27148612 0.286666667
## 87    1.49870107 0.290000000
## 88    0.13149972 0.293333333
## 89    7.39686358 0.296666667
## 90    2.33163056 0.300000000
## 91    1.12054066 0.303333333
## 92    1.28751632 0.306666667
## 93    0.50346745 0.310000000
## 94    1.59956013 0.313333333
## 95    2.21993547 0.316666667
## 96    2.44850495 0.320000000
## 97    6.06709875 0.323333333
## 98    0.19215809 0.326666667
## 99    1.35701898 0.330000000
## 100   1.92156124 0.333333333
## 101   1.86588108 0.336666667
## 102   0.72632262 0.340000000
## 103   3.76824330 0.343333333
## 104   2.75099041 0.346666667
## 105   2.29659133 0.350000000
## 106   3.15283634 0.353333333
## 107   6.65848092 0.356666667
## 108   1.99952470 0.360000000
## 109   0.21221501 0.363333333
## 110   2.76663559 0.366666667
## 111   1.30448941 0.370000000
## 112   2.95594247 0.373333333
## 113   2.50903996 0.376666667
## 114   4.15324331 0.380000000
## 115   1.25836633 0.383333333
## 116   6.40866057 0.386666667
## 117   0.15061650 0.390000000
## 118   2.11115528 0.393333333
## 119   0.65894932 0.396666667
## 120   1.29576178 0.400000000
## 121   1.97039493 0.403333333
## 122   0.37163845 0.406666667
## 123   2.62904471 0.410000000
## 124   2.18195086 0.413333333
## 125   1.43819830 0.416666667
## 126   0.67075843 0.420000000
## 127   0.51379734 0.423333333
## 128   3.14035020 0.426666667
## 129   3.02232327 0.430000000
## 130   4.74266612 0.433333333
## 131   0.95566846 0.436666667
## 132   5.28159926 0.440000000
## 133   0.44949748 0.443333333
## 134   3.02726337 0.446666667
## 135   3.21092214 0.450000000
## 136   0.79687780 0.453333333
## 137   0.32895795 0.456666667
## 138   4.62653869 0.460000000
## 139   1.64201100 0.463333333
## 140   2.71396894 0.466666667
## 141   1.94250310 0.470000000
## 142   5.86290057 0.473333333
## 143   6.06907042 0.476666667
## 144   1.13212401 0.480000000
## 145   3.62671066 0.483333333
## 146   1.40205783 0.486666667
## 147   3.83018383 0.490000000
## 148   2.11962808 0.493333333
## 149   3.51395091 0.496666667
## 150   0.08091168 0.500000000