<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Hyndsight</title>
<link>https://robjhyndman.com/hyndsight/</link>
<atom:link href="https://robjhyndman.com/hyndsight/index.xml" rel="self" type="application/rss+xml"/>
<description>Blog posts by Professor Rob J Hyndman</description>
<generator>quarto-1.9.36</generator>
<lastBuildDate>Mon, 19 Jan 2026 13:00:00 GMT</lastBuildDate>
<item>
  <title>Rolling scaled forecast accuracy</title>
  <link>https://robjhyndman.com/hyndsight/rolling_mase.html</link>
  <description><![CDATA[ 



<div>
  <div class="description">
    When we compute a MASE or RMSSE using a rolling origin, should the scaling factor be recalculated every time?
  </div>
</div><p></p>


<p>I’ve been asked this a couple of times, so perhaps it is worth a blog post.</p>
<p>For a simple training/test split, the Mean Absolute Scaled Error (MASE) <span class="citation" data-cites="HK06">(Hyndman &amp; Koehler, 2006)</span> is defined as <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BMASE%7D%20=%20%5Cfrac%7B%5Cfrac%7B1%7D%7BH%7D%5Csum_%7Bt=T+1%7D%5E%7BT+H%7D%20%7Cy_t%20-%20%5Chat%7By%7D_%7Bt%7CT%7D%7C%7D%7B%5Cfrac1%7BT-m%7D%5Csum_%7Bt=m+1%7D%5ET%20%7Cy_t%20-%20y_%7Bt-m%7D%7C%7D%0A"> and the Root Mean Squared Scaled Error (RMSSE) is defined as <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BRMSSE%7D%20=%20%5Csqrt%7B%5Cfrac%7B%5Cfrac%7B1%7D%7BH%7D%5Csum_%7Bt=T+1%7D%5E%7BT+H%7D%20(y_t%20-%20%5Chat%7By%7D_%7Bt%7CT%7D)%5E2%7D%7B%5Cfrac%7B1%7D%7BT-m%7D%5Csum_%7Bt=m+1%7D%5ET%20(y_t%20-%20y_%7Bt-m%7D)%5E2%7D%7D.%0A"> In both cases, <img src="https://latex.codecogs.com/png.latex?m=1"> for non-seasonal data, where <img src="https://latex.codecogs.com/png.latex?m"> is the seasonal period for seasonal data, and the sum in the numerator is over the <em>test</em> set, while the sum in the denominator is over the <em>training</em> set. The notation <img src="https://latex.codecogs.com/png.latex?%5Chat%7By%7D_%7Bt%7CT%7D"> means the forecast of <img src="https://latex.codecogs.com/png.latex?y_%7Bt%7D"> given data <img src="https://latex.codecogs.com/png.latex?y_1,%5Cdots,y_T">.</p>
<p>These measures are discussed in my <a href="https://otexts.com/fpp3/accuracy.html#scaled-errors">forecasting textbook with George Athanasopoulos</a>. The denominator is a scaling factor, introduced so that you can compare MASE or RMSSE values across series of different units. For example, are the forecasts of widget sales more accurate than the forecasts of electricity demand? If all your forecasts are in the same units, then you don’t need to remove the scale, and it is simpler to just use MAE or RMSE (i.e., only the numerators of the above equations).</p>
<p>Now, the question is, when we are doing <a href="https://otexts.com/fpp3/tscv.html">time series cross-validation</a>, and computing forecast accuracy over a series of training/test sets with a rolling origin, does it make sense to compute a different scaling factor each time? For example, a cross-validated MASE for <img src="https://latex.codecogs.com/png.latex?h">-step forecasts is <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BMASE%7D_h%20=%20%5Cfrac%7B1%7D%7BT-I-h+1%7D%5Csum_%7Bt=I%7D%5E%7BT-h%7D%20%5Cfrac%7B%7Cy_%7Bt+h%7D%20-%20%5Chat%7By%7D_%7Bt+h%7Ct%7D%7C%7D%7B%7B%5Cfrac1%7Bt-m%7D%5Csum_%7Bs=m+1%7D%5E%7Bt%7D%20%7Cy_s%20-%20y_%7Bs-m%7D%7C%7D%7D,%0A"> where the first <img src="https://latex.codecogs.com/png.latex?I"> observations form the smallest training set, and subsequent training sets increase one observation at a time.</p>
<p>An alternative approach would be to compute the scaling factor across all available data, rather than calculate it separately for each training set. Then MASE would become <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BMASE%7D_h%20=%20%5Cfrac%7B%5Cfrac%7B1%7D%7BT-I-h+1%7D%5Csum_%7Bt=I%7D%5E%7BT-h%7D%20%7Cy_%7Bt+h%7D%20-%20%5Chat%7By%7D_%7Bt+h%7Ct%7D%7C%7D%7B%5Cfrac1%7BT-m%7D%5Csum_%7Bt=m+1%7D%5E%7BT%7D%20%7Cy_t%20-%20y_%7Bt-m%7D%7C%7D%0A"></p>
<p>Let’s think about the advantages of this alternative:</p>
<ol type="1">
<li>It is (slightly) faster. But the denominators are very fast to compute, so this really doesn’t make much difference.</li>
<li>It removes a source of variation from the calculation. The scaled errors will be more variable when the denominator changes with each test set, and that makes it harder to see the difference between forecasting methods.</li>
<li>It uses more data in computing the scaling factor, which reduces the uncertainty in the estimate of the accuracy measure. This is potentially important if <img src="https://latex.codecogs.com/png.latex?I%20%5Cll%20T">, so that some training sets are relatively small compared to the available data.</li>
</ol>
<p>As for disadvantages:</p>
<ol type="1">
<li>The measures are no longer true measures of forecast accuracy because the calculation potentially involves future observations. On the face of it, this seems important, but in reality it isn’t. The future observations aren’t affecting the forecasts, only the scaling factor, so there is no leakage involved.</li>
<li>One of my correspondents suggested that it changed the interpretation. I think the interpretability of these scaled measures is over-rated. They can only be interpreted as a ratio of out-of-sample accuracy to in-sample accuracy, and the two are not necessarily even over the same horizons. How the scaling factor is calculated doesn’t really affect the interpretation, because there is not much value in interpretation either way.</li>
</ol>
<p>So I suggest that computing the scaling factor across all available training data when calculating cross-validated MASE and RMSSE values is a good idea.</p>
<p>That raises the question as to why shouldn’t we use all available data in the denominator when doing a simple training/test split? In that context, the only advantage that is relevant is #3 above, and unless the test set is particularly large, it shouldn’t make much difference. Nevertheless, I can’t see any real problem in using all available data in computing the scaling factor.</p>




<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-HK06" class="csl-entry">
Hyndman, R. J., &amp; Koehler, A. B. (2006). Another look at measures of forecast accuracy. <em>International J Forecasting</em>, <em>22</em>(4), 679–688. <a href="https://robjhyndman.com/publications/another-look-at-measures-of-forecast-accuracy/">https://robjhyndman.com/publications/another-look-at-measures-of-forecast-accuracy/</a>
</div>
</div></section></div> ]]></description>
  <category>forecasting</category>
  <guid>https://robjhyndman.com/hyndsight/rolling_mase.html</guid>
  <pubDate>Mon, 19 Jan 2026 13:00:00 GMT</pubDate>
  <media:content url="https://otexts.com/fpp3/fpp_files/figure-html/cv1-1.png" medium="image" type="image/png"/>
</item>
<item>
  <title>forecast package v9</title>
  <link>https://robjhyndman.com/hyndsight/forecast9.html</link>
  <description><![CDATA[ <p>It’s been nearly nine years since the <a href="../hyndsight/forecast8/">last major release of the forecast package</a>, as we have been focusing on developing <a href="https://fable.tidyverts.org/"><code>fable</code></a> and <a href="https://tidyverts.org">related packages</a> as a tidyverse-based alternative for time series forecasting in R.</p>
<p>However, version 9.0 of the <code>forecast</code> package has now been released on CRAN. Thanks to Maximilian Muecke for helping with this release, and for modernising some of the old code.</p>
<p>Here are the main new features and changes.</p>
<section id="new-forecasting-models" class="level2"><h2 class="anchored" data-anchor-id="new-forecasting-models">New forecasting models</h2>
<p>The usual workflow with the forecast package is to fit a model to a time series, and then forecast from the model. For example</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">USAccDeaths</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ets</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">forecast</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">USAccDeaths</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">auto.arima</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">forecast</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
<p>But there have always been a few functions that produce forecasts directly from a time series, without the need to explicitly fit a model first. For example,</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">USAccDeaths</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">snaive</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
<p>produces seasonal naive forecasts.</p>
<p>But now you can obtain the same forecasts by first fitting a model and then forecasting from it:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">USAccDeaths</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rw_model</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>lag <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">forecast</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
<p>Similarly, you can now fit and forecast from mean models, cubic spline models, theta models, and Croston’s method using the new <code>mean_model()</code>, <code>spline_model()</code>, <code>theta_model()</code>, and <code>croston_model()</code> functions. The existing <code>naive()</code>, <code>snaive()</code>, <code>rwf</code>, <code>meanf()</code>, <code>splinef()</code>, <code>thetaf()</code>, and <code>croston()</code> functions are still available for backward compatibility, but are now merely wrappers around the modelling and <code>forecast</code> functions.</p>
<p>This should make it easier to set up forecasting workflows involving many different models. For example, suppose you wanted to compute forecasts from five different models applied to the <code>USAccDeaths</code> time series.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://purrr.tidyverse.org/">purrr</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model_fns</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ets</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">auto.arima</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rw_model</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rw_model</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mean_model</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">args</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/vector.html">vector</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"list"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model_fns</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">args</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>lag <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">args</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>lag <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, drift <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://purrr.tidyverse.org/reference/map2.html">map2</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model_fns</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">args</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/do.call.html">do.call</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.x</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">USAccDeaths</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/names.html">names</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://purrr.tidyverse.org/reference/map.html">map</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">u</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">u</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">method</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">forecasts</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://purrr.tidyverse.org/reference/map.html">map</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">forecast</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
<p>Note that <code>rw_model</code> is included twice, with different arguments, allowing the calculation of seasonal naive, and seasonal naive with drift, forecasts.</p>
<p>Forecasting with multiple models is easy to do using the <a href="https://fable.tidyverts.org/"><code>fable</code> package</a> (and was one of the key considerations in its design). Now it is also possible to do it using the <code>forecast</code> package, with this more consistent interface.</p>
</section><section id="missing-values-in-ets-models" class="level2"><h2 class="anchored" data-anchor-id="missing-values-in-ets-models">Missing values in ETS models</h2>
<p>I wrote the first version of the ETS code (more than 25 years ago) for a consulting project where there were no missing values, so I didn’t write the code to handle them. But obviously missing values occur a lot in many applications, and I’ve now belatedly added support for them. If there are gaps in a time series, <code>ets()</code> can be applied, and it computes the likelihood and fitted values correctly using all available data. (Previously, it would fit a model to the largest contiguous segment of the data.)</p>
<p>This change has also been made to the <code>ETS()</code> function in the <a href="https://fable.tidyverts.org/"><code>fable</code> package</a>.</p>
</section><section id="bootstrapped-prediction-intervals" class="level2"><h2 class="anchored" data-anchor-id="bootstrapped-prediction-intervals">Bootstrapped prediction intervals</h2>
<p>The default prediction intervals in the forecast package are based on the (usually reasonable) assumption that forecast errors are Gaussian. Bootstrapped prediction intervals allow for non-Gaussian forecast errors. These have long been available using ETS, ARIMA, and other models; they are now also available using ARFIMA and TBATS models.</p>
</section><section id="other-changes" class="level2"><h2 class="anchored" data-anchor-id="other-changes">Other changes</h2>
<ul>
<li>Added parallelization for <code>nnetar()</code>.</li>
<li>More consistent handling of the <code>biasadj</code> argument across models.</li>
<li>More consistent interface for simulated and bootstrapped prediction intervals for all models.</li>
<li>Bug fixes and performance improvements.</li>
<li>Documentation improvements.</li>
</ul></section><section id="future-of-the-forecast-package" class="level2"><h2 class="anchored" data-anchor-id="future-of-the-forecast-package">Future of the forecast package</h2>
<p>I would still encourage people to use the <a href="https://fable.tidyverts.org/"><code>fable</code> package</a> for new projects, as it provides a more modern and consistent interface for time series forecasting in R. However, the <code>forecast</code> package will continue to be maintained, with bug fixes and minor improvements as needed.</p>


</section> ]]></description>
  <category>forecasting</category>
  <category>R</category>
  <category>time series</category>
  <guid>https://robjhyndman.com/hyndsight/forecast9.html</guid>
  <pubDate>Sun, 11 Jan 2026 13:00:00 GMT</pubDate>
  <media:content url="https://pkg.robjhyndman.com/forecast/logo.png" medium="image" type="image/png"/>
</item>
<item>
  <title>WAPE: Weighted Absolute Percentage Error</title>
  <link>https://robjhyndman.com/hyndsight/wape.html</link>
  <description><![CDATA[ 



<div>
  <div class="description">
    I was recently asked for my view on the so-called “Weighted Absolute Percentage Error” (WAPE).
  </div>
</div><p></p>


<p>The WAPE was introduced by <span class="citation" data-cites="wmape">Kolassa &amp; Schütz (2007)</span> who called it the MAD/Mean ratio. It is defined as <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BWAPE%7D%20=%20%5Cfrac%7B%5Csum_%7Bt%7D%20%7Cy_t%20-%20%5Chat%7By%7D_t%7C%7D%7B%5Csum_%7Bt%7D%20%7Cy_t%7C%7D%0A"> where</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?y_t"> is the actual value at time <img src="https://latex.codecogs.com/png.latex?t">,</li>
<li><img src="https://latex.codecogs.com/png.latex?%5Chat%7By%7D_t"> is the forecast value at time <img src="https://latex.codecogs.com/png.latex?t"></li>
</ul>
<p>You can think of it as a weighted percentage error by writing it as <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BWAPE%7D%20=%20%5Csum_%7Bt%7D%20w_t%20%5Cfrac%7B%7Cy_t%20-%20%5Chat%7By%7D_t%7C%7D%7B%7Cy_t%7C%7D%0A"> where the weights are given by <img src="https://latex.codecogs.com/png.latex?w_t%20=%20%5Cfrac%7B%7Cy_t%7C%7D%7B%5Csum_%7Bt%7D%20%7Cy_t%7C%20%7D"></p>
<p>It can also be considered a relative MAE where the comparison method has all forecasts equal to zero. In fact, <img src="https://latex.codecogs.com/png.latex?1-%5Ctext%7BWAPE%7D"> would give the skill score relative to the forecast of zero for all <img src="https://latex.codecogs.com/png.latex?t">.</p>
<p>Or you could think of it as like a MASE <span class="citation" data-cites="HK06">(Hyndman &amp; Koehler, 2006)</span> but with scaling based on the sum of absolute values on the test set rather than the sum of absolute differences on the training set.</p>
<p>This has some obvious advantages over the MAPE:</p>
<ol type="1">
<li>The MAPE is undefined when <em>any</em> actual value in the test set is zero. The WAPE is defined even when some actuals are zero. It is only undefined when <em>all</em> the actuals used in the denominator are zero.</li>
<li>Optimising the MAPE does not lead to a sensible point forecasts <span class="citation" data-cites="gneiting2011">(Gneiting, 2011)</span>, but optimising the WAPE will lead to the median forecast.</li>
</ol>
<p>However, I think there are a couple of problems that do not seem to have been widely recognized.</p>
<ol type="1">
<li>The resulting estimate is only consistent when the time series is stationary. So it should not be used with data that has trends, or seasonality, or heteroscedasticity.</li>
<li>It is quite possible to have all actuals in the test set equal to zero, especially with intermittent demand time series and small test sets. Then the denominator is zero, and the WAPE is undefined.</li>
</ol>
<p>For these reasons, I think the Mean Absolute Scaled Error (MASE) <span class="citation" data-cites="HK06">(Hyndman &amp; Koehler, 2006)</span> is a better choice than the WAPE. The MASE is defined as <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BMASE%7D%20=%20%5Cfrac%7B%5Cfrac%7B1%7D%7Bn%7D%5Csum_%7Bt%7D%20%7Cy_t%20-%20%5Chat%7By%7D_t%7C%7D%7B%5Cfrac%7B1%7D%7BT-m%7D%5Csum_%7Bt=m+1%7D%5ET%20%7Cy_t%20-%20y_%7Bt-m%7D%7C%7D%0A"> where <img src="https://latex.codecogs.com/png.latex?m=1"> for non-seasonal data, and <img src="https://latex.codecogs.com/png.latex?m"> is the seasonal period for seasonal data, and the sum in the numerator is over the <em>test</em> set (of size <img src="https://latex.codecogs.com/png.latex?n">), and the sum in the denominator is over the <em>training</em> set (of size <img src="https://latex.codecogs.com/png.latex?T">). Because the denominator is defined on the <em>training</em> data, not the <em>test</em> data, it avoids the above problems with the WAPE:</p>
<ul>
<li>It is a consistent estimator provided the series is <em>difference stationary</em>, which is a much weaker condition than stationarity.</li>
<li>The training data is also usually much longer than the test data, so it is much less likely to contain only zeros. In fact, if the training data did contain only zeros, then the obvious forecasts would all be zeros too.</li>
</ul>
<p>I don’t want to suggest there are no problems with the MASE. <span class="citation" data-cites="wmape">Kolassa &amp; Schütz (2007)</span> point out one potential drawback of MASE — when there are structural breaks or outliers in the training data. Also, like the WAPE, optimising the MASE will lead to the median forecast, which is probably not what you want, especially when you have intermittent demand time series.</p>
<p>These days, if I want a scale-free accuracy measure, I prefer the Root Mean Squared Scaled Error (RMSSE): <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BRMSSE%7D%20=%20%5Csqrt%7B%5Cfrac%7B%5Cfrac%7B1%7D%7Bn%7D%5Csum_%7Bt%7D%20(y_t%20-%20%5Chat%7By%7D_t)%5E2%7D%7B%5Cfrac%7B1%7D%7BT-m%7D%5Csum_%7Bt=m+1%7D%5ET%20(y_t%20-%20y_%7Bt-m%7D)%5E2%7D%7D,%0A"> where again the sum in the numerator is over the <em>test</em> set, and the sum in the denominator is over the <em>training</em> set. This has all the advantages (and most disadvantages) of the MASE, but optimising it leads to the mean forecast rather than the median. It also aligns better with how models are estimated. Almost all models are estimated by minimising the sum of squared errors, so it makes sense to evaluate them using squared errors as well.</p>
<p>Point forecast reconciliation also works on means rather than medians, and is optimised using least squares, so it is more natural to evaluate using a squared error measure.</p>
<section id="update-on-consistency-and-stationarity" class="level2">
<h2 class="anchored" data-anchor-id="update-on-consistency-and-stationarity">Update on consistency and stationarity</h2>
<p>The comments below suggest that I need to explain the consistency issue in more detail. Imagine you are forecasting a time series that is a simple linear trend with some noise. So <img src="https://latex.codecogs.com/png.latex?y_t%20=%20a%20+%20bt%20+%20%5Cvarepsilon_t">, where <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon_t"> is Gaussian white noise with mean 0 and variance <img src="https://latex.codecogs.com/png.latex?%5Csigma%5E2">. As a good forecaster, you would fit a linear trend to this series, so your forecasts would be <img src="https://latex.codecogs.com/png.latex?%5Chat%7By%7D_t%20=%20%5Chat%7Ba%7D%20+%20%5Chat%7Bb%7Dt">. To keep it simple, let’s ignore the estimation error in this model and set <img src="https://latex.codecogs.com/png.latex?%5Chat%7Ba%7D%20=%20a"> and <img src="https://latex.codecogs.com/png.latex?%5Chat%7Bb%7D%20=%20b">.</p>
<p>Now let’s generate forecasts for this series over a test set of <img src="https://latex.codecogs.com/png.latex?n"> observations (either the next <img src="https://latex.codecogs.com/png.latex?n"> time periods, or over a cross-validated set). If you use the RMSE to evaluate these forecasts, you obtain <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BRMSE%7D%20=%20%5Csqrt%7B%5Cfrac%7B1%7D%7Bn%7D%5Csum_%7Bt%7D%20(y_%7Bt%7D%20-%20%5Chat%7By%7D_%7Bt%7D)%5E2%7D%20=%20%5Csqrt%7B%5Cfrac%7B1%7D%7Bn%7D%20%5Csum_%7Bt%7D%20%5Cvarepsilon_%7Bt%7D%5E2%7D,%0A"> which is an estimate of the variance of the noise. As more observations are included in the calculation, the RMSE will converge to <img src="https://latex.codecogs.com/png.latex?%5Csigma">. That makes it “consistent” in a statistical sense. Practically, it also means that the expected RMSE does not depend on which observations you use to evaluate the forecasts — the RMSE values will be similar regardless of the test set used in the calculation. That is, if you evaluate the forecasts over the next week, the RMSE will be similar to the RMSE calculated on a cross-validated set.</p>
<p>Suppose, instead, we use the RMSSE to evaluate the forecasts. The RMSSE is equal to the RMSE divided by <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7B%5Cfrac%7B1%7D%7BT-1%7D%5Csum_%7Bt=2%7D%5ET%20(y_%7Bt%7D%20-%20y_%7Bt-1%7D)%5E2%7D%20=%20%5Csqrt%7B%5Cfrac%7B1%7D%7BT-1%7D%5Csum_%7Bt=2%7D%5ET%20(b%20+%20%5Cvarepsilon_t%20-%20%5Cvarepsilon_%7Bt-1%7D)%5E2%7D"> So the RMSSE is still consistent (it converges to <img src="https://latex.codecogs.com/png.latex?%5Csigma/%5Csqrt%7Bb%5E2%20+%202%5Csigma%5E2%7D">), and the expected RMSSE does not depend on which observations you use to evaluate the forecasts.</p>
<p>Now, suppose we use the WAPE to evaluate these forecasts. Then we obtain <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BWAPE%7D%20=%20%5Cfrac%7B%5Csum_%7Bt%7D%20%7Cy_t%20-%20%5Chat%7By%7D_t%7C%7D%7B%5Csum_%7Bt%7D%20%7Cy_t%7C%7D%20=%20%5Cfrac%7B%5Cfrac1n%20%5Csum_%7Bt%7D%20%7C%5Cvarepsilon_t%7C%7D%7B%5Cfrac1n%20%5Csum_%7Bt%7D%20%7Ca%20+%20bt%20+%20%5Cvarepsilon_t%7C%7D,%0A"> where both sums are over the test set. As <img src="https://latex.codecogs.com/png.latex?n"> increases, the numerator will converge to the mean absolute value of the noise, approximately <img src="https://latex.codecogs.com/png.latex?0.798%20%5Csigma">. But the denominator won’t converge at all as it depends on the trend in the series. So the WAPE is not a consistent estimator of anything.</p>
<p>To see the effect of this, imagine the time series is daily sales, and you produce forecasts each week for the next 7 observations, and evaluate them using the WAPE. Over time, your WAPE values get smaller and smaller, because the denominator is growing larger and larger as the trend accumulates. So you have the illusion of improving accuracy, but your forecasts are really no more accurate than they were at the start.</p>
<p>While I have used a linear trend as the example here, similar problems can occur whenever the underlying time series is not stationary.</p>



</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-gneiting2011" class="csl-entry">
Gneiting, T. (2011). Making and evaluating point forecasts. <em>Journal of the American Statistical Association</em>, <em>106</em>(494), 746–762.
</div>
<div id="ref-HK06" class="csl-entry">
Hyndman, R. J., &amp; Koehler, A. B. (2006). Another look at measures of forecast accuracy. <em>International J Forecasting</em>, <em>22</em>(4), 679–688. <a href="https://robjhyndman.com/publications/another-look-at-measures-of-forecast-accuracy/">https://robjhyndman.com/publications/another-look-at-measures-of-forecast-accuracy/</a>
</div>
<div id="ref-wmape" class="csl-entry">
Kolassa, S., &amp; Schütz, W. (2007). Advantages of the MAD/mean ratio over the MAPE. <em>Foresight: The International Journal of Applied Forecasting</em>, <em>6</em>, 40–43.
</div>
</div></section></div> ]]></description>
  <category>forecasting</category>
  <guid>https://robjhyndman.com/hyndsight/wape.html</guid>
  <pubDate>Thu, 07 Aug 2025 14:00:00 GMT</pubDate>
  <media:content url="https://robjhyndman.com/img/wape.png" medium="image" type="image/png" height="89" width="144"/>
