In the previous article we learn how to read and plot time series data. In this article of this series we will get to know that how to break the data into **trend** and **irregular** component.

**4). Decomposing** the time series involves trying to separate the time series into these components.

Before going ahead, lets’ discuss the types of decomposing:

**4.1). Non-seasonal data decomposing**

**4.2). seasonal data decomposing**

**4.3).seasonally adjusting**

**4.1). Non-seasonal Data**:

A non-seasonal time series consists of a trend component and an irregular component.

To estimate the trend component of a non-seasonal time series that can be described using an additive model, it is common to use a smoothing method, such as calculating the simple moving average of the time series.

The SMA() function in the “TTR” R package can be used to smooth time series data using a simple moving average.

> library("TTR")

You can then use the “SMA()” function to smooth time series data. To use the SMA() function, you need to specify the **order of the simple moving average**, using the parameter “n”.

*For example, to calculate a simple moving average of order 5, we set n=5 in the SMA() function. For example, as discussed in the last article, the time series of the age of death of 42 successive kings of England appears is non-seasonal, and can probably be described using an additive model, since the random fluctuations in the data are roughly constant in size over time.*

we can try to estimate the trend component of this time series by smoothing using a simple moving average. To smooth the time series using a simple moving average of order 3, and plot the smoothed time series data.

datatimeseriesSMA3<-SMA(datatimeseries,n=3) plot.ts(datatimeseriesSMA3)

Here is the plot lets have look

There’s still so much of randomness in this graph, if we increase the order of SMA function, lets see what happens.

datatimeseriesSMA8<-SMA(datatimeseries, n=8)

The data smoothed with a simple moving average of order 8 gives a clearer picture of the trend component, and we can see that the age of death of the English kings seems to have decreased from about 55 years old to about 38 years old during the reign of the first 20 kings, and then increased after that to about 73 years old by the end of the reign of the 40th king in the time series.

**4.2). Decomposing seasonal data: **

A seasonal time series consists of a trend component, a seasonal component and an irregular component.

We can use the **“decompose()”** function in R. This function estimates the trend, seasonal, and irregular components of a time series that can be described using an additive model. The function “decompose()” returns a list object as its result, where the estimates of the seasonal component, trend component and irregular component are stored in named elements of that list objects, called “**seasonal**”, “**trend**”, and “**random**” respectively.

Remember for decomposing time series we should have no or less than 2 periods.

In the above example we have set frequency=1, so it will give error.

For this we have to use different data, lets use this data.

childborn<-scan("http://robjhyndman.com/tsdldata/data/nybirths.dat") childborntimeseries<-ts(childborn,frequency=12,start=c(1987,1)) childborntimeseries plot.ts(childborntimeseries) #decomposing seasonal data childborntimeseriescomponents<-decompose(childborntimeseries) #check the seasonal data childborntimeseriescomponents$seasonal plot(childborntimeseriescomponents)

The plot above shows the original time series (observed), the estimated trend component (trend), the estimated seasonal component (seasonal), and the estimated irregular component (random). We see that the estimated trend component shows a small decrease from about 24 in 1947 to about 22 in 1948, followed by a steady increase from then on to about 27 in 1959.

**4.3). Seasonally Adjusting:**

If you have a seasonal time series that can be described using an **additive model**, *you can seasonally adjust the time series by estimating the seasonal component, and subtracting the estimated seasonal component from the original time series*.

We can do this using the estimate of the seasonal component calculated by the “*decompose()*” function.

For example, to seasonally adjust the time series of the number of births per month , we can estimate the seasonal component using “*decompose()*”, and then subtract the seasonal component from the original time series:

childbornstimeseriescomponents <- decompose(childborntimeseries) childborntimeseriesadjusted<-childbornstimeseries-childborntimeseriescomponents$seasonal

We can then plot the seasonally adjusted time series using the “plot()” function, by typing:

plot(childborntimeseriesseasonallyadjusted)

Lets have a look

You can see that the seasonal variation has been removed from the seasonally adjusted time series. The seasonally adjusted time series now just contains the trend component and an irregular component.

Like this you can do for any time series data Little help.

My library doesn’t have a TTR package .So how should I download it?

LikeLike