Data Science Posts and Resources

Articles on Data Science

Time-Series Forecasting

Introduction to time-series forecasting

Laxmi K Soni

23-Minute Read

What is time series?

A series of observations collected during some time intervals in know as time series. The observations which are collected are primarily dependent on the time at which it is collected. The sale of the items in a retail store like Walmart will be a time series. The periodicity of the the sale could be at daily level or weekly level. The number of people flying from Bangalore to Delhi on daily basis is a time series. Time-series forecasting uses historical time based data to forecast a variable for example

  • At Supermarket the demand for a grocery product each day
  • Count of the downloads of an App in a country for the coming month
  • Product sales in units sold each day in a retail store
  • Yearly unemployment rate
  • The closing price of a stock
  • Expected yearly yield of a crop
  • Population growth in a country
  • Daily demand for a bus/airline/train

Time series and a normal series?

Time component is important in time series. The time series is primarily dependent on the time. But a normal series say 1, 2, 3…100 has no time component to it. When a value that a series will take depends on the time it was recorded, it is called as time series.

How to define a time series object using R language

The function ts() helps to create time-series objects. As.ts() and is.ts() coerce an object to a time-series and test whether an object is a time series or not for example:

Frequency

The count of number of observations per unit of time is called as frequency. Frequency can further be distinguished as Frequency for yearly data is 1, Frequency for weekly data is 7, Frequency for quarterly data is 4 and so on…

Some useful softwares

  1. forecast: For forecasting functions

  2. tseries: For unit root tests and GARC models

  3. Mcomp: Time series data from forecasting competitions

  4. fma: For data

  5. expsmooth: For data

  6. fpp: For data

  7. ARIMA

  8. prophet

  9. SPSS

prophet

Prophet represents a time-series as a combination of trend, seasonality and holidays. This decomposed time series model can be represented by the following equation:

Capture10

where:

  • g(t) defines the trend function that models the non-periodic changes in the time series

  • s(t) defines the periodic changes (e.g., weekly and yearly seasonality)

  • h(t) means the effect of holidays

  • εt: is the error notation representing information that was missed by the model

Trend

Prophet provides two variants for the trend function, g(t) which are logistic growth modeling non-linear growth and constant rate of growth or Piece wise Linear Model.

Seasonality

s(t) is called as the seasonal component which allows to flexibly model the periodic changes due to weekly and yearly seasonability. For example, a 5-day work week can produce effects on a time series that repeat each week, while vacation schedules and school breaks can produce effects that repeat each year.

Holidays and Events

There are events which can provide predictable outcome to business. These events are holidays or any other important ocasion such as black friday.Prophet provides list of such past and future events for modeling into time-series component.

library(Quandl)
library(quantmod)
library(prophet)

Quandl.api_key("YBM9uPgpnsaDYPkAn539")

#daily_gold = Quandl("WGC/GOLD_DAILY_INR",collapse="daily",start_date="2000-01-01",end_date = "2020-08-11",type="raw")


daily_gold = Quandl("LBMA/SILVER",collapse="daily",start_date="2020-01-01",end_date = "2020-08-12",type="raw")

keeps <- c("Date", "USD")

daily_gold = daily_gold[keeps]

names(daily_gold) <- c('ds','y')



daily_gold[2] <- daily_gold[2] 

model = prophet(daily_gold)

future <- make_future_dataframe(model, periods = 10)


forecast <- predict(model, future)