</item>
<item>
  <title>fpp3 package update</title>
  <link>https://robjhyndman.com/hyndsight/fpp3-update.html</link>
  <description><![CDATA[ <div>
  <div class="description">
    The <a href="https://pkg.robjhyndman.com/fpp3/"><code>fpp3</code> package</a> has had its first major update since the book was published.
  </div>
</div>
<p></p>


<p>The <a href="https://pkg.robjhyndman.com/fpp3/"><code>fpp3</code> package</a> is a companion to the book <a href="https://OTexts.com/fpp3/">Forecasting: Principles and Practice</a> (3rd edition, Hyndman &amp; Athanasopoulos, OTexts). When you load the package, it loads the data and functions needed for the examples in the book.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://pkg.robjhyndman.com/fpp3/">fpp3</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>── Attaching packages ──────────────────────────────────────────── fpp3 1.0.0 ──</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>✔ tibble      3.2.1     ✔ tsibble     1.1.4
✔ dplyr       1.1.4     ✔ tsibbledata 0.4.1
✔ tidyr       1.3.1     ✔ feasts      0.3.2
✔ lubridate   1.9.3     ✔ fable       0.3.4
✔ ggplot2     3.5.1     ✔ fabletools  0.4.2</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
✖ lubridate::date()    masks base::date()
✖ dplyr::filter()      masks stats::filter()
✖ tsibble::intersect() masks base::intersect()
✖ tsibble::interval()  masks lubridate::interval()
✖ dplyr::lag()         masks stats::lag()
✖ tsibble::setdiff()   masks base::setdiff()
✖ tsibble::union()     masks base::union()</code></pre>
</div>
</div>
<p>This provides a shorthand way of attaching the packages needed for most time series forecasting tasks, using packages from the <a href="https://tidyverts.org">tidyverts</a>, just as the <a href="https://www.tidyverse.org/packages/"><code>tidyverse</code> package</a> does for data manipulation and visualization.</p>
<p>In the most recent update, we have added many new data sets that can be used as examples by instructors, or for users to practice their forecasting skills.</p>
<p>Each of these new data sets were previously used in an exam for the forecasting subjects taught by George Athanasopoulos and me at Monash University. Three of our exams are available online for those who are interested to see how we assess forecasting students.</p>
<ul>
<li><a href="https://af.numbat.space/exams/exam_2021.pdf">2021</a></li>
<li><a href="https://af.numbat.space/exams/exam_2022.pdf">2022</a></li>
<li><a href="https://af.numbat.space/exams/exam_2023.pdf">2023</a></li>
</ul>
<p>Each year, we take a new data set, and write the exam around how to analyse the time series. We strongly believe in assessing students on their forecasting skills with real data, rather than using artificial data, or asking technical questions that are not relevant to real-world forecasting. As a result, our exams are quite different from anything we have seen elsewhere.</p>
<p>We also think it is best to use “fresh and local”<sup>1</sup> data sets. So the data are almost always from Australia, and included data up to about a month before the exam was written.</p>
<p>In the latest update to the <a href="https://pkg.robjhyndman.com/fpp3/"><code>fpp3</code> package</a>, we have expanded these data sets to cover more series than we did in the exams, and in most cases updated them to include more recent observations.</p>
<p>The new data sets are:</p>
<ul>
<li>
<a href="https://pkg.robjhyndman.com/fpp3/reference/aus_births.html"><code>aus_births</code></a>: Australian births data</li>
<li>
<a href="https://pkg.robjhyndman.com/fpp3/reference/aus_fertility.html"><code>aus_fertility</code></a>: Australian fertility rates</li>
<li>
<a href="https://pkg.robjhyndman.com/fpp3/reference/aus_inbound.html"><code>aus_inbound</code></a>: Monthly short term (&lt;1 year) visitor arrivals to Australia</li>
<li>
<a href="https://pkg.robjhyndman.com/fpp3/reference/aus_migration.html"><code>aus_migration</code></a>: Australian migration data</li>
<li>
<a href="https://pkg.robjhyndman.com/fpp3/reference/aus_mortality.html"><code>aus_mortality</code></a>: Australian mortality data</li>
<li>
<a href="https://pkg.robjhyndman.com/fpp3/reference/aus_outbound.html"><code>aus_outbound</code></a>: Monthly short term (&lt;1 year) resident departures in Australia</li>
<li>
<a href="https://pkg.robjhyndman.com/fpp3/reference/aus_tobacco.html"><code>aus_tobacco</code></a>: Australian cigarette and tobacco expenditure</li>
<li>
<a href="https://pkg.robjhyndman.com/fpp3/reference/aus_vehicle_sales.html"><code>aus_vehicle_sales</code></a>: Australian vehicle sales</li>
<li>
<a href="https://pkg.robjhyndman.com/fpp3/reference/melb_walkers.html"><code>melb_walkers</code></a>: Average daily total pedestrian count in Melbourne</li>
<li>
<a href="https://pkg.robjhyndman.com/fpp3/reference/nsw_offences.html"><code>nsw_offences</code></a>: Monthly offences in NSW</li>
<li>
<a href="https://pkg.robjhyndman.com/fpp3/reference/ny_childcare.html"><code>ny_childcare</code></a>: New York childcare data</li>
<li>
<a href="https://pkg.robjhyndman.com/fpp3/reference/otexts_views.html"><code>otexts_views</code></a>: OTexts page views</li>
</ul>
<p>Thanks to Nuwani Palihawadana and Shanika Wickramasuriya for most of the work on this latest update, completed at the <a href="https://numbat.space/posts/hackathon_2024/">NUMBAT hackathon</a> last month.</p>
<p><img src="https://numbat.space/posts/hackathon_2024/project1.jpg" class="img-fluid"></p>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>
<ol>
<li id="fn1"><p>Thanks to <a href="https://dicook.org">Di Cook</a> for this phrase.↩︎</p></li>
</ol></section></div> ]]></description>
  <category>forecasting</category>
  <category>R</category>
  <category>teaching</category>
  <guid>https://robjhyndman.com/hyndsight/fpp3-update.html</guid>
  <pubDate>Tue, 25 Jun 2024 14:00:00 GMT</pubDate>
  <media:content url="https://pkg.robjhyndman.com/fpp3/logo.png" medium="image" type="image/png"/>
</item>
<item>
  <title>AIC calculations</title>
  <link>https://robjhyndman.com/hyndsight/lm_aic.html</link>
  <description><![CDATA[ <div>
  <div class="description">
    The AIC returned by <code>TSLM()</code> is different from that returned by <code><a href="https://rdrr.io/r/stats/lm.html">lm()</a></code>. Why?
  </div>
</div>
<p></p>


<p>I get this question a lot, so I thought it might help to explain some issues with AIC calculation.</p>
<p>First, the equation for the AIC is given by <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BAIC%7D%20=%202k%20-%202%5Clog(L),%0A"> where <img src="https://latex.codecogs.com/png.latex?L"> is the likelihood of the model and <img src="https://latex.codecogs.com/png.latex?k"> is the number of parameters that are estimated (including the error variance). For a linear regression model with iid <img src="https://latex.codecogs.com/png.latex?N(0,%5Csigma%5E2)"> errors, fitted to <img src="https://latex.codecogs.com/png.latex?n"> observations, the log-likelihood can be written as <img src="https://latex.codecogs.com/png.latex?%0A%5Clog(L)%20=%20-%5Cfrac%7Bn%7D%7B2%7D%5Clog(2%5Cpi)%20-%20%5Cfrac%7Bn%7D%7B2%7D%5Clog(%5Csigma%5E2)%20-%20%5Cfrac%7B1%7D%7B2%5Csigma%5E2%7D%5Csum_%7Bi=1%7D%5En%20%5Chat%7Be%7D_i%5E2%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Chat%7Be%7D_i"> is the residual for the <img src="https://latex.codecogs.com/png.latex?i">th observation. The AIC is then <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BAIC%7D%20=%202k%20+%20n%5Clog(2%5Cpi)%20+%20n%5Clog(%5Csigma%5E2)%20%20+%20%5Cfrac%7B1%7D%7B%5Csigma%5E2%7D%5Csum_%7Bi=1%7D%5En%20%5Chat%7Be%7D_i%5E2.%0A"> Since we don’t know <img src="https://latex.codecogs.com/png.latex?%5Csigma%5E2">, we estimate it using the mean squared error (the maximum likelihood estimator), giving <img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0A%5Ctext%7BAIC%7D%20&amp;%20=%202k%20+n%5Clog(2%5Cpi)%20+%20n%5Clog(%5Ctext%7BMSE%7D)%20+%20n%20%5C%5C%0A&amp;%20=%202k%20+%20n%5Clog(%5Ctext%7BMSE%7D)%20+%20C%0A%5Cend%7Balign*%7D"> where <img src="https://latex.codecogs.com/png.latex?C%20=%20n%20+%20n%5Clog(2%5Cpi)"> is a constant that depends only on the sample size and not on the model. This constant is often ignored. Thus, different software implementations can lead to different AIC values for the same model, since they may include or exclude the constant <img src="https://latex.codecogs.com/png.latex?C">.</p>
<p>Now, let’s look at what R returns in a simple case using the <code><a href="https://rdrr.io/r/stats/lm.html">lm()</a></code> function.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Random.html">set.seed</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2023</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://pkg.robjhyndman.com/fpp3/">fpp3</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>  time <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/seq.html">seq</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>  x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>  y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/lm.html">lm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/AIC.html">AIC</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 275.6267</code></pre>
</div>
</div>
<p>We can check how this is calculated by computing it ourselves.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mse</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/residuals.html">residuals</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/residuals.html">residuals</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">k</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">coefficients</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># With constant</span></span>
<span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">k</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Log.html">log</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mse</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Log.html">log</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pi</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 275.6267</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Without constant</span></span>
<span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">k</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Log.html">log</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mse</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -8.161047</code></pre>
</div>
</div>
<p>Clearly, <code><a href="https://rdrr.io/r/stats/AIC.html">AIC()</a></code> applied to the output from <code><a href="https://rdrr.io/r/stats/lm.html">lm()</a></code> is using the version with the constant.</p>
<p>Now compare that with what we obtain using the <code>TSLM()</code> function from the fable package.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_tsibble</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>index <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">time</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">TSLM</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glance</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">AIC</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -8.161047</code></pre>
</div>
</div>
<p>This is the AIC without the constant.</p>
<p>The situation is even more confusing with ARIMA models, and some other model classes, because some functions use approximations to the likelihood, rather than the exact likelihood.</p>
<p>Thus, AIC values can be compared across models fitted using the same functions, but not necessarily when models have been fitted using different functions.</p>



 ]]></description>
  <category>AIC</category>
  <guid>https://robjhyndman.com/hyndsight/lm_aic.html</guid>
  <pubDate>Tue, 31 Oct 2023 13:00:00 GMT</pubDate>
  <media:content url="https://robjhyndman.com/img/aic_lm.png" medium="image" type="image/png" height="69" width="144"/>
</item>
<item>
  <title>P-values for prediction intervals</title>
  <link>https://robjhyndman.com/hyndsight/forecasting-pvalues.html</link>
  <description><![CDATA[ 



<div>
  <div class="description">
    Does it make any sense to compute p-values for prediction intervals?
  </div>
</div><p></p>


<p>I received this email today:</p>
<blockquote class="blockquote">
<p>My team recently used some techniques found in your writings to perform forecasts … Our work has been well received by reviewers, but one commenter asked two questions that I was hoping you may be able to provide insight on.</p>
</blockquote>
<blockquote class="blockquote">
<p>First, they wanted to know if we could provide P-values for our prediction intervals. In our work, we said, “Observed rates were deemed significantly different from expected rates when they did not fall within the 95% PI.” This same language has been used by others published in the same journal. I am curious to hear your thoughts on giving P-values for these PIs and what the appropriate method for doing so would be (if any).</p>
</blockquote>
<blockquote class="blockquote">
<p>Second, they asked about making a correction for multiple comparisons. … I believe we could apply a Bonferroni correction to the PIs, but that feels too liberal. Moreover, I am curious if this is even called for given our statement of what is deemed significant and the fact that our prediction interval construction relies on a non-parametric method.</p>
</blockquote>
<p>Here is my reply:</p>
<ol type="1">
<li><p>I don’t think this makes any sense. A p-value is the probability of obtaining observations at least as extreme as those observed given a null hypothesis. What’s the hypothesis here? In forecasting, we don’t usually have a hypothesis. Instead, we fit a model to the data, and make predictions based on the model. I guess you could make the null hypothesis “The future observations come from the forecast distributions”, and then the p-value for each future time period would be the probability of the tails beyond the observations. But it is well-known that the estimated prediction intervals are almost always too narrow due to them not taking into account all sources of variance. So the size of this test would not be well-calibrated. I think you’re better off pushing back rather than trying to meet the request.</p></li>
<li><p>A Bonferroni correction assumes independence between the intervals, and that is not true for PIs from a forecasting model. The future forecast errors are all correlated (with the strength of the correlation depending on the model and the DGP). Usually we just say that these are pointwise PI, and so we expect 5% of observations to fall outside the 95% prediction intervals. It is possible to generate uniform PI, which contain 95% of all future sample paths, but this is a little tricky due to the correlations between horizons. It could be done via simulation – simulate a 1000 future sample paths and compute the envelope that contains 950 of them.</p></li>
</ol>
<p>It sounds like the reviewers are only familiar with inferential statistics, and not with predictive modelling. You could point them to Shmueli’s excellent <a href="https://doi.org/10.1214/10-STS330">2010 paper “To explain or predict”</a>, highlighting the differences between the two paradigms.</p>



 ]]></description>
  <category>forecasting</category>
  <category>time series</category>
  <guid>https://robjhyndman.com/hyndsight/forecasting-pvalues.html</guid>
  <pubDate>Tue, 17 Oct 2023 13:00:00 GMT</pubDate>
  <media:content url="https://robjhyndman.com/img/PI.png" medium="image" type="image/png" height="71" width="144"/>
</item>
<item>
  <title>How NASA didn’t discover the hole in the ozone layer</title>
  <link>https://robjhyndman.com/hyndsight/ozone-hole-anomaly.html</link>
  <description><![CDATA[ <div>
  <div class="description">
    I am writing a new textbook on anomaly detection. It probably won’t be finished for at least a year, but here is an excerpt.
  </div>
</div>
<p></p>


<hr>
<p>There is a widespread myth that NASA did not discover the hole in the ozone layer above the Antarctic because they had been throwing away anomalous data that would have revealed it. This is not true, but the real story is also instructive <span class="citation" data-cites="Pukelsheim1990 Christie2001 christie2004data">(Pukelsheim 1990; Christie 2001, 2004)</span>.</p>
<p>NASA had been collecting satellite data on Antarctic ozone levels using a Total Ozone Mapping Spectrometer (TOMS) since 1979, while British scientists had collected ozone data using ground sensors at the Halley Research Station, on the edge of the Brunt Ice Shelf in Antarctica, since 1957. Figure&nbsp;1 shows average daily values from the NASA measurements in blue, and from the British observations in orange. There is a clear downward trend in the British data, especially from the late 1970s, which is confirmed with the NASA data. So why wasn’t the “ozone hole” discovered until 1985?</p>
<div class="cell">
<div class="cell-output-display">
<div id="fig-halley" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-halley-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://robjhyndman.com/hyndsight/ozone-hole-anomaly_files/figure-html/fig-halley-1.png" class="img-fluid figure-img" style="width:100.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-halley-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Observations of Antarctic ozone levels since 1957, measured in Dobson units (DU). Observations are mean daily values from October each year. Ground observations are from Halley Research Station, while satellite observations were obtained using a Total Ozone Mapping Spectrometer (TOMS). The satellite data were obtained from <a href="https://ozonewatch.gsfc.nasa.gov/facts/history_SH.html">Leslie R Lait (NASA)</a>, while the Halley ground observations were obtained from <a href="https://www.antarctica.ac.uk/met/jds/ozone/">Jonathan Shanklin (British Antarctic Survey)</a>. The horizontal line shows the threshold of 180 DU, used by NASA to determine when the ozone level was unusually low.
</figcaption></figure>
</div>
</div>
</div>
<p>The British scientists had noticed the low ozone values as early as 1981, but it took a few years for the scientists to be convinced that the low values were real and not due to instrument problems, and then there were the usual publication delays. Eventually, the results were published in <span class="citation" data-cites="Farman1985">Farman et al. (1985)</span>.</p>
<p>Meanwhile, NASA was flagging observations as anomalous when they were below 180 DU (shown as a horizontal line in Figure&nbsp;1). As is clear from the figure, this is much lower than any of the plotted points before the early 1980s. However, the 180 threshold was used for the <em>daily</em> measurements, which are much more variable than the monthly averages that are plotted. Occasionally daily observations did fall below 180, and so it was a reasonable threshold for the purpose of identifying instrument problems.</p>
<p>In fact, NASA had checked the unusually low TOMS values obtained before 1985 by comparing them against other available data. But the other data available to them showed ozone values of about 300 DU, so it was assumed that the satellite sensor was malfunctioning. The British Halley data were not available to them, and only after the publication of <span class="citation" data-cites="Farman1985">Farman et al. (1985)</span> did the NASA scientists realise that the TOMS results were accurate.</p>
<p>In 1986, NASA scientists were able to confirm the British finding, also demonstrating that the ozone hole was widespread across the Antarctic <span class="citation" data-cites="stolarski1986nimbus">(Stolarski et al. 1986)</span>.</p>
<p>This example reveals some lessons about anomaly detection:</p>
<ul>
<li>The NASA threshold of 180 was based on daily data, and was designed to identify instrument problems, not genuine systematic changes in ozone levels. The implicit assumption was that ozone levels varied seasonally, but that otherwise the distribution of observations was stable. All anomaly detection involves some implicit assumptions like this, and it is well to be aware of them.</li>
<li>Sometimes what we think are anomalies are not really anomalies, but the result of incorrect assumptions.</li>
<li>Often smoothing or averaging data will help to reveal issues that are not so obvious from the original data. This reduces the variation in the data, and allows more systematic variation to be uncovered.</li>
<li>Always plot the data. In this case, a graph such as Figure&nbsp;1 would have revealed the problem in the late 1970s, but it seems no-one was producing plots like this.</li>
</ul>




<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-Christie2001" class="csl-entry">
Christie, M. 2001. <em>The Ozone Layer: A Philosophy of Science Perspective</em>. Cambridge University Press.
</div>
<div id="ref-christie2004data" class="csl-entry">
Christie, M. 2004. <span>“Data Collection and the Ozone Hole: Too Much of a Good Thing?”</span> <em>History of Meteorology</em> 1: 99–105.
</div>
<div id="ref-Farman1985" class="csl-entry">
Farman, J C, B G Gardiner, and J D Shanklin. 1985. <span>“Large Losses of Total Ozone in <span>Antarctica</span> Reveal Seasonal <span>ClO<img src="https://latex.codecogs.com/png.latex?_x">/NO<img src="https://latex.codecogs.com/png.latex?_x"></span> Interaction.”</span> <em>Nature</em> 315 (6016): 207–10.
</div>
<div id="ref-Pukelsheim1990" class="csl-entry">
Pukelsheim, F. 1990. <span>“Robustness of Statistical Gossip and the <span>Antarctic</span> Ozone Hole.”</span> <em>The IMS Bulletin</em> 19 (4): 540–45.
</div>
<div id="ref-stolarski1986nimbus" class="csl-entry">
Stolarski, R S, A J Krueger, M R Schoeberl, R D McPeters, P A Newman, and J C Alpert. 1986. <span>“Nimbus 7 Satellite Measurements of the Springtime <span>Antarctic</span> Ozone Decrease.”</span> <em>Nature</em> 322 (6082): 808–11.
</div>
</div></section></div> ]]></description>
  <category>anomalies</category>
  <guid>https://robjhyndman.com/hyndsight/ozone-hole-anomaly.html</guid>
  <pubDate>Thu, 28 Sep 2023 14:00:00 GMT</pubDate>
  <media:content url="https://robjhyndman.com/img/fig-halley-1.png" medium="image" type="image/png" height="62" width="144"/>
</item>
<item>
  <title>Forecast model selection</title>
  <link>https://robjhyndman.com/hyndsight/model-selection.html</link>
  <description><![CDATA[ 



<div>
  <div class="description">
    When using a training/test split, or time-series cross-validation, are you choosing a specific model or a model class?
  </div>
</div><p></p>


<p>This question arises most time I teach a forecasting workshop, and it was raised again in the following email I received today:</p>
<blockquote class="blockquote">
<p>I have a time series that I have split into training and test datasets with an 80%-20% ratio. I fit a series of different models (ETS, BATS, ARIMA, NN etc) to the training data and generate my forecasts from each model. When evaluating the forecasts against the test set I find the model that gives the best outcome is an ARIMA(1,1,1) that was selected using the auto.arima function. My question is this, should I proceed to fit an ARIMA(1,1,1) to the whole data set, or should I use the auto.arima function again which may give me a slightly different (p,d,q) order as there is now an extra 20% of unseen data available to the forecast model? Any guidance would be greatly received.</p>
</blockquote>
<p>If you only have one class of model to consider (e.g., only ETS or only ARIMA), then it is easy enough to select the model on all available data using the AIC, and use the selected model to forecast the future. But if you are selecting between model classes, then you need to use either a training/test split, or (preferably) a <a href="https://otexts.com/fpp3/tscv.html">time-series cross-validation</a> procedure.</p>
<p>If you use time-series cross-validation, then there would usually be different models selected for each training set, and the cross-validated error is a measure of how well the model class works for your data. In that case, there is no single model for the training data, and you are selecting the <em>model class</em> rather than a specific model. This makes it clear that you should then apply the selected model class to all the data, when forecasting beyond the end of the available data. In other words, if you choose ARIMA over ETS, then you would then fit an ARIMA model to all the data, and use that model to forecast the future.</p>
<p>You can think of a simple training/test split as a special case of time-series cross-validation, where there is a single fold. So the same argument applies. That is, you are selecting the model class that works best for your data, and so you should apply that model class to all the data, when forecasting beyond the end of the available data.</p>
<p>This example also illustrates why it is important to use a time-series cross-validation procedure, rather than a simple training/test split. In this case, the ARIMA model was selected because it happened to work best for the particular training/test split that was used. But if a different split had been used, then a different model might have been selected. So the model selection is not stable. By averaging over multiple folds using a time-series cross-validation procedure, you can get a more stable estimate of the model class that works best for your data.</p>



 ]]></description>
  <category>forecasting</category>
  <category>time series</category>
  <guid>https://robjhyndman.com/hyndsight/model-selection.html</guid>
  <pubDate>Tue, 26 Sep 2023 14:00:00 GMT</pubDate>
  <media:content url="https://otexts.com/fpp3/fpp_files/figure-html/cv1-1.png" medium="image" type="image/png"/>
