avatarHair Parra

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4947

Abstract

ion></figure><p id="bff7">is a linear function of the trend. Then,</p><figure id="594a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*vxY6swFkO0bfEwGELqDZtw.png"><figcaption></figcaption></figure><figure id="7679"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Leahd4hMr8hBV9HidA7Xiw.png"><figcaption></figcaption></figure><p id="f6d1">So for instance, if Yt is White Noise, differencing once will remove the linear trend and yield a stationary series. It turns out this trick also works for dealing with seasonality!</p><p id="a6dc"><b>Removing seasonality</b></p><p id="8276">Suppose we have the classical decomposition model</p><figure id="0073"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*OHVuEwOIdjtW3qhk3esZsw.png"><figcaption></figcaption></figure><p id="05d8">where the trend is linear and Yt is White Noise just as above. In addition, let</p><figure id="96ef"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*jPnEwVXLoFF-kjAu6Cd3-w.png"><figcaption></figcaption></figure><p id="3c06">where d is the period. Then, if we apply the <b>lag-difference operator, </b>we obtain</p><figure id="eff9"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*E8wpc57bI1osYs4qYWYYyQ.png"><figcaption></figcaption></figure><p id="497c">So indeed not only we removed the trend, but also the seasonality at the same time, and the residuals will be stationary so long Yt is.</p><h2 id="9d34">When to use each difference operator?</h2><ul><li>If there is a linear trend, we can use differencing to remove it without estimating parameters.</li><li>Applying differencing will then yield residuals which are closer to a stationary process.</li><li>However, note that <b>some data is lost</b> when applying to difference to all points (think about the boundaries! ) There is a trade-off between using different operators and predictive power!</li><li>Although differencing can get read of trend and seasonality, in practice it is good to estimate and remove the main trend first (which has the effect of focusing on the harder-to-estimate semi-noise).</li></ul><p id="726d">As a general rule of thumb, you might consider using</p><figure id="4e8a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*zpJyPpxuRDh2Q1fcSA78bQ.png"><figcaption></figcaption></figure><p id="50bb"><b>How to R</b></p><p id="1218">First load the necessary packages:</p> <figure id="4f18"> <div> <div>

            <iframe class="gist-iframe" src="/gist/JairParra/f64acb427a99ffc38390959ac41d08a8.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="f01f">Recall the <code>LakeHuron</code> dataset we had previously worked with:</p>
    <figure id="d46c">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/JairParra/b738ab9398aa19c66696e3369bfc023f.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><figure id="4150"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*DEfVeZ0XdFzSXvG6LsrUQg.png"><figcaption></figcaption></figure><p id="e31b"><a href="https://readmedium.com/a-complete-introduction-to-time-series-analysis-with-r-classical-decomposition-model-a4548a0c99b9">In previous articles</a>, we had opted for estimating and removing the trend, which would in turn yield series that were closer to stationary noise. For instance, estimating and removing different trends would give the following:</p><figure id="ea8c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*0zxSEHeEq4YlfQWOrXyvRQ.png"><figcaption></figcaption></figure><p id="237f">Instead of estimating the trend, we can simply apply differencing using the <code>timeDate::diff</code> function, yielding</p>
    <figure id="2e08">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/JairParra/6132d14f6a5828d1034833a8483e2625.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><figure id="56e2"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*YNyhAL0-TJ5GeWkhX1vPRg.png"><figcaption></figcaption></figure><p id="abc2">We see that the residuals indeed do not longer seem to have a clear trend and are also zero-centered. We can further inspect the ACF and PACF functions:</p>
    <figure id="db37">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/JairParra/07d21f1cb2d7bfaffb7d4e4a5b5dce9c.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
    

Options

  </div>
        </div>
    </figure></iframe></div></div></figure><figure id="4866"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*DLzGMn1bci1MH-1j3QUTRw.png"><figcaption></figcaption></figure><figure id="9c74"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*vXzF0kfYgen2Fd1tH5PXEA.png"><figcaption></figcaption></figure><p id="fa84">Pretty much all the lags are within bounds! That’s great! Here we only applied one lag, but what would happen if we had applied a greater lag, say 10?</p>
    <figure id="00ea">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/JairParra/2ab5b298c084f85fd709f2322791ed4c.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><figure id="e22f"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*1Wmj7LXuKyMuJBvhzvUtLg.png"><figcaption></figcaption></figure><p id="a507">Once again, we can use <code>ggAcf</code> to examine the estimated ACF:</p><figure id="e9a8"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*7JEBP60-JXa5OCp0BMOO0w.png"><figcaption></figcaption></figure><p id="99f5">Wah! That’s a huge loss of data! If we wanted to make predictions using this data, we would have less data than before to train some models. Further, it even looks like we induced some extra-non stationarity! Indeed, there is always a trade-off between obtaining closely stationary residuals through differencing and the amount of data left. In practice, differencing is usually not that big, however.</p><h2 id="3640">Next time</h2><p id="0b9e">That’s it for today! Next time, we will get a bit more mathematical and explore the <a href="https://medium.com/@hair.parra/a-complete-introduction-to-time-series-analysis-with-r-properties-of-the-autocovariance-6c7308437e5a">properties of the autocovariance function</a>, how to estimate autocorrelation, and more! Stay tuned, and happy learning!</p><div id="60cf" class="link-block">
      <a href="https://medium.com/@hair.parra/a-complete-introduction-to-time-series-analysis-with-r-properties-of-the-autocovariance-6c7308437e5a">
        <div>
          <div>
            <h2>A Complete Introduction To Time Series Analysis (with R):: Properties of the Autocovariance…</h2>
            <div><h3>Recall that back in our discussion of stationary processes, we discussed the autocovariance function as well as the ACF…</h3></div>
            <div><p>medium.com</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*I7WxHXuEuoxL1JhH.png)"></div>
          </div>
        </div>
      </a>
    </div><h2 id="3fbe">Last article</h2><p id="df00"><a href="https://readmedium.com/a-complete-introduction-to-time-series-analysis-with-r-classical-decomposition-model-part-ii-aa43b524680d">Classical Decomposition Model part II</a></p><div id="9cb5" class="link-block">
      <a href="https://readmedium.com/a-complete-introduction-to-time-series-analysis-with-r-classical-decomposition-model-part-ii-aa43b524680d">
        <div>
          <div>
            <h2>A Complete Introduction To Time Series Analysis (with R):: Classical Decomposition Model part II</h2>
            <div><h3>In the last art icle, we introduced the classical decomposition model, and had a comprehensive discussion of trend…</h3></div>
            <div><p>medium.com</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*jMWdPvJ6EqA7X6Kb57tx3g.png)"></div>
          </div>
        </div>
      </a>
    </div><h2 id="d297">Main page</h2><div id="c486" class="link-block">
      <a href="https://readmedium.com/a-complete-introduction-to-time-series-analysis-with-r-9882f2d44c9d">
        <div>
          <div>
            <h2>A Complete Introduction To Time Series Analysis (with R)</h2>
            <div><h3>During these times of the Covid19 pandemic, you have perhaps heard about the collaborative efforts to predict new…</h3></div>
            <div><p>medium.com</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*TL2PeOANEN4zG0_OqoHptQ.jpeg)"></div>
          </div>
        </div>
      </a>
    </div><h2 id="f7b9">Follow me at</h2><ol><li><a href="https://www.linkedin.com/in/hair-parra-526ba19b/">https://www.linkedin.com/in/hair-parra-526ba19b/</a></li><li><a href="https://github.com/JairParra">https://github.com/JairParra</a></li><li><a href="https://medium.com/@hair.parra">https://medium.com/@hair.parra</a></li></ol></article></body>

A Complete Introduction To Time Series Analysis (with R):: Differencing

