-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroupProject_Functions.R
136 lines (123 loc) · 3.55 KB
/
GroupProject_Functions.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
# recursive forecast for holdout dataset
recursive_forecast<-function(tsobject,tsobject_2,order_seq){
forecast<-c()
for(i in 1:length(tsobject_2)){
model<-arima(tsobject,order=order_seq,include.mean=F,optim.control=list(maxit = 1000))
forecast_point<-predict(model,1)
forecast<-c(forecast,forecast_point$pred[1])
tsobject<-c(tsobject,tsobject_2[i])
}
forecast
}
# rolling window forecast for holdout dataset
rolling_window<-function(tsobject,tsobject_2,order_seq){
forecast<-c()
for(i in 1:length(tsobject_2)){
model<-arima(tsobject,order=order_seq,include.mean=F,optim.control=list(maxit = 10000))
forecast_point<-predict(model,1)
forecast<-c(forecast,forecast_point$pred[1])
tsobject<-c(tsobject,tsobject_2[i])
tsobject<-tsobject[2:length(tsobject)]
}
forecast
}
# functions for computing the goodness-of-fit measure on the holdout dataset
computeMSE <- function(y, pred){
MSE = c()
for (i in 1:length(y)){
s <- ((y[i]-pred[i])^2)
MSE <- c(MSE,s)
}
return(sum(MSE)/length(y))
}
computeMAD <- function(y, pred){
MAD = c()
for (i in 1:length(y)){
s <- abs(y[i]-pred[i])
MAD <- c(MAD,s)
}
return(sum(MAD)/length(y))
}
calculateMAPE<-function(holdout, preds){
len<-length(holdout)
total<-0
for(i in 1:len){
total<-total+abs((holdout[i]-preds[i])/holdout[i])
#print(total)
}
total/len
}
calculateAMAPE<-function(holdout, preds){
len<-length(holdout)
total<-0
for(i in 1:len){
total<-total+abs((holdout[i]-preds[i])/(holdout[i]+preds[i]))
#print(total)
}
total/len
}
# ArchLM test
archlmtest <- function (x, lags, demean = FALSE)
{
x <- as.vector(x)
if(demean) x <- scale(x, center = TRUE, scale = FALSE)
lags <- lags + 1
mat <- embed(x^2, lags)
arch.lm <- summary(lm(mat[, 1] ~ mat[, -1]))
STATISTIC <- arch.lm$r.squared * length(resid(arch.lm))
names(STATISTIC) <- "Chisq"
PARAMETER <- lags - 1
names(PARAMETER) <- "df"
PVAL <- 1 - pchisq(STATISTIC, df = PARAMETER)
names(PVAL) <- "PVAL"
METHOD <- "ARCH LM-test"
result <- arch.lm
df <- data.frame("Chisq" = STATISTIC, "Pval" = PVAL)
return(df)
}
archlmbatch <- function (x, maxlags, demean = FALSE)
{
cat('Lag','\t','ChiSq','\t','PVal','\n')
for (i in 1:maxlags)
{
temp <- archlmtest(x, i, demean)
cat(i,'\t',temp$Chisq,'\t',temp$Pval,'\n')
}
}
# recursive forecast for final ARMA-GARCH model
arima_arch_garch_forecast_recursive<-function(Tsobject,formula,n_ahead){
Forecast<-c()
Upper_Bound<-c()
Lower_Bound<-c()
for(i in 1:n_ahead){
model<-garchFit(formula,data=Tsobject)
prediction<-predict(model,1)
meanForecast<-prediction[1,1]
upper<-1.96*prediction[1,3]+meanForecast
lower<-meanForecast-1.96*prediction[1,3]
Upper_Bound<-c(Upper_Bound,upper)
Lower_Bound<-c(Lower_Bound,lower)
Forecast<-c(Forecast,meanForecast)
Tsobject<-c(Tsobject,meanForecast)
}
list(Forecast,Upper_Bound,Lower_Bound)
}
# rolling window forecast for final ARMA-GARCH model
arima_arch_garch_forecast_rolling_window<-function(Tsobject,formula,n_ahead){
Forecast<-c()
Upper_Bound<-c()
Lower_Bound<-c()
for(i in 1:n_ahead){
model<-garchFit(formula,data=Tsobject)
prediction<-predict(model,1)
meanForecast<-prediction[1,1]
upper<-1.96*prediction[1,3]+meanForecast
lower<-meanForecast-1.96*prediction[1,3]
Upper_Bound<-c(Upper_Bound,upper)
Lower_Bound<-c(Lower_Bound,lower)
Forecast<-c(Forecast,meanForecast)
Tsobject<-c(Tsobject,meanForecast)
Tsobject<-Tsobject[2:length(Tsobject)]
}
list(Forecast,Upper_Bound,Lower_Bound)
}