</item>
<item>
  <title>Videos for Forecasting: principles and practice (3rd ed)</title>
  <link>https://robjhyndman.com/hyndsight/fpp3_videos.html</link>
  <description><![CDATA[ 



<div>
  <div class="description">
    Over the past 6 months, George Athanasopoulos and I have added videos to most sections of the 3rd edition of our textbook <em>Forecasting: principles and practice</em>.
  </div>
</div><p></p>


<p>We have taught from the book many times, but this year we decided to pre-record short videos for each section. Our students often prefer a video explanation than reading the textbook, and we thought other readers might appreciate hearing from us as well.</p>
<p>These videos are embedded in most sections of the book. So far, we’ve covered the sections that we include in our own courses, but we hope to eventually have videos for all sections. Most of these were done in a single take, so they are sometimes a little rough, but hopefully still useful.</p>
<p>You can view the <a href="https://www.youtube.com/playlist?list=PLyCNZ_xXGzpm7W9jLqbIyBAiSO5jDwJeE">entire playlist on YouTube</a>.</p>



 ]]></description>
  <category>forecasting</category>
  <category>time series</category>
  <category>fpp</category>
  <category>otexts</category>
  <category>video</category>
  <guid>https://robjhyndman.com/hyndsight/fpp3_videos.html</guid>
  <pubDate>Thu, 20 Jul 2023 14:00:00 GMT</pubDate>
  <media:content url="https://robjhyndman.com/img/fpp3videos.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Degrees of freedom for a Ljung-Box test</title>
  <link>https://robjhyndman.com/hyndsight/ljung_box_df.html</link>
  <description><![CDATA[ <div>
  <div class="description">
    The Ljung-Box test is widely used to test for autocorrelation remaining in the residuals after fitting a model to a time series. In this post, I look at the degrees of freedom used in such tests.
  </div>
</div>
<p></p>

<section id="the-ljung-box-test" class="level2"><h2 class="anchored" data-anchor-id="the-ljung-box-test">The Ljung-Box test</h2>
<p>Suppose an ARMA(<img src="https://latex.codecogs.com/png.latex?p,q">) model is fitted to a time series of length <img src="https://latex.codecogs.com/png.latex?T">, giving a series of residuals <img src="https://latex.codecogs.com/png.latex?e_1,%5Cdots,e_T">, and let the autocorrelations of this residual series be denoted by <img src="https://latex.codecogs.com/png.latex?%0Ar_k%20=%20%5Csum_%7Bt=k+1%7D%5ET%20e_te_%7Bt-k%7D%20%5CBig/%20%5Csum_%7Bt=1%7D%5ET%20e_t%5E2,%20%5Cqquad%20k=1,2,%5Cdots%0A"> The first <img src="https://latex.codecogs.com/png.latex?%5Cell"> autocorrelations are used to construct the statistic <img src="https://latex.codecogs.com/png.latex?%0A%20%20Q%20=%20T(T+2)%20%5Csum_%7Bk=1%7D%5E%5Cell%20(T-k)%5E%7B-1%7Dr_k%5E2.%0A"></p>
<p>This statistic was discussed by <span class="citation" data-cites="BP70">Box and Pierce (1970)</span>, who argued that if <img src="https://latex.codecogs.com/png.latex?T"> is large, and the model parameters correspond to the true data generating process, then <img src="https://latex.codecogs.com/png.latex?Q"> has a <img src="https://latex.codecogs.com/png.latex?%5Cchi%5E2"> distribution with <img src="https://latex.codecogs.com/png.latex?%5Cell"> degrees of freedom. Later, <span class="citation" data-cites="LB78">Ljung and Box (1978)</span> showed that if the model is correct, but with unknown parameters, then <img src="https://latex.codecogs.com/png.latex?Q"> has a <img src="https://latex.codecogs.com/png.latex?%5Cchi%5E2"> distribution with <img src="https://latex.codecogs.com/png.latex?%5Cell-p-q"> degrees of freedom.</p>
</section><section id="extensions-to-other-models" class="level2"><h2 class="anchored" data-anchor-id="extensions-to-other-models">Extensions to other models</h2>
<p>These days, the Ljung-Box test is applied to a lot more models than non-seasonal ARMA models, and it is not clear what the degrees of freedom should be for other models. For example:</p>
<ul>
<li>What if the model includes an intercept term? Should that be included in the degrees of freedom calculation?</li>
<li>What about a seasonal ARIMA model? Do we just count all coefficients?</li>
<li>Or a regression with ARMA errors? Should we include the regression coefficients when computing the degrees of freedom?</li>
<li>Or an ETS model? Do we count just the smoothing parameters, or do we include the states as well, or something else?</li>
</ul>
<p>Not long ago, I had naively assumed that the correct degrees of freedom would be <img src="https://latex.codecogs.com/png.latex?%5Cell-K"> where <img src="https://latex.codecogs.com/png.latex?K"> is the number of parameters estimated. I am in good company because Andrew Harvey in <span class="citation" data-cites="harvey90">Harvey (1990, p259)</span> made exactly the same conjecture. That was what was coded in the <a href="https://pkg.robjhyndman.com/forecast/reference/checkresiduals.html"><code>forecast::checkresiduals()</code></a> function prior to v8.21, and how the test was applied in <span class="citation" data-cites="fpp2">Hyndman and Athanasopoulos (2018)</span> and <span class="citation" data-cites="fpp3">Hyndman and Athanasopoulos (2021)</span> until February 2023. But a recent <a href="https://github.com/robjhyndman/forecast/issues/908">github discussion with Achim Zeilis</a> convinced me that it is incorrect.</p>
<p>Let’s look at a few examples. For each model, we will simulate 5000 series, each of length 250 observations. For each series, we compute the p-value of a Ljung-Box test with <img src="https://latex.codecogs.com/png.latex?%5Cell=10"> and <img src="https://latex.codecogs.com/png.latex?%5Cell-K"> degrees of freedom, for different values of <img src="https://latex.codecogs.com/png.latex?K">. Under the null hypothesis of uncorrelated residuals, the <img src="https://latex.codecogs.com/png.latex?p"> values should have a uniform distribution.</p>
<div class="cell" data-hash="ljung_box_df_cache/html/setup_73aa324bb41a8896d092ef7907d377c6">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://pkg.robjhyndman.com/forecast/">forecast</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org">ggplot2</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Random.html">set.seed</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Function to simulate p-values given a DGP model and</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># a function to fit the model to a time series</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">simulate_pvalue</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit_fn</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">l</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## simulate series</span></span>
<span>  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/NULL.html">is.null</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">xreg</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/simulate.html">simulate</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span>, n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">250</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/simulate.html">simulate</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span>, xreg<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">xreg</span>, n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">250</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span>  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/class.html">inherits</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ets"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If multiplicative errors, fix non-positive values</span></span>
<span>    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">components</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"M"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>      <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-5</span></span>
<span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## Fit model</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">m</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit_fn</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## compute p-values for various df</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pv</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">purrr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://purrr.tidyverse.org/reference/map.html">map_vec</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">m</span>,</span>
<span>    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">m</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/box.test.html">Box.test</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/residuals.html">residuals</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">m</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, lag <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">l</span>, fitdf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ljung-Box"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">p.value</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/names.html">names</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pv</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/paste.html">paste</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"K ="</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/function.html">return</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pv</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Function to replicate the above function</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">simulate_pvalues</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit_fn</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nsim</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">l</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">purrr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://purrr.tidyverse.org/reference/map_dfr.html">map_dfr</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/seq.html">seq</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nsim</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simulate_pvalue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit_fn</span>, l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">l</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Histograms of p values</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hist_pvalues</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pv</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pv</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tidyr.tidyverse.org/reference/pivot_longer.html">pivot_longer</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>cols <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/seq.html">seq</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/nrow.html">NCOL</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">arima_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/geom_histogram.html">geom_histogram</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>bins <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, boundary <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/facet_grid.html">facet_grid</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">name</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/labs.html">labs</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P value distributions"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># A nice table of the size of the test</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">table_pvalues</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pv</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tibble.tidyverse.org/reference/tibble.html">tibble</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>`test size` <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/bind_cols.html">bind_cols</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">purrr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://purrr.tidyverse.org/reference/map_dfr.html">map_df</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pv</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/ecdf.html">ecdf</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">`test size`</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">knitr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/pkg/knitr/man/kable.html">kable</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</details>
</div>
</section><section id="arima-models-with-an-intercept" class="level2"><h2 class="anchored" data-anchor-id="arima-models-with-an-intercept">ARIMA models with an intercept</h2>
<p>We will simulate from an ARIMA(2,0,0) model with a non-zero intercept. For the Ljung-Box test, we will consider <img src="https://latex.codecogs.com/png.latex?0%20%5Cle%20K%20%5Cle%203">. Note that <img src="https://latex.codecogs.com/png.latex?K=0"> was the original proposal by <span class="citation" data-cites="BP70">Box and Pierce (1970)</span>, <img src="https://latex.codecogs.com/png.latex?K=2=p+q"> counts only ARMA coefficients, and <img src="https://latex.codecogs.com/png.latex?K=3"> counts all parameters estimated in the model. The resulting distributions of the p-values are shown below.</p>
<div class="cell" data-hash="ljung_box_df_cache/html/ARIMA_bf5b946ec3736ef80cfade5f8f0a5bbd">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pkg.robjhyndman.com/forecast/reference/Arima.html">Arima</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/MathFun.html">sqrt</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">lynx</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, order<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit_fn</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pkg.robjhyndman.com/forecast/reference/Arima.html">Arima</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span>, order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, include.mean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">arima_pvalues</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simulate_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit_fn</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">arima_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://robjhyndman.com/hyndsight/ljung_box_df_files/figure-html/ARIMA-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">arima_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead><tr class="header">
<th style="text-align: right;">test size</th>
<th style="text-align: right;">K = 0</th>
<th style="text-align: right;">K = 1</th>
<th style="text-align: right;">K = 2</th>
<th style="text-align: right;">K = 3</th>
</tr></thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">0.01</td>
<td style="text-align: right;">0.0046</td>
<td style="text-align: right;">0.0072</td>
<td style="text-align: right;">0.0124</td>
<td style="text-align: right;">0.0220</td>
</tr>
<tr class="even">
<td style="text-align: right;">0.05</td>
<td style="text-align: right;">0.0226</td>
<td style="text-align: right;">0.0354</td>
<td style="text-align: right;">0.0534</td>
<td style="text-align: right;">0.0818</td>
</tr>
<tr class="odd">
<td style="text-align: right;">0.10</td>
<td style="text-align: right;">0.0474</td>
<td style="text-align: right;">0.0678</td>
<td style="text-align: right;">0.1016</td>
<td style="text-align: right;">0.1514</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Clearly the one with <img src="https://latex.codecogs.com/png.latex?K=2"> is better than the alternatives. The table shows the empirical size of the test for different threshold levels. The empirical sizes are closest to the nominal sizes when <img src="https://latex.codecogs.com/png.latex?K=2=p+q">. So we shouldn’t count the intercept when computing the degrees of freedom.</p>
</section><section id="seasonal-arima-model" class="level2"><h2 class="anchored" data-anchor-id="seasonal-arima-model">Seasonal ARIMA model</h2>
<p>Next, we will simulate from an ARIMA(0,1,1)(0,1,1)<img src="https://latex.codecogs.com/png.latex?_%7B12%7D"> model, often called the “airline model” due to its application to the Air passenger series in <span class="citation" data-cites="BJ2016">Box et al. (2016)</span>. In fact, our DGP for the simulations will be a model fitted to the <code>AirPassengers</code> data set. Again, we consider <img src="https://latex.codecogs.com/png.latex?0%5Cle%20K%20%5Cle%203">. There are two parameters to be estimated.</p>
<div class="cell" data-hash="ljung_box_df_cache/html/SARIMA_4f343c5c334a772e904f79bb38dbff4b">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pkg.robjhyndman.com/forecast/reference/Arima.html">Arima</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Log.html">log</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">AirPassengers</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, order<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, seasonal<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit_fn</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pkg.robjhyndman.com/forecast/reference/Arima.html">Arima</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span>, order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, seasonal<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sarima_pvalues</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simulate_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit_fn</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sarima_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://robjhyndman.com/hyndsight/ljung_box_df_files/figure-html/SARIMA-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sarima_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead><tr class="header">
<th style="text-align: right;">test size</th>
<th style="text-align: right;">K = 0</th>
<th style="text-align: right;">K = 1</th>
<th style="text-align: right;">K = 2</th>
<th style="text-align: right;">K = 3</th>
</tr></thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">0.01</td>
<td style="text-align: right;">0.0100</td>
<td style="text-align: right;">0.0170</td>
<td style="text-align: right;">0.0282</td>
<td style="text-align: right;">0.0456</td>
</tr>
<tr class="even">
<td style="text-align: right;">0.05</td>
<td style="text-align: right;">0.0484</td>
<td style="text-align: right;">0.0684</td>
<td style="text-align: right;">0.1018</td>
<td style="text-align: right;">0.1452</td>
</tr>
<tr class="odd">
<td style="text-align: right;">0.10</td>
<td style="text-align: right;">0.0882</td>
<td style="text-align: right;">0.1218</td>
<td style="text-align: right;">0.1774</td>
<td style="text-align: right;">0.2526</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Interesting. Although there are two parameters here, the tests with <img src="https://latex.codecogs.com/png.latex?K=0"> and <img src="https://latex.codecogs.com/png.latex?K=1"> do better than <img src="https://latex.codecogs.com/png.latex?K=2">. I would have expected <img src="https://latex.codecogs.com/png.latex?K=p+q+P+Q"> to be the right choice, but the test with <img src="https://latex.codecogs.com/png.latex?K=2"> has empirical size about twice the nominal size.</p>
<p>As a guess, perhaps the seasonal parameters aren’t having an effect with <img src="https://latex.codecogs.com/png.latex?%5Cell=10">. We can test what happens for larger <img src="https://latex.codecogs.com/png.latex?%5Cell"> by setting <img src="https://latex.codecogs.com/png.latex?%5Cell=24"> (covering two years), and repeating the exercise.</p>
<div class="cell" data-hash="ljung_box_df_cache/html/SARIMA2_dc188e115e90f0c6d881e8c3058e4d5f">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sarima_pvalues</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simulate_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit_fn</span>, l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sarima_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://robjhyndman.com/hyndsight/ljung_box_df_files/figure-html/SARIMA2-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sarima_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead><tr class="header">
<th style="text-align: right;">test size</th>
<th style="text-align: right;">K = 0</th>
<th style="text-align: right;">K = 1</th>
<th style="text-align: right;">K = 2</th>
<th style="text-align: right;">K = 3</th>
</tr></thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">0.01</td>
<td style="text-align: right;">0.0134</td>
<td style="text-align: right;">0.0164</td>
<td style="text-align: right;">0.0216</td>
<td style="text-align: right;">0.0278</td>
</tr>
<tr class="even">
<td style="text-align: right;">0.05</td>
<td style="text-align: right;">0.0492</td>
<td style="text-align: right;">0.0614</td>
<td style="text-align: right;">0.0748</td>
<td style="text-align: right;">0.0940</td>
</tr>
<tr class="odd">
<td style="text-align: right;">0.10</td>
<td style="text-align: right;">0.0864</td>
<td style="text-align: right;">0.1066</td>
<td style="text-align: right;">0.1312</td>
<td style="text-align: right;">0.1590</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>I was expecting <img src="https://latex.codecogs.com/png.latex?K=2"> to do best there, but not so. <img src="https://latex.codecogs.com/png.latex?K=1"> is the most uniform, and <img src="https://latex.codecogs.com/png.latex?K=0"> gives empirical sizes closest to the nominal sizes, with the results getting worse as <img src="https://latex.codecogs.com/png.latex?K"> increases. Perhaps always setting <img src="https://latex.codecogs.com/png.latex?K=p+q"> would be a sensible strategy for ARIMA models, even if they contain seasonal components. This needs some theoretical analysis.</p>
</section><section id="regression-with-arma-errors" class="level2"><h2 class="anchored" data-anchor-id="regression-with-arma-errors">Regression with ARMA errors</h2>
<p>We will simulate from a linear trend model with AR(1) errors. Here, <img src="https://latex.codecogs.com/png.latex?K=1"> counts only ARMA coefficients, while <img src="https://latex.codecogs.com/png.latex?K=3"> counts all parameters estimated. The resulting distributions of the p-values are shown below.</p>
<div class="cell" data-hash="ljung_box_df_cache/html/RegARIMA_cbaf48eec0adfe101d66336169d575ee">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pkg.robjhyndman.com/forecast/reference/Arima.html">Arima</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/seq.html">seq</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">250</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/arima.sim.html">arima.sim</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">250</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>               order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, xreg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/seq.html">seq</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">250</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit_fn</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pkg.robjhyndman.com/forecast/reference/Arima.html">Arima</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span>, order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, include.constant <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, xreg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/seq.html">seq</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">250</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">regarima_pvalues</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simulate_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit_fn</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">regarima_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://robjhyndman.com/hyndsight/ljung_box_df_files/figure-html/RegARIMA-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">regarima_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead><tr class="header">
<th style="text-align: right;">test size</th>
<th style="text-align: right;">K = 0</th>
<th style="text-align: right;">K = 1</th>
<th style="text-align: right;">K = 2</th>
<th style="text-align: right;">K = 3</th>
</tr></thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">0.01</td>
<td style="text-align: right;">0.0072</td>
<td style="text-align: right;">0.0104</td>
<td style="text-align: right;">0.0176</td>
<td style="text-align: right;">0.0294</td>
</tr>
<tr class="even">
<td style="text-align: right;">0.05</td>
<td style="text-align: right;">0.0310</td>
<td style="text-align: right;">0.0532</td>
<td style="text-align: right;">0.0782</td>
<td style="text-align: right;">0.1200</td>
</tr>
<tr class="odd">
<td style="text-align: right;">0.10</td>
<td style="text-align: right;">0.0696</td>
<td style="text-align: right;">0.0996</td>
<td style="text-align: right;">0.1478</td>
<td style="text-align: right;">0.2150</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>The test with <img src="https://latex.codecogs.com/png.latex?K=1"> looks the most uniform, with the size of the test closest to the nominal values. So only counting ARMA coefficients seems to be correct here.</p>
</section><section id="regression-model" class="level2"><h2 class="anchored" data-anchor-id="regression-model">Regression model</h2>
<p>Next, we will consider a linear trend model with iid errors. That is the same as the previous model, but with a simpler error structure.</p>
<div class="cell" data-hash="ljung_box_df_cache/html/trend_e646b62c4f784041c6d6ca8ffbfb8c90">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pkg.robjhyndman.com/forecast/reference/Arima.html">Arima</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/seq.html">seq</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">250</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">250</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>               order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, xreg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/seq.html">seq</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">250</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit_fn</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pkg.robjhyndman.com/forecast/reference/Arima.html">Arima</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span>, include.constant <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, xreg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/seq.html">seq</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">250</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">trend_pvalues</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simulate_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit_fn</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">trend_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://robjhyndman.com/hyndsight/ljung_box_df_files/figure-html/trend-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">trend_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead><tr class="header">
<th style="text-align: right;">test size</th>
<th style="text-align: right;">K = 0</th>
<th style="text-align: right;">K = 1</th>
<th style="text-align: right;">K = 2</th>
<th style="text-align: right;">K = 3</th>
</tr></thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">0.01</td>
<td style="text-align: right;">0.0148</td>
<td style="text-align: right;">0.0214</td>
<td style="text-align: right;">0.0338</td>
<td style="text-align: right;">0.0554</td>
</tr>
<tr class="even">
<td style="text-align: right;">0.05</td>
<td style="text-align: right;">0.0580</td>
<td style="text-align: right;">0.0844</td>
<td style="text-align: right;">0.1204</td>
<td style="text-align: right;">0.1746</td>
</tr>
<tr class="odd">
<td style="text-align: right;">0.10</td>
<td style="text-align: right;">0.1054</td>
<td style="text-align: right;">0.1478</td>
<td style="text-align: right;">0.2062</td>
<td style="text-align: right;">0.2816</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>The test with <img src="https://latex.codecogs.com/png.latex?K=0"> looks best. If we think of a regression model as a RegARIMA model with ARIMA(0,0,0) errors, this is consistent with the previous results, setting <img src="https://latex.codecogs.com/png.latex?K=p+q">.</p>
</section><section id="etsann-model" class="level2"><h2 class="anchored" data-anchor-id="etsann-model">ETS(A,N,N) model</h2>
<p>Now let’s try an ETS(A,N,N) model, again using 5000 series each of length 250. If we count only the smoothing parameter, <img src="https://latex.codecogs.com/png.latex?K=1">, but if we count all estimated parameters, <img src="https://latex.codecogs.com/png.latex?K=2">. The distributions of p-values are shown below.</p>
<div class="cell" data-hash="ljung_box_df_cache/html/ETSANN_9f1c0d5c8fff6f356206cab28c8540d1">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pkg.robjhyndman.com/forecast/reference/ets.html">ets</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fma</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://pkg.robjhyndman.com/fma/reference/strikes.html">strikes</a></span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ANN"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit_fn</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pkg.robjhyndman.com/forecast/reference/ets.html">ets</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span>, model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ANN"</span>, damped <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ets_pvalues</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simulate_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit_fn</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ets_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://robjhyndman.com/hyndsight/ljung_box_df_files/figure-html/ETSANN-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ets_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead><tr class="header">
<th style="text-align: right;">test size</th>
<th style="text-align: right;">K = 0</th>
<th style="text-align: right;">K = 1</th>
<th style="text-align: right;">K = 2</th>
<th style="text-align: right;">K = 3</th>
</tr></thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">0.01</td>
<td style="text-align: right;">0.0064</td>
<td style="text-align: right;">0.0112</td>
<td style="text-align: right;">0.0174</td>
<td style="text-align: right;">0.0316</td>
</tr>
<tr class="even">
<td style="text-align: right;">0.05</td>
<td style="text-align: right;">0.0334</td>
<td style="text-align: right;">0.0522</td>
<td style="text-align: right;">0.0778</td>
<td style="text-align: right;">0.1196</td>
</tr>
<tr class="odd">
<td style="text-align: right;">0.10</td>
<td style="text-align: right;">0.0702</td>
<td style="text-align: right;">0.0960</td>
<td style="text-align: right;">0.1442</td>
<td style="text-align: right;">0.2098</td>
</tr>
</tbody>
</table>
</div>
</div>
<p><img src="https://latex.codecogs.com/png.latex?K=1"> looks about right. That makes sense as an ETS(A,N,N) model is equivalent to an ARIMA(0,1,1) model.</p>
</section><section id="etsmnn-model" class="level2"><h2 class="anchored" data-anchor-id="etsmnn-model">ETS(M,N,N) model</h2>
<p>Next, let’s try an ETS(M,N,N) model, which has no ARIMA equivalent, but which has one smoothing parameter and one initial state to estimate.</p>
<div class="cell" data-hash="ljung_box_df_cache/html/ETSMNN_f5812bbdc26dd90d28e49ba2533a74d9">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pkg.robjhyndman.com/forecast/reference/ets.html">ets</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fma</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://pkg.robjhyndman.com/fma/reference/strikes.html">strikes</a></span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MNN"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit_fn</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pkg.robjhyndman.com/forecast/reference/ets.html">ets</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span>, model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MNN"</span>, damped <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ets_pvalues</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simulate_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit_fn</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ets_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://robjhyndman.com/hyndsight/ljung_box_df_files/figure-html/ETSMNN-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ets_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead><tr class="header">
<th style="text-align: right;">test size</th>
<th style="text-align: right;">K = 0</th>
<th style="text-align: right;">K = 1</th>
<th style="text-align: right;">K = 2</th>
<th style="text-align: right;">K = 3</th>
</tr></thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">0.01</td>
<td style="text-align: right;">0.0054</td>
<td style="text-align: right;">0.0084</td>
<td style="text-align: right;">0.0172</td>
<td style="text-align: right;">0.0308</td>
</tr>
<tr class="even">
<td style="text-align: right;">0.05</td>
<td style="text-align: right;">0.0334</td>
<td style="text-align: right;">0.0548</td>
<td style="text-align: right;">0.0814</td>
<td style="text-align: right;">0.1274</td>
</tr>
<tr class="odd">
<td style="text-align: right;">0.10</td>
<td style="text-align: right;">0.0708</td>
<td style="text-align: right;">0.1062</td>
<td style="text-align: right;">0.1520</td>
<td style="text-align: right;">0.2136</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Again, <img src="https://latex.codecogs.com/png.latex?K=1"> appears to be the best.</p>
</section><section id="etsaan-model" class="level2"><h2 class="anchored" data-anchor-id="etsaan-model">ETS(A,A,N) model</h2>
<p>An ETS(A,A,N) model is equivalent to an ARIMA(0,2,2) model, so I expect this one to need <img src="https://latex.codecogs.com/png.latex?K=2">.</p>
<div class="cell" data-hash="ljung_box_df_cache/html/ETSAAN_ce0bcb2507c97eab13f6cc5c746220c5">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pkg.robjhyndman.com/forecast/reference/ets.html">ets</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fma</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://pkg.robjhyndman.com/fma/reference/strikes.html">strikes</a></span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AAN"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit_fn</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pkg.robjhyndman.com/forecast/reference/ets.html">ets</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span>, model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AAN"</span>, damped <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ets_pvalues</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simulate_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit_fn</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ets_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://robjhyndman.com/hyndsight/ljung_box_df_files/figure-html/ETSAAN-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ets_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead><tr class="header">
<th style="text-align: right;">test size</th>
<th style="text-align: right;">K = 0</th>
<th style="text-align: right;">K = 1</th>
<th style="text-align: right;">K = 2</th>
<th style="text-align: right;">K = 3</th>
</tr></thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">0.01</td>
<td style="text-align: right;">0.0034</td>
<td style="text-align: right;">0.0062</td>
<td style="text-align: right;">0.0106</td>
<td style="text-align: right;">0.0172</td>
</tr>
<tr class="even">
<td style="text-align: right;">0.05</td>
<td style="text-align: right;">0.0180</td>
<td style="text-align: right;">0.0308</td>
<td style="text-align: right;">0.0514</td>
<td style="text-align: right;">0.0802</td>
</tr>
<tr class="odd">
<td style="text-align: right;">0.10</td>
<td style="text-align: right;">0.0430</td>
<td style="text-align: right;">0.0658</td>
<td style="text-align: right;">0.1008</td>
<td style="text-align: right;">0.1486</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>This time, my conjecture is correct, and <img src="https://latex.codecogs.com/png.latex?K=2"> works well.</p>
</section><section id="etsaaa-model" class="level2"><h2 class="anchored" data-anchor-id="etsaaa-model">ETS(A,A,A) model</h2>
<p>Finally, we will check a seasonal ETS model</p>
<div class="cell" data-hash="ljung_box_df_cache/html/ETSAAA_1385d6238e693baceddf0a02ce3fa351">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pkg.robjhyndman.com/forecast/reference/ets.html">ets</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Log.html">log</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">AirPassengers</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, model<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AAA"</span>, damped<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit_fn</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pkg.robjhyndman.com/forecast/reference/ets.html">ets</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span>, model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AAA"</span>, damped <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ets_pvalues</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simulate_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit_fn</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ets_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://robjhyndman.com/hyndsight/ljung_box_df_files/figure-html/ETSAAA-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb19" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ets_pvalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead><tr class="header">
<th style="text-align: right;">test size</th>
<th style="text-align: right;">K = 0</th>
<th style="text-align: right;">K = 1</th>
<th style="text-align: right;">K = 2</th>
<th style="text-align: right;">K = 3</th>
</tr></thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">0.01</td>
<td style="text-align: right;">0.0178</td>
<td style="text-align: right;">0.0276</td>
<td style="text-align: right;">0.0396</td>
<td style="text-align: right;">0.0602</td>
</tr>
<tr class="even">
<td style="text-align: right;">0.05</td>
<td style="text-align: right;">0.0634</td>
<td style="text-align: right;">0.0916</td>
<td style="text-align: right;">0.1360</td>
<td style="text-align: right;">0.1942</td>
</tr>
<tr class="odd">
<td style="text-align: right;">0.10</td>
<td style="text-align: right;">0.1206</td>
<td style="text-align: right;">0.1672</td>
<td style="text-align: right;">0.2244</td>
<td style="text-align: right;">0.3078</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Here there are 3 smoothing parameters, and 13 initial states to estimate. So I was expecting <img src="https://latex.codecogs.com/png.latex?K=3"> to do best, but it is the worst. Instead, <img src="https://latex.codecogs.com/png.latex?K=0"> is the best. I’m not sure what to make of this result.</p>
</section><section id="conclusions" class="level2"><h2 class="anchored" data-anchor-id="conclusions">Conclusions</h2>
<p>Based only on this empirical evidence:</p>
<ul>
<li>For ARIMA models, use <img src="https://latex.codecogs.com/png.latex?%5Cell-p-q"> degrees of freedom.</li>
<li>For seasonal ARIMA models, it appears that <img src="https://latex.codecogs.com/png.latex?%5Cell-p-q"> also gives the best results.</li>
<li>For regression with ARIMA errors, use <img src="https://latex.codecogs.com/png.latex?%5Cell-p-q"> degrees of freedom.</li>
<li>For OLS regression, use <img src="https://latex.codecogs.com/png.latex?%5Cell"> degrees of freedom.</li>
<li>For non-seasonal ETS models, use <img src="https://latex.codecogs.com/png.latex?K="> the number of smoothing parameters.</li>
<li>For seasonal ETS models, use <img src="https://latex.codecogs.com/png.latex?K=0">.</li>
</ul>
<p>The last two of these appear to be contradictory, and it is not clear why.</p>
<p>It seems like this might be a good project for a PhD student to explore. In particular, can these suggestions based on empirical evidence be supported theoretically? It would also be good to explore other models such as TBATS, ARFIMA, NNETAR, etc.</p>
<p>For now, I might avoid teaching the Ljung-Box test, and just get students to look at the ACF plot of the residuals instead.</p>
</section><section id="other-literature" class="level2"><h2 class="anchored" data-anchor-id="other-literature">Other literature</h2>
<ul>
<li>
<span class="citation" data-cites="Kim2004">Kim et al. (2004)</span> shows that <img src="https://latex.codecogs.com/png.latex?Q%20%5Csim%20%5Cchi%5E2_%5Cell"> for an AR(1) model with ARCH errors.</li>
<li>
<span class="citation" data-cites="McLeod1983">McLeod and Li (1983)</span> consider the equivalent test applied to autocorrelations of squared residuals, and show that <img src="https://latex.codecogs.com/png.latex?Q%5E*%20%5Csim%20%5Cchi%5E2_%5Cell">.</li>
<li>
<span class="citation" data-cites="Mahdi2016">Mahdi (2016)</span> discusses a variation on the LB test for seasonal ARIMA models considering only autocorrelations at the seasonal lags.</li>
<li>Several other portmanteau tests (i.e., based on multiple autocorrelations) are available, and perhaps we should be using them and not the older Ljung-Box test. See <span class="citation" data-cites="Mahdi2021">Mahdi (2021)</span> for some recent developments.</li>
</ul></section><section id="references" class="level2">



</section><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-BJ2016" class="csl-entry">
Box, George E P, Gwilym M Jenkins, Gregory C Reinsel, and Greta M Ljung. 2016. <em>Time Series Analysis: Forecasting and Control</em>. 5th ed. John Wiley; Sons.
</div>
<div id="ref-BP70" class="csl-entry">
Box, George E P, and David A Pierce. 1970. <span>“Distribution of Residual Autocorrelations in Autoregressive-Integrated Moving Average Time Series Models.”</span> <em>Journal of the American Statistical Association</em> 65 (332): 1509–26. <a href="https://doi.org/10.2307/2284333">https://doi.org/10.2307/2284333</a>.
</div>
<div id="ref-harvey90" class="csl-entry">
Harvey, Andrew C. 1990. <em>Forecasting, Structural Time Series Models and the Kalman Filter</em>. Cambridge University Press.
</div>
<div id="ref-fpp2" class="csl-entry">
Hyndman, Rob J, and George Athanasopoulos. 2018. <em>Forecasting: Principles and Practice</em>. 2nd ed. OTexts. <a href="https://OTexts.org/fpp2">OTexts.org/fpp2</a>.
</div>
<div id="ref-fpp3" class="csl-entry">
Hyndman, Rob J, and George Athanasopoulos. 2021. <em>Forecasting: Principles and Practice</em>. 3rd ed. OTexts. <a href="https://OTexts.org/fpp3">OTexts.org/fpp3</a>.
</div>
<div id="ref-Kim2004" class="csl-entry">
Kim, Eunhee, Jeongcheol Ha, Youngsook Jeon, and Sangyeol Lee. 2004. <span>“Ljung-Box Test in Unit Root <span>AR-ARCH</span> Model.”</span> <em>Communications for Statistical Applications and Methods</em> 11 (2): 323–27. <a href="https://doi.org/10.5351/ckss.2004.11.2.323">https://doi.org/10.5351/ckss.2004.11.2.323</a>.
</div>
<div id="ref-LB78" class="csl-entry">
Ljung, Greta M, and George E P Box. 1978. <span>“On a Measure of Lack of Fit in Time Series Models.”</span> <em>Biometrika</em> 65 (2): 297–303. <a href="https://doi.org/10.1093/biomet/65.2.297">https://doi.org/10.1093/biomet/65.2.297</a>.
</div>
<div id="ref-Mahdi2016" class="csl-entry">
Mahdi, Esam. 2016. <span>“Portmanteau Test Statistics for Seasonal Serial Correlation in Time Series Models.”</span> <em>SpringerPlus</em> 5 (1): 1485. <a href="https://doi.org/10.1186/s40064-016-3167-4">https://doi.org/10.1186/s40064-016-3167-4</a>.
</div>
<div id="ref-Mahdi2021" class="csl-entry">
Mahdi, Esam. 2021. <span>“New Goodness-of-Fit Tests for Time Series Models.”</span> <a href="http://arxiv.org/abs/2008.08176">http://arxiv.org/abs/2008.08176</a>.
</div>
<div id="ref-McLeod1983" class="csl-entry">
McLeod, A I, and W K Li. 1983. <span>“Diagnostic Checking <span>ARMA</span> Time Series Models Using Squared-Residual Autocorrelations.”</span> <em>Journal of Time Series Analysis</em> 4 (4): 269–73. <a href="https://doi.org/10.1111/j.1467-9892.1983.tb00373.x">https://doi.org/10.1111/j.1467-9892.1983.tb00373.x</a>.
</div>
</div></section></div> ]]></description>
  <category>forecasting</category>
  <category>time series</category>
  <guid>https://robjhyndman.com/hyndsight/ljung_box_df.html</guid>
  <pubDate>Tue, 27 Jun 2023 14:00:00 GMT</pubDate>
  <media:content url="https://robjhyndman.com/img/ETSMNN.png" medium="image" type="image/png" height="54" width="144"/>
