-
Notifications
You must be signed in to change notification settings - Fork 0
/
Google Trends Analysis.R
95 lines (68 loc) · 2.79 KB
/
Google Trends Analysis.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
library(readr)
library(DIMORA)
library(lmtest)
library(Metrics)
library(forecast)
library(lubridate)
source("support.R")
Google_Trends <- read.timeseries.google("./gtrend_data/Google Trends Data.csv")
plot(ts(Google_Trends[2:4], freq=365.25/7,
start=decimal_date(ymd("2017-12-10"))), main = "Google Trends")
amazon = get.ts.google(Google_Trends$Amazon, "2017-12-10")
netflix = get.ts.google(Google_Trends$Netflix, "2017-12-10")
zoom = get.ts.google(Google_Trends$Netflix, "2017-12-10")
###AMAZON
autoplot(amazon)
acf(amazon, lag.max = 260) #There is seasonality
pacf(amazon, lag.max = 104)
#Qui ho tolto il 2017 perchè avendo solo 4 osservazioni dava prroblemi di intepretabilità nel plot
seas_amz= ts(Google_Trends$Amazon[4:260], freq=52,
start=decimal_date(ymd("2018-1-6")))
ggseasonplot(seas_amz, main = "Seasonal Plot: Amazon")
ggseasonplot(seas_amz, polar=TRUE) +
ylab("Amazon Trends") +
ggtitle("Amazon Trend: a weekly overview")
#Per maggiore interpretqbilità , potebbe essere un'idea fare questi plot su base mensile
###BM###
amzn = Google_Trends$Amazon
amzn_bm<-BM(amzn,display = T)
summary(amzn_bm)
pred_bmamzn<- predict(amzn_bm, newx=c(1:260))
pred.bminstamzn<- make.instantaneous(pred_bmamzn)
plot(amzn, type= "l", xlim=c(1,260))
lines(pred.bminstamzn, lwd=2, col=2)
###ARIMA model
#We know our data as seasonal. So I need to differencing.
amazon %>% diff(lag=52) %>% ggtsdisplay() #dati ancora stagionali, bisogna differenziare ancora
amazon %>% diff(lag=52) %>% diff() %>% ggtsdisplay()#Now the ts is without seasonality.
diff_amz <- amazon %>% diff(lag=52) %>% diff()
diff_amz %>%
Arima(order=c(1,0,2), seasonal=c(1,0,0)) %>% residuals() %>% ggtsdisplay()
fit3 <- Arima(diff_amz, order=c(1,0,2), seasonal=c(1,0,0))
checkresiduals(fit3)
plot(diff_amz)
lines(fitted(fit3), col = 2)
fit3 %>% forecast(h=52) %>% autoplot()
#Now I try to use the auto.arima function on the original amazon time serie:
amazon %>%
auto.arima() %>% residuals() %>% ggtsdisplay()
#As we expected, there is one differencing component and one seasonal differencing component.
fit1 <- Arima(amazon, order=c(1,1,2), seasonal=c(1,1,0))
checkresiduals(fit1)
plot(amazon)
lines(fitted(fit1), col = 2)
#FORECASTING
amazon %>%
Arima(order=c(1,1,2), seasonal=c(1,1,0), lambda=0) %>% forecast() %>%
autoplot() +
ylab("Amazon Trends") + xlab("Year")
###Holt Winters
HW1 <- HoltWinters(amazon, seasonal = "multiplicative")
#HW2 <- HoltWinters(amazon, seasonal = "additive")
#HW2 <- HoltWinters(amazon, alpha=0.2, beta=0.1, gamma=0.1)
plot(amazon, ylab="Amazon Trend")
lines(HW1$fitted[,1], col="red")
#lines(HW2$fitted[,1], col="red")
lines(fitted(fit1), col="blue") #Confronto con modello ARIMA
cat("RMSE of best ARIMA model is", rmse(amazon, fitted(fit1)))
cat("RMSE of HW model is", rmse(amazon, HW1$fitted[,1]))