Applying differencing to a Time Series can remove both the trend and seasonal components

In the last two articles , we studied the classical decomposition model, which allows us to interpret our time series the following way:

Using this powerful assumption, we can further estimate both the trend and the season, so that we have a better idea of what our series is actually “made of”. For instance, we could use the moving-average filter to estimate the trend, then estimating seasonality and carrying a classical analysis. In this article, we will learn a powerful technique to remove trend and sometimes even seasonality simultaneously: differencing. Let’s see what this is all about! For this, we will make use of two important operators: the backward shift operator and the difference operator.

Backward Shift Operator

Just as it names implies, if we are given some observation at time X, the backward shift operator simply outputs the previous observation in time. Note that we can apply it more than once, which gives

That is, applied j-times at time t, it returns back the (t-j)’th observation.

Lag-1 Difference Operator

Just as its name implies, we have a difference between the current point in time and the previous one. We see that the Difference operator can also be expressed in terms of the Backward shift operator!

Difference and Lag-Difference Operators

This is well-defined, as

Note that these operators are different! Although both are generalized versions of the Lag-1 difference operators, the general lag difference operator is just producing a difference between the current time point and the one j steps before, whereas the difference operator really is just applying differencing over and over. Let’s make this clear with an example:

Which is clearly not the same as

Removing trend and seasonality through differencing

Removing trend Ok, so why do we need these? It turns out that these are very powerful in helping us remove both trend and even seasonality! How? First, suppose that we have the model

and

is a linear function of the trend. Then,

So for instance, if Yt is White Noise, differencing once will remove the linear trend and yield a stationary series. It turns out this trick also works for dealing with seasonality!

Removing seasonality

Suppose we have the classical decomposition model

where the trend is linear and Yt is White Noise just as above. In addition, let

where d is the period. Then, if we apply the lag-difference operator, we obtain

So indeed not only we removed the trend, but also the seasonality at the same time, and the residuals will be stationary so long Yt is.

When to use each difference operator?

  • If there is a linear trend, we can use differencing to remove it without estimating parameters.
  • Applying differencing will then yield residuals which are closer to a stationary process.
  • However, note that some data is lost when applying to difference to all points (think about the boundaries! ) There is a trade-off between using different operators and predictive power!
  • Although differencing can get read of trend and seasonality, in practice it is good to estimate and remove the main trend first (which has the effect of focusing on the harder-to-estimate semi-noise).

As a general rule of thumb, you might consider using

How to R

First load the necessary packages:

Recall the LakeHuron dataset we had previously worked with:

In previous articles, we had opted for estimating and removing the trend, which would in turn yield series that were closer to stationary noise. For instance, estimating and removing different trends would give the following:

Instead of estimating the trend, we can simply apply differencing using the timeDate::diff function, yielding

We see that the residuals indeed do not longer seem to have a clear trend and are also zero-centered. We can further inspect the ACF and PACF functions:

Pretty much all the lags are within bounds! That’s great! Here we only applied one lag, but what would happen if we had applied a greater lag, say 10?

Once again, we can use ggAcf to examine the estimated ACF:

Wah! That’s a huge loss of data! If we wanted to make predictions using this data, we would have less data than before to train some models. Further, it even looks like we induced some extra-non stationarity! Indeed, there is always a trade-off between obtaining closely stationary residuals through differencing and the amount of data left. In practice, differencing is usually not that big, however.

Next time

That’s it for today! Next time, we will get a bit more mathematical and explore the properties of the autocovariance function, how to estimate autocorrelation, and more! Stay tuned, and happy learning!

Last article

Classical Decomposition Model part II

Main page

Follow me at

  1. https://www.linkedin.com/in/hair-parra-526ba19b/
  2. https://github.com/JairParra
  3. https://medium.com/@hair.parra
Timeseries
Statistics
Machine Learning
R
Forecasting
Recommended from ReadMedium