</item>
<item>
  <title>Forecasting podcasts</title>
  <link>https://robjhyndman.com/hyndsight/podcasts/</link>
  <description><![CDATA[ <p>I’ve been interviewed for several podcasts over the last few years. It’s always fun to talk about my work, and I hope there are enough differences between them to make it interesting for listeners. Here is a full list of them.</p>
<div class="cell">
<div class="cell-output-display">
<table class="table caption-top table-sm table-striped small">
<thead><tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">Date</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">Podcast</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">Episode</th>
</tr></thead>
<tbody>
<tr class="odd">
<td style="text-align: left; width: 25%;">26 May 2023</td>
<td style="text-align: left; width: 30%; font-weight: bold;">Forecasting Impact</td>
<td style="text-align: left; width: 45%;"><a href="https://forecastingimpact.buzzsprout.com/1641538/12809499-forecasting-software-panel">Forecasting software panel</a></td>
</tr>
<tr class="even">
<td style="text-align: left; width: 25%;">14 March 2022</td>
<td style="text-align: left; width: 30%; font-weight: bold;">Faculty.net</td>
<td style="text-align: left; width: 45%;"><a href="https://faculti.net/forecasting-in-social-settings/">Forecasting in social settings</a></td>
</tr>
<tr class="odd">
<td style="text-align: left; width: 25%;">17 November 2021</td>
<td style="text-align: left; width: 30%; font-weight: bold;">The Random Sample</td>
<td style="text-align: left; width: 45%;"><a href="https://www.therandomsample.com.au/podcast/software-as-a-first-class-research-output/">Software as a first class research output</a></td>
</tr>
<tr class="even">
<td style="text-align: left; width: 25%;">24 May 2021</td>
<td style="text-align: left; width: 30%; font-weight: bold;">Data Skeptic</td>
<td style="text-align: left; width: 45%;"><a href="https://podcastaddict.com/episode/123511469">Forecasting principles and practice</a></td>
</tr>
<tr class="odd">
<td style="text-align: left; width: 25%;">12 April 2021</td>
<td style="text-align: left; width: 30%; font-weight: bold;">Seriously Social</td>
<td style="text-align: left; width: 45%;"><a href="https://seriouslysocial.org.au/podcasts/forecasting-the-future-the-science-of-prediction/">Forecasting the future: the science of prediction</a></td>
</tr>
<tr class="even">
<td style="text-align: left; width: 25%;">6 February 2021</td>
<td style="text-align: left; width: 30%; font-weight: bold;">Forecasting Impact</td>
<td style="text-align: left; width: 45%;"><a href="https://forecastingimpact.buzzsprout.com/1641538/7708129-rob-hyndman">Rob Hyndman</a></td>
</tr>
<tr class="odd">
<td style="text-align: left; width: 25%;">19 July 2020</td>
<td style="text-align: left; width: 30%; font-weight: bold;">The Curious Quant</td>
<td style="text-align: left; width: 45%;"><a href="https://share.transistor.fm/s/dc2dfd76">Forecasting COVID, time series, and why causality doesnt matter as much as you think</a></td>
</tr>
<tr class="even">
<td style="text-align: left; width: 25%;">27 May 2020</td>
<td style="text-align: left; width: 30%; font-weight: bold;">The Random Sample</td>
<td style="text-align: left; width: 45%;"><a href="https://www.therandomsample.com.au/podcast/forecasting-the-future-the-future-of-forecasting/">Forecasting the future &amp; the future of forecasting</a></td>
</tr>
<tr class="odd">
<td style="text-align: left; width: 25%;">9 October 2019</td>
<td style="text-align: left; width: 30%; font-weight: bold;">Thought Capital</td>
<td style="text-align: left; width: 45%;"><a href="https://impact.monash.edu/podcasts/forecasts-are-always-wrong-but-we-need-them-anyway/">Forecasts are always wrong (but we need them anyway)</a></td>
</tr>
</tbody>
</table>
</div>
</div>



 ]]></description>
  <category>forecasting</category>
  <category>podcast</category>
  <guid>https://robjhyndman.com/hyndsight/podcasts/</guid>
  <pubDate>Thu, 25 May 2023 14:00:00 GMT</pubDate>
  <media:content url="https://robjhyndman.com/img/podcasts.png" medium="image" type="image/png" height="37" width="144"/>
</item>
<item>
  <title>Forecasting workshops in New York and Chicago</title>
  <link>https://robjhyndman.com/hyndsight/workshops2023.html</link>
  <description><![CDATA[ 



<div>
  <div class="description">
    I’m giving a 2-day workshop on “Tidy Time Series and Forecasting in R”, first at the New York R Conference in July, and then at the Posit Conference in Chicago in September.
  </div>
</div><p></p>


<p>Places are limited, so please book in early.</p>
<ul>
<li><a href="https://rstats.ai/nyr#tidy-time-series-and-forecasting-in-r">New York</a>, 11-12 July 2023. Followed by the <a href="https://rstats.ai/nyr">New York R conference</a>. (Register for the workshop as part of the NYR conference registration.)</li>
<li><a href="https://posit-conf-2023.github.io/workshops/workshops/forecasting/">Chicago</a>, 17-18 September 2023. Followed by the <a href="https://posit.co/conference">Posit conference</a>. (Register for the workshop as part of the Posit conference registration.)</li>
</ul>
<p>The workshop introduces the <a href="https://tidyverts.org">tidyverts</a> set of packages. Further details about the workshop are <a href="https://robjhyndman.com/teaching/#tidy-time-series-forecasting-in-r">here</a>.</p>
<iframe width="560" height="315" src="https://www.youtube.com/embed/g4221KXM7hU" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="">
</iframe>
<p><a href="https://tidyverts.org"><img src="https://robjhyndman.com/img/tidyverts.png" class="img-fluid"></a></p>



 ]]></description>
  <category>forecasting</category>
  <category>R</category>
  <category>conferences</category>
  <category>teaching</category>
  <guid>https://robjhyndman.com/hyndsight/workshops2023.html</guid>
  <pubDate>Wed, 12 Apr 2023 14:00:00 GMT</pubDate>
  <media:content url="https://robjhyndman.com/img/tidyverts.png" medium="image" type="image/png" height="98" width="144"/>
</item>
<item>
  <title>Monash Quarto Templates</title>
  <link>https://robjhyndman.com/hyndsight/quarto_templates.html</link>
  <description><![CDATA[ 



<div>
  <div class="description">
    I’ve created some quarto templates with Monash University branding.
  </div>
</div><p></p>


<section id="monash-letter-template" class="level2">
<h2 class="anchored" data-anchor-id="monash-letter-template"><a href="https://github.com/quarto-monash/letter">Monash Letter Template</a></h2>
<p>This is a Quarto template that assists you in creating a letter on Monash University letterhead.</p>
<table>
<tbody><tr>
<td valign="top">
<pre><code>quarto use template quarto-monash/letter</code></pre>
<p>This will install the extension and create an example qmd file that you can use as a starting place for your letter.</p>
<p>You may also use this format with an existing Quarto project or document. From the quarto project or document directory, run the following command to install this format:</p>
<pre><code>quarto install extension quarto-monash/letter</code></pre>
</td>
<td valign="top">
<a href="https://github.com/quarto-monash/letter"><img src="https://github.com/quarto-monash/letter/raw/main/examples/template.png" class="img-fluid" width="700"></a>
</td>
</tr>
</tbody></table>
</section>
<section id="monash-memo-template" class="level2">
<h2 class="anchored" data-anchor-id="monash-memo-template"><a href="https://github.com/quarto-monash/memo">Monash Memo Template</a></h2>
<p>This is a Quarto template that assists you in creating a memo, with optional Monash University branding.</p>
<table>
<tbody><tr>
<td valign="top">
<pre><code>quarto use template quarto-monash/memo</code></pre>
<p>This will install the extension and create an example qmd file that you can use as a starting place for your memo.</p>
<p>You may also use this format with an existing Quarto project or document. From the quarto project or document directory, run the following command to install this format:</p>
<pre><code>quarto install extension quarto-monash/memo</code></pre>
</td>
<td valign="top">
<a href="https://github.com/quarto-monash/memo"><img src="https://github.com/quarto-monash/memo/raw/main/examples/template.png" class="img-fluid" width="700"></a>
</td>
</tr>
</tbody></table>
</section>
<section id="monash-working-paper-template" class="level2">
<h2 class="anchored" data-anchor-id="monash-working-paper-template"><a href="https://github.com/quarto-monash/workingpaper">Monash Working Paper Template</a></h2>
<p>This is a Quarto template that assists you in creating a working paper for the Department of Econometrics &amp; Business Statistics, Monash University.</p>
<table>
<tbody><tr>
<td valign="top">
<pre><code>quarto use template quarto-monash/workingpaper</code></pre>
<p>This will install the extension and create an example qmd file that you can use as a starting place for your working paper.</p>
<p>You may also use this format with an existing Quarto project or document. From the quarto project or document directory, run the following command to install this format:</p>
<pre><code>quarto install extension quarto-monash/workingpaper</code></pre>
</td>
<td valign="top">
<a href="https://github.com/quarto-monash/workingpaper"><img src="https://github.com/quarto-monash/workingpaper/raw/main/examples/template.png" class="img-fluid" width="900"></a>
</td>
</tr>
</tbody></table>
</section>
<section id="monash-report-template" class="level2">
<h2 class="anchored" data-anchor-id="monash-report-template"><a href="https://github.com/quarto-monash/report">Monash Report Template</a></h2>
<p>This is a Quarto template that assists you in creating a Monash University report.</p>
<table>
<tbody><tr>
<td valign="top">
<pre><code>quarto use template quarto-monash/report</code></pre>
<p>This will install the extension and create an example qmd file that you can use as a starting place for your report.</p>
<p>You may also use this format with an existing Quarto project or document. From the quarto project or document directory, run the following command to install this format:</p>
<pre><code>quarto install extension quarto-monash/report</code></pre>
</td>
<td valign="top">
<a href="https://github.com/quarto-monash/report"><img src="https://github.com/quarto-monash/report/raw/main/examples/template.png" class="img-fluid" width="900"></a>
</td>
</tr>
</tbody></table>
</section>
<section id="monash-thesis-template" class="level2">
<h2 class="anchored" data-anchor-id="monash-thesis-template"><a href="https://github.com/quarto-monash/thesis">Monash Thesis Template</a></h2>
<p>This is a Quarto template for a Monash University thesis.</p>
<table>
<tbody><tr>
<td valign="top">
<pre><code>quarto use template quarto-monash/thesis</code></pre>
<p>This will copy the template to your current directory, which you can use as a starting place for your thesis.</p>
</td>
<td width="300" valign="top" align="right">
<a href="https://github.com/quarto-monash/thesis"><img src="https://github.com/quarto-monash/thesis/raw/main/examples/template.png" class="img-fluid quarto-figure quarto-figure-right" width="300"></a>
</td>
</tr>
</tbody></table>
</section>
<section id="monash-presentation-template" class="level2">
<h2 class="anchored" data-anchor-id="monash-presentation-template"><a href="https://github.com/quarto-monash/presentation">Monash Presentation Template</a></h2>
<p>This is a Quarto template that assists you in creating a presentation using either Beamer (pdf) or RevealJS (html), with Monash University branding.</p>
<table>
<tbody><tr>
<td valign="top">
<pre><code>quarto use template quarto-monash/presentation</code></pre>
<p>This will install the extension and create an example qmd file that you can use as a starting place for your presentation.</p>
<p>You may also use this format with an existing Quarto project or document. From the quarto project or document directory, run the following command to install this format:</p>
<pre><code>quarto install extension quarto-monash/presentation</code></pre>
</td>
<td valign="top" width="300">
<a href="https://github.com/quarto-monash/presentation"><img src="https://github.com/quarto-monash/presentation/raw/main/examples/pdftemplate.png" class="img-fluid" width="1200"></a>
</td>
</tr>
</tbody></table>
</section>
<section id="rmarkdown-templates" class="level2">
<h2 class="anchored" data-anchor-id="rmarkdown-templates">Rmarkdown templates</h2>
<p>These are all based on my Rmarkdown templates which are distributed via the <a href="https://github.com/numbats/monash"><code>monash</code> R package</a>.</p>


</section>

 ]]></description>
  <category>quarto</category>
  <category>rmarkdown</category>
  <category>Monash University</category>
  <category>reproducible research</category>
  <guid>https://robjhyndman.com/hyndsight/quarto_templates.html</guid>
  <pubDate>Sun, 01 Jan 2023 13:00:00 GMT</pubDate>
  <media:content url="https://robjhyndman.com/img/monash_letter_template.png" medium="image" type="image/png" height="203" width="144"/>
</item>
<item>
  <title>We need more open data in Australia</title>
  <link>https://robjhyndman.com/hyndsight/open-data.html</link>
  <description><![CDATA[ 



<div>
  <div class="description">
    <em>I made the following comments in an ISI webinar today on Statistical and Data Science Issues in Australasia.</em>
  </div>
</div><p></p>


<p>Australia has a problem with government data. Actually it has three problems with government data: 1. It is often kept secret. 2. If it is available, it is often out-of-date. 3. If it is available and timely, it is often in a form that makes any analysis difficult.</p>
<p>I think it would be better for the country if government data was available freely, immediately, and in a form that is useful for analysis. Of course, we should make an exception if there are privacy issues, or some other harm that would be caused by releasing it. Let me explain using some examples.</p>
<p>Take mortality data. During the pandemic, it has been important to know how many people died of any cause, so we could know the effect of the pandemic overall. Obviously some people were dying of COVID-19, but others might have been dying because they were unable to get treated when medical staff were overwhelmed by COVID patients. On the other hand, lockdowns may have reduced deaths due to road crashes, but perhaps they also affected deaths due to suicide. If we could compare the total deaths each week during the pandemic, with the corresponding totals in previous years, we could determine the overall effect of the pandemic on Australian mortality.</p>
<p>You would think, that during a global pandemic, having good mortality data would be important. But in June 2020, nearly six months after the start of COVID-19, the most recent available mortality data in Australia was from 2018. Eighteen months out of date! Think about that. For the first six months of the biggest public health event in 100 years, we had no official data on the effect of COVID-19 on Australian mortality. Eventually the Australian Bureau of Statistics got their act together and started producing provisional mortality data more frequently, but only after several of us complained loudly and publicly. Even now, the provisional mortality data available from the ABS is more than 3 months out of date. Contrast that to other countries. I could find mortality data on 38 countries, and Australia was the 5th worst for producing timely mortality data.</p>
<p>Another example concerns COVID-19 case numbers. There is still no reliable Australian government repository of daily COVID-19 cases by state. Some states are now producing historical data, but for most of 2020, when we really needed reliable information, the public information was incomplete. For much of the first two years of the pandemic, the state health departments were putting out their little dashboard images containing the numbers, but these were preliminary numbers, and did not include cases that were registered late, and some other data revisions. To do any serious analysis, you needed daily case numbers from the beginning of the pandemic, but these were not available on government websites until relatively recently. Some media organizations, and some individuals, were collating the case numbers from the dashboard images and putting them online in the form of spreadsheets, and people were using them to do analysis, but these data were usually inaccurate and subject to revisions. The state health departments generally didn’t update the initial numbers that were released, even though they had more reliable information. So the public data was inaccurate, and most people wanting to do any data analysis were relying on media outlets, or a few 14 year old boys running <a href="https://covidlive.com.au">covidlive.com.au</a>, to get even that.</p>
<p>For nearly three years, I have been part of the forecasting team appointed to provide advice to all of the Chief Health Officers of the states and territories of Australia. Every week, we produce forecasts of COVID daily case numbers for all states and territories. For that purpose, we were able to put together a relatively good data set of case numbers for all states, but we were explicitly forbidden to make the data publicly available, even though our data was more accurate than what was appearing in the media.</p>
<p>Similarly, our forecasts were kept secret even though they were being used to make policy decisions. Premiers would justify their policies by vaguely referring to “the modelling”, or occasionally “the Doherty modelling” (even though most of us are not at the Doherty institute), but we would have preferred to have our forecasts available. So the good data and the forecasts are kept secret, and what is available is of poorer quality, or out-of-date.</p>
<p>Why? There are no privacy issues here. No harm would be done by working more transparently. On the contrary, if everyone had access to the best available data, then the independent modelling that was being done would have been of a higher quality.</p>
<p>We use a forecasting ensemble, where we have several forecasting models, and we combine them to produce the final forecasts that are submitted to the various state governments each week. Because we can’t share the data, the only forecasts that are included are those from members of our team. Generally in forecasting, it is better to use a wide range of models, not rely on a select few. But we can’t do that in Australia because of government obsession with secrecy.</p>
<p>Compare that to the United States where there was an official repository of data set up early in the pandemic, and anyone could download it and produce forecasts, and submit those forecasts to the Centre for Disease Control for inclusion in their analysis. Therefore, the US forecasting ensemble that was being used for policy decisions was based on a much larger range of models, and anyone could contribute to it. The resulting forecasts are then published publicly, so anyone can see what is being forecast, and what information a government has available when making policy decisions.</p>
<p>I’ve focused on COVID, but similar problems arise in many other areas in Australia. We have a culture of secrecy around data that is damaging to our public discourse, it leads to worse analysis, it means less transparency in government, and it feeds distrust of government because it is not clear why decisions are being made. Making more data publicly available leads to a better society.</p>



 ]]></description>
  <category>data</category>
  <guid>https://robjhyndman.com/hyndsight/open-data.html</guid>
  <pubDate>Mon, 12 Dec 2022 13:00:00 GMT</pubDate>
  <media:content url="https://robjhyndman.com/img/isi_webinar.png" medium="image" type="image/png" height="72" width="144"/>
