%No. Trapped	year	log(x) of No. Trapped	MeanCorrected
D=[269	1821.000	2.430	-.474
321	1822.000	2.507	-.397
585	1823.000	2.767	-.136
871	1824.000	2.940	.037
1475	1825.000	3.169	.265
2821	1826.000	3.450	.547
3928	1827.000	3.594	.691
5943	1828.000	3.774	.871
4950	1829.000	3.695	.791
2577	1830.000	3.411	.508
523	1831.000	2.719	-.185
98	1832.000	1.991	-.912
184	1833.000	2.265	-.639
279	1834.000	2.446	-.458
409	1835.000	2.612	-.292
2285	1836.000	3.359	.455
2685	1837.000	3.429	.526
3409	1838.000	3.533	.629
1824	1839.000	3.261	.358
409	1840.000	2.612	-.292
151	1841.000	2.179	-.724
45	1842.000	1.653	-1.250
68	1843.000	1.833	-1.071
213	1844.000	2.328	-.575
546	1845.000	2.737	-.166
1033	1846.000	3.014	.111
2129	1847.000	3.328	.425
2536	1848.000	3.404	.501
957	1849.000	2.981	.078
361	1850.000	2.558	-.346
377	1851.000	2.576	-.327
225	1852.000	2.352	-.551
360	1853.000	2.556	-.347
731	1854.000	2.864	-.039
1538	1855.000	3.187	.284
2725	1856.000	3.435	.532
2871	1857.000	3.458	.555
2119	1858.000	3.326	.423
684	1859.000	2.835	-.068
299	1860.000	2.476	-.428
236	1861.000	2.373	-.530
245	1862.000	2.389	-.514
552	1863.000	2.742	-.161
1623	1864.000	3.210	.307
3311	1865.000	3.520	.617
6721	1866.000	3.827	.924
4245	1867.000	3.628	.724
687	1868.000	2.837	-.066
255	1869.000	2.407	-.497
473	1870.000	2.675	-.229
358	1871.000	2.554	-.350
784	1872.000	2.894	-.009
1594	1873.000	3.202	.299
1676	1874.000	3.224	.321
2251	1875.000	3.352	.449
1426	1876.000	3.154	.251
756	1877.000	2.879	-.025
299	1878.000	2.476	-.428
201	1879.000	2.303	-.600
229	1880.000	2.360	-.544
469	1881.000	2.671	-.232
736	1882.000	2.867	-.037
2042	1883.000	3.310	.407
2811	1884.000	3.449	.545
4421	1885.000	3.646	.742
2511	1886.000	3.400	.496
389	1887.000	2.590	-.313
73	1888.000	1.863	-1.040
39	1889.000	1.591	-1.312
49	1890.000	1.690	-1.213
59	1891.000	1.771	-1.133
188	1892.000	2.274	-.629
377	1893.000	2.576	-.327
1292	1894.000	3.111	.208
4031	1895.000	3.605	.702
3495	1896.000	3.543	.640
587	1897.000	2.769	-.135
105	1898.000	2.021	-.882
153	1899.000	2.185	-.719
387	1900.000	2.588	-.316
758	1901.000	2.880	-.024
1307	1902.000	3.116	.213
3465	1903.000	3.540	.636
6991	1904.000	3.845	.941
6313	1905.000	3.800	.897
3794	1906.000	3.579	.676
1836	1907.000	3.264	.360
345	1908.000	2.538	-.366
382	1909.000	2.582	-.321
808	1910.000	2.907	.004
1388	1911.000	3.142	.239
2713	1912.000	3.433	.530
3800	1913.000	3.580	.676
3091	1914.000	3.490	.587
2985	1915.000	3.475	.572
3790	1916.000	3.579	.675
674	1917.000	2.829	-.075
81	1918.000	1.908	-.995
80	1919.000	1.903	-1.000
108	1920.000	2.033	-.870
229	1921.000	2.360	-.544
399	1922.000	2.601	-.302
1132	1923.000	3.054	.150
2432	1924.000	3.386	.483
3574	1925.000	3.553	.650
2935	1926.000	3.468	.564
1537	1927.000	3.187	.283
529	1928.000	2.723	-.180
485	1929.000	2.686	-.218
662	1930.000	2.821	-.083
1000	1931.000	3.000	.097
1590	1932.000	3.201	.298
2657	1933.000	3.424	.521
3396	1934.000	3.531	.628
];
Xt=D(:,1:1);
t=D(:,2:2);
logXt=D(:,3:3);
logXtm=D(:,4:4);
plot(t,Xt,'+',t,Xt,'-');
xlabel('Year');
ylabel('Number of Lynx Trapped');
suptitle('Time Series Plot of Canadian Lynx Data')
title('Original Time Series')
pause;
xt=detrend(logXt);
%xt=detrend(Xt);
plot(t,xt,'+',t,xt,'-');
xlabel('Year');
ylabel('Residuals (ft)');
suptitle('Time Series Plot of Canadian Lynx Data')
title('Residuals after Detrending');
pause;
xt1=xt(1:length(xt)-1);
xt2=xt(2:length(xt));
plot(xt1,xt2,'+')
ylabel('Number of Lynx Trapped');
ylabel('Number of Lynx Trapped -  Lag 1');
suptitle('Time Series Plot of Canadian Lynx 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 Canadian Lynx Data Residuals-AR(1)');
suptitle('Canadian Lynx Data')
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
stats3
acf(rt3,40,'ACF of Canadian Lynx Data Residuals-AR(2)');
pause;
%IDF to get PACF also ACF again.
idf(rt3,40);
pause;
yt=zeros(length(Xt),1);
%yt=yt';
i=1;
while (i <= length(Xt))
if (i > 10)
yt(i) = Xt(i)-Xt(i-10);
else
yt(i)=Xt(i);
end
i=i+1;
end
idf(yt,40);

