/* We are going to generate 4 different time series to demonstrate a comprehensive way of testing for trend and seasonality in the presence of autocorrelated errors. The test equation is a deterministic quadratic trend and fixed seasonal effects model with AR(4) error model, the order of which is determined by backward selection. */ options pagesize=60 linesize=74 nodate; ods html; ods graphics on; /* The seasonal dummy variables are created here. */ data dummy; do t = 1 to 120; t2 = t*t; d1 = (mod(t,12)=1); d2 = (mod(t,12)=2); d3 = (mod(t,12)=3); d4 = (mod(t,12)=4); d5 = (mod(t,12)=5); d6 = (mod(t,12)=6); d7 = (mod(t,12)=7); d8 = (mod(t,12)=8); d9 = (mod(t,12)=9); d10 = (mod(t,12)=10); d11 = (mod(t,12)=11); d12 = (mod(t,12)=0); output; end; /* This segment of the program generates an AR(1) series with no trend or seasonal effects. */ data mc1; x1 = 25; iseed = 12345; do t = 0 to 120; a = rannor(iseed); x2 = 10.0 + 0.6*x1 + a; y=x2; if t > 0 then output; x1 = x2; end; keep y; data combine1; merge mc1 dummy; run; proc gplot data=combine1; symbol v=dot c=black i=join h=.8; title1 'AR(1) Series with Rho = 0.6 No Trend and No Seasonal Effects'; title2 'X=Time Y=Y Series'; axis1 order=(0 to 120 by 10) label=(f=duplex 'Time'); axis2 order=(10 to 40 by 5) label=(f=duplex 'Y Series'); plot y*t / haxis=axis1 vaxis=axis2; run; title 'Test of trend, season, and both in data that has neither'; proc autoreg data = combine1; model y = t t2 d2 d3 d4 d5 d6 d7 d8 d9 d10 d11 d12/ nlag = 4 method=ml backstep slstay=0.05; Trend: test t,t2; Season: test d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12; Trend_Season: test t,t2,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12; run; /* This segment of the program generates a Deterministic Seasonal with no trend. The Seasonal Effects add to one. */ data mc2; x1 = 200; iseed = 50385; do t = 0 to 120; a = rannor(iseed); if (mod(t,12)=1) then s1 = -50; else s1 = 0; if (mod(t,12)=2) then s2 = -25; else s2 = 0; if (mod(t,12)=3) then s3 = 25; else s3 = 0; if (mod(t,12)=4) then s4 = -25; else s4 = 0; if (mod(t,12)=5) then s5 = -50; else s5 = 0; if (mod(t,12)=6) then s6 = 50; else s6 = 0; if (mod(t,12)=7) then s7 = 75; else s7 = 0; if (mod(t,12)=8) then s8 = 50; else s8 = 0; if (mod(t,12)=9) then s9 = 5; else s9 = 0; if (mod(t,12)=10) then s10 = -25; else s10 = 0; if (mod(t,12)=11) then s11 = -50; else s11 = 0; if (mod(t,12)=12) then s12 = 20; else s12 = 0; x2 = 80.0 + 0.6*x1 + s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10 + s11 + s12 + 10*a; y=x2; if t > 0 then output; x1 = x2; end; keep y t; proc gplot data=mc2; symbol v=dot c=black i=join h=.8; title1 'AR(1) Series with Rho = 0.6 with Fixed Seasonals and No Trend'; title2 'X=Time Y=Y Series'; axis1 order=(0 to 120 by 10) label=(f=duplex 'Time'); axis2 order=(0 to 400 by 50) label=(f=duplex 'Y Series'); plot y*t / haxis=axis1 vaxis=axis2; run; data combine2; merge mc2 dummy; run; title 'Test of trend, season, and both in data that has no trend but seasonal effects'; proc autoreg data = combine2; model y = t t2 d2 d3 d4 d5 d6 d7 d8 d9 d10 d11 d12/ nlag = 4 method=ml backstep slstay=0.05; Trend: test t,t2; Season: test d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12; Trend_Season: test t,t2,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12; run; /* This segment of the program generates a Deterministic Trend with no Seasonal Effects. */ data mc3; e1=0; iseed = 6356; do t = 0 to 120; a = rannor(iseed); e2 = 0.6*e1 + 20*a; y = 100.0 + 4.0*t + e2; If t > 0 then output; e1 = e2; end; keep y; data combine3; merge mc3 dummy; run; proc gplot data=combine3; symbol v=dot c=black i=join h=.8; title1 'Deterministic Trend Data with AR(1) Error but No Seasonal Effects'; title2 'X=Time Y=Y Series'; axis1 order=(0 to 120 by 10) label=(f=duplex 'Time'); axis2 order=(-100 to 700 by 100) label=(f=duplex 'Y Series'); plot y*t / haxis=axis1 vaxis=axis2; run; title 'Test of trend, season, and both in data that has slope but no seasonal effects'; proc autoreg data = combine3; model y = t t2 d2 d3 d4 d5 d6 d7 d8 d9 d10 d11 d12/ nlag = 4 method=ml backstep slstay=0.05; Trend: test t,t2; Season: test d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12; Trend_Season: test t,t2,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12; run; /* This segment of the program generates a Deterministic Trend with Seasonal Effects. The Seasonal Effects add to one. */ data mc4; e1 = 0; iseed = 402675; do t = 0 to 120; a = rannor(iseed); if (mod(t,12)=1) then s1 = -50; else s1 = 0; if (mod(t,12)=2) then s2 = -25; else s2 = 0; if (mod(t,12)=3) then s3 = 25; else s3 = 0; if (mod(t,12)=4) then s4 = -25; else s4 = 0; if (mod(t,12)=5) then s5 = -50; else s5 = 0; if (mod(t,12)=6) then s6 = 50; else s6 = 0; if (mod(t,12)=7) then s7 = 75; else s7 = 0; if (mod(t,12)=8) then s8 = 50; else s8 = 0; if (mod(t,12)=9) then s9 = 5; else s9 = 0; if (mod(t,12)=10) then s10 = -25; else s10 = 0; if (mod(t,12)=11) then s11 = -50; else s11 = 0; if (mod(t,12)=12) then s12 = 20; else s12 = 0; e2 = 0.6*e1 + 20*a; y = 100.0 + 4.0*t + s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10 + s11 + s12 + e2; If t > 0 then output; e1 = e2; end; keep y; data combine4; merge mc4 dummy; proc gplot data=combine4; symbol v=dot c=black i=join h=.8; title1 'Deterministic Trend Data with AR(1) Errors and Seasonal Effects'; title2 'X=Time Y=Y Series'; axis1 order=(0 to 120 by 10) label=(f=duplex 'Time'); axis2 order=(-100 to 700 by 100) label=(f=duplex 'Y Series'); plot y*t / haxis=axis1 vaxis=axis2; run; title 'Test of trend, season, and both in data that has slope and seasonal effects'; proc autoreg data = combine4; model y = t t2 d2 d3 d4 d5 d6 d7 d8 d9 d10 d11 d12/ nlag = 4 method=ml backstep slstay=0.05; Trend: test t,t2; Season: test d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12; Trend_Season: test t,t2,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12; run;