</item>
<item>
  <title>Derivations of forecast variance for benchmark methods</title>
  <link>https://robjhyndman.com/hyndsight/forecasting_variances.html</link>
  <description><![CDATA[ 



<div>
  <div class="description">
    In my <a href="https://otexts.com/fpp3/prediction-intervals.html#benchmark-methods">forecasting textbook</a> coauthored with George Athanasopoulos, we provide formulas for the forecast variances of four simple benchmark forecasting methods, but we don’t explain where they come from. So here are the derivations.
  </div>
</div><p></p>


<p>We assume that the residuals from the method are uncorrelated and homoscedastic, with mean 0 and variance <img src="https://latex.codecogs.com/png.latex?%5Csigma%5E2">. Let <img src="https://latex.codecogs.com/png.latex?y_1,%5Cdots,y_T"> denote the time series observations, and let <img src="https://latex.codecogs.com/png.latex?%5Chat%7By%7D_%7BT+h%7CT%7D"> be the estimated forecast mean (or point forecast). Then we can write <span id="eq-eq1"><img src="https://latex.codecogs.com/png.latex?%0Ay_t%20=%20%5Chat%7By%7D_%7Bt%7Ct-1%7D%20+%20%5Cvarepsilon_t%0A%5Ctag%7B1%7D"></span> where <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon"> is a white noise process. Let <img src="https://latex.codecogs.com/png.latex?%5Chat%5Csigma_h%5E2"> be the estimated <img src="https://latex.codecogs.com/png.latex?h">-step forecast variance.</p>
<section id="random-walknaive-method" class="level2">
<h2 class="anchored" data-anchor-id="random-walknaive-method">Random walk/Naive method</h2>
<p>For a random walk, Equation&nbsp;1 suggests that the appropriate model is <img src="https://latex.codecogs.com/png.latex?y_t%20=%20y_%7Bt-1%7D%20+%20%5Cvarepsilon_t."> Therefore <img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0Ay_%7BT+1%7D%20&amp;=%20y_T%20+%20%5Cvarepsilon_T%20%5C%5C%0Ay_%7BT+2%7D%20&amp;=%20y_%7BT+1%7D%20+%20%5Cvarepsilon_%7BT+1%7D%20%20=%20y_T%20+%20%5Cvarepsilon_T%20+%20%5Cvarepsilon_%7BT+1%7D%5C%5C%0A%5Cvdots%20%5C%5C%0Ay_%7BT+h%7D%20&amp;=%20y_T%20+%20%5Csum_%7Bi=1%7D%5Eh%20%5Cvarepsilon_%7BT+i%7D.%0A%5Cend%7Balign*%7D"> Consequently <img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0A%5Chat%7By%7D_%7BT+h%7CT%7D%20&amp;=%20E(y_%7BT+h%7CT%7D)%20=%20y_T%5C%5C%0A%5Ctext%7Band%7D%5Cqquad%0A%5Chat%7B%5Csigma%7D%5E2_%7Bh%7D%20&amp;=%20V(y_%7BT+h%7CT%7D)%20=%20h%5Csigma%5E2%0A%5Cend%7Balign*%7D"></p>
</section>
<section id="seasonal-naive-method" class="level2">
<h2 class="anchored" data-anchor-id="seasonal-naive-method">Seasonal naive method</h2>
<p>Here the model is <img src="https://latex.codecogs.com/png.latex?y_t%20=%20y_%7Bt-m%7D%20+%20%5Cvarepsilon_t,"> where <img src="https://latex.codecogs.com/png.latex?m"> is the seasonal period. Thus <img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0Ay_%7BT+1%7D%20&amp;=%20y_%7BT+1-m%7D%20+%20%5Cvarepsilon_%7BT+1%7D%20%5C%5C%0Ay_%7BT+2%7D%20&amp;=%20y_%7BT+2-m%7D%20+%20%5Cvarepsilon_%7BT+2%7D%20%5C%5C%0A%5Cvdots%20%5C%5C%0Ay_%7BT+m%7D%20&amp;=%20y_%7BT%7D%20+%20%5Cvarepsilon_%7BT+m%7D%20%5C%5C%0Ay_%7BT+m+1%7D%20&amp;=%20y_%7BT+1%7D%20+%20%5Cvarepsilon_%7BT+m+1%7D%20=%20y_%7BT+1-m%7D%20+%20%5Cvarepsilon_%7BT+1%7D%20+%20%5Cvarepsilon_%7BT+m+1%7D%5C%5C%0A%5Cvdots%5C%5C%0Ay_%7BT+2m%7D%20&amp;=%20y_%7BT+m%7D%20+%20%5Cvarepsilon_%7BT+2m%7D%20=%20y_%7BT%7D%20+%20%5Cvarepsilon_%7BT+m%7D%20+%20%5Cvarepsilon_%7BT+2m%7D%5C%5C%0Ay_%7BT+2m+1%7D%20&amp;=%20y_%7BT+m+1%7D%20+%20%5Cvarepsilon_%7BT+2m+1%7D%20=%20y_%7BT+1-m%7D%20+%20%5Cvarepsilon_%7BT+1%7D%20+%20%5Cvarepsilon_%7BT+m+1%7D%20+%20%5Cvarepsilon_%7BT+2m+1%7D%5C%5C%0A%5Cvdots%5C%5C%0Ay_%7BT+h%7D%20&amp;=%20y_%7BT+h-m(k+1)%7D%20+%20%5Csum_%7Bi=0%7D%5E%7Bk%7D%20%5Cvarepsilon_%7BT+h+m(i-k)%7D,%0A%5Cend%7Balign*%7D"> where <img src="https://latex.codecogs.com/png.latex?k"> is the integer part of <img src="https://latex.codecogs.com/png.latex?(h-1)%20/m"> (i.e., the number of complete years in the forecast period prior to time <img src="https://latex.codecogs.com/png.latex?T+h">). Therefore <img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0A%5Chat%7By%7D_%7BT+h%7CT%7D%20&amp;=%20E(y_%7BT+h%7CT%7D)%20=%20y_%7BT+h-m(k+1)%7D%5C%5C%0A%5Ctext%7Band%7D%5Cqquad%0A%5Chat%7B%5Csigma%7D%5E2_%7Bh%7D%20&amp;=%20V(y_%7BT+h%7CT%7D)%20=%20(k+1)%5Csigma%5E2%0A%5Cend%7Balign*%7D"></p>
</section>
<section id="mean-method" class="level2">
<h2 class="anchored" data-anchor-id="mean-method">Mean method</h2>
<p>The model underpinning the mean method is <img src="https://latex.codecogs.com/png.latex?y_t%20=%20c%20+%20%5Cvarepsilon_t"> for some constant <img src="https://latex.codecogs.com/png.latex?c"> to be estimated. The least-squares estimate of <img src="https://latex.codecogs.com/png.latex?c"> is the mean, <img src="https://latex.codecogs.com/png.latex?%5Chat%7Bc%7D%20=%20%5Cbar%7By%7D%20=%20(y_1+%5Cdots+y_T)/T."> Thus, <img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0Ay_%7BT+1%7D%20&amp;=%20c%20+%20%5Cvarepsilon_%7BT+1%7D%20%5C%5C%0Ay_%7BT+2%7D%20&amp;=%20c%20+%20%5Cvarepsilon_%7BT+1%7D%20%5C%5C%0A%5Cvdots%20%5C%5C%0Ay_%7BT+h%7D%20&amp;=%20c%20+%20%5Cvarepsilon_%7BT+h%7D.%0A%5Cend%7Balign*%7D"> Therefore <img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0A%5Chat%7By%7D_%7BT+h%7CT%7D%20&amp;=%20E(y_%7BT+h%7CT%7D)%20=%20%5Chat%7Bc%7D%20=%20%5Cbar%7By%7D%5C%5C%0A%5Ctext%7Band%7D%5Cqquad%0A%5Chat%7B%5Csigma%7D%5E2_%7Bh%7D%20&amp;=%20V(y_%7BT+h%7CT%7D)%20=%20V(%5Chat%7Bc%7D)%20+%20%5Csigma%5E2%20=%20(T%5E%7B-1%7D%20+%201)%5Csigma%5E2%0A%5Cend%7Balign*%7D"></p>
</section>
<section id="drift-method" class="level2">
<h2 class="anchored" data-anchor-id="drift-method">Drift method</h2>
<p>For a random walk with drift <img src="https://latex.codecogs.com/png.latex?y_%7Bt%7D%20=%20c%20+%20y_%7Bt-1%7D%20+%20%5Cvarepsilon_t."> Therefore, <img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0Ay_%7BT+1%7D%20&amp;=%20c%20+%20y_T%20+%20%5Cvarepsilon_T%20%5C%5C%0Ay_%7BT+2%7D%20&amp;=%202%20+%20y_%7BT+1%7D%20+%20%5Cvarepsilon_%7BT+1%7D%20%20=%202c%20+%20y_T%20+%20%5Cvarepsilon_T%20+%20%5Cvarepsilon_%7BT+1%7D%5C%5C%0A%5Cvdots%20%5C%5C%0Ay_%7BT+h%7D%20&amp;=%20hc%20+%20y_T%20+%20%5Csum_%7Bi=1%7D%5Eh%20%5Cvarepsilon_%7BT+i%7D.%0A%5Cend%7Balign*%7D"> Now the least squares estimate of <img src="https://latex.codecogs.com/png.latex?c"> is <img src="https://latex.codecogs.com/png.latex?%5Chat%7Bc%7D%20=%20(y_%7BT%7D-y_1)/(T-1)">. Therefore <img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0A%5Chat%7By%7D_%7BT+h%7CT%7D%20&amp;=%20E(y_%7BT+h%7CT%7D)%20=%20h%5Chat%7Bc%7D%20+%20y_T%5C%5C%0A%5Ctext%7Band%7D%5Cqquad%0A%5Chat%7B%5Csigma%7D%5E2_%7Bh%7D%20&amp;=%20V(y_%7BT+h%7CT%7D)%20=%20V(%5Chat%7Bc%7D)%20+%20h%5Csigma%5E2%20=%20%5Cfrac%7Bh%5E2%5Csigma%5E2%7D%7BT-1%7D%20+%20h%5Csigma%5E2.%0A%5Cend%7Balign*%7D"></p>


</section>

 ]]></description>
  <category>forecasting</category>
  <guid>https://robjhyndman.com/hyndsight/forecasting_variances.html</guid>
  <pubDate>Wed, 07 Dec 2022 13:00:00 GMT</pubDate>
  <media:content url="https://robjhyndman.com/img/fvar.png" medium="image" type="image/png" height="43" width="144"/>
</item>
<item>
  <title>Australian Academy of Science</title>
  <link>https://robjhyndman.com/hyndsight/faa.html</link>
  <description><![CDATA[ 



<div>
  <div class="description">
    The Australian Academy of Science has put out a new video about my work.
  </div>
</div><p></p>


<iframe width="800" height="450" src="https://www.youtube.com/embed/uF36wh5Czc0" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="">
</iframe>



 ]]></description>
  <category>forecasting</category>
  <category>anomalies</category>
  <category>AAS</category>
  <guid>https://robjhyndman.com/hyndsight/faa.html</guid>
  <pubDate>Thu, 24 Nov 2022 13:00:00 GMT</pubDate>
  <media:content url="https://robjhyndman.com/img/faa_video.png" medium="image" type="image/png" height="75" width="144"/>
