Power BI and R- Timeseries series Part 9- Decompose None seasonal Data

1

In the last posts, I have explained about some main concepts of Time series. How to decompose time series that has irregular, trend and seasonality components have been explained in: seasonality component decompose).

now image we have a dataset that does not have any seasonality also does not show a clear trend.

This data is about the age of death of kings from https://robjhyndman.com/tsdldata/misc/kings.dat

We have to read the data using scan function :

 kings <- scan("http://robjhyndman.com/tsdldata/misc/kings.dat",skip=3)

now, I am going to convert the data into a time series object using “TS” function. Then I am using plot.ts() to show the  data

kingTSObj<-ts(kings)
plot.ts(kingTSObj)

The result of running the code will be below image:

1

as you can see, there is no seasonality in data, just a none clear trend, and irregular components.
Normally, to have a clear image of “trend” of data, we use”Smoothing approach”, Smoothing usually use to better see the trend of data. We use this approach to smooth the irregular components to remove the irregular components.

 

Simple moving average

SMA, is about calculating the average of data for specific time span,

let explain the concept by below example” we have a data as:

Daily Closing Prices: 11,12,13,14,15,16,17

so for each day we calculate the average of last 5 recent days

First day of 5-day SMA: (11 + 12 + 13 + 14 + 15) / 5 = 13

Second day of 5-day SMA: (12 + 13 + 14 + 15 + 16) / 5 = 14

Third day of 5-day SMA: (13 + 14 + 15 + 16 + 17) / 5 = 15

in R there is a package that helps us to smooth the data

install.packages(“TTR”)
library(“TTR”)

I am going to smooth the data with 3 past data point we have as below

library("TTR")
	kingsTSSMA<-SMA(kingTSObj,n=3)
	plot.ts(kingsTSSMA)

However after running the code, you will see the chart is not that much smoother, so we need to consider more data span, entries 5 and 6. but finally 8 spans make the chart more smooth

 kings <- scan("http://robjhyndman.com/tsdldata/misc/kings.dat",skip=3)
kingTSObj<-ts(kings)
library("TTR")
kingsTSSMA<-SMA(kingTSObj,n=8)
plot.ts(kingsTSSMA)

3

so know we able to see the obvious increasing trend in data without any irregular components

 

 

reference: https://radacad.com/new-series-of-time-series-part-1

http://a-little-book-of-r-for-time-series.readthedocs.io/en/latest/src/timeseries.html

https://onlinecourses.science.psu.edu/stat510/?q=node/70

Leila Etaati on LinkedinLeila Etaati on TwitterLeila Etaati on Youtube
Leila Etaati
Trainer, Consultant, Mentor
Leila is the first Microsoft AI MVP in New Zealand and Australia, She has Ph.D. in Information System from the University Of Auckland. She is the Co-director and data scientist in RADACAD Company with more than 100 clients in around the world. She is the co-organizer of Microsoft Business Intelligence and Power BI Use group (meetup) in Auckland with more than 1200 members, She is the co-organizer of three main conferences in Auckland: SQL Saturday Auckland (2015 till now) with more than 400 registrations, Difinity (2017 till now) with more than 200 registrations and Global AI Bootcamp 2018. She is a Data Scientist, BI Consultant, Trainer, and Speaker. She is a well-known International Speakers to many conferences such as Microsoft ignite, SQL pass, Data Platform Summit, SQL Saturday, Power BI world Tour and so forth in Europe, USA, Asia, Australia, and New Zealand. She has over ten years’ experience working with databases and software systems. She was involved in many large-scale projects for big-sized companies. She also AI and Data Platform Microsoft MVP. Leila is an active Technical Microsoft AI blogger for RADACAD.

Leave a Reply