Nearly every phenomenon can be represented as a time series.
It is therefore not surprising that time series analysis is one of most popular topics within data-science communities.
As is often the case, R provides a great tool for time-series decomposition, starting with the stl()
function provided within base R itself. This function will be the base of our recipe.
This recipe will mainly use the stl()
function, which implements the Loess()
method for time-series decomposition.
Using this method, we are able to separate a time series into three different parts:
As mentioned earlier, this function is provided with every R base version, and we therefore don't need to install any additional packages.
A dataset, named nottem, is provided with the R base as well, and is composed and defined by R documentation as a time series object containing average air temperatures at Nottingham Castle, in degrees Fahrenheit, for 20 years.
You can easily inspect it in the viewer pane by running the View()
function on it:
View(nottem)
stl()
function to the nottem
dataset:nottem_decomposition <- stl(nottem, s.window = "periodic")
plot(nottem_decomposition)
Let's take a look at the following image:
plot(nottem_decomposition$time.series[,2],ylab = "trend_component")
Let's take a look at the following graph: