# How to decompose a time series with trend and seasonal components using loess method in R?

R ProgrammingServer Side ProgrammingProgramming

#### Artificial Intelligence : The Future Of Programming

15 Lectures 54 mins

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### C Programming from scratch- Master C Programming

Best Seller

60 Lectures 8 hours

To decompose a time series with trend and seasonal components using loess method in R, we can follow the below steps −

• First of all, create a time series object.

• Then, use stl function to decompose the time series with trend and seasonal components.

## Example

#### Create the time series object

Let’s create a time series object using ts function −

x<-sample(1:1000,48)
TimeSeries<-ts(x,start=c(2017,1),end=c(2020,12),frequency=12)
TimeSeries

## Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov  Dec
2017  3  686 888 748 632 170 922 395 293 460 244 454
2018 540 560 496 436 619 936 286 243 495 580 542  14
2019 171 983 923 422 698 302 838 858 398  45   8 927
2020 700 323 743 201 588 654 197 620 199 929 954 213

Decompose the time series

Using stl function to decompose the time series object TimeSeries with trend and seasonal components as shown below −

x<-sample(1:1000,48)
TimeSeries<-ts(x,start=c(2017,1),end=c(2020,12),frequency=12)
stl(TimeSeries,s.window="periodic")

## Output

Call:
stl(x = TimeSeries, s.window = "periodic")

Components
seasonal  trend    remainder
Jan 2017 -13.51116  620.0779 -270.566700
Feb 2017 -25.67301  597.0258  266.647194
Mar 2017 -101.08461 573.9738 -19.889167
Apr 2017  219.41788 551.1312  225.450967
May 2017 -83.57931  528.2885 -430.709219
Jun 2017  74.54494  505.7850  219.670064
Jul 2017  30.41916  483.2815  151.299379
Aug 2017 -159.59590 460.6482  111.947695
Sep 2017 -80.86115  438.0150 -243.153796
Oct 2017 -186.38804 418.7237 -67.335678
Nov 2017  96.33461  399.4325  51.232899
Dec 2017  229.97668 398.6426 -196.619290
Jan 2018 -13.51116  397.8527 -41.341565
Feb 2018 -25.67301  405.1831  114.489873
Mar 2018 -101.08461 412.5136 -273.428943
Apr 2018  219.41788 419.9627 -136.380615
May 2018 -83.57931  427.4119  196.167391
Jun 2018  74.54494  433.9023  188.552751
Jul 2018  30.41916  440.3927  116.188144
Aug 2018 -159.59590 449.1809  184.414994
Sep 2018 -80.86115  457.9691 -63.107963
Oct 2018 -186.38804 473.2262 -234.838182
Nov 2018  96.33461  488.4833 -283.817943
Dec 2018  229.97668 494.2690  197.754316
Jan 2019 -13.51116  500.0547  10.456488
Feb 2019 -25.67301  507.5730 -413.900027
Mar 2019 -101.08461 515.0914  233.993203
Apr 2019  219.41788 538.5873  117.994862
May 2019 -83.57931  562.0831  470.496200
Jun 2019  74.54494  586.6381 -6.183045
Jul 2019  30.41916  611.1931 -486.612258
Aug 2019 -159.59590 619.9637 -230.367763
Sep 2019 -80.86115  628.7342 -57.873076
Oct 2019 -186.38804 614.9466  529.441447
Nov 2019  96.33461  601.1590  42.506428
Dec 2019  229.97668 585.6774 -23.654081
Jan 2020 -13.51116  570.1958  284.315323
Feb 2020 -25.67301  561.9913  15.681732
Mar 2020 -101.08461 553.7867  42.297887
Apr 2020  219.41788 546.5084 -222.926267
May 2020 -83.57931  539.2301 -250.650742
Jun 2020  74.54494  530.2467 -414.791602
Jul 2020  30.41916  521.2633  208.317571
Aug 2020 -159.59590 514.3819 -74.786011
Sep 2020 -80.86115  507.5006  357.360600
Oct 2020 -186.38804 504.2319 -232.843895
Nov 2020  96.33461  500.9633  185.702068
Dec 2020  229.97668 500.1919  18.831369
Updated on 11-Nov-2021 06:49:57