# R FOR HYDROLOGISTS – Seasonality and Trend Decomposition

April 17, 2018
By

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

SEASONALITY AND TREND DECOMPOSITION
If you don’t have the data, please first get it from the first tutorial here. Also, you need to install and load the `ggplot2` package.
``` if(!require(ggplot2)){install.packages(ggplot2, dep=T)} ```
Answers to these exercises are available here.

Time series decomposition is a mathematical procedure which transforms a time series into multiple different time series’, often is split into 3 components:
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.
Trend: The underlying trend of the series. If the river level has increased or decreased constantly through the years.
Random: Also called “noise”, this is the residuals of the original time series after the seasonal and trend series are removed.

Exercise 1
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 tutorial that the time series has a yearly seasonality.
Please create a time series object of the `LEVEL` values with the function `ts`. Hint: frequency = 365.

Exercise 2
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 `LEVEL` values with the function ` decompose` and plot it.

Exercise 3
Please run an additive decomposition for the `LEVEL` values with the function ` decompose` and plot it.

Exercise 4
Use the additive formula Time series = Seasonal + Trend + Random to reconstruct the series and plot it.

Exercise 5
Use the multiplicative formula Time series = Seasonal * Trend * Random to reconstruct the series and plot it.

Exercise 6
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.

Exercise 7
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.

Exercise 8
Now, please use your function to calculate the periodogram for the seasonal and random components. Hint: remember the first deal with the NA values.

Exercise 9
Create an overlapped plot of the seasonal and random spectrum’s. Hint: you will see that the only different component is the random.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.