<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/r-for-hydrologists-seasonality-and-trend-decomposition/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.r-exercises.com/2018/04/17/r-for-hydrologists-seasonality-and-trend-decomposition/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2018/02/magdalena.jpg?ssl=1" target="_blank"><img sizes="(max-width: 150px) 100vw, 150px" src="https://i1.wp.com/www.r-exercises.com/wp-content/uploads/2018/02/magdalena-150x150.jpg?resize=150%2C150&ssl=1" alt="" width="150" class="wp-image-10623" data-recalc-dims="1" height="150"></a><br>
<strong>R FOR HYDROLOGISTS </strong></p>
<p>SEASONALITY AND TREND DECOMPOSITION<br>
If you don’t have the data, please first get it from the first tutorial <a rel="nofollow" href="https://www.r-exercises.com/2018/02/27/r-for-hydrologists-loading-and-plotting-the-data-part-1/" target="_blank">here</a>. Also, you need to install and load the <code>ggplot2</code> package.<br>
<code><br>
if(!require(ggplot2)){install.packages(ggplot2, dep=T)}<br>
</code><br>
Answers to these exercises are available <a rel="nofollow" href="http://r-exercises.com/2018/04/17/r-for-hydrologists-seasonality-and-trend-decomposition-solution/" target="_blank">here</a>.</p>
<p>Time series decomposition is a mathematical procedure which transforms a time series into multiple different time series’, often is split into 3 components:<br>
Seasonal: Patterns that repeat with a fixed period of time. For example, in the river, we can see that every year the river has a high level regimen in the middle of the year and low levels in the beginning and end of the year. That is a yearly seasonality.<br>
Trend: The underlying trend of the series. If the river level has increased or decreased constantly through the years.<br>
Random: Also called “noise”, this is the residuals of the original time series after the seasonal and trend series are removed.</p>
<p><strong>Exercise 1</strong><br>
A centered moving average can be used to smooth the time series and detect the underlying trend. To perform the decomposition, it is vital to use a moving window of the exact size of the seasonality. In this case, we know from the last <a rel="nofollow" href="http://r-exercises.com/2018/04/10/r-for-hydrologists-frequency-analysis%20/" target="_blank"> tutorial </a> that the time series has a yearly seasonality.<br>
Please create a time series object of the <code>LEVEL</code> values with the function <code>ts</code>. Hint: frequency = 365.</p>
<p><strong>Exercise 2</strong><br>
The decomposition can be calculated it in two different ways: an additive decomposition, when the values increase as a trend and the seasonality stays relatively constant. In a multiplicative decomposition, the amplitude of the seasonality also increases along with the trend. Please run an additive decomposition for the <code>LEVEL</code> values with the function <code> decompose</code> and plot it.</p>
<p><strong>Exercise 3</strong><br>
Please run an additive decomposition for the <code>LEVEL</code> values with the function <code> decompose</code> and plot it.</p>
<p><strong>Exercise 4</strong><br>
Use the additive formula Time series = Seasonal + Trend + Random to reconstruct the series and plot it.</p>
<p><strong>Exercise 5</strong><br>
Use the multiplicative formula Time series = Seasonal * Trend * Random to reconstruct the series and plot it.</p>
<p><strong>Exercise 6</strong><br>
As you can see, the trend is the same in the two decomposition’s, but the seasonal and random components are different. Please calculate and plot the difference between multiplicative and additive models.</p>
<p><strong>Exercise 7</strong><br>
In order to compare each other, we can estimate the power spectra and convert it in to a probability distribution. The trick is very easy; you just have to normalize it. Please write a function for it.</p>
<p><strong>Exercise 8</strong><br>
Now, please use your function to calculate the periodogram for the seasonal and random components. Hint: remember the first deal with the NA values.</p>
<p><strong>Exercise 9</strong><br>
Create an overlapped plot of the seasonal and random spectrum’s. Hint: you will see that the only different component is the random.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="https://www.r-exercises.com/2017/04/17/forecasting-exponential-smoothing-exercises-part-3/" title="Forecasting: Exponential Smoothing Exercises (Part-3)" target="_blank">Forecasting: Exponential Smoothing Exercises (Part-3) </a></li>
<li><a rel="nofollow" href="https://www.r-exercises.com/2017/04/25/forecasting-for-small-business-exercises-part-3/" title="Forecasting for small business Exercises (Part-3)" target="_blank">Forecasting for small business Exercises (Part-3) </a></li>
<li><a rel="nofollow" href="https://www.r-exercises.com/2017/07/13/parallel-computing-exercises-foreach-and-doparallel-part-2/" title="Parallel Computing Exercises: Foreach and DoParallel (Part-2)" target="_blank">Parallel Computing Exercises: Foreach and DoParallel (Part-2) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/" target="_blank">Explore all our (>1000) R exercises</a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/r-courses/" target="_blank">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
	<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.r-exercises.com/2018/04/17/r-for-hydrologists-seasonality-and-trend-decomposition/"> R-exercises</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/-i5HvWneBw0" height="1"></a></div>