print(forecast)
##             ds    trend additive_terms additive_terms_lower
## 1   2020-01-02 18.63425     0.16652525           0.16652525
## 2   2020-01-03 18.59643     0.18912739           0.18912739
## 3   2020-01-06 18.48298     0.11055330           0.11055330
## 4   2020-01-07 18.44516     0.09853439           0.09853439
## 5   2020-01-08 18.40734     0.21000328           0.21000328
## 6   2020-01-09 18.36952     0.16652525           0.16652525
## 7   2020-01-10 18.33170     0.18912739           0.18912739
## 8   2020-01-13 18.21824     0.11055330           0.11055330
## 9   2020-01-14 18.18042     0.09853439           0.09853439
## 10  2020-01-15 18.14260     0.21000328           0.21000328
## 11  2020-01-16 18.10478     0.16652525           0.16652525
## 12  2020-01-17 18.06696     0.18912739           0.18912739
## 13  2020-01-20 17.95350     0.11055330           0.11055330
## 14  2020-01-21 17.91568     0.09853439           0.09853439
## 15  2020-01-22 17.87786     0.21000328           0.21000328
## 16  2020-01-23 17.84004     0.16652525           0.16652525
## 17  2020-01-24 17.80222     0.18912739           0.18912739
## 18  2020-01-27 17.68877     0.11055330           0.11055330
## 19  2020-01-28 17.65095     0.09853439           0.09853439
## 20  2020-01-29 17.61313     0.21000328           0.21000328
## 21  2020-01-30 17.57531     0.16652525           0.16652525
## 22  2020-01-31 17.53749     0.18912739           0.18912739
## 23  2020-02-03 17.42403     0.11055330           0.11055330
## 24  2020-02-04 17.38621     0.09853439           0.09853439
## 25  2020-02-05 17.34839     0.21000328           0.21000328
## 26  2020-02-06 17.31057     0.16652525           0.16652525
## 27  2020-02-07 17.27275     0.18912739           0.18912739
## 28  2020-02-10 17.15928     0.11055330           0.11055330
## 29  2020-02-11 17.12145     0.09853439           0.09853439
## 30  2020-02-12 17.08363     0.21000328           0.21000328
## 31  2020-02-13 17.04580     0.16652525           0.16652525
## 32  2020-02-14 16.99846     0.18912739           0.18912739
## 33  2020-02-17 16.85644     0.11055330           0.11055330
## 34  2020-02-18 16.80910     0.09853439           0.09853439
## 35  2020-02-19 16.76176     0.21000328           0.21000328
## 36  2020-02-20 16.70505     0.16652525           0.16652525
## 37  2020-02-21 16.64833     0.18912739           0.18912739
## 38  2020-02-24 16.47819     0.11055330           0.11055330
## 39  2020-02-25 16.42147     0.09853439           0.09853439
## 40  2020-02-26 16.36475     0.21000328           0.21000328
## 41  2020-02-27 16.30802     0.16652525           0.16652525
## 42  2020-02-28 16.25129     0.18912739           0.18912739
## 43  2020-03-02 16.08110     0.11055330           0.11055330
## 44  2020-03-03 16.02437     0.09853439           0.09853439
## 45  2020-03-04 15.96764     0.21000328           0.21000328
## 46  2020-03-05 15.91091     0.16652525           0.16652525
## 47  2020-03-06 15.85418     0.18912739           0.18912739
## 48  2020-03-09 15.68398     0.11055330           0.11055330
## 49  2020-03-10 15.62725     0.09853439           0.09853439
## 50  2020-03-11 15.57052     0.21000328           0.21000328
## 51  2020-03-12 15.51379     0.16652525           0.16652525
## 52  2020-03-13 15.45706     0.18912739           0.18912739
## 53  2020-03-16 15.28687     0.11055330           0.11055330
## 54  2020-03-17 15.23013     0.09853439           0.09853439
## 55  2020-03-18 15.17340     0.21000328           0.21000328
## 56  2020-03-19 15.11667     0.16652525           0.16652525
## 57  2020-03-20 15.05994     0.18912739           0.18912739
## 58  2020-03-23 14.88975     0.11055330           0.11055330
## 59  2020-03-24 14.83302     0.09853439           0.09853439
## 60  2020-03-25 14.77629     0.21000328           0.21000328
## 61  2020-03-26 14.75111     0.16652525           0.16652525
## 62  2020-03-27 14.72594     0.18912739           0.18912739
## 63  2020-03-30 14.65042     0.11055330           0.11055330
## 64  2020-03-31 14.62525     0.09853439           0.09853439
## 65  2020-04-01 14.60007     0.21000328           0.21000328
## 66  2020-04-02 14.61159     0.16652525           0.16652525
## 67  2020-04-03 14.62310     0.18912739           0.18912739
## 68  2020-04-06 14.65764     0.11055330           0.11055330
## 69  2020-04-07 14.66916     0.09853439           0.09853439
## 70  2020-04-08 14.68067     0.21000328           0.21000328
## 71  2020-04-09 14.71275     0.16652525           0.16652525
## 72  2020-04-14 14.87316     0.09853439           0.09853439
## 73  2020-04-15 14.90525     0.21000328           0.21000328
## 74  2020-04-16 14.93733     0.16652525           0.16652525
## 75  2020-04-17 14.96941     0.18912739           0.18912739
## 76  2020-04-20 15.07333     0.11055330           0.11055330
## 77  2020-04-21 15.10797     0.09853439           0.09853439
## 78  2020-04-22 15.14261     0.21000328           0.21000328
## 79  2020-04-23 15.17725     0.16652525           0.16652525
## 80  2020-04-24 15.21189     0.18912739           0.18912739
## 81  2020-04-27 15.32133     0.11055330           0.11055330
## 82  2020-04-28 15.35781     0.09853439           0.09853439
## 83  2020-04-29 15.39429     0.21000328           0.21000328
## 84  2020-04-30 15.43077     0.16652525           0.16652525
## 85  2020-05-01 15.46725     0.18912739           0.18912739
## 86  2020-05-04 15.57725     0.11055330           0.11055330
## 87  2020-05-05 15.61392     0.09853439           0.09853439
## 88  2020-05-06 15.65059     0.21000328           0.21000328
## 89  2020-05-07 15.68725     0.16652525           0.16652525
## 90  2020-05-11 15.83392     0.11055330           0.11055330
## 91  2020-05-12 15.87059     0.09853439           0.09853439
## 92  2020-05-13 15.90726     0.21000328           0.21000328
## 93  2020-05-14 15.94392     0.16652525           0.16652525
## 94  2020-05-15 15.98059     0.18912739           0.18912739
## 95  2020-05-18 16.09059     0.11055330           0.11055330
## 96  2020-05-19 16.12726     0.09853439           0.09853439
## 97  2020-05-20 16.16393     0.21000328           0.21000328
## 98  2020-05-21 16.20059     0.16652525           0.16652525
## 99  2020-05-22 16.23726     0.18912739           0.18912739
## 100 2020-05-26 16.38393     0.09853439           0.09853439
## 101 2020-05-27 16.42060     0.21000328           0.21000328
## 102 2020-05-28 16.45726     0.16652525           0.16652525
## 103 2020-05-29 16.49393     0.18912739           0.18912739
## 104 2020-06-01 16.60393     0.11055330           0.11055330
## 105 2020-06-02 16.64060     0.09853439           0.09853439
## 106 2020-06-03 16.67727     0.21000328           0.21000328
## 107 2020-06-04 16.71393     0.16652525           0.16652525
## 108 2020-06-05 16.75060     0.18912739           0.18912739
## 109 2020-06-08 16.86060     0.11055330           0.11055330
## 110 2020-06-09 16.89727     0.09853439           0.09853439
## 111 2020-06-10 16.93393     0.21000328           0.21000328
## 112 2020-06-11 16.97060     0.16652525           0.16652525
## 113 2020-06-12 17.00727     0.18912739           0.18912739
## 114 2020-06-15 17.11727     0.11055330           0.11055330
## 115 2020-06-16 17.15394     0.09853439           0.09853439
## 116 2020-06-17 17.19060     0.21000328           0.21000328
## 117 2020-06-18 17.22727     0.16652525           0.16652525
## 118 2020-06-19 17.26394     0.18912739           0.18912739
## 119 2020-06-22 17.37394     0.11055330           0.11055330
## 120 2020-06-23 17.45752     0.09853439           0.09853439
## 121 2020-06-24 17.54110     0.21000328           0.21000328
## 122 2020-06-25 17.62467     0.16652525           0.16652525
## 123 2020-06-26 17.70825     0.18912739           0.18912739
## 124 2020-06-29 17.95899     0.11055330           0.11055330
## 125 2020-06-30 18.14085     0.09853439           0.09853439
## 126 2020-07-01 18.32271     0.21000328           0.21000328
## 127 2020-07-02 18.50458     0.16652525           0.16652525
## 128 2020-07-03 18.68644     0.18912739           0.18912739
## 129 2020-07-06 19.23203     0.11055330           0.11055330
## 130 2020-07-07 19.41389     0.09853439           0.09853439
## 131 2020-07-08 19.59575     0.21000328           0.21000328
## 132 2020-07-09 19.77761     0.16652525           0.16652525
## 133 2020-07-10 19.95948     0.18912739           0.18912739
## 134 2020-07-13 20.50507     0.11055330           0.11055330
## 135 2020-07-14 20.68693     0.09853439           0.09853439
## 136 2020-07-15 20.86879     0.21000328           0.21000328
## 137 2020-07-16 21.05065     0.16652525           0.16652525
## 138 2020-07-17 21.23252     0.18912739           0.18912739
## 139 2020-07-20 21.77810     0.11055330           0.11055330
## 140 2020-07-21 21.95997     0.09853439           0.09853439
## 141 2020-07-22 22.14183     0.21000328           0.21000328
## 142 2020-07-23 22.32369     0.16652525           0.16652525
## 143 2020-07-24 22.50555     0.18912739           0.18912739
## 144 2020-07-27 23.05114     0.11055330           0.11055330
## 145 2020-07-28 23.23300     0.09853439           0.09853439
## 146 2020-07-29 23.41487     0.21000328           0.21000328
## 147 2020-07-30 23.59673     0.16652525           0.16652525
## 148 2020-07-31 23.77859     0.18912739           0.18912739
## 149 2020-08-03 24.32418     0.11055330           0.11055330
## 150 2020-08-04 24.50604     0.09853439           0.09853439
## 151 2020-08-05 24.68791     0.21000328           0.21000328
## 152 2020-08-06 24.86977     0.16652525           0.16652525
## 153 2020-08-07 25.05163     0.18912739           0.18912739
## 154 2020-08-10 25.59722     0.11055330           0.11055330
## 155 2020-08-11 25.77908     0.09853439           0.09853439
## 156 2020-08-12 25.96094     0.21000328           0.21000328
## 157 2020-08-13 26.14281     0.16652525           0.16652525
## 158 2020-08-14 26.32467     0.18912739           0.18912739
## 159 2020-08-15 26.50653    -0.38737181          -0.38737181
## 160 2020-08-16 26.68839    -0.38737181          -0.38737181
## 161 2020-08-17 26.87026     0.11055330           0.11055330
## 162 2020-08-18 27.05212     0.09853439           0.09853439
## 163 2020-08-19 27.23398     0.21000328           0.21000328
## 164 2020-08-20 27.41585     0.16652525           0.16652525
## 165 2020-08-21 27.59771     0.18912739           0.18912739
## 166 2020-08-22 27.77957    -0.38737181          -0.38737181
##     additive_terms_upper      weekly weekly_lower weekly_upper
## 1             0.16652525  0.16652525   0.16652525   0.16652525
## 2             0.18912739  0.18912739   0.18912739   0.18912739
## 3             0.11055330  0.11055330   0.11055330   0.11055330
## 4             0.09853439  0.09853439   0.09853439   0.09853439
## 5             0.21000328  0.21000328   0.21000328   0.21000328
## 6             0.16652525  0.16652525   0.16652525   0.16652525
## 7             0.18912739  0.18912739   0.18912739   0.18912739
## 8             0.11055330  0.11055330   0.11055330   0.11055330
## 9             0.09853439  0.09853439   0.09853439   0.09853439
## 10            0.21000328  0.21000328   0.21000328   0.21000328
## 11            0.16652525  0.16652525   0.16652525   0.16652525
## 12            0.18912739  0.18912739   0.18912739   0.18912739
## 13            0.11055330  0.11055330   0.11055330   0.11055330
## 14            0.09853439  0.09853439   0.09853439   0.09853439
## 15            0.21000328  0.21000328   0.21000328   0.21000328
## 16            0.16652525  0.16652525   0.16652525   0.16652525
## 17            0.18912739  0.18912739   0.18912739   0.18912739
## 18            0.11055330  0.11055330   0.11055330   0.11055330
## 19            0.09853439  0.09853439   0.09853439   0.09853439
## 20            0.21000328  0.21000328   0.21000328   0.21000328
## 21            0.16652525  0.16652525   0.16652525   0.16652525
## 22            0.18912739  0.18912739   0.18912739   0.18912739
## 23            0.11055330  0.11055330   0.11055330   0.11055330
## 24            0.09853439  0.09853439   0.09853439   0.09853439
## 25            0.21000328  0.21000328   0.21000328   0.21000328
## 26            0.16652525  0.16652525   0.16652525   0.16652525
## 27            0.18912739  0.18912739   0.18912739   0.18912739
## 28            0.11055330  0.11055330   0.11055330   0.11055330
## 29            0.09853439  0.09853439   0.09853439   0.09853439
## 30            0.21000328  0.21000328   0.21000328   0.21000328
## 31            0.16652525  0.16652525   0.16652525   0.16652525
## 32            0.18912739  0.18912739   0.18912739   0.18912739
## 33            0.11055330  0.11055330   0.11055330   0.11055330
## 34            0.09853439  0.09853439   0.09853439   0.09853439
## 35            0.21000328  0.21000328   0.21000328   0.21000328
## 36            0.16652525  0.16652525   0.16652525   0.16652525
## 37            0.18912739  0.18912739   0.18912739   0.18912739
## 38            0.11055330  0.11055330   0.11055330   0.11055330
## 39            0.09853439  0.09853439   0.09853439   0.09853439
## 40            0.21000328  0.21000328   0.21000328   0.21000328
## 41            0.16652525  0.16652525   0.16652525   0.16652525
## 42            0.18912739  0.18912739   0.18912739   0.18912739
## 43            0.11055330  0.11055330   0.11055330   0.11055330
## 44            0.09853439  0.09853439   0.09853439   0.09853439
## 45            0.21000328  0.21000328   0.21000328   0.21000328
## 46            0.16652525  0.16652525   0.16652525   0.16652525
## 47            0.18912739  0.18912739   0.18912739   0.18912739
## 48            0.11055330  0.11055330   0.11055330   0.11055330
## 49            0.09853439  0.09853439   0.09853439   0.09853439
## 50            0.21000328  0.21000328   0.21000328   0.21000328
## 51            0.16652525  0.16652525   0.16652525   0.16652525
## 52            0.18912739  0.18912739   0.18912739   0.18912739
## 53            0.11055330  0.11055330   0.11055330   0.11055330
## 54            0.09853439  0.09853439   0.09853439   0.09853439
## 55            0.21000328  0.21000328   0.21000328   0.21000328
## 56            0.16652525  0.16652525   0.16652525   0.16652525
## 57            0.18912739  0.18912739   0.18912739   0.18912739
## 58            0.11055330  0.11055330   0.11055330   0.11055330
## 59            0.09853439  0.09853439   0.09853439   0.09853439
## 60            0.21000328  0.21000328   0.21000328   0.21000328
## 61            0.16652525  0.16652525   0.16652525   0.16652525
## 62            0.18912739  0.18912739   0.18912739   0.18912739
## 63            0.11055330  0.11055330   0.11055330   0.11055330
## 64            0.09853439  0.09853439   0.09853439   0.09853439
## 65            0.21000328  0.21000328   0.21000328   0.21000328
## 66            0.16652525  0.16652525   0.16652525   0.16652525
## 67            0.18912739  0.18912739   0.18912739   0.18912739
## 68            0.11055330  0.11055330   0.11055330   0.11055330
## 69            0.09853439  0.09853439   0.09853439   0.09853439
## 70            0.21000328  0.21000328   0.21000328   0.21000328
## 71            0.16652525  0.16652525   0.16652525   0.16652525
## 72            0.09853439  0.09853439   0.09853439   0.09853439
## 73            0.21000328  0.21000328   0.21000328   0.21000328
## 74            0.16652525  0.16652525   0.16652525   0.16652525
## 75            0.18912739  0.18912739   0.18912739   0.18912739
## 76            0.11055330  0.11055330   0.11055330   0.11055330
## 77            0.09853439  0.09853439   0.09853439   0.09853439
## 78            0.21000328  0.21000328   0.21000328   0.21000328
## 79            0.16652525  0.16652525   0.16652525   0.16652525
## 80            0.18912739  0.18912739   0.18912739   0.18912739
## 81            0.11055330  0.11055330   0.11055330   0.11055330
## 82            0.09853439  0.09853439   0.09853439   0.09853439
## 83            0.21000328  0.21000328   0.21000328   0.21000328
## 84            0.16652525  0.16652525   0.16652525   0.16652525
## 85            0.18912739  0.18912739   0.18912739   0.18912739
## 86            0.11055330  0.11055330   0.11055330   0.11055330
## 87            0.09853439  0.09853439   0.09853439   0.09853439
## 88            0.21000328  0.21000328   0.21000328   0.21000328
## 89            0.16652525  0.16652525   0.16652525   0.16652525
## 90            0.11055330  0.11055330   0.11055330   0.11055330
## 91            0.09853439  0.09853439   0.09853439   0.09853439
## 92            0.21000328  0.21000328   0.21000328   0.21000328
## 93            0.16652525  0.16652525   0.16652525   0.16652525
## 94            0.18912739  0.18912739   0.18912739   0.18912739
## 95            0.11055330  0.11055330   0.11055330   0.11055330
## 96            0.09853439  0.09853439   0.09853439   0.09853439
## 97            0.21000328  0.21000328   0.21000328   0.21000328
## 98            0.16652525  0.16652525   0.16652525   0.16652525
## 99            0.18912739  0.18912739   0.18912739   0.18912739
## 100           0.09853439  0.09853439   0.09853439   0.09853439
## 101           0.21000328  0.21000328   0.21000328   0.21000328
## 102           0.16652525  0.16652525   0.16652525   0.16652525
## 103           0.18912739  0.18912739   0.18912739   0.18912739
## 104           0.11055330  0.11055330   0.11055330   0.11055330
## 105           0.09853439  0.09853439   0.09853439   0.09853439
## 106           0.21000328  0.21000328   0.21000328   0.21000328
## 107           0.16652525  0.16652525   0.16652525   0.16652525
## 108           0.18912739  0.18912739   0.18912739   0.18912739
## 109           0.11055330  0.11055330   0.11055330   0.11055330
## 110           0.09853439  0.09853439   0.09853439   0.09853439
## 111           0.21000328  0.21000328   0.21000328   0.21000328
## 112           0.16652525  0.16652525   0.16652525   0.16652525
## 113           0.18912739  0.18912739   0.18912739   0.18912739
## 114           0.11055330  0.11055330   0.11055330   0.11055330
## 115           0.09853439  0.09853439   0.09853439   0.09853439
## 116           0.21000328  0.21000328   0.21000328   0.21000328
## 117           0.16652525  0.16652525   0.16652525   0.16652525
## 118           0.18912739  0.18912739   0.18912739   0.18912739
## 119           0.11055330  0.11055330   0.11055330   0.11055330
## 120           0.09853439  0.09853439   0.09853439   0.09853439
## 121           0.21000328  0.21000328   0.21000328   0.21000328
## 122           0.16652525  0.16652525   0.16652525   0.16652525
## 123           0.18912739  0.18912739   0.18912739   0.18912739
## 124           0.11055330  0.11055330   0.11055330   0.11055330
## 125           0.09853439  0.09853439   0.09853439   0.09853439
## 126           0.21000328  0.21000328   0.21000328   0.21000328
## 127           0.16652525  0.16652525   0.16652525   0.16652525
## 128           0.18912739  0.18912739   0.18912739   0.18912739
## 129           0.11055330  0.11055330   0.11055330   0.11055330
## 130           0.09853439  0.09853439   0.09853439   0.09853439
## 131           0.21000328  0.21000328   0.21000328   0.21000328
## 132           0.16652525  0.16652525   0.16652525   0.16652525
## 133           0.18912739  0.18912739   0.18912739   0.18912739
## 134           0.11055330  0.11055330   0.11055330   0.11055330
## 135           0.09853439  0.09853439   0.09853439   0.09853439
## 136           0.21000328  0.21000328   0.21000328   0.21000328
## 137           0.16652525  0.16652525   0.16652525   0.16652525
## 138           0.18912739  0.18912739   0.18912739   0.18912739
## 139           0.11055330  0.11055330   0.11055330   0.11055330
## 140           0.09853439  0.09853439   0.09853439   0.09853439
## 141           0.21000328  0.21000328   0.21000328   0.21000328
## 142           0.16652525  0.16652525   0.16652525   0.16652525
## 143           0.18912739  0.18912739   0.18912739   0.18912739
## 144           0.11055330  0.11055330   0.11055330   0.11055330
## 145           0.09853439  0.09853439   0.09853439   0.09853439
## 146           0.21000328  0.21000328   0.21000328   0.21000328
## 147           0.16652525  0.16652525   0.16652525   0.16652525
## 148           0.18912739  0.18912739   0.18912739   0.18912739
## 149           0.11055330  0.11055330   0.11055330   0.11055330
## 150           0.09853439  0.09853439   0.09853439   0.09853439
## 151           0.21000328  0.21000328   0.21000328   0.21000328
## 152           0.16652525  0.16652525   0.16652525   0.16652525
## 153           0.18912739  0.18912739   0.18912739   0.18912739
## 154           0.11055330  0.11055330   0.11055330   0.11055330
## 155           0.09853439  0.09853439   0.09853439   0.09853439
## 156           0.21000328  0.21000328   0.21000328   0.21000328
## 157           0.16652525  0.16652525   0.16652525   0.16652525
## 158           0.18912739  0.18912739   0.18912739   0.18912739
## 159          -0.38737181 -0.38737181  -0.38737181  -0.38737181
## 160          -0.38737181 -0.38737181  -0.38737181  -0.38737181
## 161           0.11055330  0.11055330   0.11055330   0.11055330
## 162           0.09853439  0.09853439   0.09853439   0.09853439
## 163           0.21000328  0.21000328   0.21000328   0.21000328
## 164           0.16652525  0.16652525   0.16652525   0.16652525
## 165           0.18912739  0.18912739   0.18912739   0.18912739
## 166          -0.38737181 -0.38737181  -0.38737181  -0.38737181
##     multiplicative_terms multiplicative_terms_lower multiplicative_terms_upper
## 1                      0                          0                          0
## 2                      0                          0                          0
## 3                      0                          0                          0
## 4                      0                          0                          0
## 5                      0                          0                          0
## 6                      0                          0                          0
## 7                      0                          0                          0
## 8                      0                          0                          0
## 9                      0                          0                          0
## 10                     0                          0                          0
## 11                     0                          0                          0
## 12                     0                          0                          0
## 13                     0                          0                          0
## 14                     0                          0                          0
## 15                     0                          0                          0
## 16                     0                          0                          0
## 17                     0                          0                          0
## 18                     0                          0                          0
## 19                     0                          0                          0
## 20                     0                          0                          0
## 21                     0                          0                          0
## 22                     0                          0                          0
## 23                     0                          0                          0
## 24                     0                          0                          0
## 25                     0                          0                          0
## 26                     0                          0                          0
## 27                     0                          0                          0
## 28                     0                          0                          0
## 29                     0                          0                          0
## 30                     0                          0                          0
## 31                     0                          0                          0
## 32                     0                          0                          0
## 33                     0                          0                          0
## 34                     0                          0                          0
## 35                     0                          0                          0
## 36                     0                          0                          0
## 37                     0                          0                          0
## 38                     0                          0                          0
## 39                     0                          0                          0
## 40                     0                          0                          0
## 41                     0                          0                          0
## 42                     0                          0                          0
## 43                     0                          0                          0
## 44                     0                          0                          0
## 45                     0                          0                          0
## 46                     0                          0                          0
## 47                     0                          0                          0
## 48                     0                          0                          0
## 49                     0                          0                          0
## 50                     0                          0                          0
## 51                     0                          0                          0
## 52                     0                          0                          0
## 53                     0                          0                          0
## 54                     0                          0                          0
## 55                     0                          0                          0
## 56                     0                          0                          0
## 57                     0                          0                          0
## 58                     0                          0                          0
## 59                     0                          0                          0
## 60                     0                          0                          0
## 61                     0                          0                          0
## 62                     0                          0                          0
## 63                     0                          0                          0
## 64                     0                          0                          0
## 65                     0                          0                          0
## 66                     0                          0                          0
## 67                     0                          0                          0
## 68                     0                          0                          0
## 69                     0                          0                          0
## 70                     0                          0                          0
## 71                     0                          0                          0
## 72                     0                          0                          0
## 73                     0                          0                          0
## 74                     0                          0                          0
## 75                     0                          0                          0
## 76                     0                          0                          0
## 77                     0                          0                          0
## 78                     0                          0                          0
## 79                     0                          0                          0
## 80                     0                          0                          0
## 81                     0                          0                          0
## 82                     0                          0                          0
## 83                     0                          0                          0
## 84                     0                          0                          0
## 85                     0                          0                          0
## 86                     0                          0                          0
## 87                     0                          0                          0
## 88                     0                          0                          0
## 89                     0                          0                          0
## 90                     0                          0                          0
## 91                     0                          0                          0
## 92                     0                          0                          0
## 93                     0                          0                          0
## 94                     0                          0                          0
## 95                     0                          0                          0
## 96                     0                          0                          0
## 97                     0                          0                          0
## 98                     0                          0                          0
## 99                     0                          0                          0
## 100                    0                          0                          0
## 101                    0                          0                          0
## 102                    0                          0                          0
## 103                    0                          0                          0
## 104                    0                          0                          0
## 105                    0                          0                          0
## 106                    0                          0                          0
## 107                    0                          0                          0
## 108                    0                          0                          0
## 109                    0                          0                          0
## 110                    0                          0                          0
## 111                    0                          0                          0
## 112                    0                          0                          0
## 113                    0                          0                          0
## 114                    0                          0                          0
## 115                    0                          0                          0
## 116                    0                          0                          0
## 117                    0                          0                          0
## 118                    0                          0                          0
## 119                    0                          0                          0
## 120                    0                          0                          0
## 121                    0                          0                          0
## 122                    0                          0                          0
## 123                    0                          0                          0
## 124                    0                          0                          0
## 125                    0                          0                          0
## 126                    0                          0                          0
## 127                    0                          0                          0
## 128                    0                          0                          0
## 129                    0                          0                          0
## 130                    0                          0                          0
## 131                    0                          0                          0
## 132                    0                          0                          0
## 133                    0                          0                          0
## 134                    0                          0                          0
## 135                    0                          0                          0
## 136                    0                          0                          0
## 137                    0                          0                          0
## 138                    0                          0                          0
## 139                    0                          0                          0
## 140                    0                          0                          0
## 141                    0                          0                          0
## 142                    0                          0                          0
## 143                    0                          0                          0
## 144                    0                          0                          0
## 145                    0                          0                          0
## 146                    0                          0                          0
## 147                    0                          0                          0
## 148                    0                          0                          0
## 149                    0                          0                          0
## 150                    0                          0                          0
## 151                    0                          0                          0
## 152                    0                          0                          0
## 153                    0                          0                          0
## 154                    0                          0                          0
## 155                    0                          0                          0
## 156                    0                          0                          0
## 157                    0                          0                          0
## 158                    0                          0                          0
## 159                    0                          0                          0
## 160                    0                          0                          0
## 161                    0                          0                          0
## 162                    0                          0                          0
## 163                    0                          0                          0
## 164                    0                          0                          0
## 165                    0                          0                          0
## 166                    0                          0                          0
##     yhat_lower yhat_upper trend_lower trend_upper     yhat
## 1     17.39110   20.25923    18.63425    18.63425 18.80078
## 2     17.45216   20.19620    18.59643    18.59643 18.78556
## 3     17.20842   19.90242    18.48298    18.48298 18.59353
## 4     17.04965   19.88040    18.44516    18.44516 18.54369
## 5     17.22148   19.95720    18.40734    18.40734 18.61734
## 6     17.25028   19.89888    18.36952    18.36952 18.53604
## 7     17.07936   19.91734    18.33170    18.33170 18.52082
## 8     16.85058   19.69068    18.21824    18.21824 18.32879
## 9     16.79554   19.62736    18.18042    18.18042 18.27895
## 10    16.91489   19.76443    18.14260    18.14260 18.35260
## 11    16.98485   19.65750    18.10478    18.10478 18.27131
## 12    17.07376   19.73210    18.06696    18.06696 18.25609
## 13    16.66025   19.54494    17.95350    17.95350 18.06406
## 14    16.61562   19.40317    17.91568    17.91568 18.01422
## 15    16.74405   19.48219    17.87786    17.87786 18.08787
## 16    16.63800   19.28093    17.84004    17.84004 18.00657
## 17    16.45650   19.35138    17.80222    17.80222 17.99135
## 18    16.41490   19.11742    17.68877    17.68877 17.79932
## 19    16.30239   19.19076    17.65095    17.65095 17.74948
## 20    16.49555   19.21424    17.61313    17.61313 17.82313
## 21    16.44063   19.12669    17.57531    17.57531 17.74183
## 22    16.33698   19.13289    17.53749    17.53749 17.72662
## 23    16.20231   18.92205    17.42403    17.42403 17.53458
## 24    16.08889   19.03030    17.38621    17.38621 17.48474
## 25    16.13654   18.98537    17.34839    17.34839 17.55839
## 26    16.10070   18.82362    17.31057    17.31057 17.47710
## 27    16.05406   18.88891    17.27275    17.27275 17.46187
## 28    15.74754   18.68187    17.15928    17.15928 17.26983
## 29    15.80537   18.63200    17.12145    17.12145 17.21999
## 30    15.87946   18.62283    17.08363    17.08363 17.29363
## 31    15.80637   18.73725    17.04580    17.04580 17.21233
## 32    15.82662   18.53327    16.99846    16.99846 17.18759
## 33    15.56886   18.39301    16.85644    16.85644 16.96700
## 34    15.59925   18.29529    16.80910    16.80910 16.90764
## 35    15.57295   18.33392    16.76176    16.76176 16.97177
## 36    15.51531   18.41282    16.70505    16.70505 16.87157
## 37    15.40651   18.27713    16.64833    16.64833 16.83746
## 38    15.21437   18.01811    16.47819    16.47819 16.58874
## 39    15.10931   17.80640    16.42147    16.42147 16.52000
## 40    15.05764   17.98101    16.36475    16.36475 16.57476
## 41    15.12667   17.86857    16.30802    16.30802 16.47455
## 42    15.10679   17.76927    16.25129    16.25129 16.44042
## 43    14.82536   17.63134    16.08110    16.08110 16.19165
## 44    14.88003   17.61923    16.02437    16.02437 16.12290
## 45    14.84379   17.65668    15.96764    15.96764 16.17764
## 46    14.70339   17.50947    15.91091    15.91091 16.07743
## 47    14.74935   17.44290    15.85418    15.85418 16.04330
## 48    14.37057   17.10590    15.68398    15.68398 15.79454
## 49    14.32009   17.17107    15.62725    15.62725 15.72579
## 50    14.43055   17.20218    15.57052    15.57052 15.78052
## 51    14.26057   17.18279    15.51379    15.51379 15.68031
## 52    14.36209   17.08260    15.45706    15.45706 15.64619
## 53    13.96497   16.69332    15.28687    15.28687 15.39742
## 54    13.91805   16.75307    15.23013    15.23013 15.32867
## 55    13.98803   16.69534    15.17340    15.17340 15.38341
## 56    13.93026   16.76769    15.11667    15.11667 15.28320
## 57    13.82984   16.64272    15.05994    15.05994 15.24907
## 58    13.61427   16.40292    14.88975    14.88975 15.00030
## 59    13.60436   16.43455    14.83302    14.83302 14.93155
## 60    13.54577   16.36273    14.77629    14.77629 14.98629
## 61    13.58181   16.31344    14.75111    14.75111 14.91764
## 62    13.50474   16.32659    14.72594    14.72594 14.91507
## 63    13.40274   16.09977    14.65042    14.65042 14.76097
## 64    13.36023   16.13587    14.62525    14.62525 14.72378
## 65    13.43379   16.13524    14.60007    14.60007 14.81008
## 66    13.40142   16.12089    14.61159    14.61159 14.77811
## 67    13.46894   16.24460    14.62310    14.62310 14.81223
## 68    13.46062   16.22295    14.65764    14.65764 14.76820
## 69    13.39023   15.98002    14.66916    14.66916 14.76769
## 70    13.45042   16.31242    14.68067    14.68067 14.89067
## 71    13.57711   16.31386    14.71275    14.71275 14.87928
## 72    13.54036   16.41653    14.87316    14.87316 14.97170
## 73    13.74125   16.47642    14.90525    14.90525 15.11525
## 74    13.73690   16.55975    14.93733    14.93733 15.10385
## 75    13.87325   16.55140    14.96941    14.96941 15.15854
## 76    13.78775   16.56646    15.07333    15.07333 15.18388
## 77    13.68995   16.58523    15.10797    15.10797 15.20650
## 78    13.91127   16.67812    15.14261    15.14261 15.35261
## 79    13.84532   16.79474    15.17725    15.17725 15.34377
## 80    13.97558   16.79610    15.21189    15.21189 15.40101
## 81    14.15406   16.87239    15.32133    15.32133 15.43188
## 82    14.10910   16.90622    15.35781    15.35781 15.45635
## 83    14.20282   16.95020    15.39429    15.39429 15.60430
## 84    14.24405   16.96866    15.43077    15.43077 15.59730
## 85    14.22044   17.05904    15.46725    15.46725 15.65638
## 86    14.22732   17.05510    15.57725    15.57725 15.68781
## 87    14.28519   17.08625    15.61392    15.61392 15.71246
## 88    14.47917   17.27375    15.65059    15.65059 15.86059
## 89    14.46733   17.27797    15.68725    15.68725 15.85378
## 90    14.60046   17.40840    15.83392    15.83392 15.94448
## 91    14.57386   17.29694    15.87059    15.87059 15.96912
## 92    14.72281   17.47311    15.90726    15.90726 16.11726
## 93    14.72607   17.54327    15.94392    15.94392 16.11045
## 94    14.84667   17.60594    15.98059    15.98059 16.16972
## 95    14.89165   17.60839    16.09059    16.09059 16.20115
## 96    14.81039   17.69530    16.12726    16.12726 16.22579
## 97    15.03676   17.88411    16.16393    16.16393 16.37393
## 98    15.00473   17.77557    16.20059    16.20059 16.36712
## 99    15.01168   17.86050    16.23726    16.23726 16.42639
## 100   15.04244   17.82622    16.38393    16.38393 16.48246
## 101   15.28753   18.04393    16.42060    16.42060 16.63060
## 102   15.11104   17.93390    16.45726    16.45726 16.62379
## 103   15.17068   18.05695    16.49393    16.49393 16.68306
## 104   15.39417   18.15347    16.60393    16.60393 16.71448
## 105   15.36179   18.13057    16.64060    16.64060 16.73913
## 106   15.56594   18.47054    16.67727    16.67727 16.88727
## 107   15.45947   18.33828    16.71393    16.71393 16.88046
## 108   15.60897   18.27906    16.75060    16.75060 16.93973
## 109   15.60659   18.31581    16.86060    16.86060 16.97115
## 110   15.68619   18.41948    16.89727    16.89727 16.99580
## 111   15.73514   18.52113    16.93393    16.93393 17.14394
## 112   15.70223   18.62811    16.97060    16.97060 17.13713
## 113   15.75834   18.62525    17.00727    17.00727 17.19640
## 114   15.87998   18.55898    17.11727    17.11727 17.22782
## 115   15.80991   18.68615    17.15394    17.15394 17.25247
## 116   16.07775   18.84311    17.19060    17.19060 17.40061
## 117   16.01030   18.82745    17.22727    17.22727 17.39380
## 118   16.08948   18.77623    17.26394    17.26394 17.45307
## 119   16.00668   18.92952    17.37394    17.37394 17.48449
## 120   16.09522   18.88058    17.45752    17.45752 17.55605
## 121   16.30634   19.11177    17.54110    17.54110 17.75110
## 122   16.33007   19.26722    17.62467    17.62467 17.79120
## 123   16.50474   19.39125    17.70825    17.70825 17.89738
## 124   16.63573   19.33872    17.95899    17.95899 18.06954
## 125   16.89582   19.63421    18.14085    18.14085 18.23939
## 126   17.06531   19.96433    18.32271    18.32271 18.53272
## 127   17.28920   20.06014    18.50458    18.50458 18.67110
## 128   17.43874   20.33050    18.68644    18.68644 18.87557
## 129   17.95697   20.76118    19.23203    19.23203 19.34258
## 130   18.03567   20.87520    19.41389    19.41389 19.51242
## 131   18.37302   21.31322    19.59575    19.59575 19.80576
## 132   18.55109   21.43097    19.77761    19.77761 19.94414
## 133   18.67997   21.52348    19.95948    19.95948 20.14860
## 134   19.23490   22.02983    20.50507    20.50507 20.61562
## 135   19.44373   22.23094    20.68693    20.68693 20.78546
## 136   19.61330   22.52100    20.86879    20.86879 21.07879
## 137   19.83782   22.51895    21.05065    21.05065 21.21718
## 138   20.06176   22.82972    21.23252    21.23252 21.42164
## 139   20.44451   23.29320    21.77810    21.77810 21.88866
## 140   20.65310   23.36781    21.95997    21.95997 22.05850
## 141   21.00160   23.72612    22.14183    22.14183 22.35183
## 142   21.18394   24.02696    22.32369    22.32369 22.49022
## 143   21.19914   24.03448    22.50555    22.50555 22.69468
## 144   21.72625   24.53404    23.05114    23.05114 23.16170
## 145   21.95588   24.71382    23.23300    23.23300 23.33154
## 146   22.23401   25.06013    23.41487    23.41487 23.62487
## 147   22.31558   25.15388    23.59673    23.59673 23.76326
## 148   22.39638   25.35407    23.77859    23.77859 23.96772
## 149   22.99761   25.71607    24.32418    24.32418 24.43473
## 150   23.23262   26.04558    24.50604    24.50604 24.60458
## 151   23.52933   26.33102    24.68791    24.68791 24.89791
## 152   23.64610   26.36515    24.86977    24.86977 25.03629
## 153   23.91204   26.58423    25.05163    25.05163 25.24076
## 154   24.35016   27.07859    25.59722    25.59722 25.70777
## 155   24.56891   27.28522    25.77908    25.77908 25.87762
## 156   24.79741   27.46271    25.96094    25.96094 26.17095
## 157   24.91051   27.74427    26.14281    26.14281 26.30933
## 158   25.18245   27.93028    26.32467    26.32467 26.51380
## 159   24.66304   27.50257    26.50401    26.51164 26.11916
## 160   24.96422   27.72828    26.67998    26.70194 26.30102
## 161   25.55810   28.33668    26.85449    26.89341 26.98081
## 162   25.74247   28.54675    27.02815    27.08556 27.15065
## 163   26.06872   28.82748    27.19966    27.28065 27.44399
## 164   26.21675   29.11274    27.36953    27.47522 27.58237
## 165   26.42933   29.23691    27.53924    27.67091 27.78684
## 166   26.04561   28.77800    27.70939    27.86783 27.39220
plot(model,forecast)

