How Business Cycles impact the search “hits” for Unemployment?
Here, I am interested to explore whether the search for topics such as Unemployment, Medicaid, Health Insurance and Food Stamps is influenced by business cycles (defined as periods of economic expansion and contraction) in the United States. I examine period of 2004 to present day as that allows us to see 4 years before the Recession onset in 2008 and also that Google Trends began in 2004.
Application in R: Note that we are using Philippe Massicotte’s gtrendsR the go-to package for running Google Trends queries in R. Not reuqired any setting up API keys or anything.
gtrendsR#define multiple keywords
keywords = c("Unemployment","Medicaid", "Health Insurance", "Food Stamps" )
#define the time window
time = "all"
#define channels
channel = 'web' # Web is default
#other channel include 'news', 'images','youtube'
#use '?gtrends' to find more description for arguments
trends = gtrends(keywords,
gprop = channel,
time = time,
geo="US",
onlyInterest = TRUE
)
trends %>% summary()
trends %>%
.$interest_over_time %>%
glimpse()
head(trends$interest_over_time, 10)
trends.dcast = dcast(trends$interest_over_time, date ~ keyword+geo, value.var = "hits")
head(trends.dcast)plot(trends.dcast$date, trends.dcast$Unemployment_US,
type="n", ylim=c(0,100),
ylab="Google Search Hits (Normalized)",
xlab = "Date (Year/Month/Date)",
xaxs="i", yaxs="i",
las = 2, tck=-0.0, tcl = -0.0,
main = "Google Search Hits for :\nUnemployment, Medicaid, Health Insurance and Food Stamps", cex.main=1)
lines(trends.dcast$date, trends.dcast$Unemployment_US,
type="l", lwd=2.5, col="red2")
lines(trends.dcast$date, trends.dcast$Medicaid_US,
type="l", lwd=2.5, col="black")
lines(trends.dcast$date, trends.dcast$`Health Insurance_US`,
type="l", lwd=2.5, col="darkgreen")
lines(trends.dcast$date, trends.dcast$`Food Stamps_US`,
type="l", lwd=2.5, col="purple")
legend("topright", legend=c("Unemployment",
"Medicaid",
"Health Insurance",
"Food Stamps"),
col=c("red2", "black", "darkgreen","purple"), lty=1, cex=0.8)trends %>%
.$interest_over_time %>%
mutate_at("hits", ~ifelse(. == "<1", 0.5, .)) %>% # replace with 0.5
mutate_at("hits", ~as.numeric(.)) %>%
ggplot(aes(x = date, y = hits)) +
geom_line(colour = "red", size = 1.5) +
facet_wrap(~keyword) +
ggthemes::theme_fivethirtyeight() -> plot2
plot2