</item>
<item>
  <title>Python implementations of time series forecasting and anomaly detection</title>
  <link>https://robjhyndman.com/hyndsight/python_time_series.html</link>
  <description><![CDATA[ 



<div>
  <div class="description">
    Regular readers will know that I develop statistical models and algorithms, and I write R implementations of them. I’m often asked if there are also Python implementations available. There are.
  </div>
</div><p></p>


<section id="nixtla" class="level2">
<h2 class="anchored" data-anchor-id="nixtla">Nixtla</h2>
<p>The best Python implementations for my time series methods are available from <a href="https://github.com/nixtla">Nixtla</a>. Here are some of their packages related to my work, all compatible with <a href="http://scikit-learn.org/"><code>scikit-learn</code></a>.</p>
<ul>
<li><a href="https://nixtla.github.io/statsforecast/"><strong>statsforecast</strong></a>: Automatic ARIMA and ETS forecasting <span class="citation" data-cites="HK08 HKSG02">(Hyndman et al., 2002; Hyndman &amp; Khandakar, 2008)</span>.</li>
<li><a href="https://nixtla.github.io/hierarchicalforecast/"><strong>hierarchicalforecast</strong></a>: Hierarchical forecasting <span class="citation" data-cites="hierarchical mint">(Hyndman et al., 2011; Wickramasuriya et al., 2019)</span>.</li>
<li><a href="https://github.com/Nixtla/tsfeatures"><strong>tsfeatures</strong></a>: Time series features <span class="citation" data-cites="m3pca fforms fforma">(Kang et al., 2017; Montero-Manso et al., 2020; T. S. Talagala et al., 2018)</span>.</li>
</ul>
<p>They have also produced a lot of other great time series tools that are fast (optimized using <a href="https://numba.pydata.org"><code>numba</code></a>) and perform well compared to various alternatives.</p>
</section>
<section id="gluonts" class="level2">
<h2 class="anchored" data-anchor-id="gluonts">GluonTS</h2>
<p><a href="https://ts.gluon.ai/">GluonTS</a> from Amazon is excellent and provides lots of probabilistic time series forecasting models, with wrappers to some of my R code, and statsforecast from Nixtla. The other models in GluonTS are also well worth exploring.</p>
</section>
<section id="merlion" class="level2">
<h2 class="anchored" data-anchor-id="merlion">Merlion</h2>
<p><a href="https://github.com/salesforce/Merlion">Merlion</a> from Salesforce is another interesting python library which includes both my automatic ARIMA and automatic ETS algorithms, along with other forecasting methods. It also has some anomaly detection methods for time series.</p>
</section>
<section id="pmdarima" class="level2">
<h2 class="anchored" data-anchor-id="pmdarima">pmdarima</h2>
<p>The first attempt to port my <a href="https://pkg.robjhyndman.com/forecast/reference/auto.arima.html"><code>auto.arima()</code></a> function to Python was <a href="https://alkaline-ml.com/pmdarima/"><code>pmdarima</code></a>.</p>
</section>
<section id="sktime" class="level2">
<h2 class="anchored" data-anchor-id="sktime">sktime</h2>
<p><a href="https://www.sktime.net">sktime</a> has the most complete set of time series methods for Python including</p>
<ul>
<li><a href="https://sktime-backup.readthedocs.io/en/stable/api_reference/auto_generated/sktime.forecasting.statsforecast.StatsForecastAutoARIMA.html"><strong>AutoARIMA</strong></a>: <span class="citation" data-cites="HK08">(Hyndman &amp; Khandakar, 2008)</span>;</li>
<li><a href="https://sktime-backup.readthedocs.io/en/stable/api_reference/auto_generated/sktime.forecasting.ets.AutoETS.html"><strong>ETS</strong></a>: <span class="citation" data-cites="HKSG02">(Hyndman et al., 2002)</span>;</li>
<li><a href="https://sktime-backup.readthedocs.io/en/stable/api_reference/auto_generated/sktime.forecasting.bats.BATS.html"><strong>BATS</strong></a>/<a href="https://sktime-backup.readthedocs.io/en/stable/api_reference/auto_generated/sktime.forecasting.tbats.TBATS.html"><strong>TBATS</strong></a>: <span class="citation" data-cites="DHS11">(De Livera et al., 2011)</span>;</li>
<li><a href="https://sktime-backup.readthedocs.io/en/stable/api_reference/auto_generated/sktime.forecasting.theta.ThetaForecaster.html"><strong>Theta</strong></a>: <span class="citation" data-cites="theta HB03">(Assimakopoulos &amp; Nikolopoulos, 2000; Hyndman &amp; Billah, 2003)</span>;</li>
<li><a href="https://sktime-backup.readthedocs.io/en/stable/api_reference/auto_generated/sktime.forecasting.trend.STLForecaster.html"><strong>STLForecaster</strong></a>: <span class="citation" data-cites="mstl">(Bandara et al., 2022)</span>;</li>
<li><a href="https://sktime-backup.readthedocs.io/en/stable/api_reference/auto_generated/sktime.forecasting.croston.Croston.html"><strong>Croston</strong></a>: <span class="citation" data-cites="SH05">(Shenstone &amp; Hyndman, 2005)</span>;</li>
<li><a href="https://sktime-backup.readthedocs.io/en/stable/api_reference/auto_generated/sktime.forecasting.compose.BaggingForecaster.html"><strong>Bagged-ETS</strong></a>: <span class="citation" data-cites="baggedETS">(Bergmeir et al., 2016)</span>;</li>
</ul>
<p>and more. These are also compatible with <a href="http://scikit-learn.org/"><code>scikit-learn</code></a>.</p>
<p>Recently, <a href="https://katiebuc.github.io/gsoc/">Kate Buchhorn</a> has ported some of my anomaly detection algorithms to Python and made them available in <a href="https://www.sktime.net">sktime</a> including:</p>
<ul>
<li><a href="https://github.com/sktime/sktime/pull/3338/files"><strong>STRAY</strong></a>: High-dimensional anomaly detection <span class="citation" data-cites="stray">(P. D. Talagala et al., 2021)</span>.</li>
<li><a href="https://github.com/sktime/sktime/pull/3373/files"><strong>DOBIN</strong></a>: Dimension reduction for anomaly detection <span class="citation" data-cites="dobin">(Kandanaarachchi &amp; Hyndman, 2021)</span>.</li>
</ul>
</section>
<section id="statsmodels" class="level2">
<h2 class="anchored" data-anchor-id="statsmodels">statsmodels</h2>
<p>The <a href="https://www.statsmodels.org">statsmodels</a> collection includes a few functions based on my work:</p>
<ul>
<li><a href="https://www.statsmodels.org/stable/examples/notebooks/generated/ets.html"><strong>ETS</strong></a>: <span class="citation" data-cites="HKSG02">(Hyndman et al., 2002)</span>;</li>
<li><a href="https://www.statsmodels.org/stable/examples/notebooks/generated/theta-model.html"><strong>Theta</strong></a>: <span class="citation" data-cites="theta HB03">(Assimakopoulos &amp; Nikolopoulos, 2000; Hyndman &amp; Billah, 2003)</span>;</li>
<li><a href="https://www.statsmodels.org/dev/generated/statsmodels.tsa.seasonal.MSTL.html"><strong>MSTL</strong></a>: <span class="citation" data-cites="mstl">(Bandara et al., 2022)</span>;</li>
<li><a href="https://www.statsmodels.org/dev/generated/statsmodels.graphics.functional.hdrboxplot.html"><strong>functional boxplot</strong></a>: <span class="citation" data-cites="HS10">(Hyndman &amp; Shang, 2010)</span>;</li>
<li><a href="https://www.statsmodels.org/dev/generated/statsmodels.graphics.functional.hdrboxplot.html"><strong>functional HDR boxplot</strong></a>: <span class="citation" data-cites="HS10">(Hyndman &amp; Shang, 2010)</span>;</li>
<li><a href="https://www.statsmodels.org/dev/generated/statsmodels.graphics.functional.rainbowplot.html"><strong>rainbowplot</strong></a>: <span class="citation" data-cites="HS10">(Hyndman &amp; Shang, 2010)</span>.</li>
</ul>
</section>
<section id="pyhts" class="level2">
<h2 class="anchored" data-anchor-id="pyhts">pyhts</h2>
<p><a href="https://angelpone.github.io/">Bohan Zhang</a> has produced <a href="https://angelpone.github.io/pyhts/">pyhts</a>, a re-implementation of the <a href="https://pkg.earo.me/hts/">hts</a> package in Python, based on <span class="citation" data-cites="hierarchical">Hyndman et al. (2011)</span>, <span class="citation" data-cites="fasthts">Hyndman et al. (2016)</span> and <span class="citation" data-cites="mint">Wickramasuriya et al. (2019)</span>.</p>
</section>
<section id="darts" class="level2">
<h2 class="anchored" data-anchor-id="darts">Darts</h2>
<p><a href="https://unit8co.github.io/darts/">Darts</a> is a Python library for wrangling and forecasting time series. It includes wrappers for ETS, ARIMA and other models from <a href="https://nixtla.github.io/statsforecast/"><code>statsforecast</code></a> and <a href="https://alkaline-ml.com/pmdarima/"><code>pmdarima</code></a>, and some <a href="https://unit8co.github.io/darts/examples/16-hierarchical-reconciliation.html">reconciliation functionality</a>.</p>



</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-theta" class="csl-entry">
Assimakopoulos, V., &amp; Nikolopoulos, K. (2000). The theta model: A decomposition approach to forecasting. <em>International Journal of Forecasting</em>, <em>16</em>(4), 521–530. <a href="https://doi.org/10.1016/S0169-2070(00)00066-2">https://doi.org/10.1016/S0169-2070(00)00066-2</a>
</div>
<div id="ref-mstl" class="csl-entry">
Bandara, K., Hyndman, R. J., &amp; Bergmeir, C. (2022). <span>MSTL</span>: A seasonal-trend decomposition algorithm for time series with multiple seasonal patterns. <em>International J Operational Research</em>. <a href="https://robjhyndman.com/publications/mstl/">robjhyndman.com/publications/mstl/</a>
</div>
<div id="ref-baggedETS" class="csl-entry">
Bergmeir, C., Hyndman, R. J., &amp; Benıtez, J. M. (2016). Bagging exponential smoothing methods using <span>STL</span> decomposition and <span>Box-Cox</span> transformation. <em>International Journal of Forecasting</em>, <em>32</em>(2), 303–312. <a href="https://robjhyndman.com/publications/bagging-ets">robjhyndman.com/publications/bagging-ets</a>
</div>
<div id="ref-DHS11" class="csl-entry">
De Livera, A. M., Hyndman, R. J., &amp; Snyder, R. D. (2011). Forecasting time series with complex seasonal patterns using exponential smoothing. <em>J American Statistical Association</em>, <em>106</em>(496), 1513–1527. <a href="https://robjhyndman.com/publications/complex-seasonality/">robjhyndman.com/publications/complex-seasonality/</a>
</div>
<div id="ref-hierarchical" class="csl-entry">
Hyndman, R. J., Ahmed, R. A., Athanasopoulos, G., &amp; Shang, H. L. (2011). Optimal combination forecasts for hierarchical time series. <em>Computational Statistics &amp; Data Analysis</em>, <em>55</em>(9), 2579–2589. <a href="https://robjhyndman.com/publications/hierarchical/">robjhyndman.com/publications/hierarchical/</a>
</div>
<div id="ref-HB03" class="csl-entry">
Hyndman, R. J., &amp; Billah, M. B. (2003). Unmasking the theta method. <em>International Journal of Forecasting</em>, <em>19</em>(2), 287–290. <a href="https://robjhyndman.com/publications/unmasking-the-theta-method/">robjhyndman.com/publications/unmasking-the-theta-method/</a>
</div>
<div id="ref-HK08" class="csl-entry">
Hyndman, R. J., &amp; Khandakar, Y. (2008). Automatic time series forecasting: The forecast package for <span>R</span>. <em>Journal of Statistical Software</em>, <em>26</em>(3), 1–22. <a href="https://robjhyndman.com/publications/automatic-forecasting/">robjhyndman.com/publications/automatic-forecasting/</a>
</div>
<div id="ref-HKSG02" class="csl-entry">
Hyndman, R. J., Koehler, A. B., Snyder, R. D., &amp; Grose, S. (2002). A state space framework for automatic forecasting using exponential smoothing methods. <em>International Journal of Forecasting</em>, <em>18</em>(3), 439–454. <a href="https://robjhyndman.com/publications/hksg/">robjhyndman.com/publications/hksg/</a>
</div>
<div id="ref-fasthts" class="csl-entry">
Hyndman, R. J., Lee, A., &amp; Wang, E. (2016). Fast computation of reconciled forecasts for hierarchical and grouped time series. <em>Computational Statistics &amp; Data Analysis</em>, <em>97</em>, 16–32.
</div>
<div id="ref-HS10" class="csl-entry">
Hyndman, R. J., &amp; Shang, H. L. (2010). Rainbow plots, bagplots and boxplots for functional data. <em>J Computational &amp; Graphical Statistics</em>, <em>19</em>(1), 29–45. <a href="https://robjhyndman.com/publications/rainbow-fda">robjhyndman.com/publications/rainbow-fda</a>
</div>
<div id="ref-dobin" class="csl-entry">
Kandanaarachchi, S., &amp; Hyndman, R. J. (2021). Dimension reduction for outlier detection using <span>DOBIN</span>. <em>J Computational &amp; Graphical Statistics</em>, <em>30</em>(1), 204–219. <a href="https://robjhyndman.com/publications/dobin">robjhyndman.com/publications/dobin</a>
</div>
<div id="ref-m3pca" class="csl-entry">
Kang, Y., Hyndman, R. J., &amp; Smith-Miles, K. (2017). Visualising forecasting algorithm performance using time series instance spaces. <em>International Journal of Forecasting</em>, <em>33</em>(2), 345–358. <a href="https://robjhyndman.com/publications/ts-feature-space/">robjhyndman.com/publications/ts-feature-space/</a>
</div>
<div id="ref-fforma" class="csl-entry">
Montero-Manso, P., Athanasopoulos, G., Hyndman, R. J., &amp; Talagala, T. S. (2020). <span>FFORMA</span>: Feature-based forecast model averaging. <em>International Journal of Forecasting</em>, <em>36</em>(1), 86–92. <a href="https://robjhyndman.com/publications/fforma/">robjhyndman.com/publications/fforma/</a>
</div>
<div id="ref-SH05" class="csl-entry">
Shenstone, L., &amp; Hyndman, R. J. (2005). Stochastic models underlying croston’s method for intermittent demand forecasting. <em>Journal of Forecasting</em>, <em>24</em>(6), 389–402. <a href="https://robjhyndman.com/publications/croston/">robjhyndman.com/publications/croston/</a>
</div>
<div id="ref-stray" class="csl-entry">
Talagala, P. D., Hyndman, R. J., &amp; Smith-Miles, K. (2021). Anomaly detection in high-dimensional data. <em>J Computational &amp; Graphical Statistics</em>, <em>30</em>(2), 360–374. <a href="https://robjhyndman.com/publications/stray/">robjhyndman.com/publications/stray/</a>
</div>
<div id="ref-fforms" class="csl-entry">
Talagala, T. S., Hyndman, R. J., &amp; Athanasopoulos, G. (2018). <em>Meta-learning how to forecast time series</em> (Working Paper 6/18). Department of Econometrics &amp; Business Statistics, Monash University. <a href="https://robjhyndman.com/publications/fforms/">robjhyndman.com/publications/fforms/</a>
</div>
<div id="ref-mint" class="csl-entry">
Wickramasuriya, S. L., Athanasopoulos, G., &amp; Hyndman, R. J. (2019). Optimal forecast reconciliation for hierarchical and grouped time series through trace minimization. <em>J American Statistical Association</em>, <em>114</em>(526), 804–819. <a href="https://robjhyndman.com/publications/mint">robjhyndman.com/publications/mint</a>
</div>
</div></section></div> ]]></description>
  <category>Python</category>
  <category>forecasting</category>
  <category>anomalies</category>
  <category>hts</category>
  <guid>https://robjhyndman.com/hyndsight/python_time_series.html</guid>
  <pubDate>Thu, 06 Oct 2022 13:00:00 GMT</pubDate>
  <media:content url="https://robjhyndman.com/img/python.png" medium="image" type="image/png" height="157" width="144"/>
