We’re into the second day of Business Science Demo Week. What’s demo week? Every day this week we are demoing an R package: tidyquant (Monday), timetk (Tuesday), sweep (Wednesday), tibbletime (Thursday) and h2o (Friday)! That’s five packages in five days! We’ll give you intel on what you need to know about these packages to go from zero to hero. Second up is timetk, your toolkit for time series in R. Here we go!
Previous Demo Week Demos
class(Monday) %
tk_index() %__%
tk_get_timeseries_summary() %__%
glimpse()## Observations: 1
## Variables: 12
## $ n.obs 84
## $ start 2010-01-01
## $ end 2016-12-01
## $ units &quot;days&quot;
## $ scale &quot;month&quot;
## $ tzone &quot;UTC&quot;
## $ diff.minimum 2419200
## $ diff.q1 2592000
## $ diff.median 2678400
## $ diff.mean 2629475
## $ diff.q3 2678400
## $ diff.maximum 2678400We can see important features like start, end, units, etc. We also have the quantiles of the time-diffs (difference in seconds between observations), which is useful for assessing the degree of regularity. Because the scale is monthly, the number of seconds between each month follows an irregular distribution.
Step 1: Augment Time Series Signature
The tk_augment_timeseries_signature() function expands out the timestamp information column-wise into a machine learning feature set, adding columns of time series information to the original data frame.
# Augment (adds data frame columns)
beer_sales_tbl_aug %
tk_augment_timeseries_signature()
beer_sales_tbl_aug## # A tibble: 84 x 30
## date price index.num diff year year.iso half quarter
## ## 1 2010-01-01 6558 1262304000 NA 2010 2009 1 1
## 2 2010-02-01 7481 1264982400 2678400 2010 2010 1 1
## 3 2010-03-01 9475 1267401600 2419200 2010 2010 1 1
## 4 2010-04-01 9424 1270080000 2678400 2010 2010 1 2
## 5 2010-05-01 9351 1272672000 2592000 2010 2010 1 2
## 6 2010-06-01 10552 1275350400 2678400 2010 2010 1 2
## 7 2010-07-01 9077 1277942400 2592000 2010 2010 2 3
## 8 2010-08-01 9273 1280620800 2678400 2010 2010 2 3
## 9 2010-09-01 9420 1283299200 2678400 2010 2010 2 3
## 10 2010-10-01 9413 1285891200 2592000 2010 2010 2 4
## # ... with 74 more rows, and 22 more variables: month ,
## # month.xts , month.lbl , day , hour ,
## # minute , second , hour12 , am.pm ,
## # wday , wday.xts , wday.lbl , mday ,
## # qday , yday , mweek , week , week.iso ,
## # week2 , week3 , week4 , mday7 Step 2: Model
Apply any regression model to the data. We’ll use lm(). Note that we drop the date and diff columns. Most algorithms do not work with dates, and the diff column is not useful for machine learning (it’s more useful for finding time gaps in the data).
# linear regression model used, but can use any model
fit_lm |t|) ## (Intercept) 3.660e+08 1.245e+08 2.940 0.004738 ** ## index.num 5.900e-03 2.003e-03 2.946 0.004661 ** ## year -1.974e+05 6.221e+04 -3.173 0.002434 ** ## year.iso 1.159e+04 6.546e+03 1.770 0.082006 . ## half -2.132e+03 6.107e+02 -3.491 0.000935 ***
## quarter -1.239e+04 2.190e+04 -0.566 0.573919 ## month -3.910e+03 7.355e+03 -0.532 0.597058 ## month.xts NA NA NA NA ## month.lbl.L NA NA NA NA ## month.lbl.Q -1.643e+03 2.069e+02 -7.942 8.59e-11 ***
## month.lbl.C 8.368e+02 5.139e+02 1.628 0.108949 ## month.lbl^4 6.452e+02 1.344e+02 4.801 1.18e-05 ***
## month.lbl^5 7.563e+02 4.241e+02 1.783 0.079852 . ## month.lbl^6 3.206e+02 1.609e+02 1.992 0.051135 . ## month.lbl^7 -3.537e+02 1.867e+02 -1.894 0.063263 . ## month.lbl^8 3.687e+02 3.217e+02 1.146 0.256510 ## month.lbl^9 NA NA NA NA ## month.lbl^10 6.339e+02 2.240e+02 2.830 0.006414 ** ## month.lbl^11 NA NA NA NA ## day NA NA NA NA ## hour NA NA NA NA ## minute NA NA NA NA ## second NA NA NA NA ## hour12 NA NA NA NA ## am.pm NA NA NA NA ## wday -8.264e+01 1.898e+01 -4.353 5.63e-05 ***
## wday.xts NA NA NA NA ## wday.lbl.L NA NA NA NA ## wday.lbl.Q -7.109e+02 1.093e+02 -6.503 2.13e-08 ***
## wday.lbl.C 2.355e+02 1.336e+02 1.763 0.083273 . ## wday.lbl^4 8.033e+01 1.133e+02 0.709 0.481281 ## wday.lbl^5 6.480e+01 8.029e+01 0.807 0.422951 ## wday.lbl^6 2.276e+01 8.200e+01 0.278 0.782319 ## mday NA NA NA NA ## qday -1.362e+02 2.418e+02 -0.563 0.575326 ## yday -2.356e+02 1.416e+02 -1.664 0.101627 ## mweek -1.670e+02 1.477e+02 -1.131 0.262923 ## week -1.764e+02 1.890e+02 -0.933 0.354618 ## week.iso 2.315e+02 1.256e+02 1.842 0.070613 . ## week2 -1.235e+02 1.547e+02 -0.798 0.428283 ## week3 NA NA NA NA ## week4 NA NA NA NA ## mday7 NA NA NA NA ## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## ## Residual standard error: 260.4 on 57 degrees of freedom
## Multiple R-squared: 0.9798,	Adjusted R-squared: 0.9706 ## F-statistic: 106.4 on 26 and 57 DF, p-value: __ 2.2e-16Step 3: Build Future (New) Data
Use tk_index() to extract the index.
# Retrieves the timestamp information
beer_sales_idx %
tk_index()
tail(beer_sales_idx)## [1] &quot;2016-07-01&quot; &quot;2016-08-01&quot; &quot;2016-09-01&quot; &quot;2016-10-01&quot; &quot;2016-11-01&quot;
## [6] &quot;2016-12-01&quot;Make a future index from the existing index with tk_make_future_timeseries. The function internally checks the periodicity and returns the correct sequence. Note that we have a whole vignette on how to make future time series, which is helpful due to the complexity of the topic.
# Make future index
future_idx %
tk_make_future_timeseries(n_future = 12)
future_idx## [1] &quot;2017-01-01&quot; &quot;2017-02-01&quot; &quot;2017-03-01&quot; &quot;2017-04-01&quot; &quot;2017-05-01&quot;
## [6] &quot;2017-06-01&quot; &quot;2017-07-01&quot; &quot;2017-08-01&quot; &quot;2017-09-01&quot; &quot;2017-10-01&quot;
## [11] &quot;2017-11-01&quot; &quot;2017-12-01&quot;From the future index, use tk_get_timeseries_signature() to turn index into time signature data frame.
new_data_tbl %
tk_get_timeseries_signature()
new_data_tbl## # A tibble: 12 x 29
## index index.num diff year year.iso half quarter month
## ## 1 2017-01-01 1483228800 NA 2017 2016 1 1 1
## 2 2017-02-01 1485907200 2678400 2017 2017 1 1 2
## 3 2017-03-01 1488326400 2419200 2017 2017 1 1 3
## 4 2017-04-01 1491004800 2678400 2017 2017 1 2 4
## 5 2017-05-01 1493596800 2592000 2017 2017 1 2 5
## 6 2017-06-01 1496275200 2678400 2017 2017 1 2 6
## 7 2017-07-01 1498867200 2592000 2017 2017 2 3 7
## 8 2017-08-01 1501545600 2678400 2017 2017 2 3 8
## 9 2017-09-01 1504224000 2678400 2017 2017 2 3 9
## 10 2017-10-01 1506816000 2592000 2017 2017 2 4 10
## 11 2017-11-01 1509494400 2678400 2017 2017 2 4 11
## 12 2017-12-01 1512086400 2592000 2017 2017 2 4 12
## # ... with 21 more variables: month.xts , month.lbl ,
## # day , hour , minute , second , hour12 ,
## # am.pm , wday , wday.xts , wday.lbl ,
## # mday , qday , yday , mweek , week ,
## # week.iso , week2 , week3 , week4 ,
## # mday7 Step 4: Predict the New Data
Use the predict() function for your regression model. Note that we drop the index and diff columns, the same as before when using the lm() function.
# Make predictions
pred