-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04.html
More file actions
306 lines (190 loc) · 11 KB
/
Copy path04.html
File metadata and controls
306 lines (190 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<!DOCTYPE html>
<html>
<head>
<title>Introductory Time Series</title>
<meta charset="utf-8">
<meta name="author" content="Richard M. Smith" />
<link href="libs/remark-css/example.css" rel="stylesheet" />
<script src="libs/htmlwidgets/htmlwidgets.js"></script>
<script src="libs/jquery/jquery.min.js"></script>
<link href="libs/dygraphs/dygraph.css" rel="stylesheet" />
<script src="libs/dygraphs/dygraph-combined.js"></script>
<script src="libs/moment/moment.js"></script>
<script src="libs/moment-timezone/moment-timezone-with-data.js"></script>
<script src="libs/moment-fquarter/moment-fquarter.min.js"></script>
<script src="libs/dygraphs-binding/dygraphs.js"></script>
</head>
<body>
<textarea id="source">
class: center, middle, inverse, title-slide
# Introductory Time Series
## Session 4
### Richard M. Smith
### 2018/01/25
---
---
# Class overview
- Homework / Project
- Git
- Differencing & ACF
- Modeling Overview
- General Methods & Toolbox Review
- Exponential smoothing
- ARIMA models
- Quiz
---
# Class 3 Homework
- Final 2 questions from Session 2 in-class test
- Due 2/1 before class
--
# Project
- 20 - 25 minutes presentation
- 5 - 10 muinutes questions
A: Patrick, Jeff, Treva, Nathan, Derek (Discussant group B)
B: Ian, Tammy, Paul, Jack, Rachel (Discussant group C)
C: Jay, Andrew, Taylor, Adam, Brenden (Discussant group A)
---
# Git Resources
- [Main site & documentation](https://git-scm.com/)
- [Getting Started - Git Basics](https://git-scm.com/book/en/v2/Getting-Started-Git-Basics)
- [Happy Git and GitHub for the useR](http://happygitwithr.com/)
- [Oh shit, git!](http://ohshitgit.com/)
---
# Basic Commands
- `git init`
- `git status`
- `git add .`
- `git commit`
- `-m "Commit message"`
- `git reflog`
- `git reset HEAD@{index}`
---
## Differencing and stationarity
Transformations such as logarithms can help to stabilize the variance of a time series. Differencing can help stabilize the mean of a time series by removing changes in the level of a time series, and therefore eliminating (or reducing) trend and seasonality.
---
## Tests for stationarity
- Unit root tests: statistical tests of stationarity.
- Designed to determine whether differencing is required.
- *Augmented Dickey-Fuller (ADF) test*
The null-hypotheses for an ADF is that the data are non-stationary (unit root). Thus, small p-values suggest stationarity.
- `adf.test` in *tseries* package
---
# Forecasting Strategies
- Leading variables
- Associated variable that is easier to predict
- Generalized new product (e.g. Bass diffusion model)
- Extrapolate past trends with adaptive estimates
---
# Simple
- Average `\(\hat{y}_{T+h|Y} = \bar{y} = (y_1 + ... + y_T)/T\)`
- Naive `\(\hat{y}_{T+h|T} = y_T\)`
- Seasonal Naive `\(\hat{y}_{T+h|T} = y_{T+h-km}\)`
- Drift: Variation on naive allowing increases / decreases over time
- The "Drift"" is the average change seen in the historical data
---
# Transformations & Adjustments
- Calendar
- Population
- Inflation
- Power transformations
- Bias Adjustments
---
# Residual Diagnostics
- Residual mean close to zero will provide unbiased forecasts
- Non-normal distribution may lead to inaccurate prediction intervals
- `forecast::checkresiduals`
---
# Judgmental Forecasts
Keys to improved accuracy:
- Important Domain Knowledge
- Timely, up-to-date Information
- [Superforecasting](https://goo.gl/CdJxfV)
---
# Key Principles
1. Set the forecast task clearly & concisely
2. Implement a systematic approach
3. Document and justify
- Seek critical feedback
4. Segregate forecasters and users
---
# New Product Forecasting
- Bound the problem
- Bass Model
1. Total people who buy the product `\(m\)`
2. Coefficient of innovation `\(p\)`
3. Coefficient of imitation `\(q\)`
---
# ETS vs. ARIMA
- ETS - (Error, Trend, Seasonal) or ExponenTial Smoothing
- ARIMA - Autoregressive integrated moving average
- While exponential smoothing based on description of the trend and seasonality in the data, ARIMA models aim to describe the autocorrelations
---
# Simple Exponential Smoothing
Forecasts produced using exponential smoothing methods are weighted averages of past observations, with the weights decaying exponentially as the observations get older.
- `forecast::ses`
- Appropriate for forecasting data with no clear trend or seasonal pattern
---
# Trend Methods
- Holt's linear trend
The forecasts generated by Holt’s linear method display a constant trend (increasing or decreasing) indefinitely into the future. Empirical evidence indicates that these methods tend to over-forecast, especially for longer forecast horizons.
---
# Holt-Winters
- Extended trend method to capture seasonality
- Exponentially weighted moving averages to update estimates of the seasonally adjusted mean, slope, and seasonals
---
# `ets` function
- Unlike the `ses`, `holt`, `hw` functions, `ets` does not produce forecasts
- Esimates model parameters and returns information
---
# Regression & Generalized Least Squares
With positive serial correlation in the residual series, implies that standard errors of the estimated regression parameters are likely to be underestimated. Less accurate for the same sample size (See section 5.3.3). GLS can be used to provide better estimates of the standard errors of the regression parameters to account for the autocorrelartion in the residual series.
- `gls`
---
# Dygraphs
The dygraphs package is an R interface to the dygraphs JavaScript charting library. It provides rich facilities for charting time-series data in R, including:
- Automatically plots xts time series objects (or any object convertible to xts).
- Highly configurable axis and series display (including optional second Y-axis).
- Rich interactive features including zoom/pan and series/point highlighting.
- Display upper/lower bars (e.g. prediction intervals) around series.
- Various graph overlays including shaded regions, event lines, and point annotations.
- Use at the R console just like conventional R plots (via RStudio Viewer).
- Seamless embedding within R Markdown documents and Shiny web applications
---
# Why
- Level of interaction
- People come from expectation of some interaction
- Built in functions for lots of standard views
- https://rstudio.github.io/dygraphs/
---
# Example
<div id="htmlwidget-ba3be8037748f695d0e1" style="width:504px;height:504px;" class="dygraphs html-widget"></div>
<script type="application/json" data-for="htmlwidget-ba3be8037748f695d0e1">{"x":{"attrs":{"labels":["month","mdeaths","fdeaths"],"legend":"auto","retainDateWindow":false,"axes":{"x":{"pixelsPerLabel":60}},"showRangeSelector":true,"rangeSelectorHeight":40,"rangeSelectorPlotFillColor":" #A7B1C4","rangeSelectorPlotStrokeColor":"#808FAB","interactionModel":"Dygraph.Interaction.defaultModel"},"scale":"monthly","annotations":[],"shadings":[],"events":[],"format":"date","data":[["1974-01-01T00:00:00.000Z","1974-02-01T00:00:00.000Z","1974-03-01T00:00:00.000Z","1974-04-01T00:00:00.000Z","1974-05-01T00:00:00.000Z","1974-06-01T00:00:00.000Z","1974-07-01T00:00:00.000Z","1974-08-01T00:00:00.000Z","1974-09-01T00:00:00.000Z","1974-10-01T00:00:00.000Z","1974-11-01T00:00:00.000Z","1974-12-01T00:00:00.000Z","1975-01-01T00:00:00.000Z","1975-02-01T00:00:00.000Z","1975-03-01T00:00:00.000Z","1975-04-01T00:00:00.000Z","1975-05-01T00:00:00.000Z","1975-06-01T00:00:00.000Z","1975-07-01T00:00:00.000Z","1975-08-01T00:00:00.000Z","1975-09-01T00:00:00.000Z","1975-10-01T00:00:00.000Z","1975-11-01T00:00:00.000Z","1975-12-01T00:00:00.000Z","1976-01-01T00:00:00.000Z","1976-02-01T00:00:00.000Z","1976-03-01T00:00:00.000Z","1976-04-01T00:00:00.000Z","1976-05-01T00:00:00.000Z","1976-06-01T00:00:00.000Z","1976-07-01T00:00:00.000Z","1976-08-01T00:00:00.000Z","1976-09-01T00:00:00.000Z","1976-10-01T00:00:00.000Z","1976-11-01T00:00:00.000Z","1976-12-01T00:00:00.000Z","1977-01-01T00:00:00.000Z","1977-02-01T00:00:00.000Z","1977-03-01T00:00:00.000Z","1977-04-01T00:00:00.000Z","1977-05-01T00:00:00.000Z","1977-06-01T00:00:00.000Z","1977-07-01T00:00:00.000Z","1977-08-01T00:00:00.000Z","1977-09-01T00:00:00.000Z","1977-10-01T00:00:00.000Z","1977-11-01T00:00:00.000Z","1977-12-01T00:00:00.000Z","1978-01-01T00:00:00.000Z","1978-02-01T00:00:00.000Z","1978-03-01T00:00:00.000Z","1978-04-01T00:00:00.000Z","1978-05-01T00:00:00.000Z","1978-06-01T00:00:00.000Z","1978-07-01T00:00:00.000Z","1978-08-01T00:00:00.000Z","1978-09-01T00:00:00.000Z","1978-10-01T00:00:00.000Z","1978-11-01T00:00:00.000Z","1978-12-01T00:00:00.000Z","1979-01-01T00:00:00.000Z","1979-02-01T00:00:00.000Z","1979-03-01T00:00:00.000Z","1979-04-01T00:00:00.000Z","1979-05-01T00:00:00.000Z","1979-06-01T00:00:00.000Z","1979-07-01T00:00:00.000Z","1979-08-01T00:00:00.000Z","1979-09-01T00:00:00.000Z","1979-10-01T00:00:00.000Z","1979-11-01T00:00:00.000Z","1979-12-01T00:00:00.000Z"],[2134,1863,1877,1877,1492,1249,1280,1131,1209,1492,1621,1846,2103,2137,2153,1833,1403,1288,1186,1133,1053,1347,1545,2066,2020,2750,2283,1479,1189,1160,1113,970,999,1208,1467,2059,2240,1634,1722,1801,1246,1162,1087,1013,959,1179,1229,1655,2019,2284,1942,1423,1340,1187,1098,1004,970,1140,1110,1812,2263,1820,1846,1531,1215,1075,1056,975,940,1081,1294,1341],[901,689,827,677,522,406,441,393,387,582,578,666,830,752,785,664,467,438,421,412,343,440,531,771,767,1141,896,532,447,420,376,330,357,445,546,764,862,660,663,643,502,392,411,348,387,385,411,638,796,853,737,546,530,446,431,362,387,430,425,679,821,785,727,612,478,429,405,379,393,411,487,574]]},"evals":["attrs.interactionModel"],"jsHooks":[]}</script>
</textarea>
<script src="https://remarkjs.com/downloads/remark-latest.min.js"></script>
<script>var slideshow = remark.create({
"highlightStyle": "github",
"highlightLines": true,
"countIncrementalSlides": false
});
if (window.HTMLWidgets) slideshow.on('afterShowSlide', function (slide) {window.dispatchEvent(new Event('resize'));});
(function() {var d = document, s = d.createElement("style"), r = d.querySelector(".remark-slide-scaler"); if (!r) return; s.type = "text/css"; s.innerHTML = "@page {size: " + r.style.width + " " + r.style.height +"; }"; d.head.appendChild(s);})();</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
}
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://cdn.bootcss.com/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML';
if (location.protocol !== 'file:' && /^https?:/.test(script.src))
script.src = script.src.replace(/^https?:/, '');
document.getElementsByTagName('head')[0].appendChild(script);
})();
</script>
</body>
</html>