Created
April 26, 2020 17:58
-
-
Save stillmatic/2a042cefe2021bf19dda3378858b3d01 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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