-
Notifications
You must be signed in to change notification settings - Fork 7
/
intro.Rmd
84 lines (60 loc) · 3.79 KB
/
intro.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
---
title: "Introduction to trendecon"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Introduction}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
*trendecon* - a package for obtaining and analyzing Google Trends timeseries data directly from R.
People's searches on Google can tell a lot about their demand for various goods and services as well as their perception of the state of the economy.
Hence, carefully chosen sets of Google Trends keywords can serve as timely economic indicators.
Functions within this package assist with obtaining, storing, comparing, and visualizing Google Trends results.
This in turn provides a convenient way to explore different keyword combinations and evaluate their econometric value and performance.
## Google Trends ##
Google Trends (https://trends.google.com/) is an online search tool from Google that can be used to analyse the popularity of various Google search terms across a specified time interval.
The popularity measure of a particular search term, given a specified time interval, is relative - it is a number ranging from 0 to 100 where the value of 100 marks the date when the specified search term was most popular.
By analyzing such relative popularity scores we can track the search term usage across time, compare its popularity between different geographical regions, or contrast it with other search terms.
For more information please see [Google Trends FAQ](Check https://support.google.com/trends/answer/4365533?hl=en).
## Using trendecon to obtain Google Trends data ##
The main function `ts_gtrends()` takes one or multiple search term keywords and a geographical location as main arguments and downloads data from the Google Trends API.
Suppose we are interested in the demand for cultural events in Switzerland.
Think of what people would google if they want to go to a cultural event.
For instance, they might google "cinema", "oper", "kino" and "theater".
The following command downloads the Google Trends data for these search terms:
```{r}
library(trendecon)
gt_data <- ts_gtrends(
keyword = c("cinema", "oper", "kino", "theater"),
geo = "CH",
)
```
By default, a weekly timeseries for the last 5 years is obtained but this interval can be changed by adjusting the `time` argument (See "Further Help" for more).
The output will be a "tibble" table with 3 columns: id (keyword), time (date), and value (relative popularity score):
```{r}
head(gt_data)
```
We can then plot the time series using a `ts_plot()` function from the `tsbox` package:
```{r, fig.width=7, fig.height=3}
library(tsbox)
ts_plot(gt_data)
```
To retrieve the common signal of all four time series we can, for instance, use the first principal component:
```{r, fig.width=7, fig.height=3}
ts_plot(ts_pick(ts_prcomp(gt_data), "PC1"))
```
Alternatively, `ts_gtrends()` function can accept Google Trends categories instead of search term keywords.
Unlike with search terms, Google Trends categories are expressed with category numbers. Such a category number can be specified instead of a keyword in which case the trend data for the predefined Google search category will be retrieved.
Under the hood it wraps around the `gtrends()` function from the [*gtrendsR*](https://cran.r-project.org/web/packages/gtrendsR/gtrendsR.pdf) package and modifies it by
a) vectorizing it so that multiple keywords can be requested in one go,
b) returning the output in a "tibble" form that can be passed to functions from the "tsbox" package,
c) automatically retrying in case of failures.
## Further Help ##
- For more details about the time interval specification see `help(gtrends)`.
- For an explanation of additional parameters accepted by the `ts_gtrends()` function see `help(ts_gtrends)`.