Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 35

INTERNATIONAL UNIVERSITY – VNU HCMC

DEPARTMENT OF INDUSTRIAL & SYSTEMS


ENGINEERING

TIME SERIES REPORT

Nguyễn Thị Lan Hương IELSIU16152


Tạ Lan Phương IELSIU16085
Trần Hoàng Nguyên IELSIU16078
INDEX
A. INTRODUCTION ...........................................................................................................3

B. IMPLEMENTATION

I. Plot the time series ...........................................................................................................4


II. Using the plot to verify the outlier of the time series ......................................................5
III. Test stationary of time series ...........................................................................................6
IV. Forecasting
1. Naïve ..........................................................................................................................8
2. Simple moving average..............................................................................................11
3. Simple exponential smoothing...................................................................................20
4. Moving Average ........................................................................................................24
5. Autoregressive model ................................................................................................27
6. ARMA(p,q) ................................................................................................................29
7. ARIMA(p,d,q)............................................................................................................31
8. TBATS method ..........................................................................................................33
C. CONCLUSION................................................................................................................36
A.INTRODUCTION
Time series models have been the basis for any study of a behavior of process or metrics over a
period of time. The application of time series models is manifold, including sales forecasting,
weather forecasting, inventory studies, etc. In decisions that involve factor of uncertainty of the
future, time series models have been found one of the most effective methods of forecasting. Most
often, future course of actions and decisions for such processes will depend on what would be an
anticipated result. The need for these anticipated results has encouraged organizations to develop
forecasting techniques to be better prepared to face the seemingly uncertain future. Also, these
models can be combined with other data mining techniques to help understand the behavior of the
data and to be able to predict future trends and patterns in the data behavior.

The techniques that our group uses to analyze the data is mainly covered in class. In the section of
data verification, our group use box plot, a method for graphically depicting groups of numerical
data through their quartiles, in order to check the data and remove the outliers before forecasting.
In forecasting section, our group focuses into analyze the data by using code R combines with other
models. This includes

 Simple Moving Average and Simple Exponential Smoothing to smooth the data and identify
the trend direction and to determine support and resistance levels
 Autoregressive model (AR(p)) to predict future behavior based on past behavior. It’s used for
forecasting when there is some correlation between values in a time series and the value
that precede and succeed them
 Moving Average model (MA(q)) uses past forecast errors in a regression-like model
 ARMA (p,q) models provide a parsimonious description of a (weekly) stationary stochastic
processin terms of two polynomials, one for the autoregression and the second for the
moving average
 ARIMA (p,d,q) model is a generalization of an autoregressive moving average (ARMA) model.
