Book Image

Mastering Data analysis with R

By : Gergely Daróczi
Book Image

Mastering Data analysis with R

By: Gergely Daróczi

Overview of this book

Table of Contents (19 chapters)
Mastering Data Analysis with R
Credits
www.PacktPub.com
Preface

Seasonal decomposition


Well, it looks like the number of flights fluctuates a lot on weekdays, which is indeed a dominant characteristic of human-related activities. Let's verify that by identifying and removing the weekly seasonality by decomposing this time-series into the seasonal, trend, and random components with moving averages.

Although this can be done manually by utilizing the diff and lag functions, there's a much more straightforward way to do so with the decompose function from the stats package:

> plot(decompose(ts(daily$N, frequency = 7)))

Removing the spikes in the means of weekly seasonality reveals the overall trend of the number of flights in 2011. As the x axis shows the number of weeks since January 1 (based on the frequency being 7), the peak interval between 25 and 35 refers to the summertime, and the lowest number of flights happened on the 46th week – probably due to Thanksgiving Day.

But the weekly seasonality is probably more interesting. Well, it's pretty hard...