-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathui-2f.R
188 lines (156 loc) · 4.91 KB
/
ui-2f.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
rm(list=ls(all=TRUE))
setwd("../data/uncertainty/")
library("lubridate")
# library(devtools)
# devtools::install_github("PMassicotte/gtrendsR")
library("gtrendsR")
library("outliers")
# Lagging variables
lagf <- function(x,max.lag) embed(c(rep(NA,max.lag), x), max.lag+1)
# Outliers c sd away from the mean
away.mean <- function(z, c)
{
ub <- mean(z)+c*sd(z)
lb <- mean(z)-c*sd(z)
out <- (z>ub)|(z<lb)
return(out)
}
# Load the reuters data
xmr <- read.csv("raw/mreuters.csv", header=TRUE)
dmr <- as.Date(as.character(xmr[,1]))
xmr <- as.matrix(xmr[,2:NCOL(xmr)])
# select series to use
su <- 6
mainkwd <- "Uncertainty GB"
# Set the Google
kwd1 <- "uncertainty"
kwd2 <- "risk"
reg <- "GB" # Region
tsd <- "all" # Time Frame: "all" (since 2004),
cct <- 0 # category
kwd <- kwd1
stp <- "web"
mtitle1 <- paste(kwd, stp, sep=", ")
gt <- gtrends(kwd, reg, tsd, stp, cct)
x1 <- gt$interest_over_time$hits
d1 <- gt$interest_over_time$date
kwd <- kwd1
stp <- "news"
mtitle2 <- paste(kwd, stp, sep=", ")
gt <- gtrends(kwd, reg, tsd, stp, cct)
x2 <- gt$interest_over_time$hits
d2 <- gt$interest_over_time$date
kwd <- kwd2
stp <- "web"
mtitle3 <- paste(kwd, stp, sep=", ")
gt <- gtrends(kwd, reg, tsd, stp, cct)
x3 <- gt$interest_over_time$hits
d3 <- gt$interest_over_time$date
kwd <- kwd2
stp <- "news"
mtitle4 <- paste(kwd, stp, sep=", ")
gt <- gtrends(kwd, reg, tsd, stp, cct)
x4 <- gt$interest_over_time$hits
d4 <- gt$interest_over_time$date
sel <- d1 %in% dmr
d <- d1[sel]
z1 <- x1[sel]
z2 <- x2[sel]
z3 <- x3[sel]
z4 <- x4[sel]
pdf(file=paste("out/", su, "a.pdf", sep=""), width=11.7, height=8.3)
par(mfrow=c(2,2))
plot(d, z1, type="l", main=mtitle1, xlab="", ylab="")
plot(d, z2, type="l", main=mtitle2, xlab="", ylab="")
plot(d, z3, type="l", main=mtitle3, xlab="", ylab="")
plot(d, z4, type="l", main=mtitle4, xlab="", ylab="")
par(mfrow=c(1,1))
dev.off()
z <- cbind(z1, z2, z3, z4)
# Combination: simple average
s <- rowMeans(z, na.rm=TRUE)
# Compare uncleaned series
per1 <- d<as.Date("2012-01-01")
per2 <- d>=as.Date("2012-01-01")
c1 <- round(cor(s[per1], xmr[per1,su]),2)
c2 <- round(cor(s[per2], xmr[per2,su]),2)
c3 <- round(cor(s, xmr[,su]),2)
sub <- paste("Correlation: ", c1, "(<2012), ",
c2, "(>2012), ", c3, "(Overall)", sep="")
pdf(file=paste("out/", su, "b.pdf", sep=""), width=11.7, height=8.3)
par(mar=c(5,4,4,5)+.1)
plot(d, s, type="l", xlab=sub, ylab="Google",
main=mainkwd, col="blue")
par(new=TRUE)
plot(d, xmr[,su], type="l", xlab="", ylab="", xaxt="n",yaxt="n",
col="red")
axis(side = 4)
mtext(side = 4, line = 3, "Reuters")
dev.off()
# Outliers?
pdf(file=paste("out/", su, "c.pdf", sep=""), width=8.3, height=11.7)
par(mfrow=c(2,1))
# od <- scores(s, type="z", prob=0.99)
od <- away.mean(s, 4)
op <- od
op[which(op==TRUE)] <- s[which(op==TRUE)]
op[op==FALSE] <- NA
plot(d, s, main=paste("Google ", mainkwd, sep=""), type="l",
ylab="", xlab="")
points(d, op, col="red", pch=19)
# clean the series
sc <- s
sc[od] <- median(s)
#od <- scores(xmr[,su], type="z", prob=0.99)
od <- away.mean(xmr[,su], 4)
op <- od
op[which(op==TRUE)] <- xmr[which(op==TRUE),su]
op[op==FALSE] <- NA
plot(d, xmr[,su], main=paste("Reuters ", mainkwd, sep=""), type="l",
ylab="", xlab="")
points(d, op, col="red", pch=19)
# clean the series
rc <- xmr[,su]
rc[od] <- median(rc)
par(mfrow=c(1,1))
dev.off()
# Seasonal?
tss <- ts(sc, frequency=12)
ss <- stl(tss, s.window="per")
pdf(file=paste("out/", su, "d1.pdf", sep=""), width=8.3, height=11.7)
plot(ss, main=paste("Google ", mainkwd, sep=""))
dev.off()
# Deseasonalise but do not de-trend
scc <- tss-ss$time.series[,1]
tss <- ts(rc, frequency=12)
ss <- stl(tss, s.window="per")
pdf(file=paste("out/", su, "d2.pdf", sep=""), width=8.3, height=11.7)
plot(ss, main=paste("Reuters ", mainkwd, sep=""))
dev.off()
# Deseasonalise but do not de-trend
rcc <- tss-ss$time.series[,1]
# Compare the cleaned series
per1 <- d<as.Date("2012-01-01")
per2 <- d>=as.Date("2012-01-01")
c1 <- round(cor(scc[per1], rcc[per1]),2)
c2 <- round(cor(scc[per2], rcc[per2]),2)
c3 <- round(cor(scc, rcc),2)
sub <- paste("Correlation: ", c1, "(<2012), ",
c2, "(>2012), ", c3, "(Overall)", sep="")
pdf(file=paste("out/", su, "e.pdf", sep=""), width=11.7, height=8.3)
par(mar=c(5,4,4,5)+.1)
plot(d, scc, type="l", xlab=sub, ylab="Google",
main=paste(mainkwd, ", clean", sep=""), col="blue")
par(new=TRUE)
plot(d, rcc, type="l", xlab="", ylab="", xaxt="n",yaxt="n",
col="red")
axis(side = 4)
mtext(side = 4, line = 3, "Reuters")
dev.off()
ktemp <- cbind(as.numeric(sc), as.numeric(scc), as.numeric(rc), as.numeric(rcc))
rownames(ktemp) <- as.character(d)
colnames(ktemp) <- c(paste("Google-",mainkwd, "-Raw", sep=""),
paste("Google-",mainkwd, "-Cleaned", sep=""),
paste("Reuters-",mainkwd, "-Raw", sep=""),
paste("Reuters-",mainkwd, "-Cleaned", sep=""))
write.csv(ktemp, paste("out/OUTx-", mainkwd, ".csv", sep=""))