seasonal modeling in time series with r
TRANSCRIPT
![Page 1: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/1.jpg)
Presentation onProject:
Time series Analysis & Forecasting
![Page 2: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/2.jpg)
Presented By
Mahmuda Mohammad Reg no:2013134029 MSc 2nd Semester Department of StatisticsShahjalal University of Science & Technology, Sylhet
2Shahjalal University of Science & Technology,Sylhet
![Page 3: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/3.jpg)
Coordinated By
Dr. Mohammad Shahidul Islam MS, PhD & Postdoc (Canada), MSc (SUST)
ProfessorDepartment of Statistics
Shahjalal University of Science & Technology, Sylhet
3Shahjalal University of Science & Technology,Sylhet
![Page 4: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/4.jpg)
Index
4Shahjalal University of Science & Technology,Sylhet
![Page 5: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/5.jpg)
Monthly Yields on Treasury Securities
• Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec• 1953 2.83 3.05 3.11 2.93 2.95 2.87 2.66 2.68 2.59• 1954 2.48 2.47 2.37 2.29 2.37 2.38 2.30 2.36 2.38 2.43 2.48 2.51• 1955 2.61 2.65 2.68 2.75 2.76 2.78 2.90 2.97 2.97 2.88 2.89 2.96• 1956 2.90 2.84 2.96 3.18 3.07 3.00 3.11 3.33 3.38 3.34 3.49 3.59• 1957 3.46 3.34 3.41 3.48 3.60 3.80 3.93 3.93 3.92 3.97 3.72 3.21• 1958 3.09 3.05 2.98 2.88 2.92 2.97 3.20 3.54 3.76 3.80 3.74 3.86• 1959 4.02 3.96 3.99 4.12 4.31 4.34 4.40 4.43 4.68 4.53 4.53 4.69• 1960 4.72 4.49 4.25 4.28 4.35 4.15 3.90 3.80 3.80 3.89 3.93 3.84• 1961 3.84 3.78 3.74 3.78 3.71 3.88 3.92 4.04 3.98 3.92 3.94 4.06• 1962 4.08 4.04 3.93 3.84 3.87 3.91 4.01 3.98 3.98 3.93 3.92 3.86• 1963 3.83 3.92 3.93 3.97 3.93 3.99 4.02 4.00 4.08 4.11 4.12 4.13• 1964 4.17 4.15 4.22 4.23 4.20 4.17 4.19 4.19 4.20 4.19 4.15 4.18• 1965 4.19 4.21 4.21 4.20 4.21 4.21 4.20 4.25 4.29 4.35 4.45 4.62• 1966 4.61 4.83 4.87 4.75 4.78 4.81 5.02 5.22 5.18 5.01 5.16 4.84• 1967 4.58 4.63 4.54 4.59 4.85 5.02 5.16 5.28 5.30 5.48 5.75 5.70• 1968 5.53 5.56 5.74 5.64 5.87 5.72 5.50 5.42 5.46 5.58 5.70 6.03• 1969 6.04 6.19 6.30 6.17 6.32 6.57 6.72 6.69 7.16 7.10 7.14 7.65• 1970 7.79 7.24 7.07 7.39 7.91 7.84 7.46 7.53 7.39 7.33 6.84 6.39• 1971 6.24 6.11 5.70 5.83 6.39 6.52 6.73 6.58 6.14 5.93 5.81 5.93• 1972 5.95 6.08 6.07 6.19 6.13 6.11 6.11 6.21 6.55 6.48 6.28 6.36• 1973 6.46 6.64 6.71 6.67 6.85 6.90 7.13 7.40 7.09 6.79 6.73 6.74• 1974 6.99 6.96 7.21 7.51 7.58 7.54 7.81 8.04 8.04 7.90 7.68 7.43• 1975 7.50 7.39 7.73 8.23 8.06 7.86 8.06 8.40 8.43 8.14 8.05 8.00• 1976 7.74 7.79 7.73 7.56 7.90 7.86 7.83 7.77 7.59 7.41 7.29 6.87•
• To b continued
library(tseries)data(package="tseries")data(tcm) TS<-tcm10yTS
5Shahjalal University of Science & Technology,Sylhet
![Page 6: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/6.jpg)
Monthly Yields on Treasury Securities
• 1977 7.21 7.39 7.46 7.37 7.46 7.28 7.33 7.40 7.34 7.52 7.58 7.69• 1978 7.96 8.03 8.04 8.15 8.35 8.46 8.64 8.41 8.42 8.64 8.81 9.01• 1979 9.10 9.10 9.12 9.18 9.25 8.91 8.95 9.03 9.33 10.30 10.65 10.39• 1980 10.80 12.41 12.75 11.47 10.18 9.78 10.25 11.10 11.51 11.75 12.68 12.84• 1981 12.57 13.19 13.12 13.68 14.10 13.47 14.28 14.94 15.32 15.15 13.39 13.72• 1982 14.59 14.43 13.86 13.87 13.62 14.30 13.95 13.06 12.34 10.91 10.55 10.54• 1983 10.46 10.72 10.51 10.40 10.38 10.85 11.38 11.85 11.65 11.54 11.69 11.83• 1984 11.67 11.84 12.32 12.63 13.41 13.56 13.36 12.72 12.52 12.16 11.57 11.50• 1985 11.38 11.51 11.86 11.43 10.85 10.16 10.31 10.33 10.37 10.24 9.78 9.26• 1986 9.19 8.70 7.78 7.30 7.71 7.80 7.30 7.17 7.45 7.43 7.25 7.11• 1987 7.08 7.25 7.25 8.02 8.61 8.40 8.45 8.76 9.42 9.52 8.86 8.99• 1988 8.67 8.21 8.37 8.72 9.09 8.92 9.06 9.26 8.98 8.80 8.96 9.11• 1989 9.09 9.17 9.36 9.18 8.86 8.28 8.02 8.11 8.19 8.01 7.87 7.84• 1990 8.21 8.47 8.59 8.79 8.76 8.48 8.47 8.75 8.89 8.72 8.39 8.08• 1991 8.09 7.85 8.11 8.04 8.07 8.28 8.27 7.90 7.65 7.53 7.42 7.09• 1992 7.03 7.34 7.54 7.48 7.39 7.26 6.84 6.59 6.42 6.59 6.87 6.77• 1993 6.60 6.26 5.98 5.97 6.04 5.96 5.81 5.68 5.36 5.33 5.72 5.77• 1994 5.75 5.97 6.48 6.97 7.18 7.10 7.30 7.24 7.46 7.74 7.96 7.81• 1995 7.78 7.47 7.20 7.06 6.63 6.17 6.28 6.49 6.20 6.04 5.93 5.71• 1996 5.65 5.81 6.27 6.51 6.74 6.91 6.87 6.64 6.83 6.53 6.20 6.30• 1997 6.58 6.42 6.69 6.89 6.71 6.49 6.22 6.30 6.21 6.03 5.88 5.81• 1998 5.54 5.57 5.65 5.64 5.65 5.50 5.46 5.34 4.81 4.53 4.83 4.65• 1999 4.72 5.00 5.23 5.18 5.54 5.90 5.79 5.94 5.92
6Shahjalal University of Science & Technology,Sylhet
![Page 7: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/7.jpg)
Abstruct
7Shahjalal University of Science & Technology,Sylhet
The aim of the project is to conduct a time seriesanalysis and forecasting the monthly yields of treasury securities. Among the most effective approches for analyzing time series data ARIMA is employed in the data. In this project appropriate model is adaptivelyformed based on the given data.
![Page 8: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/8.jpg)
Introduction
The treasury yield is the return on investment, expressed as percentage, on the us govt’s debt obligations (bonds, notes and bills).
Looked at the another way the treasury yield is the interest rate the US govt pays to borrow money for different lengths of time. The higher the yields on 10-,20-,30- years treasuries, the better the economic outlook.
Shahjalal University of Science & Technology,Sylhet
8
![Page 9: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/9.jpg)
Methods
9Shahjalal University of Science & Technology,Sylhet
• BoxCox transformation• Adf test• Kpss test• ARIMA, ACF, PACF• AIC,BIC• Ljung-Box test • McLeodLi test
![Page 10: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/10.jpg)
Material
R software Version 3.2.2
10Shahjalal University of Science & Technology,Sylhet
![Page 11: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/11.jpg)
Analysis
Start the analysis with R
Shahjalal University of Science & Technology,Sylhet
11
![Page 12: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/12.jpg)
Plot of the Data
plot(TS)
There is a stong trend and seasonality pattern
Time
TS
1960 1970 1980 1990 2000
24
68
1012
14
12Shahjalal University of Science & Technology,Sylhet
![Page 13: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/13.jpg)
Box test for transformationlibrary(MASS) boxcox(TS~1)
To stabilize the fluctuation we use the Boxcox; the family of transformation
-2 -1 0 1 2
-155
0-1
500
-145
0-1
400
-135
0-1
300
log-
Like
lihoo
d
95%
13Shahjalal University of Science & Technology,Sylhet
![Page 14: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/14.jpg)
Lambda value a<- boxcox(TS~1) lam<- a$x[which.max (a$y)]lam[1] 0.2222222 TSV<- ((TS^lam)-1)/lamplot(TSV)
We get the value of the lambda which is .22222 Which leads us to the decision that no transformation is needed.
14Shahjalal University of Science & Technology,Sylhet
Time
TSV
1960 1970 1980 1990 2000
1.0
1.5
2.0
2.5
3.0
3.5
![Page 15: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/15.jpg)
Adf test adf.test(TSV)
Augmented Dickey-Fuller Test
data: TSVDickey-Fuller = -1.4095, Lag order = 8, p-value = 0.8282alternative hypothesis: stationary So we have come to conclusion that the series is nonstationary
15Shahjalal University of Science & Technology,Sylhet
![Page 16: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/16.jpg)
KPSS test kpss.test(TSV)
KPSS Test for Level Stationarity
data: TSVKPSS Level = 5.375, Truncation lag parameter = 5, p-value = 0.01
Warning message:In kpss.test(TSV) : p-value smaller than printed p-value
So we may accept the alternative hypothesis that it is difference stationary. Which leads us to take the difference of the series to produce a stationary series.
16Shahjalal University of Science & Technology,Sylhet
![Page 17: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/17.jpg)
Decompositiondec<-decompose(TS)plot(dec)
24
68
12
obse
rved
24
68
12
trend
-0.1
00.
00
seas
onal
-1.5
-0.5
0.5
1.5
1960 1970 1980 1990 2000
rand
om
Time
Decomposition of additive time series
17Shahjalal University of Science & Technology,Sylhet
![Page 18: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/18.jpg)
difference to de- seasonalize the data diff<-diff(TSV)plot(diff)
After taking the seasonal difference the seasonality has gone but there is still the evidence of trend
18Shahjalal University of Science & Technology,Sylhet
Time
diff
1960 1970 1980 1990 2000
-0.6
-0.4
-0.2
0.0
0.2
0.4
0.6
![Page 19: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/19.jpg)
To remove the trend we take the first difference diff1<-diff(diff)plot(diff1)
So after taking both the seasonal difference and non seasonal 1st difference stationary has gone now
Shahjalal University of Science & Technology,Sylhet
19
Time
diff1
1960 1970 1980 1990 2000
-0.3
-0.2
-0.1
0.0
0.1
0.2
![Page 20: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/20.jpg)
Adf test
Augmented Dickey-Fuller Test
data: diff1Dickey-Fuller = -6.7294, Lag order = 8, p-value = 0.01alternative hypothesis: stationary
Warning message:In adf.test(diff1) : p-value smaller than printed p-value
The series is now stationary as we may accept the alternative hypothesis with the p value less than 0.05
20Shahjalal University of Science & Technology,Sylhet
![Page 21: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/21.jpg)
To choose the order of the seasonal ARIMA we investigate the ACF and PACF:
index<-1:60> col<-rep("white",60)> col[c(1,12,24,36,48,60)]<-"black"> col<-rep("white",60)> col[c(1,12,24,36,48,6)]<-"black" par(mfrow=c(2,1))> acf(diff1,60,col=col)> pacf(diff1,60,col=col)
Shahjalal University of Science & Technology,Sylhet
21
0 1 2 3 4 5
-0.5
0.5
Lag
AC
F
Series diff1
0 1 2 3 4 5
-0.4
0.0
Lag
Par
tial A
CF
Series diff1
![Page 22: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/22.jpg)
library(forecast)tsdisplay(diff1)
Shahjalal University of Science & Technology,Sylhet
22
diff1
1960 1970 1980 1990 2000
-0.3
-0.1
0.1
0 5 10 20 30
-0.4
-0.2
0.0
0.2
Lag
AC
F
0 5 10 20 30
-0.4
-0.2
0.0
0.2
Lag
PA
CF
![Page 23: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/23.jpg)
Seasonal: Looking at lags that are multiples of 12 (we have monthly data). Not much is going on there, although there is a (barely) significant spike in the ACF at lag 1. Nothing significant is happening at the higher lags. Maybe a seasonal MA(1) or MA(2) might work.
Non-seasonal: Looking at just the first 2 or 3 lags, it seems possible that a MA(1) might work based on the single spike in the ACF and the PACF tapering to 0. With S=12 the nonseasonal aspect is sometimes difficult to interpret in such a narrow window.
On the basis of the ACF and PACF of the 12th differences, we identified an
ARIMA(0,1,1)×(0,1,1)12 model as a possibility.
Shahjalal University of Science & Technology,Sylhet
23
![Page 24: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/24.jpg)
Possible Combination Arima(TS,order=c(0,1,1),seasonal=list(order=c(0,1,1),12),lambda=lam)$bic-1662.896Arima(TS,order=c(0,1,2),seasonal=list(order=c(0,1,1),12),lambda=lam)$bic-1661.526Arima(TS,order=c(0,1,1),seasonal=list(order=c(0,1,2),12),lambda=lam)$bic -1657.353Arima(TS,order=c(0,1,2),seasonal=list(order=c(0,1,2),12),lambda=lam)$bic-1655.843 Arima(TS,order=c(0,1,3),seasonal=list(order=c(0,1,2),12),lambda=lam)$bic -1649.544Arima(TS,order=c(0,1,2),seasonal=list(order=c(0,1,3),12),lambda=lam)$bic-1649.876Arima(TS,order=c(0,1,3),seasonal=list(order=c(0,1,3),12),lambda=lam)$bic-1643.578Arima(TS,order=c(1,1,1),seasonal=list(order=c(1,1,1),12),lambda=lam)$bic -1655.57
Shahjalal University of Science & Technology,Sylhet
24
![Page 25: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/25.jpg)
BIC values
( p,d,q) (P,D,Q) (BIC)(0,1,1) (0,1,1) -1662.896
(0,1,2) (0,1,1) -1661.526
(0,1,1)
(0,1,2) -1657.353
(0,1,2) (0,1,2) -1655.843
(0,1,3) (0,1,2) -1649.544
(0,1,2) (0,1,3) -1649.876
(0,1,3) (0,1,3) -1643.578
(1,1,1) (1,1,1) -1655.57Shahjalal University of Science & Technology,Sylhet
25
![Page 26: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/26.jpg)
Residual AnalysisFit<-Arima(TS,order=c(0,1,1),seasonal=list(order=c(0,1,1),12),lambda=lam)e<-resid(Fit) tsdisplay(e)
Except for significance at lag 11 and 21 the model seems to have captured the essence of dependence in the series .
Shahjalal University of Science & Technology,Sylhet
26
e
1960 1970 1980 1990 2000
-0.2
0.0
0.1
0.2
0 5 10 20 30
-0.1
00.
000.
10
Lag
AC
F
0 5 10 20 30
-0.1
00.
000.
10
Lag
PA
CF
![Page 27: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/27.jpg)
The Ljung-Box testBox.test(e,type="Ljung")
Box-Ljung testdata: e• X-squared = 1.3268, df = 1, p-value = 0.2494
The test leading to p value .2494 a further indication that the model captured the dependence in the time series .
Shahjalal University of Science & Technology,Sylhet
27
![Page 28: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/28.jpg)
For formally testing whether the squared residuals are autocorrelated :
library(TSA)McLeod.Li.test(y=e)
The figure shows that the McLeod .Li tests are all significant at the 5% significance level.This formally shows strong evidence for ARCH in the data.
Shahjalal University of Science & Technology,Sylhet
28
0 5 10 15 20 25
0.0
0.2
0.4
0.6
0.8
1.0
Lag
P-v
alue
![Page 29: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/29.jpg)
summary(Fit)
Series: TS ARIMA(0,1,1)(0,1,1)[12] Box Cox transformation: lambda= 0.2222222
Coefficients: ma1 sma1 0.5047 -1.0000s.e. 0.0410 0.0439
sigma^2 estimated as 0.002457: log likelihood=840.9AIC=-1675.8 AICc=-1675.75 BIC=-1662.9
Training set error measures: ME RMSE MAE MPE MAPE MASETraining set -0.01004224 0.2580049 0.1730594 -0.1069817 2.423545 0.1951395 ACF1Training set -0.08384966
Shahjalal University of Science & Technology,Sylhet
29
![Page 30: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/30.jpg)
Forecast plot(forecast(Fit),h=10)
Forecasting or predicting future as yet unobserved values is one of the main reasons for developing time series model. We showed how to do this with ARIMA model.
Shahjalal University of Science & Technology,Sylhet
30
Forecasts from ARIMA(0,1,1)(0,1,1)[12]
1990 2000
24
68
1012
14
![Page 31: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/31.jpg)
Discussion
From the visual pattern of the McLeodLi test we get the strong evidence for ARCH in the data .So we should conduct the further modeling .But in our analysis we have stopped here .
The Seasonal ARIMA model we have got is of order (0,1,1)* (0,1,1),12
31Shahjalal University of Science & Technology,Sylhet
![Page 32: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/32.jpg)
Conclusion
32Shahjalal University of Science & Technology,Sylhet
The main aim of time series modeling is to carefully collect and regorously study the past observations of a time series to develop an appropriate model which describes the inherent structure of the series .This model is then used to generate future values for the series .Time series forecasting thus can be termed as the act of predicting the future by understanding the past.
In this project among the most effective approches for analyzing time series data ,ARIMA is employed and appropriate
model is adaptively formed based on the given data.
![Page 33: Seasonal modeling in time series with R](https://reader034.vdocuments.net/reader034/viewer/2022052606/58713f261a28abf0568b7aef/html5/thumbnails/33.jpg)
Thank You
33Shahjalal University of Science & Technology,Sylhet