Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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