Xt=[10.38
11.86
10.97
10.8
9.79
10.39
10.42
10.82
11.4
11.32
11.44
11.68
11.17
10.53
10.01
9.91
9.14
9.16
9.55
9.67
8.44
8.24
9.1
9.09
9.35
8.82
9.32
9.01
9
9.8
9.83
9.72
9.89
10.01
9.37
8.69
8.19
8.67
9.55
8.92
8.09
9.37
10.13
10.14
9.51
9.24
8.66
8.86
8.05
7.79
6.75
6.75
7.82
8.64
10.58
9.48
7.38
6.9
6.94
6.24
6.84
6.85
6.9
7.79
8.18
7.51
7.23
8.42
9.61
9.05
9.26
9.22
9.38
9.1
7.95
8.12
9.75
10.85
10.41
9.96
9.61
8.76
8.18
7.21
7.13
9.1
8.25
7.91
6.89
5.96
6.8
7.68
8.38
8.52
9.74
9.31
9.89
9.96];
n=length(Xt);
t=1875:1:1875+n-1;
t=t';
t1=[ones(n,1) t];
plot(t,Xt,'+',t,Xt,'-');
xlabel('Year');
ylabel('Level of Lake (-570 ft) (ft)');
title('Time Series Plot of Lake Huron Data')
%[b,bint,rt,rint,stats] = regress(Xt,t1);
%b
rt=detrend(Xt);
%Detrending same as removing linear predictor
pause;
plot(t,rt,'+',t,rt,'-');
xlabel('Year');
ylabel('Residuals (ft)');
title('Time Series Plot of Lake Huron Data');
pause;
acf(rt,40,'ACF of Lake Huron Residuals');
pause;
%stem(r,1:1:40);
%pause;
Xt1=Xt(1:length(Xt)-1);
Xt2=Xt(2:length(Xt));
plot(Xt1,Xt2,'+')
xlabel('Level of Lake (-570 ft) (ft)');
ylabel('Level of Lake (-570 ft) (ft) -  Lag 1');
title('Time Series Plot of Lake Huron Data')
pause;
% Normalizing-begin
XtNORM=(Xt-mean(Xt))/sqrt(var(Xt));
Xt1z=XtNORM(1:length(XtNORM)-1);
Xt2z=XtNORM(2:length(XtNORM));
% 
plot(Xt1z,Xt2z,'+')
xlabel('Standardized Series Xt')
ylabel('Standardized Series Xt-1')
pause;
%Creating and incidence matrix - 1 X
Xt11=[ones(length(Xt1),1) Xt1];
% OLS - Regression
[b2,bint2,rt2,rint2,stats2] = regress(Xt2,Xt11);
b2
% Autocorrelation Function
acf(rt2,40,'ACF of Lake Huron Residuals-AR(1)');
pause;
%Creating incidence for Second Order AR(2)
Xt13=Xt(1:length(Xt)-2);
Xt23=Xt(2:length(Xt)-1);
Xt3=Xt(3:length(Xt));
Xt123=[ones(length(Xt3),1)  Xt23 Xt13];
%OLS Regression for the AR(2)
[b3,bint3,rt3,rint3,stats3] = regress(Xt3,Xt123);
b3
stat3
acf(rt3,40,'ACF of Lake Huron Residuals-AR(2)');
pause;
%IDF to get PACF also ACF again.
idf(rt3,40);