Both of these models are fitted to time series data either to better understand the data or to
predict future points in the series. ARIMA models are applied in some cases where data
show evidence of non-stationary, where an initial differencing step (can be applied one or
more times to eliminate the non-stationary

Moreover, our group use the Augmented Dickey- Fuller (ADF), to test the null hypothesis that a unit
root is present in a time series sample. The alternative hypothesis is different depending on which
version of the test is used, but is usually stationary or trend-stationary.

In the Error analysis section, our group uses:

 Mean Error: The mean error (ME) value is simply computed as the average error value
(average of observed minus one-step-ahead forecast). Obviously, a drawback of this
measure is that positive and negative error values can cancel each other out, so this
measure is not a very good indicator of overall fit.
 Mean absolute error: The mean absolute error (MAE) value is computed as the average
absolute value. If this value is 0 (zero), the fit (forecast) is perfect. As compared to the mean
squared error value, this measure of fit will “de-emphasize” outliers, that is, unique or rare
large error values will affect the MAE less than the MSE value.
 Mean squared error. These value are computed as the sum (or average) of the squared error
values. This is the most commonly used lack-of-fit indicator in statistical fitting procedures.
 Mean absolute percentage error ( MAPE). As is the case with the mean error value, a mean
percentage error near 0 (zero) can be produced by large positive and negative percentage
error that cancel each other out. Thus, a better measure of relative overall fit is the mean
absolute percentage error. Also, this measure is usually more meaningful than the mean
squared error.

Our group also base on the Akaike information criterion (AIC) is the estimator of the relative
quality of statistical models for a given set of data. Given a collection of models for the data, AIC
estimates the quality of each model, relative to each of the other models. Thus, AIC provides a
means for model selection.

Base on the index of the AIC, Mean Error, Mean Squared Error, we can know what models is best
fit and provide the most accuracy for the time series data by comparing and choosing the lowest
index of error between each methods. Therefore, we can continue to make the forecast
planning for the future.
B. IMPLEMENTATION
I. Plot the time series
1. Input the data from excel to R and assign the data to variable “data13”

> library(readxl)
> data13 <- read_excel("D:\\data13.xlsx")
> View(data13)
> attach(data13)

2. Plot the dataset

> plot(Price, type = "o", ylab = "Price", main = "TIME SERIES PLOT")
> axis(1, at=1:2769, labels = data13$Date)
II. Using box plot to verify the outliers of the time series
1. Summary the data

> summary(Price)
Min. 1st Qu. Median Mean 3rd Qu. Max.
41.31 78.50 87.92 103.34 136.95 181.73

2. Find the datum for the boxplot

IQR=Q3-Q1= 136.95 – 78.50 = 58.45


Q1-1.5IQR= 78.50 – 1.5 x 58.45 = -9.175
Q3+1.5IQR= 136.95 + 1.5 x 58.45 = 224.625

3. Plot the box plot and verify outliers

> boxplot(Price, horizontal = T)

From the boxplot above, there is no outliers for this time series.
III. Test stationary for time series
1. The Augmented Dickey–Fuller (ADF)

> install.packages("tseries")
> library(tseries)
> adf.test(Price, alternative="stationary")

Augmented Dickey-Fuller Test

data: Price
Dickey-Fuller = -1.7633, Lag order = 14, p-value = 0.6786
alternative hypothesis: stationary
2. ACF

> acf(Price, lag.max = 1500, type = c("correlation", "covariance", "partia


l"), plot = TRUE, na.action = na.contiguous, demean = TRUE, main = "ACF")

The ACF plotted below decays very slowly, relative large at long term. There for the time series is not
stationary.

3. Transform the time series into stationary using Differencing

> plot(diff(Price), ylab = "differencing Price", type = "l")


 Check the stationary of differencing time series by plot its ACF

> a<-acf(diff(Price),lag.max=1500,plot=F)
> plot(a, main = "ACF of Differencing")

 The graph below shows that the ACF of differencing time series drops to zero relatively
quickly, which mean now the time series is stationary after being differenced.
IV. Forecasting
1. Naïve method
>install.packages(“forecast”)
>library(forecast)
> naivemd <- forecast::naive(Price, h = 50, level = c(80,95), fan = FALSE,
lambda = NULL, biasadj = FALSE, bootstrap = FALSE, npaths = 5000)
> naivemd
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
1/3/2017 179 177.4426 180.5574 176.6182 181.3818
1/4/2017 179 176.7975 181.2025 175.6316 182.3684
1/5/2017 179 176.3026 181.6974 174.8746 183.1254
1/6/2017 179 175.8853 182.1147 174.2364 183.7636
1/9/2017 179 175.5176 182.4824 173.6742 184.3258
1/10/2017 179 175.1852 182.8148 173.1658 184.8342
1/11/2017 179 174.8796 183.1204 172.6984 185.3016
1/12/2017 179 174.5951 183.4049 172.2633 185.7367
1/13/2017 179 174.3279 183.6721 171.8546 186.1454
1/17/2017 179 174.0752 183.9248 171.4681 186.5319
1/18/2017 179 173.8348 184.1652 171.1005 186.8995
1/19/2017 179 173.6051 184.3949 170.7492 187.2508
1/20/2017 179 173.3848 184.6152 170.4123 187.5877
1/23/2017 179 173.1729 184.8271 170.0881 187.9119
1/24/2017 179 172.9683 185.0317 169.7754 188.2246
1/25/2017 179 172.7705 185.2295 169.4728 188.5272
1/26/2017 179 172.5788 185.4212 169.1796 188.8204
1/27/2017 179 172.3926 185.6074 168.8949 189.1051
1/30/2017 179 172.2116 185.7884 168.618 189.382
1/31/2017 179 172.0352 185.9648 168.3483 189.6517
2/1/2017 179 171.8632 186.1368 168.0853 189.9147
2/2/2017 179 171.6953 186.3047 167.8284 190.1716
2/3/2017 179 171.5311 186.4689 167.5773 190.4227
2/6/2017 179 171.3705 186.6295 167.3316 190.6684
2/7/2017 179 171.2131 186.7869 167.091 190.909
2/8/2017 179 171.0589 186.9411 166.8552 191.1448
2/9/2017 179 170.9077 187.0923 166.6238 191.3762
2/10/2017 179 170.7592 187.2408 166.3967 191.6033
2/13/2017 179 170.6133 187.3867 166.1737 191.8263
2/14/2017 179 170.4699 187.5301 165.9544 192.0456
2/15/2017 179 170.3289 187.6711 165.7387 192.2613
2/16/2017 179 170.1902 187.8098 165.5265 192.4735
2/17/2017 179 170.0536 187.9464 165.3176 192.6824
2/21/2017 179 169.919 188.081 165.1119 192.8881
2/22/2017 179 169.7865 188.2135 164.9091 193.0909
2/23/2017 179 169.6558 188.3442 164.7092 193.2908
2/24/2017 179 169.5269 188.4731 164.5121 193.4879
2/27/2017 179 169.3997 188.6003 164.3176 193.6824
2/28/2017 179 169.2742 188.7258 164.1257 193.8743
3/1/2017 179 169.1503 188.8497 163.9362 194.0638
3/2/2017 179 169.028 188.972 163.7491 194.2509
3/3/2017 179 168.9071 189.0929 163.5642 194.4358
3/6/2017 179 168.7876 189.2124 163.3815 194.6185
3/7/2017 179 168.6696 189.3304 163.201 194.799
3/8/2017 179 168.5528 189.4472 163.0225 194.9775
3/9/2017 179 168.4374 189.5626 162.8459 195.1541
3/10/2017 179 168.3232 189.6768 162.6713 195.3287
3/13/2017 179 168.2102 189.7898 162.4985 195.5015
3/14/2017 179 168.0984 189.9016 162.3275 195.6725
3/15/2017 179 167.9877 190.0123 162.1582 195.8418

> forecast::checkresiduals(naivemd)

Ljung-Box test

data: Residuals from Naive method


Q* = 14.401, df = 10, p-value = 0.1555

Model df: 0. Total lags used: 10

> naivefct <- forecast(naivemd)


> plot(naivefct)
2. Simple Moving Average
a. 30-period

Step 1:

> forecast::ma(Price, order = 30)


> ma30 <- forecast::ma(Price, order = 30)
> plot(ma30, main = "MA(30)", ylab = "Price", xlab = "Date")
> axis(1, at=1:2769, labels = data13$Date)
Step2: Forecast for SMA

> forecast(ma30, 50)


Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
12/9/2016 175.4455 175.3933 175.4977 175.3656 175.5254
12/12/2016 175.6288 175.512 175.7456 175.4502 175.8074
12/13/2016 175.8122 175.6167 176.0076 175.5133 176.111
12/14/2016 175.9955 175.7094 176.2815 175.558 176.433
12/15/2016 176.1788 175.7915 176.5661 175.5864 176.7712
12/16/2016 176.3621 175.8639 176.8604 175.6002 177.1241
12/19/2016 176.5455 175.9275 177.1634 175.6004 177.4906
12/20/2016 176.7288 175.9828 177.4748 175.588 177.8697
12/21/2016 176.9121 176.0304 177.7938 175.5637 178.2606
12/22/2016 177.0955 176.0707 178.1203 175.5282 178.6627
12/23/2016 177.2788 176.104 178.4536 175.482 179.0756
12/27/2016 177.4621 176.1306 178.7937 175.4257 179.4986
12/28/2016 177.6455 176.1508 179.1401 175.3596 179.9314
12/29/2016 177.8288 176.1649 179.4927 175.284 180.3736
12/30/2016 178.0121 176.173 179.8513 175.1994 180.8248
1/3/2017 178.1955 176.1754 180.2155 175.106 181.2849
1/4/2017 178.3788 176.1722 180.5854 175.0041 181.7535
1/5/2017 178.5621 176.1636 180.9606 174.8939 182.2303
1/6/2017 178.7454 176.1498 181.3411 174.7757 182.7152
1/9/2017 178.9288 176.1308 181.7267 174.6496 183.2079
1/10/2017 179.1121 176.1068 182.1174 174.516 183.7082
1/11/2017 179.2954 176.078 182.5129 174.3748 184.2161
1/12/2017 179.4788 176.0444 182.9131 174.2264 184.7312
1/13/2017 179.6621 176.0061 183.318 174.0708 185.2534
1/17/2017 179.8454 175.9633 183.7275 173.9082 185.7826
1/18/2017 180.0288 175.916 184.1415 173.7388 186.3187
1/19/2017 180.2121 175.8643 184.5599 173.5627 186.8615
1/20/2017 180.3954 175.8083 184.9826 173.38 187.4108
1/23/2017 180.5787 175.748 185.4095 173.1908 187.9667
1/24/2017 180.7621 175.6836 185.8405 172.9952 188.5289
1/25/2017 180.9454 175.6151 186.2757 172.7934 189.0974
1/26/2017 181.1287 175.5426 186.7149 172.5854 189.6721
1/27/2017 181.3121 175.4661 187.1581 172.3714 190.2528
1/30/2017 181.4954 175.3856 187.6052 172.1513 190.8395
1/31/2017 181.6787 175.3014 188.0561 171.9254 191.4321
2/1/2017 181.8621 175.2133 188.5108 171.6937 192.0304
2/2/2017 182.0454 175.1215 188.9693 171.4562 192.6346
2/3/2017 182.2287 175.026 189.4314 171.2131 193.2443
2/6/2017 182.4121 174.9269 189.8972 170.9645 193.8596
2/7/2017 182.5954 174.8241 190.3666 170.7103 194.4805
2/8/2017 182.7787 174.7178 190.8396 170.4507 195.1068
2/9/2017 182.962 174.608 191.316 170.1857 195.7384
2/10/2017 183.1454 174.4948 191.796 169.9154 196.3753
2/13/2017 183.3287 174.3781 192.2793 169.6399 197.0175
2/14/2017 183.512 174.258 192.7661 169.3592 197.6649
2/15/2017 183.6954 174.1345 193.2562 169.0733 198.3174
2/16/2017 183.8787 174.0078 193.7496 168.7824 198.975
2/17/2017 184.062 173.8777 194.2463 168.4865 199.6376
2/21/2017 184.2454 173.7444 194.7463 168.1856 200.3051
2/22/2017 184.4287 173.6079 195.2494 167.8798 200.9776

> plot(forecast(ma30, 50), main = "Forecast for SMA")


b. 120-period

Step1: Plot SMA

> forecast::ma(Price, order = 120)


> ma120 <- forecast::ma(Price, order = 120)
> plot(ma120, main = "MA(120)", ylab = "Price", xlab = "Date")
> axis(1, at=1:2769, labels = data13$Date)
Step2: Forecast for SMA

> forecast(ma120, 50)

Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
10/7/2013 96.61871 96.61016 96.62726 96.60563 96.63178
10/8/2013 96.58925 96.57014 96.60836 96.56002 96.61848
10/9/2013 96.55979 96.52781 96.59178 96.51088 96.60871
10/10/2013 96.53033 96.48352 96.57715 96.45873 96.60194
10/11/2013 96.50088 96.43748 96.56427 96.40392 96.59783
10/14/2013 96.47142 96.38988 96.55296 96.34671 96.59613
10/15/2013 96.44196 96.34082 96.5431 96.28728 96.59664
10/16/2013 96.4125 96.29041 96.53459 96.22578 96.59922
10/17/2013 96.38304 96.23874 96.52735 96.16235 96.60374
10/18/2013 96.35359 96.18586 96.52131 96.09708 96.6101
10/21/2013 96.32413 96.13185 96.51641 96.03006 96.6182
10/22/2013 96.29467 96.07674 96.5126 95.96137 96.62797
10/23/2013 96.26521 96.02059 96.50984 95.89109 96.63934
10/24/2013 96.23575 95.96342 96.50808 95.81926 96.65225
10/25/2013 96.2063 95.90529 96.5073 95.74595 96.66664
10/28/2013 96.17684 95.84622 96.50746 95.6712 96.68248
10/29/2013 96.14738 95.78623 96.50852 95.59506 96.6997
10/30/2013 96.11792 95.72537 96.51048 95.51756 96.71828
10/31/2013 96.08846 95.66364 96.51329 95.43875 96.73818
11/1/2013 96.05901 95.60107 96.51694 95.35866 96.75936
11/4/2013 96.02955 95.53769 96.52141 95.27731 96.78178
11/5/2013 96.00009 95.4735 96.52667 95.19475 96.80543
11/6/2013 95.97063 95.40854 96.53272 95.11099 96.83027
11/7/2013 95.94117 95.34282 96.53953 95.02607 96.85628
11/8/2013 95.91172 95.27634 96.54709 94.94 96.88343
11/11/2013 95.88226 95.20914 96.55538 94.85281 96.91171
11/12/2013 95.8528 95.14121 96.56439 94.76452 96.94108
11/13/2013 95.82334 95.07258 96.5741 94.67515 96.97153
11/14/2013 95.79388 95.00325 96.58451 94.58472 97.00305
11/15/2013 95.76443 94.93325 96.5956 94.49325 97.0356
11/18/2013 95.73497 94.86257 96.60736 94.40075 97.06918
11/19/2013 95.70551 94.79124 96.61978 94.30725 97.10377
11/20/2013 95.67605 94.71925 96.63285 94.21275 97.13935
11/21/2013 95.64659 94.64663 96.64656 94.11728 97.17591
11/22/2013 95.61713 94.57337 96.6609 94.02084 97.21343
11/25/2013 95.58768 94.4995 96.67586 93.92345 97.2519
11/26/2013 95.55822 94.42501 96.69143 93.82512 97.29131
11/27/2013 95.52876 94.34992 96.7076 93.72587 97.33165
11/29/2013 95.4993 94.27423 96.72438 93.62571 97.37289
12/2/2013 95.46984 94.19795 96.74174 93.52465 97.41504
12/3/2013 95.44039 94.12109 96.75968 93.4227 97.45808
12/4/2013 95.41093 94.04366 96.7782 93.31986 97.50199
12/5/2013 95.38147 93.96565 96.79729 93.21617 97.54678
12/6/2013 95.35201 93.88709 96.81693 93.11161 97.59242
12/9/2013 95.32255 93.80797 96.83714 93.0062 97.63891
12/10/2013 95.2931 93.72831 96.85789 92.89996 97.68624
12/11/2013 95.26364 93.6481 96.87918 92.79288 97.7344
12/12/2013 95.23418 93.56735 96.90101 92.68498 97.78338
12/13/2013 95.20472 93.48607 96.92337 92.57627 97.83317
12/16/2013 95.17526 93.40427 96.94626 92.46676 97.88377

> plot(forecast(ma120, 50), main = "Forecast for SMA")


c. 365-period

Step1: Plot SMA

> forecast::ma(Price, order = 365)


> ma365 <- forecast::ma(Price, order = 365)
> plot(ma365, main = "MA(365)", ylab = "Price", xlab = "Date")
> axis(1, at=1:2769, labels = data13$Date)
Step2: Forecast for SMA

> forecast(ma365, 50)


Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
4/14/2016 162.6021 162.596 162.6082 162.5928 162.6115
4/15/2016 162.666 162.6526 162.6794 162.6455 162.6865
4/18/2016 162.7299 162.7076 162.7521 162.6959 162.7639
4/19/2016 162.7937 162.7614 162.8261 162.7442 162.8432
4/20/2016 162.8576 162.8139 162.9013 162.7908 162.9244
4/21/2016 162.9215 162.8654 162.9775 162.8357 163.0072
4/22/2016 162.9854 162.9159 163.0548 162.8792 163.0915
4/25/2016 163.0492 162.9655 163.1329 162.9212 163.1772
4/26/2016 163.1131 163.0143 163.2119 162.962 163.2642
4/27/2016 163.177 163.0622 163.2917 163.0015 163.3525
4/28/2016 163.2408 163.1094 163.3723 163.0398 163.4419
4/29/2016 163.3047 163.1558 163.4536 163.077 163.5325
5/2/2016 163.3686 163.2015 163.5357 163.1131 163.6241
5/3/2016 163.4325 163.2465 163.6184 163.1481 163.7168
5/4/2016 163.4963 163.2909 163.7017 163.1822 163.8105
5/5/2016 163.5602 163.3346 163.7858 163.2152 163.9052
5/6/2016 163.6241 163.3778 163.8704 163.2474 164.0008
5/9/2016 163.6879 163.4203 163.9556 163.2786 164.0973
5/10/2016 163.7518 163.4622 164.0414 163.3089 164.1947
5/11/2016 163.8157 163.5036 164.1278 163.3384 164.293
5/12/2016 163.8796 163.5444 164.2147 163.367 164.3922
5/13/2016 163.9434 163.5847 164.3022 163.3947 164.4921
5/16/2016 164.0073 163.6244 164.3902 163.4217 164.5929
5/17/2016 164.0712 163.6636 164.4787 163.4479 164.6945
5/18/2016 164.135 163.7023 164.5678 163.4733 164.7968
5/19/2016 164.1989 163.7406 164.6573 163.4979 164.8999
5/20/2016 164.2628 163.7783 164.7473 163.5218 165.0038
5/23/2016 164.3267 163.8155 164.8378 163.545 165.1083
5/24/2016 164.3905 163.8523 164.9287 163.5674 165.2136
5/25/2016 164.4544 163.8887 165.0202 163.5892 165.3197
5/26/2016 164.5183 163.9245 165.112 163.6102 165.4264
5/27/2016 164.5822 163.9599 165.2044 163.6306 165.5337
5/31/2016 164.646 163.9949 165.2971 163.6502 165.6418
6/1/2016 164.7099 164.0295 165.3903 163.6693 165.7505
6/2/2016 164.7738 164.0636 165.4839 163.6876 165.8599
6/3/2016 164.8376 164.0973 165.578 163.7054 165.9699
6/6/2016 164.9015 164.1306 165.6725 163.7225 166.0806
6/7/2016 164.9654 164.1634 165.7673 163.7389 166.1919
6/8/2016 165.0293 164.1959 165.8626 163.7548 166.3038
6/9/2016 165.0931 164.228 165.9583 163.77 166.4163
6/10/2016 165.157 164.2596 166.0544 163.7846 166.5294
6/13/2016 165.2209 164.2909 166.1508 163.7986 166.6431
6/14/2016 165.2847 164.3218 166.2477 163.8121 166.7574
6/15/2016 165.3486 164.3523 166.3449 163.825 166.8723
6/16/2016 165.4125 164.3825 166.4425 163.8372 166.9877
6/17/2016 165.4764 164.4123 166.5405 163.849 167.1038
6/20/2016 165.5402 164.4417 166.6388 163.8601 167.2204
6/21/2016 165.6041 164.4707 166.7375 163.8707 167.3375
6/22/2016 165.668 164.4994 166.8366 163.8807 167.4552
6/23/2016 165.7318 164.5277 166.936 163.8902 167.5735

> plot(forecast(ma365, 50), main = "Forecast for SMA")


3. Simple Exponential Smoothing
a. Single (Without trend and without seasonal components)

Step 1: Assign HoltWinters to fit with beta and gamma

> fit <- HoltWinters(Price, beta = F, gamma = F)


> fit

Holt-Winters exponential smoothing without trend and without seasonal comp


onent.

Call:
HoltWinters(x = Price, beta = F, gamma = F)

Smoothing parameters:
alpha: 0.9708688
beta : FALSE
gamma: FALSE

Coefficients:
[,1]
a 178.9798

Step 2: Forecast and Plot data

> library(forecast)
> forecast(fit, 50)
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
1/3/2017 178.9798 177.4235 180.5361 176.5997 181.3599
1/4/2017 178.9798 176.8107 181.1489 175.6625 182.2971
1/5/2017 178.9798 176.3364 181.6232 174.937 183.0226
1/6/2017 178.9798 175.935 182.0246 174.3232 183.6364
1/9/2017 178.9798 175.5808 182.3789 173.7814 184.1782
1/10/2017 178.9798 175.2601 182.6996 173.291 184.6687
1/11/2017 178.9798 174.9649 182.9947 172.8396 185.1201
1/12/2017 178.9798 174.69 183.2696 172.4191 185.5405
1/13/2017 178.9798 174.4317 183.5279 172.0241 185.9355
1/17/2017 178.9798 174.1873 183.7723 171.6503 186.3093
1/18/2017 178.9798 173.9548 184.0048 171.2947 186.6649
1/19/2017 178.9798 173.7325 184.2271 170.9548 187.0048
1/20/2017 178.9798 173.5193 184.4403 170.6287 187.3309
1/23/2017 178.9798 173.3142 184.6455 170.3149 187.6447
1/24/2017 178.9798 173.1161 184.8435 170.0121 187.9475
1/25/2017 178.9798 172.9246 185.035 169.7192 188.2404
1/26/2017 178.9798 172.7389 185.2207 169.4352 188.5244
1/27/2017 178.9798 172.5587 185.401 169.1595 188.8001
1/30/2017 178.9798 172.3833 185.5763 168.8913 189.0683
1/31/2017 178.9798 172.2125 185.7472 168.63 189.3296
2/1/2017 178.9798 172.0458 185.9138 168.3752 189.5844
2/2/2017 178.9798 171.8831 186.0765 168.1264 189.8333
2/3/2017 178.9798 171.7241 186.2356 167.8831 190.0765
2/6/2017 178.9798 171.5684 186.3912 167.6451 190.3146
2/7/2017 178.9798 171.416 186.5436 167.4119 190.5477
2/8/2017 178.9798 171.2665 186.6931 167.1834 190.7762
2/9/2017 178.9798 171.12 186.8397 166.9592 191.0004
2/10/2017 178.9798 170.976 186.9836 166.7391 191.2205
2/13/2017 178.9798 170.8347 187.1249 166.5229 191.4367
2/14/2017 178.9798 170.6957 187.2639 166.3104 191.6492
2/15/2017 178.9798 170.5591 187.4006 166.1014 191.8582
2/16/2017 178.9798 170.4246 187.535 165.8957 192.0639
2/17/2017 178.9798 170.2922 187.6674 165.6932 192.2664
2/21/2017 178.9798 170.1618 187.7978 165.4938 192.4658
2/22/2017 178.9798 170.0333 187.9264 165.2973 192.6624
2/23/2017 178.9798 169.9066 188.053 165.1035 192.8561
2/24/2017 178.9798 169.7816 188.178 164.9124 193.0472
2/27/2017 178.9798 169.6584 188.3013 164.7239 193.2357
2/28/2017 178.9798 169.5367 188.4229 164.5378 193.4218
3/1/2017 178.9798 169.4166 188.543 164.3541 193.6055
3/2/2017 178.9798 169.298 188.6617 164.1727 193.7869
3/3/2017 178.9798 169.1808 188.7788 163.9935 193.9661
3/6/2017 178.9798 169.065 188.8946 163.8164 194.1432
3/7/2017 178.9798 168.9505 189.0091 163.6413 194.3183
3/8/2017 178.9798 168.8373 189.1223 163.4682 194.4914
3/9/2017 178.9798 168.7254 189.2342 163.2971 194.6626
3/10/2017 178.9798 168.6147 189.3449 163.1277 194.8319
3/13/2017 178.9798 168.5052 189.4545 162.9602 194.9994
3/14/2017 178.9798 168.3967 189.5629 162.7944 195.1652
3/15/2017 178.9798 168.2894 189.6702 162.6303 195.3293

> plot(forecast(fit, 50))

b. Double (With trend and without seasonal pattern)

Step 1: Assign HoltWinters to fit with gamma

> fit <- HoltWinters(Price, gamma = F)


> fit
Holt-Winters exponential smoothing with trend and without seasonal compone
nt.

Call:
HoltWinters(x = Price, gamma = F)

Smoothing parameters:
alpha: 0.9745618
beta : 0.02645347
gamma: FALSE

Coefficients:
[,1]
a 178.98518
b 0.12439

Step 2: Forecast and Plot data


> forecast(fit, 50)

Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
1/3/2017 179.1096 177.5298 180.6893 176.6936 181.5256
1/4/2017 179.234 176.9995 181.4684 175.8166 182.6513
1/5/2017 179.3584 176.5978 182.1189 175.1365 183.5802
1/6/2017 179.4827 176.2606 182.7049 174.555 184.4105
1/9/2017 179.6071 175.9629 183.2514 174.0338 185.1805
1/10/2017 179.7315 175.6918 183.7712 173.5534 185.9097
1/11/2017 179.8559 175.4399 184.272 173.1022 186.6097
1/12/2017 179.9803 175.2021 184.7585 172.6727 187.2879
1/13/2017 180.1047 174.9753 185.2341 172.26 187.9494
1/17/2017 180.2291 174.757 185.7012 171.8603 188.5979
1/18/2017 180.3535 174.5454 186.1616 171.4708 189.2362
1/19/2017 180.4779 174.3391 186.6166 171.0895 189.8663
1/20/2017 180.6023 174.1371 187.0674 170.7147 190.4898
1/23/2017 180.7266 173.9385 187.5148 170.3451 191.1082
1/24/2017 180.851 173.7426 187.9595 169.9796 191.7224
1/25/2017 180.9754 173.5488 188.402 169.6174 192.3334
1/26/2017 181.0998 173.3567 188.8429 169.2578 192.9419
1/27/2017 181.2242 173.1659 189.2826 168.9 193.5484
1/30/2017 181.3486 172.9759 189.7213 168.5437 194.1535
1/31/2017 181.473 172.7866 190.1594 168.1883 194.7577
2/1/2017 181.5974 172.5976 190.5971 167.8335 195.3613
2/2/2017 181.7218 172.4088 191.0347 167.4789 195.9647
2/3/2017 181.8462 172.22 191.4723 167.1243 196.5681
2/6/2017 181.9705 172.031 191.9101 166.7693 197.1718
2/7/2017 182.0949 171.8417 192.3482 166.4139 197.776
2/8/2017 182.2193 171.6518 192.7868 166.0578 198.3809
2/9/2017 182.3437 171.4615 193.226 165.7007 198.9867
2/10/2017 182.4681 171.2704 193.6658 165.3427 199.5935
2/13/2017 182.5925 171.0786 194.1064 164.9834 200.2015
2/14/2017 182.7169 170.8859 194.5479 164.6229 200.8108
2/15/2017 182.8413 170.6923 194.9903 164.261 201.4215
2/16/2017 182.9657 170.4977 195.4336 163.8976 202.0337
2/17/2017 183.0901 170.3021 195.878 163.5326 202.6475
2/21/2017 183.2144 170.1054 196.3234 163.166 203.2629
2/22/2017 183.3388 169.9076 196.77 162.7976 203.8801
2/23/2017 183.4632 169.7086 197.2178 162.4274 204.499
2/24/2017 183.5876 169.5085 197.6668 162.0554 205.1198
2/27/2017 183.712 169.307 198.117 161.6815 205.7425
2/28/2017 183.8364 169.1043 198.5685 161.3056 206.3672
3/1/2017 183.9608 168.9003 199.0213 160.9278 206.9938
3/2/2017 184.0852 168.695 199.4754 160.5479 207.6225
3/3/2017 184.2096 168.4883 199.9308 160.166 208.2532
3/6/2017 184.334 168.2803 200.3877 159.7819 208.886
3/7/2017 184.4583 168.0708 200.8459 159.3958 209.5209
3/8/2017 184.5827 167.86 201.3055 159.0075 210.1579
3/9/2017 184.7071 167.6478 201.7665 158.6171 210.7972
3/10/2017 184.8315 167.4341 202.2289 158.2245 211.4386
3/13/2017 184.9559 167.219 202.6928 157.8296 212.0822
3/14/2017 185.0803 167.0024 203.1582 157.4326 212.728
3/15/2017 185.2047 166.7844 203.625 157.0333 213.3761

> plot(forecast(fit, 50))

4. Moving Average Model MA(q)


a. MA(1)

> ma1 <- arima(Price, order = c(0,0,1))


> summary(ma1)
Call:
arima(x = Price, order = c(0, 0, 1))

Coefficients:
ma1 intercept
0.9751 103.3237
s.e. 0.0030 0.6840
sigma^2 estimated as 332.2: log likelihood = -11968.76, aic = 23943.52

Training set error measures:


ME RMSE MAE MPE MAPE MASE
ACF1
Training set 0.01718168 18.22748 15.52183 -5.709827 15.72028 17.42385 0.94
9294

b. MA(2)
> ma2 <- arima(Price, order = c(0,0,2))
> summary(ma2)
Call:
arima(x = Price, order = c(0, 0, 2))

Coefficients:
ma1 ma2 intercept
1.7445 0.9054 103.3685
s.e. 0.0077 0.0062 0.7159

sigma^2 estimated as 106.6: log likelihood = -10396.05, aic = 20800.09

Training set error measures:


ME RMSE MAE MPE MAPE MASE
ACF1
Training set 0.001646883 10.32486 8.575334 -3.100418 8.69319 9.626142 0.78
96925

c. MA(3)

> ma3 <- arima(Price, order = c(0,0,3))


> summary(ma3)
Call:
arima(x = Price, order = c(0, 0, 3))

Coefficients:
ma1 ma2 ma3 intercept
2.0810 1.9554 0.8032 103.3661
s.e. 0.0121 0.0136 0.0099 0.7560

sigma^2 estimated as 46.45: log likelihood = -9246.43, aic = 18502.87

Training set error measures:


ME RMSE MAE MPE MAPE MASE
ACF1
Training set 0.003985992 6.81561 5.532316 -1.936149 5.617873 6.210237 0.63
95904

d. MA(4)

> ma4 <- arima(Price, order = c(0,0,4))


> summary(ma4)
Call:
arima(x = Price, order = c(0, 0, 4))

Coefficients:
ma1 ma2 ma3 ma4 intercept
2.2910 2.7545 1.9376 0.6853 103.3758
s.e. 0.0169 0.0270 0.0212 0.0111 0.8198

sigma^2 estimated as 24.8: log likelihood = -8377.99, aic = 16767.98


Training set error measures:
ME RMSE MAE MPE MAPE MASE
ACF1
Training set 0.003347959 4.980156 3.974629 -1.304005 4.053247 4.461673 0.4
534592

We choose MA(4) to make forecast

> fcastma4 <- forecast(ma4)


> fcastma4
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
1/3/2017 171.0073 164.625 177.3896 161.2464 180.7682
1/4/2017 152.6127 136.6583 168.5671 128.2126 177.0129
1/5/2017 128.8316 105.0913 152.5719 92.52396 165.1392
1/6/2017 110.6977 83.92978 137.4657 69.75969 151.6358
1/9/2017 103.3758 76.2529 130.4987 61.89491 144.8566
1/10/2017 103.3758 76.2529 130.4987 61.89491 144.8566
1/11/2017 103.3758 76.2529 130.4987 61.89491 144.8566
1/12/2017 103.3758 76.2529 130.4987 61.89491 144.8566
1/13/2017 103.3758 76.2529 130.4987 61.89491 144.8566
1/17/2017 103.3758 76.2529 130.4987 61.89491 144.8566

> plot(fcastma4)

5. Autoregressive Model AR(p)


a. AR(1)

> ar1 <- arima(Price, order = c(1,0,0), method = "ML")


> summary(ar1)
Call:
arima(x = Price, order = c(1, 0, 0), method = "ML")

Coefficients:
ar1 intercept
0.9997 103.6602
s.e. 0.0004 45.6097

sigma^2 estimated as 1.477: log likelihood = -4472.34, aic = 8950.67

Training set error measures:


ME RMSE MAE MPE MAPE MASE
ACF1
Training set 0.03621249 1.215165 0.8909309 0.01699546 0.9377641 1.000104 -
0.02881937

b. AR(2)

> ar2 <- arima(Price, order = c(2,0,0), method = "ML")


> summary(ar2)
Call:
arima(x = Price, order = c(2, 0, 0), method = "ML")

Coefficients:
ar1 ar2 intercept
0.9718 0.0279 103.7314
s.e. 0.0190 0.0190 44.9552

sigma^2 estimated as 1.475: log likelihood = -4471.26, aic = 8950.51

Training set error measures:


ME RMSE MAE MPE MAPE MASE
ACF1
Training set 0.037198 1.214698 0.8906234 0.01754294 0.9378559 0.9997589 -0
.001425036

c. AR(3)

> ar3 <- arima(Price, order = c(3,0,0), method = "ML")


> summary(ar3)
Call:
arima(x = Price, order = c(3, 0, 0), method = "ML")

Coefficients:
ar1 ar2 ar3 intercept
0.9714 0.0109 0.0177 103.5145
s.e. 0.0190 0.0265 0.0190 NaN

sigma^2 estimated as 1.475: log likelihood = -4467.28, aic = 8944.55

Training set error measures:


ME RMSE MAE MPE MAPE MASE
ACF1
Training set 0.03816039 1.214344 0.8907145 0.02156871 0.9377347 0.9998613
-0.0009760125
 In this method, we choose the AR have the smallest AICs for forecasting. Thus, AR(3) has the
smallest AIC (8950.67 for AR(1) > 8950.51 for AR(2) > 8944.55 for AR(3))

> fcastar3 <- forecast(ar3)


> fcastar3

Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
1/3/2017 178.9875 177.4313 180.5437 176.6074 181.3676
1/4/2017 178.9726 176.8031 181.1422 175.6546 182.2907
1/5/2017 178.9706 176.3413 181.5999 174.9495 182.9918
1/6/2017 178.9683 175.9478 181.9889 174.3488 183.5878
1/9/2017 178.9658 175.599 182.3326 173.8167 184.1149
1/10/2017 178.9632 175.2826 182.6438 173.3342 184.5922
1/11/2017 178.9607 174.9911 182.9304 172.8897 185.0318
1/12/2017 178.9582 174.7192 183.1972 172.4752 185.4412
1/13/2017 178.9557 174.4634 183.4479 172.0854 185.8259
1/17/2017 178.9531 174.2212 183.685 171.7163 186.19
> plot(fcastar3)

6. ARMA(p,q)
a. ARMA(1,1)

> arma1 <- arima(Price, order = c(1,0,1), method = "ML")


> summary(arma1)
Call:
arima(x = as.numeric(Price), order = c(1, 0, 1), method = "ML")

Coefficients:
ar1 ma1 intercept
0.9997 -0.0289 103.6360
s.e. 0.0004 0.0193 44.9809

sigma^2 estimated as 1.475: log likelihood = -4471.22, aic = 8950.44

Training set error measures:


ME RMSE MAE MPE MAPE MASE
ACF1
Training set 0.03729644 1.214683 0.8906375 0.01761312 0.9378666 0.9997748
-0.0004564211

b. ARMA(2,2)

> arma2 <- arima(Price, order = c(2,0,2), method = "CSS")


> summary(arma2)
Call:
arima(x = Price, order = c(2, 0, 2), method = "CSS")

Coefficients:
ar1 ar2 ma1 ma2 intercept
0.2868 0.7136 0.6842 -0.0170 103.1956
s.e. 0.3672 0.3673 0.3680 0.0202 NaN

sigma^2 estimated as 1.474: part log likelihood = -4466.66

Training set error measures:


ME RMSE MAE MPE MAPE MASE A
CF1
Training set 0.03716937 1.213848 0.8889393 0.02326459 0.9362148 0.9978685 0.0001283
948

 As ARMA(2,2) has smaller MASE(0.9978685<0.9997748), we choose ARMA(2,2) to forecast.

> fcastarma <- forecast(arma2)


> fcastarma
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
1/3/2017 178.9903 177.4341 180.5465 176.6103 181.3703
1/4/2017 179.0092 176.8401 181.1782 175.6919 182.3264
1/5/2017 179.0192 176.3721 181.6662 174.9709 183.0675
1/6/2017 179.0355 175.9864 182.0846 174.3723 183.6986
1/9/2017 179.0473 175.6419 182.4527 173.8392 184.2554
1/10/2017 179.0623 175.3353 182.7894 173.3623 184.7624
1/11/2017 179.0751 175.0513 183.0989 172.9212 185.2289
1/12/2017 179.0895 174.7897 183.3892 172.5135 185.6654
1/13/2017 179.1027 174.5432 183.6621 172.1296 186.0758
1/17/2017 179.1167 174.3117 183.9218 171.7681 186.4654
> plot(forecast(arma2))
7. ARIMA(p,d,q)

Without Drift

a. ARIMA(0,1,0)

> fit1 <- arima(Price, order = c(0,1,0))


> fit1
Call:
arima(x = Price, order = c(0, 1, 0))

sigma^2 estimated as 1.477: log likelihood = -4467.18, aic = 8936.36

b. ARIMA(0,1,1)

> fit2 <- arima(Price, order = c(0,1,1))


> fit2
Call:
arima(x = Price, order = c(0, 1, 1))

Coefficients:
ma1
-0.0291
s.e. 0.0193

sigma^2 estimated as 1.476: log likelihood = -4466.04, aic = 8936.09

c. ARIMA(1,1,0)

> fit3 <- arima(Price, order = c(1,1,0))


> fit3
Call:
arima(x = Price, order = c(1, 1, 0))

Coefficients:
ar1
-0.0281
s.e. 0.0190

sigma^2 estimated as 1.476: log likelihood = -4466.08, aic = 8936.16

d. ARIMA(0,2,0)

> fit4 <- arima(Price, order = c(0,2,0))


> fit4
Call:
arima(x = Price, order = c(0, 2, 0))

sigma^2 estimated as 3.036: log likelihood = -5462.84, aic = 10927.68

e. ARIMA(2,1,1)

> fit5 <- arima(Price, order = c(2,1,1))


> fit5
Call:
arima(x = Price, order = c(2, 1, 1))

Coefficients:
ar1 ar2 ma1
-0.0904 -0.0196 0.0617
s.e. 0.5274 0.0240 0.5272

sigma^2 estimated as 1.475: log likelihood = -4465.64, aic = 8939.27

f. ARIMA(1,1,2)

> fit6 <- arima(Price, order = c(1,1,2))


> fit6
Call:
arima(x = Price, order = c(1, 1, 2))

Coefficients:
ar1 ma1 ma2
-0.0703 0.0418 -0.0183
s.e. 0.5633 0.5630 0.0241

sigma^2 estimated as 1.475: log likelihood = -4465.66, aic = 8939.32

g. ARIMA(0,2,2)

> fit7 <- arima(Price, order = c(0,2,2))


> fit7
Call:
arima(x = Price, order = c(0, 2, 2))

Coefficients:
ma1 ma2
-1.0282 0.0289
s.e. 0.0195 0.0193

sigma^2 estimated as 1.475: log likelihood = -4467.53, aic = 8941.05


h. ARIMA(0,2,1)

> fit8 <- arima(Price, order = c(0,2,1))


> fit8
Call:
arima(x = Price, order = c(0, 2, 1))

Coefficients:
ma1
-0.9993
s.e. 0.0019

sigma^2 estimated as 1.477: log likelihood = -4468.73, aic = 8941.45

i. ARIMA(2,1,0)

> fit9 <- arima(Price, order = c(2,1,0))


> fit9
Call:
arima(x = Price, order = c(2, 1, 0))

Coefficients:
ar1 ar2
-0.0286 -0.0178
s.e. 0.0190 0.0190

sigma^2 estimated as 1.475: log likelihood = -4465.65, aic = 8937.29

j. ARIMA(2,1,2)

> fit <- arima(Price, order = c(2,1,2))


> fit
Call:
arima(x = Price, order = c(2, 1, 2))

Coefficients:
ar1 ar2 ma1 ma2
-0.5762 -0.8798 0.5643 0.8531
s.e. 0.0411 NaN 0.0455 NaN

sigma^2 estimated as 1.472: log likelihood = -4462.98, aic = 8935.96

 Thus, ARIMA(0,1,1) is the best model with more accurate without drift as it has smallest
AIC(=8936.09)

With Drift

> arimamodelfit <- auto.arima(Price, approximation = FALSE, trace = FALSE)


> summary(arimamodelfit)
Series: Price
ARIMA(0,1,1) with drift

Coefficients:
ma1 drift
-0.0301 0.0366
s.e. 0.0193 0.0224

sigma^2 estimated as 1.475: log likelihood=-4464.71


AIC=8935.42 AICc=8935.43 BIC=8953.2
Training set error measures:
ME RMSE MAE MPE MAPE MA
SE ACF1
Training set 2.417142e-05 1.213921 0.8883417 -0.01882609 0.9361825 0.99719
76 0.000570219

8. TBATS Method

> Tbats13 <- tbats(Price)


> Tbats13
BATS(0.797, {0,0}, -, -)

Call: tbats(y = Price)

Parameters
Lambda: 0.796644
Alpha: 0.9740243

Seed States:
[,1]
[1,] 38.57155
attr(,"lambda")
[1] 0.7966441

Sigma: 0.4756216
AIC: 22999.25
> checkresiduals(Tbats13)

Ljung-Box test

data: Residuals from BATS


Q* = 10.03, df = 7, p-value = 0.1869

Model df: 3. Total lags used: 10


> plot(forecast(Tbats13))
CONCLUSION
Forecasting time series data is a problem encountered a lot in real life. Mastering techniques for
analyzing and solving the given time series data is a good method based on the fact that time series
data are often difficult to identify because of their characteristics and the processes are linear or
non-linear, especially for large, complex data.

The procedures described in this report is general and includes the steps required to apply in time
series data forecasting. In the forthcoming future, we will be able to develop a software that applies
the techniques and performs the evaluation of the results obtained when applying stock exchange
on some time series data sets.

You might also like