prophet_plot_components(model,forecast)

Implementing forcasting using TimeSeries object in R

library(tseries)
library(zoo)
library(forecast)
library(normwhn.test)

dat = structure(
c(1100, 1150, 1200, 1150, 1150, 1200, 1300, 1100, 1200,1250, 1200, 1250, 
1300,1400,1550,1400,1450,1450,1450,1450,1500,1550,1600,1650,
1650,1650,1650,1650,1750,1850,1750,1800,1850,1950,1950,2000,
1950,2000,2050,2100,2150,2200,2250,2450,2650,2700,2850,2850,
2750,2750,2750,2750,2800,2800,2850,2850,3000,2950,3000,2950,
2900,2850,2750,2750,2450,2400,2450,2500,2750,2550,2600,2300,
2470,2570,2570,2550,2500,2470,2550,2550,2470,2470,2370,2400,
2870,2648,2656,2600,2656,2608,2460,2644,2564,2508,2524,2508,
3006,3027,3129,3142,3119,2920,2902,2875,2880,2726,2543,2836,
2887,2978,2901,2946,2890,2947,2811,2899,3017,2989,2967,2902,
2994,3046,3074,3100,3141,3002,2934,2901,2908,3003,3097,3083,
3205,3253,3221,3192,3170,3240,3345,3480,3750,3725,3800,4000,
3800,4000,4200,4500,4800,5000,5100,5500,5500,5800,6000,6200), .Tsp = c(2008, 2020.91666666667, 12), class = "ts")

