#!/usr/bin/R
#
###########################################
#
# File: plot_data_science_trends.R
# Author: Ra Inta, for Big Head Analytics, LLC and Accelebrate, Inc.
# Description: Plot some Google Trends data related to "What is a Data Scientist?"
# Created: August 28, 2018
# Last Modified: August 31, 2018
#
###########################################
#
# MIT License (see blurb at the end)
# Copyright (c) 2018, Ra Inta
#
###########################################

library(ggplot2)
library(scales)
library(ggthemes)

# According to 'Keywords Everywhere,' the current volume for 'What is Data Science?' is 18,100/month
trends_scale_factor = 181/0.7

# You will have to obtain your own CSV file from the Google Trends website: https://trends.google.com/trends/
trend_dat <- read.csv("Gtrends_WhatIsADataScientist_USonly.csv", stringsAsFactors=F)
# Annoyingly, Google Trends gives month only (no day), so we have to coerce by pretending it's the first day of the month
trend_dat$Month <- as.Date(paste(trend_dat$Month,"-01",sep=""), "%Y-%m-%d")
trend_dat$TotQuery <- (trend_dat$what.is.data.science)*trends_scale_factor

# Plot the query data, along with a local polynomial smoothed line to see the trend
trend_plt <- ggplot(trend_dat, aes(x=Month, y=TotQuery)) + geom_line(size=3) + geom_smooth(method="loess",size=3)
trend_plt <- trend_plt + scale_x_date(labels=date_format(format="%Y"), breaks = pretty_breaks(8)) + scale_y_continuous(labels = comma)
trend_plt <- trend_plt + xlab("Year") + ylab("Number of monthly queries") + ggtitle("Google queries for 'What is a Data Scientist?' (US only)")
trend_plt <- trend_plt + theme(text=element_text(size=24, family="Souvenir"))
trend_plt <- trend_plt + ggsave(trend_plt, file="google_trend_whatIsDataScientist_USonly.png", dpi = 400)

## Let's examine the seasonality using R's awesome decompose function! 
# To be lazy, we'll get rid of extra columns from the dataframe
trend_dat$what.is.data.science <- NULL
trend_dat$Month <- NULL

# Create the time-series object using ts
ts_trend = ts(trend_dat, start = c(2004, 01, 01), frequency = 12)

# Decompose the trend from seasonal variation using decompose
# The trend looks like the amplitude increases over time, so we'll assume it multiplies with the trend:
decompose_trend = decompose(ts_trend, "multiplicative")
 
# Save the plot
png(filename="Gtrend_seasonal_plot.png")
plot(decompose_trend, lwd=2, cex.axis=2.0)
dev.off()

# What is the ratio of peak-to-trough?
max(decompose_trend$seasonal)/min(decompose_trend$seasonal)  # 5.279642

# Where do the maxima and minima appear during the year?
png(filename="Gtrend_seasonal_periodicity.png")
plot(decompose_trend$seasonal[1:12], , type="l", lwd=2, cex.axis=2.0, xlab="Month", ylab="Relative amplitude" )
dev.off()

# MIT License
#
# Copyright (c) 2018, Ra Inta
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

##################################################
# End of plot_data_science_query.R
##################################################