<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/smoothing-time-series-data/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.displayr.com/smoothing-time-series-data/?utm_medium=Feed&utm_source=Syndication"> R – Displayr</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>These include both global methods, which involve fitting a regression over the whole time series; and more flexible local methods, where we relax the constraint by a single parametric function. Further details about how to construct estimated smooths in R can be found <a rel="nofollow" href="https://www.displayr.com/how-to-add-trend-lines-in-r-using-plotly/%20%E2%80%8E" target="_blank">here</a>.</p>
<h2>1. Global trends over time</h2>
<h3>i. Linear</h3>
<p>One of the simplest methods to identify trends is to fit the time series to the linear regression model.</p>
<h3>ii. Quadratic</h3>
<p>For more flexibility, we can also fit the time series to a quadratic expression — that is, we use linear regression with the expanded basis functions (predictors) <em>1, x, x<sup>2</sup></em>.</p>
<h3>iii. Polynomial</h3>
<p>If the linear model is not flexible enough, it can be useful to try a higher-order polynomial.  In practice, polynomials of degrees higher than three are rarely used. As demonstrated in the example below, changing from quadratic and cubic trend lines does not always significantly improve the goodness of fit.</p>
<p> </p>
<p><img sizes="(max-width: 809px) 100vw, 809px" src="https://i0.wp.com/www.displayr.com/wp-content/uploads/2018/07/smoothing-time-series-global.png?w=450&ssl=1" alt="Global trend lines" class="wp-image-5641" data-recalc-dims="1"></p>
<h2>2. Local smoothers</h2>
<p>The first three approaches assume that the time series follows a single trend. Often, we want to relax this assumption. For example, we do not want variation at the beginning of the time-series to affect estimates near the end of the time series. In the following section, we demonstrate the use of local smoothers using the Nile data set (included in R’s built in data sets). It contains measurements of the annual river flow of the Nile over 100 years and is less regular than the data set used in first example.</p>
<h3>i. Moving averages</h3>
<p>The easiest local smoother to grasp intuitively is the moving average (or running mean) smoother. It consists of taking the mean of a fixed number of nearby points. As we only use nearby points, adding new data to the end of the time series does not change estimated values of historical results. Even with this simple method we see that the question of how to choose the neighborhood is crucial for local smoothers. Increasing the bandwidth from 5 to 20 suggests that there is a gradual decrease in annual river flow from 1890 to 1905, instead of a sharp decrease at around 1900.</p>
<p><img sizes="(max-width: 956px) 100vw, 956px" src="https://i0.wp.com/www.displayr.com/wp-content/uploads/2018/07/smoothing-time-series-movingaverage.png?w=450&ssl=1" alt="Moving average smoothers" class="wp-image-5642" data-recalc-dims="1"></p>
<h3>ii. Running line</h3>
<p>The running-line smoother reduces this bias by fitting a linear regression in a local neighborhood of the target value <em>x</em><sub>i</sub>. A popular algorithm using the running line smoother is Friedman’s super-smoother, which uses cross-validation to find the best span. As seen in the plot below, the Friedman’s super-smoother with the cross-validated span is able to detect the sharp decrease in annual river flow at around 1900.</p>
<p><img sizes="(max-width: 979px) 100vw, 979px" src="https://i0.wp.com/www.displayr.com/wp-content/uploads/2018/07/smoothing-time-series-runningline.png?w=450&ssl=1" alt="Running line smoothers" class="wp-image-5644" data-recalc-dims="1"></p>
<h3>iii. Kernel smoothers</h3>
<p>An alternative approach to specifying a neighborhood is to decrease weights further away from the target value. In the figure below, we see that the continuous Gaussian kernel gives a smoother trend than a moving average or running-line smoother.</p>
<p><img sizes="(max-width: 974px) 100vw, 974px" src="https://i0.wp.com/www.displayr.com/wp-content/uploads/2018/07/smoothing-time-series-kernel.png?w=450&ssl=1" alt="Kernel smoothers" class="wp-image-5647" data-recalc-dims="1"></p>
<h3>iv. Smoothing splines</h3>
<p>Splines consist of a piece-wise polynomial with pieces defined by a sequence of knots where the pieces join smoothly. It is most common to use cubic splines. Higher order polynomials can have erratic behavior at the boundaries of the domain.</p>
<p>The smoothing spline avoids the problem of over-fitting by using regularized regression. This involves minimizing a criterion that includes both a penalty for the least squares error and roughness penalty. Knots are initially placed at all of the data points. But the smoothing spline avoids over-fitting because the roughness penalty shrinks the coefficients of some of the basis functions towards zero. The smoothing parameter lambda controls the trade-off between goodness of fit and smoothness. It can be chosen by cross-validation.</p>
<p><img sizes="(max-width: 972px) 100vw, 972px" src="https://i1.wp.com/www.displayr.com/wp-content/uploads/2018/07/smoothing-time-series-spline.png?w=450&ssl=1" alt="Smoothing spline" class="wp-image-5648" data-recalc-dims="1"></p>
<h3>v. LOESS</h3>
<p>LOESS (locally estimated scatterplot smoother) combines local regression with kernels by using locally weighted polynomial regression (by default, quadratic regression with tri-cubic weights). It is one of the most frequently used smoothers because of its flexibility. However, unlike Friedman’s super smoother or the smoothing spline, LOESS does not use cross-validation to select a span.</p>
<p><img sizes="(max-width: 973px) 100vw, 973px" src="https://i1.wp.com/www.displayr.com/wp-content/uploads/2018/07/smoothing-time-series-loess.png?w=450&ssl=1" alt="LOESS" class="wp-image-5653" data-recalc-dims="1"></p>
<p><strong>Find out more about <a rel="nofollow" href="https://www.displayr.com/blog/" target="_blank">data visualizations</a> here. </strong></p>
<p> </p>
<p> </p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.displayr.com/smoothing-time-series-data/?utm_medium=Feed&utm_source=Syndication"> R – Displayr</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/u7V8VvWpuFc" height="1"></a></div>