arimaModel <- arima(dat, order=c(0, 1, 1), list(order=c(0, 1, 0), period = 12))

summary(arimaModel)
## 
## Call:
## arima(x = dat, order = c(0, 1, 1), seasonal = list(order = c(0, 1, 0), period = 12))
## 
## Coefficients:
##           ma1
##       -0.1067
## s.e.   0.0720
## 
## sigma^2 estimated as 22135:  log likelihood = -918.26,  aic = 1840.53
## 
## Training set error measures:
##                    ME     RMSE      MAE       MPE     MAPE     MASE        ACF1
## Training set 14.32495 142.4436 105.2636 0.3245391 3.827641 1.189896 -0.02882344
preds <- predict(arimaModel, n.ahead = 12)

print(preds)
## $pred
##           Jan      Feb      Mar      Apr      May      Jun      Jul      Aug
## 2021 5998.214 6198.214 6398.214 6698.214 6998.214 7198.214 7298.214 7698.214
##           Sep      Oct      Nov      Dec
## 2021 7698.214 7998.214 8198.214 8398.214
## 
## $se
##           Jan      Feb      Mar      Apr      May      Jun      Jul      Aug
## 2021 148.7772 199.4956 239.7131 274.0916 304.6146 332.3461 357.9354 381.8136
##           Sep      Oct      Nov      Dec
## 2021 404.2839 425.5694 445.8398 465.2279
plot(dat, col = 'green', main = 'Acutal vs ARIMA Model',
     ylab = 'Gold Rates', lwd = 3)

lines(arimaModel$fitted, col = 'black', lwd = 3, lty = 2)

legend('topleft', legend = c('Actual', 'ARIMA Fit'),
       col = c('green','black'), lwd = c(3,3), lty = c(1,2))

ts.plot(dat, preds$pred, main = 'Actual vs ARIMA Predictions',
        col = c('green','black'), lty = c(1,2), lwd = c(3,3))

legend('topleft', legend = c('Actual Data', 'ARIMA Predictions'),
       col = c('green','black'), lwd = c(3,3), lty = c(1,2))

Summary

The main aim of time-series modeling is to carefully collect and regorously study the past observations of the time-series to develop an appropriate model which describes the inherent structure of the series. The model is then used to generate the future values for the series. Time series forecasting is thus can be termed as act of predicting the future by understanding the past.

Say Something

Comments

Nothing yet.

Recent Posts

Categories

About

about