</item>
<item>
  <title>Notation for forecast reconciliation</title>
  <link>https://robjhyndman.com/hyndsight/reconciliation-notation.html</link>
  <description><![CDATA[ 



<div>
  <div class="description">
    The various papers on forecast reconcilation written over the last 13 years have not used a consistent notation. We have revised our notation as we have slowly come to understand the problem better. This is common in a new area, but it makes it tricky to read the literature as you need to figure out how the notation of each paper maps to what you’ve already read.
  </div>
</div><p></p>


<p>Recently I spent a few weeks visiting <a href="https://homes.stat.unipd.it/tommasodifonzo/">Professor Tommaso Di Fonzo</a> at the University of Padova (Italy), and one of the things we discussed was finding a notation we were both happy with so we could be more consistent in our future papers.</p>
<p>This is what we came up with. Hopefully others will agree and use it too!</p>
<p>For readers new to forecast reconciliation, <a href="https://otexts.com/fpp3/hierarchical.html">Chapter 11 of FPP3</a> provides an introduction.</p>
<p>We observe <img src="https://latex.codecogs.com/png.latex?n"> time series at time <img src="https://latex.codecogs.com/png.latex?t">, written as <img src="https://latex.codecogs.com/png.latex?%5Cbm%7By%7D_t">. The base forecasts of <img src="https://latex.codecogs.com/png.latex?%5Cbm%7By%7D_%7BT+h%7D"> given data <img src="https://latex.codecogs.com/png.latex?%5Cbm%7By%7D_1,%5Cdots,%5Cbm%7By%7D_T"> are denoted by <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cbm%7By%7D%7D_h">.</p>
<section id="structural-representation" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="structural-representation">Structural representation</h2>
<p>This was the original formulation of the problem due to <span class="citation" data-cites="HynEtAl2011">Hyndman et al. (2011)</span>, but presented here in our new notation.</p>
<p>Let <img src="https://latex.codecogs.com/png.latex?%5Cbm%7Bb%7D_t"> be a vector of <img src="https://latex.codecogs.com/png.latex?n_b"> “bottom-level” time series at time <img src="https://latex.codecogs.com/png.latex?t">, and let <img src="https://latex.codecogs.com/png.latex?%5Cbm%7Ba%7D_t"> be a corresponding vector of <img src="https://latex.codecogs.com/png.latex?n_a%20=%20n-n_b"> aggregated time series, where <img src="https://latex.codecogs.com/png.latex?%0A%5Cbm%7Ba%7D_t%20=%20%5Cbm%7BA%7D%5Cbm%7Bb%7D_t%0A"> and <img src="https://latex.codecogs.com/png.latex?%5Cbm%7BA%7D"> is the <img src="https://latex.codecogs.com/png.latex?n_a%5Ctimes%20n_b"> “aggregation” matrix specifying how the bottom-level series <img src="https://latex.codecogs.com/png.latex?%5Cbm%7Bb%7D_t"> are to be aggregated to form <img src="https://latex.codecogs.com/png.latex?%5Cbm%7Ba%7D_t">. The full vector of time series is given by <img src="https://latex.codecogs.com/png.latex?%0A%5Cbm%7By%7D_t%20=%20%5Cbegin%7Bbmatrix%7D%5Cbm%7Ba%7D_t%20%5C%5C%5Cbm%7Bb%7D_t%5Cend%7Bbmatrix%7D.%0A"> This leads to the <img src="https://latex.codecogs.com/png.latex?n%5Ctimes%20n_b"> “summing” or “structural” matrix given by <img src="https://latex.codecogs.com/png.latex?%0A%20%20%5Cbm%7BS%7D%20=%20%5Cbegin%7Bbmatrix%7D%5Cbm%7BA%7D%20%5C%5C%20%5Cbm%7BI%7D_%7Bn_b%7D%5Cend%7Bbmatrix%7D%0A"> such that <img src="https://latex.codecogs.com/png.latex?%5Cbm%7By%7D_t%20=%20%5Cbm%7BS%7D%5Cbm%7Bb%7D_t">.</p>
<p>All bottom-up, middle-out, top-down and linear reconciliation methods can be written as <img src="https://latex.codecogs.com/png.latex?%0A%20%20%5Ctilde%7B%5Cbm%7By%7D%7D_h%20=%20%5Cbm%7BS%7D%5Cbm%7BG%7D%5Chat%7B%5Cbm%7By%7D%7D_h,%0A"> for different matrices <img src="https://latex.codecogs.com/png.latex?%5Cbm%7BG%7D">.</p>
<p>Optimal reconciled forecasts are obtained with <img src="https://latex.codecogs.com/png.latex?%5Cbm%7BG%7D=(%5Cbm%7BS%7D'%5Cbm%7BW%7D%5E%7B-1%7D%5Cbm%7BS%7D)%5E%7B-1%7D%5Cbm%7BS%7D'%5Cbm%7BW%7D%5E%7B-1%7D">, or <img src="https://latex.codecogs.com/png.latex?%0A%20%20%5Ctilde%7B%5Cbm%7By%7D%7D_h%20=%20%5Cbm%7BM%7D%5Chat%7B%5Cbm%7By%7D%7D_h,%0A"> where the <img src="https://latex.codecogs.com/png.latex?n%5Ctimes%20n"> “mapping” matrix is given by <span id="eq-mapping1"><img src="https://latex.codecogs.com/png.latex?%0A%20%20%5Cbm%7BM%7D%20=%20%5Cbm%7BS%7D(%5Cbm%7BS%7D'%5Cbm%7BW%7D%5E%7B-1%7D%5Cbm%7BS%7D)%5E%7B-1%7D%5Cbm%7BS%7D'%5Cbm%7BW%7D%5E%7B-1%7D,%0A%5Ctag%7B1%7D"></span> <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cbm%7By%7D%7D_h"> are the <img src="https://latex.codecogs.com/png.latex?h">-step forecasts of <img src="https://latex.codecogs.com/png.latex?y_%7BT+h%7D"> given data to time <img src="https://latex.codecogs.com/png.latex?T">, and <img src="https://latex.codecogs.com/png.latex?%5Cbm%7BW%7D"> is an <img src="https://latex.codecogs.com/png.latex?n%20%5Ctimes%20n"> positive definite matrix. Different choices for <img src="https://latex.codecogs.com/png.latex?%5Cbm%7BW%7D"> lead to different solutions such as OLS, WLS and MinT <span class="citation" data-cites="WicEtAl2019">(Wickramasuriya et al. 2019)</span>.</p>
</section>
<section id="linear-combinations" class="level2">
<h2 class="anchored" data-anchor-id="linear-combinations">Linear combinations</h2>
<p>There is actually no reason for <img src="https://latex.codecogs.com/png.latex?%5Cbm%7Ba%7D_t"> to be restricted to aggregates of <img src="https://latex.codecogs.com/png.latex?%5Cbm%7Bb%7D_t">. They can include any linear combination of the bottom-level series <img src="https://latex.codecogs.com/png.latex?%5Cbm%7Bb%7D_t">, so the corresponding <img src="https://latex.codecogs.com/png.latex?%5Cbm%7BA%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Cbm%7BS%7D"> matrices may contain any real values, not just 0s and 1s. Nevertheless, we will use the same notation for this more general setting.</p>
</section>
<section id="zero-constrained-representation" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="zero-constrained-representation">Zero-constrained representation</h2>
<p>This representation is more efficient and was used by <span class="citation" data-cites="Di_FonGir2021a">Di Fonzo and Girolimetto (2021)</span>. It was also discussed in <span class="citation" data-cites="WicEtAl2019">Wickramasuriya et al. (2019)</span>. Here it is in the new notation.</p>
<p>We can express the structural representation using the <img src="https://latex.codecogs.com/png.latex?n_a%20%5Ctimes%20n"> constraint matrix <img src="https://latex.codecogs.com/png.latex?%5Cbm%7BC%7D%20=%20%5Cbegin%7Bbmatrix%7D%5Cbm%7BI%7D_%7Bn_a%7D%20&amp;%20-%5Cbm%7BA%7D%5Cend%7Bbmatrix%7D,"> so that <img src="https://latex.codecogs.com/png.latex?%5Cbm%7BC%7D%5Cbm%7By%7D_t%20=%20%5Cbm%7B0%7D_%7Bn_a%7D">. Then we can write the mapping matrix as <span id="eq-mapping2"><img src="https://latex.codecogs.com/png.latex?%0A%20%20%5Cbm%7BM%7D%20=%20%5Cbm%7BI%7D_n%20-%20%5Cbm%7BW%7D%5Cbm%7BC%7D'(%5Cbm%7BC%7D%5Cbm%7BW%7D%5Cbm%7BC%7D')%5E%7B-1%7D%5Cbm%7BC%7D.%0A%5Ctag%7B2%7D"></span> Note that Equation&nbsp;2 involves inverting an <img src="https://latex.codecogs.com/png.latex?n_a%20%5Ctimes%20n_a"> matrix, rather than the <img src="https://latex.codecogs.com/png.latex?n_b%20%5Ctimes%20n_b"> matrix in Equation&nbsp;1. For most practical problems, <img src="https://latex.codecogs.com/png.latex?n_a%20%3C%20n_b">, so Equation&nbsp;2 is more efficient.</p>
<p>This form of the mapping matrix also allows us to interpret the reconciliation as an additive adjustment to the base forecasts. If the base forecasts are already reconciled, then <img src="https://latex.codecogs.com/png.latex?%5Cbm%7BC%7D%5Chat%7B%5Cbm%7By%7D%7D_h%20=%20%5Cbm%7B0%7D_%7Bn_a%7D"> and so <img src="https://latex.codecogs.com/png.latex?%5Cbm%7BM%7D=%5Cbm%7BI%7D_%7Bn%7D">.</p>
</section>
<section id="general-zero-constrained-representation" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="general-zero-constrained-representation">General zero-constrained representation</h2>
<p>The most general way to express the problem is not to denote individual series as bottom-level or aggregated, but to define the linear constraints <img src="https://latex.codecogs.com/png.latex?%0A%20%20%5Cbm%7B%5CGamma%7D%5Cbm%7By%7D_t%20=%20%5Cbm%7B0%7D%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Cbm%7B%5CGamma%7D"> is an <img src="https://latex.codecogs.com/png.latex?r%5Ctimes%20n"> matrix, not necessarily full rank, which may contain any real values.</p>
<p>If <img src="https://latex.codecogs.com/png.latex?%5Cbm%7B%5CGamma%7D"> is full rank, then Equation&nbsp;2 holds with <img src="https://latex.codecogs.com/png.latex?%5Cbm%7BC%7D%20=%20%5Cbm%7B%5CGamma%7D">.</p>
</section>
<section id="temporal-reconciliation" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="temporal-reconciliation">Temporal reconciliation</h2>
<p>Temporal reconciliation was proposed by <span class="citation" data-cites="AthEtAl2017">Athanasopoulos et al. (2017)</span>. Here it is in our new notation.</p>
<p>For simplicity we will assume the original (scalar) time series is observed with a single seasonality of period <img src="https://latex.codecogs.com/png.latex?m"> (e.g., <img src="https://latex.codecogs.com/png.latex?m=12"> for monthly data), and the total length of the series <img src="https://latex.codecogs.com/png.latex?T"> is an integer multiple of <img src="https://latex.codecogs.com/png.latex?m">. We will denote the original series by <img src="https://latex.codecogs.com/png.latex?y">, and the various temporally aggregated series by <img src="https://latex.codecogs.com/png.latex?x">.</p>
<p>Let <img src="https://latex.codecogs.com/png.latex?%5C%7Bk_1,%5Cdots,k_p%5C%7D"> denote the <img src="https://latex.codecogs.com/png.latex?p"> factors of <img src="https://latex.codecogs.com/png.latex?m"> in ascending order, where <img src="https://latex.codecogs.com/png.latex?k_1=1"> and <img src="https://latex.codecogs.com/png.latex?k_p=m">. For each factor <img src="https://latex.codecogs.com/png.latex?k"> of <img src="https://latex.codecogs.com/png.latex?m">, we can construct a temporally aggregated series <img src="https://latex.codecogs.com/png.latex?x_j%5E%7B%5Bk%5D%7D%20=%20%5Csum_%7Bt%20=%20(j-1)k+1%7D%5E%7Bjk%7D%20y_t"> for <img src="https://latex.codecogs.com/png.latex?j=1,%5Cdots,T/k">. Of course, <img src="https://latex.codecogs.com/png.latex?x_j%5E%7B%5B1%5D%7D%20=%20y_t">.</p>
<p>Since the observation index <img src="https://latex.codecogs.com/png.latex?j"> varies with each aggregation level, we define <img src="https://latex.codecogs.com/png.latex?%5Ctau"> as the observation index of the most aggregated level (e.g., annual), so that <img src="https://latex.codecogs.com/png.latex?j=%5Ctau"> at that level.</p>
<p>For each aggregation level, we stack the observations in the column vectors <img src="https://latex.codecogs.com/png.latex?%0A%5Cbm%7Bx%7D_%5Ctau%5E%7B%5Bk%5D%7D%20=%20%5Cbegin%7Bbmatrix*%7D%5Bl%5D%0A%20%20x_%7BM_k(%5Ctau-1)+1%7D%5E%7B%5Bk%5D%7D%5C%5C%0A%20%20x_%7BM_k(%5Ctau-1)+2%7D%5E%7B%5Bk%5D%7D%5C%5C%0A%20%20%5Cquad%5Cvdots%5C%5C%0A%20%20x_%7BM_k%5Ctau%7D%5E%7B%5Bk%5D%7D%0A%20%20%5Cend%7Bbmatrix*%7D,%0A"> where <img src="https://latex.codecogs.com/png.latex?k=%5C%7Bk_1,%5Cdots,k_p%5C%7D">, <img src="https://latex.codecogs.com/png.latex?%5Ctau=1,%5Cdots,N">, and <img src="https://latex.codecogs.com/png.latex?N=T/m">. Collecting these in one column vector, we obtain <img src="https://latex.codecogs.com/png.latex?%5Cbm%7Bx%7D_%5Ctau%20=%20%5Cbegin%7Bbmatrix*%7D%5Bl%5D%0A%20%20%7Bx_%5Ctau%5E%7B%5Bk_p%5D%7D%7D%5C%5C%0A%20%20%7B%5Cbm%7Bx%7D_%5Ctau%5E%7B%5Bk_%7Bp-1%7D%5D%7D%7D%5C%5C%0A%20%20%5Cquad%5Cvdots%5C%5C%0A%20%20%7B%5Cbm%7Bx%7D_%5Ctau%5E%7B%5Bk_1%5D%7D%7D%5C%5C%0A%20%20%5Cend%7Bbmatrix*%7D."></p>
<p>The structural representation of this formulation is <img src="https://latex.codecogs.com/png.latex?%5Cbm%7Bx%7D_%5Ctau%20=%20%5Cbm%7BS%7D%20%5Cbm%7Bx%7D_%5Ctau%5E%7B%5B1%5D%7D"> where <img src="https://latex.codecogs.com/png.latex?%5Cbm%7BS%7D%20=%20%5Cbegin%7Bbmatrix%7D%5Cbm%7BA%7D%5C%5C%5Cbm%7BI%7D_m%5Cend%7Bbmatrix%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Cbm%7BA%7D%20=%0A%5Cbegin%7Bbmatrix%7D%0A%5Cbm%7B1%7D'_m%20%5C%5C%0A%5Cbm%7BI%7D_%7Bm/k_%7Bp-1%7D%7D%20%5Cotimes%20%5Cbm%7B1%7D'_%7Bk_%7Bp-1%7D%7D%20%5C%5C%0A%5Cvdots%5C%5C%0A%5Cbm%7BI%7D_%7Bm/k_%7B2%7D%7D%20%5Cotimes%20%5Cbm%7B1%7D'_%7Bk_%7B2%7D%7D%20%5C%5C%0A%5Cend%7Bbmatrix%7D%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Cbm%7B1%7D_m"> is an <img src="https://latex.codecogs.com/png.latex?m">-vector of 1s.</p>
<p>The zero-constrained representation is <img src="https://latex.codecogs.com/png.latex?%5Cbm%7BC%7D%20=%20%5Cbegin%7Bbmatrix%7D%20%5Cbm%7BI%7D_%7Bm%7D%20&amp;%20-%5Cbm%7BA%7D%5Cend%7Bbmatrix%7D">.</p>
<p>If there are multiple seasonalities that are not integer multiples of each other, the resulting additional temporal aggregations can simply be stacked in <img src="https://latex.codecogs.com/png.latex?%5Cbm%7Bx%7D_t">, and <img src="https://latex.codecogs.com/png.latex?%5Cbm%7BA%7D"> can be extended accordingly.</p>
</section>
<section id="cross-temporal-reconcilation" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="cross-temporal-reconcilation">Cross-temporal reconcilation</h2>
<p>Now consider the case where we have both cross-sectional and temporal aggregations, as discussed in <span class="citation" data-cites="Di_FonGir2021a">Di Fonzo and Girolimetto (2021)</span>.</p>
<p>Suppose we have observed <img src="https://latex.codecogs.com/png.latex?%5Cbm%7By%7D_t"> at the most temporally disaggregated level, including all the cross-sectionally disaggregated and aggregated (or constrained) series. Let <img src="https://latex.codecogs.com/png.latex?y_%7Bi,t%7D"> be the <img src="https://latex.codecogs.com/png.latex?i">th element of the vector <img src="https://latex.codecogs.com/png.latex?%5Cbm%7By%7D_t">, <img src="https://latex.codecogs.com/png.latex?i=1,%5Cdots,n">. For each <img src="https://latex.codecogs.com/png.latex?i">, we can expand <img src="https://latex.codecogs.com/png.latex?y_%7Bi,t%7D"> to include all the temporally aggregated variants, giving a vector of length <img src="https://latex.codecogs.com/png.latex?p">: <img src="https://latex.codecogs.com/png.latex?%5Cbm%7Bx%7D_%7Bi,%5Ctau%7D%20=%20%5Cbegin%7Bbmatrix%7D%0A%20%20%7Bx_%7Bi,%5Ctau%7D%5E%7B%5Bk_p%5D%7D%7D%5C%5C%0A%20%20%5Cvdots%5C%5C%0A%20%20%7B%5Cbm%7Bx%7D_%7Bi,%5Ctau%7D%5E%7B%5Bk_1%5D%7D%7D%0A%20%20%5Cend%7Bbmatrix%7D."> These can then be stacked into a long vector: <img src="https://latex.codecogs.com/png.latex?%5Cbm%7Bx%7D_%5Ctau%20=%20%5Cbegin%7Bbmatrix%7D%0A%20%20%5Cbm%7Bx%7D_%7B1,%5Ctau%7D%5C%5C%0A%20%20%5Cvdots%5C%5C%0A%20%20%5Cbm%7Bx%7D_%7Bn,%5Ctau%7D%0A%20%20%5Cend%7Bbmatrix%7D."></p>
<p>If <img src="https://latex.codecogs.com/png.latex?%5Cbm%7BS%7D_%7Bcs%7D"> denotes the structural matrix for the cross-sectional reconciliation, and <img src="https://latex.codecogs.com/png.latex?%5Cbm%7BS%7D_%7Bte%7D"> denotes the structural matrix for the temporal reconciliation, then the cross-temporal structural matrix is <img src="https://latex.codecogs.com/png.latex?%5Cbm%7BS%7D_%7Bct%7D%20=%20%5Cbm%7BS%7D_%7Bcs%7D%20%5Cotimes%20%5Cbm%7BS%7D_%7Bte%7D">, where <img src="https://latex.codecogs.com/png.latex?%5Cotimes"> is the Kronecker product. Then <img src="https://latex.codecogs.com/png.latex?%5Cbm%7Bx%7D_%5Ctau%20=%20%5Cbm%7BS%7D_%7Bct%7D%20%5Cbm%7Bb%7D_%5Ctau,"> where the bottom-level series <img src="https://latex.codecogs.com/png.latex?%5Cbm%7Bb%7D_%5Ctau%20=%20%5Cbegin%7Bbmatrix%7D%0A%20%20%7B%5Cbm%7Bx%7D_%7B1,%5Ctau%7D%5E%7B%5B1%5D%7D%7D%20%5C%5C%0A%20%20%5Cvdots%5C%5C%0A%20%20%7B%5Cbm%7Bx%7D_%7Bn,%5Ctau%7D%5E%7B%5B1%5D%7D%7D%0A%20%20%5Cend%7Bbmatrix%7D.%0A"></p>



</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-AthEtAl2017" class="csl-entry">
Athanasopoulos, George, Rob J. Hyndman, Nikolaos Kourentzes, and Fotios Petropoulos. 2017. <span>“<span class="nocase">Forecasting with temporal hierarchies</span>.”</span> <em>European Journal of Operational Research</em> 262 (1): 60–74. <a href="https://doi.org/10.1016/j.ejor.2017.02.046">https://doi.org/10.1016/j.ejor.2017.02.046</a>.
</div>
<div id="ref-Di_FonGir2021a" class="csl-entry">
Di Fonzo, Tommaso, and Daniele Girolimetto. 2021. <span>“<span class="nocase">Cross-temporal forecast reconciliation: Optimal combination method and heuristic alternatives</span>.”</span> <em>International Journal of Forecasting</em> forthcoming. <a href="https://doi.org/10.1016/j.ijforecast.2021.08.004">https://doi.org/10.1016/j.ijforecast.2021.08.004</a>.
</div>
<div id="ref-HynEtAl2011" class="csl-entry">
Hyndman, Rob J., Roman A. Ahmed, George Athanasopoulos, and Han Lin Shang. 2011. <span>“<span class="nocase">Optimal combination forecasts for hierarchical time series</span>.”</span> <em>Computational Statistics &amp; Data Analysis</em> 55 (9): 2579–89. <a href="https://doi.org/10.1016/j.csda.2011.03.006">https://doi.org/10.1016/j.csda.2011.03.006</a>.
</div>
<div id="ref-WicEtAl2019" class="csl-entry">
Wickramasuriya, Shanika L., George Athanasopoulos, and Rob J. Hyndman. 2019. <span>“Optimal Forecast Reconciliation for Hierarchical and Grouped Time Series Through Trace Minimization.”</span> <em>Journal of the American Statistical Association</em> 114 (526): 804–19. <a href="https://doi.org/10.1080/01621459.2018.1448825">https://doi.org/10.1080/01621459.2018.1448825</a>.
</div>
</div></section></div> ]]></description>
  <category>forecasting</category>
  <category>hts</category>
  <guid>https://robjhyndman.com/hyndsight/reconciliation-notation.html</guid>
  <pubDate>Wed, 05 Oct 2022 13:00:00 GMT</pubDate>
  <media:content url="https://otexts.com/fpp3/figs/hts.png" medium="image" type="image/png"/>
</item>
<item>
  <title>WOMBAT 2022</title>
  <link>https://robjhyndman.com/hyndsight/wombat2022.html</link>
  <description><![CDATA[ 



<div>
  <div class="description">
    WOMBAT is back! The WOMBAT conferences are “Workshops Organized by the Monash Business Analytics Team”. The first one was held in 2016, and later editions took place in 2017 and 2019. The <a href="https://www.monash.edu/business/events/wombat-2022-communicating-with-data">2022 version</a> will take place on 6-7 December.
  </div>
</div><p></p>


<p>The focus this year is on communicating with data. As with all WOMBAT events, the purpose is to bring together analysts from academia, industry and government to learn and discuss new open source tools for business analytics and data science.</p>
<section id="december" class="level2">
<h2 class="anchored" data-anchor-id="december">6 December</h2>
<p>The first day will be virtual with 8 tutorials to choose from. I will be giving one on <a href="https://numbats.github.io/WOMBAT2022/tutorial/rob.html">“Exploratory time series analysis using R”</a>. Each tutorial has limited places, so register early!</p>
</section>
<section id="december-1" class="level2">
<h2 class="anchored" data-anchor-id="december-1">7 December</h2>
<p><img src="https://robjhyndman.com/img/amanda_cox.jpg" class="img-fluid" style="float: right;width: 50%; padding-left: 10px;"> The second day will be in-person workshop, limited 60 people. The keynote speaker is <a href="http://amandacox.github.io/">Amanda Cox</a>, Head of special data projects, USAFacts. She is well-known for the sixteen years she spent at <em>The New York Times</em> producing some amazing data visualizations. She will speak on “Charts and Words: Being more influential with your data graphics”.</p>
<p>Other invited speakers will talk about data communication in environment, health and sport.</p>
<p>The workshop on December 7 will be held at the <a href="https://www.rsyltc.org.au/">Royal South Yarra Lawn Tennis Club</a>, located near the Yarra River, at <a href="https://goo.gl/maps/E6F9fL5E6SHorn818">310 Williams Rd N, Toorak</a>.</p>
</section>
<section id="registration" class="level2">
<h2 class="anchored" data-anchor-id="registration">Registration</h2>
<p>The <strong>6 Dec online tutorials</strong> are each limited to 20 participants. <a href="https://www.trybooking.com/CCNOH"><strong>Register for tutorials only here</strong></a>. Registering for the 7 Dec workshop provides a 30% discount on tutorial registration. Your discount code will be sent in the confirmation email after you have first registered for 7 Dec.</p>
<p>The <strong>7 Dec in-person event</strong> is limited to 60 attendees. <a href="https://www.trybooking.com/CCEGM"><strong>Register here</strong></a>. Registration includes lunch, morning and afternoon tea.</p>
</section>
<section id="more-details" class="level2">
<h2 class="anchored" data-anchor-id="more-details">More details</h2>
<p>For more details, see <a href="https://www.monash.edu/business/events/wombat-2022-communicating-with-data">the event website</a>.</p>


</section>

 ]]></description>
  <category>conferences</category>
  <category>data science</category>
  <category>Monash University</category>
  <category>R</category>
  <category>statistics</category>
  <guid>https://robjhyndman.com/hyndsight/wombat2022.html</guid>
  <pubDate>Mon, 19 Sep 2022 14:00:00 GMT</pubDate>
</item>
<item>
  <title>Migrating from Disqus to giscus</title>
  <link>https://robjhyndman.com/hyndsight/disqus2giscus.html</link>
  <description><![CDATA[ 



<div>
  <div class="description">
    I’ve long wanted to ditch Disqus as the commenting system on this blog, as it is bloated, adds a lot of extra and unnecessary links, and generally looks noisy.
  </div>
</div><p></p>


<p>I’ve been using Disqus for more than 13 years, largely because it was the only available solution at the time I added comments. To make Disqus interface a little cleaner, I disabled all the advertising and as much of the other noise as possible, but it still looked like something from mySpace (for those of you who remember the 20th century).</p>
<p>But now there are several alternatives, and I’ve opted for <a href="https://giscus.app">giscus</a> which is very lightweight, is built on Github Discussions, and is open source with no tracking or advertising. The other system I considered was <a href="https://github.com/utterance/utterances">utterances</a> which is also hosted on Github, but uses issues rather than discussions. Consequently, comments on utterances can’t be threaded (with replies to previous comments). Also, giscus appears to have a much more active development team behind it.</p>
<p>The first step was to set up giscus on my blog. With quarto, this simply requires adding a few lines to the <code>_metadata.yml</code> file in the relevant folder. Here is what it looks like for me:</p>
<pre><code>comments:
  giscus:
    repo:  robjhyndman/robjhyndman.com
    repo-id: "R_kgDOH5G3Uw"
    category: "Announcements"
    category-id: "DIC_kwDOH5G3U84CRUp9"
    mapping: "pathname"
    reactions-enabled: true
    loading: lazy
    input-position: "bottom"
    theme: "light"</code></pre>
<p>Then I needed to set up giscus on the Github repo that hosts the website (<a href="https://github.com/robjhyndman/robjhyndman.com"><code>robjhyndman/robjhyndman.com</code></a>). The <a href="https://giscus.app">instructions on the giscus website</a> make it very simple.</p>
<p>The last step was the hardest – how to migrate 4000 comments from Disqus to giscus. Here I followed the nice <a href="https://masalmon.eu/2019/10/02/disqus/">blog post of Maëlle Salmon</a> to download the Disqus comments as an xml file, and wrangle them into a tibble. Then I needed to use the GraphQL API for Github Discussions to generate all the comments on the Github repo. Fortunately, Mitch O’Hara-Wild came to my rescue (as usual), and helped with some of this code. The <a href="../Rfiles/disqus2giscus.R">resulting code is here</a> if anyone wants to try to do the same. You will need to change some specific details in lines 9-13. Everything else should work as it is.</p>



 ]]></description>
  <category>computing</category>
  <guid>https://robjhyndman.com/hyndsight/disqus2giscus.html</guid>
  <pubDate>Thu, 08 Sep 2022 14:00:00 GMT</pubDate>
  <media:content url="https://robjhyndman.com/img/giscus.png" medium="image" type="image/png" height="85" width="144"/>
</item>
</channel>
</rss>
