Skip to content

Instantly share code, notes, and snippets.

@stillmatic
Created April 26, 2020 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stillmatic/2a042cefe2021bf19dda3378858b3d01 to your computer and use it in GitHub Desktop.
Save stillmatic/2a042cefe2021bf19dda3378858b3d01 to your computer and use it in GitHub Desktop.
library(gtrendsR)
library(CausalImpact)
library(tidyverse)
plot_for_name <- function(food, time="today+5-y") {
df <- gtrendsR::gtrends(food, geo="US", time=time)
idx <- length(df$interest_over_time$date) - 6
ggplot2::ggplot(df$interest_over_time, aes(x=date, y=hits)) +
ggtitle(paste0("Search volume for '", food, "'"), subtitle = "Source: Google Trends, US Data") + geom_line() +
geom_vline(xintercept = df$interest_over_time$date[idx], color='red') +
ggplot2::labs(x='Date', y='Relative Search Volume')
}
ci_for_name <- function(food, time="today+5-y") {
df <- gtrendsR::gtrends(food, geo="US", time=time)
n_points = length(df$interest_over_time$date)
idx <- n_points - 6
food_ci = CausalImpact(df$interest_over_time$hits,
pre.period = c(35,idx - 1),
post.period = c(idx, n_points - 1)),
model.args = list(nseasons = 52))
return(food_ci)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment