forked from 321k/Google-Trends
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDaily data example.R
231 lines (175 loc) · 6.08 KB
/
Daily data example.R
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
library(Rmisc)
library(ggplot2)
library(dplyr)
# The Google Trends formating functions -----------------------------------
#This script automates the downloading of Google Trends.
#It works best with firefox in combination with the Tab Mix Plus add-on that is used to automate tab closing.
#Ask firefox not to prompt for new downloads and this script should run automatically.
#Google Trends restricts the number of download to roughly 400 at a time.
URL_GT=function(keyword="", country=NA, region=NA, year=NA, month=1, length=3){
start="http://www.google.com/trends/trendsReport?hl=en-US&q="
end="&cmpt=q&content=1&export=1"
geo=""
date=""
#Geographic restrictions
if(!is.na(country)) {
geo="&geo="
geo=paste(geo, country, sep="")
if(!is.na(region)) geo=paste(geo, "-", region, sep="")
}
queries=keyword[1]
if(length(keyword)>1) {
for(i in 2:length(keyword)){
queries=paste(queries, "%2C ", keyword[i], sep="")
}
}
#Dates
if(!is.na(year)){
date="&date="
date=paste(date, month, "%2F", year, "%20", length, "m", sep="")
}
URL=paste(start, queries, geo, date, end, sep="")
URL <- gsub(" ", "%20", URL)
return(URL)
}
downloadGT=function(URL, downloadDir){
#Determine if download has been completed by comparing the number of files in the download directory to the starting number
startingFiles=list.files(downloadDir)
browseURL(URL)
endingFiles=list.files(downloadDir)
while(length(setdiff(endingFiles,startingFiles))==0) {
Sys.sleep(3)
endingFiles=list.files(downloadDir)
}
filePath=setdiff(endingFiles,startingFiles)
return(filePath)
}
readGT=function(filePath){
rawFiles=list()
for(i in 1:length(filePath)){
if(length(filePath)==1) rawFiles[[1]]=read.csv(filePath, header=F, blank.lines.skip=F)
if(length(filePath)>1) rawFiles[[i]]=read.csv(filePath[i], header=F, blank.lines.skip=F)
}
output=data.frame()
name=vector()
for(i in 1:length(rawFiles)){
data=rawFiles[[i]]
name=as.character(t(data[5,-1]))
#Select the time series
start=which(data[,1]=="")[1]+3
stop=which(data[,1]=="")[2]-2
#Skip to next if file is empty
if(ncol(data)<2) next
if(is.na(which(data[,1]=="")[2]-2)) next
data=data[start:stop,]
data[,1]=as.character(data[,1])
#Convert all columns except date column into numeric
for(j in 2:ncol(data)) data[,j]=as.numeric(as.character(data[,j]))
#FORMAT DATE
len=nchar(data[1,1])
#Monthly data
if(len==7) {
data[,1]=as.Date(paste(data[,1], "-1", sep=""), "%Y-%m-%d")
data[,1]=sapply(data[,1], seq, length=2, by="1 month")[2,]-1
data[,1]=as.Date(data[,1], "%Y-%m-%d", origin="1970-01-01")
}
#Weekly data
if(len==23){
data[,1]=sapply(data[,1], substr, start=14, stop=30)
data[,1]=as.Date(data[,1], "%Y-%m-%d")
}
#Daily data
if(len==10) data[,1]=as.Date(data[,1], "%Y-%m-%d")
#Structure into panel data format
panelData=data[1:2]
panelData[3]=name[1]
names(panelData)=c("Date", "SVI", "Keyword")
if(ncol(data)>2) {
for(j in 3:ncol(data)) {
appendData=data[c(1,j)]
appendData[3]=name[j-1]
names(appendData)=c("Date", "SVI", "Keyword")
panelData=rbind(panelData, appendData)
}
}
#Add file name
panelData[ncol(panelData)+1]=filePath[i]
#Add path to filename
names(panelData)[4]="Path"
#Merge several several files into one
if(i==1) output=panelData
if(i>1) output=rbind(output, panelData)
}
return(output)
}
readGeoGT=function(filePath){
output=data.frame()
rawFiles=list()
for(i in 1:length(filePath)){
if(length(filePath)==1) rawFiles[[1]]=read.csv(filePath, header=F, blank.lines.skip=F)
if(length(filePath)>1) rawFiles[[i]]=read.csv(filePath[i], header=F, blank.lines.skip=F)
}
for(i in 1:length(rawFiles)){
data=rawFiles[[i]]
start=which(data[,1]=="")[3]+3
stop=which(data[,1]=="")[4]-1
names=data[start-1,]
for(j in 1:ncol(names)) names(data)[j]=as.character(names[1,j])
data=data[start:stop,]
data[,1]=as.character(data[,1])
data[,-1]=as.numeric(as.character(data[,-1]))
data[ncol(data)+1]=filePath[i]
output=rbind(output, data)
}
return(output)
}
# Downloading the data ----------------------------------------------------
search_terms = c("bull market", "bear market", "recession")
years = c(2005,2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016)
months = c(1,4,7,10)
res.daily=list()
counter=1
for(year in years){
for(month in months){
url=URL_GT(search_terms, year=year, month=month)
GT_dir = downloadGT(url, downloadDir)
GT_dir = paste(downloadDir, GT_dir, sep='/')
res.daily[[counter]] = readGT(GT_dir)
counter=counter+1
}
}
df.daily <- do.call("rbind", res.daily)
url = URL_GT(search_terms)
GT_dir = downloadGT(url, downloadDir)
GT_dir = paste(downloadDir, GT_dir, sep='/')
df.weekly = readGT(GT_dir)
# Formating the data ------------------------------------------------------
df.merged = merge(df.daily, df.weekly, by=c('Date', 'Keyword'), all.x=T)
df.merged$adjustment_factor = df.merged$SVI.y /df.merged$SVI.x
for(i in search_terms){
r=which(df.merged$Keyword==i)
for(j in 2:length(r)){
if(!is.finite(df.merged$adjustment_factor[r][j])){
df.merged$adjustment_factor[r][j] = df.merged$adjustment_factor[r][j-1]
}
}
}
df.merged$daily = df.merged$adjustment_factor * df.merged$SVI.x
df.merged$weekly = df.merged$SVI.y
for(i in search_terms){
r=which(df.merged$Keyword==i)
for(j in 2:length(r)){
if(is.na(df.merged$weekly[r][j])){
df.merged$weekly[r][j] = df.merged$weekly[r][j-1]
}
}
}
# Plotting the data -------------------------------------------------------
df.merged$daily[which(is.infinite(df.merged$daily))] = NA
p1 = df.merged %>%
ggplot(aes(Date, daily, color=Keyword))+geom_line()
p2 = df.merged %>%
ggplot(aes(Date, weekly, color=Keyword))+geom_line()
multiplot(p1,p2)
# Saving the data ---------------------------------------------------------
write.csv(df.merged,'df.merged.csv')