Newer
Older
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
### Clean up the workspace
rm(list = ls())
## additional libraries
library(devtools)
library(splines)
library(quantreg)
## Load the onlineforecast package - this is the development version
load_all(".") # need to be under the package directory
# Load the data
D <- Dsolar
names(D)
# Short Data Anlysis
tstart <- as.POSIXct("2009-01-01", tz = "GMT")
tend <- as.POSIXct("2011-01-01", tz = "GMT")
D <- subset(D, in_range(tstart,D$t,tend))
plotmat <- matrix(1:3, nrow = 3, byrow=TRUE)
layout(plotmat, widths=c(1,1,1), heights=c(1,1,1))
par(mar=c(0, 3.5, 0.5, 1), lwd = 2, cex.lab = 1.5, cex.axis = 1.5)
plot(D$t, D$Ps, type = "l", bty = "l", xlab = "", ylab = "", axes = F)
axis(2, seq(0,ceiling(max(D$Ps)+0.2), by = 0.5), las = 1)
plot(D$t, lagvec(D$I$k1,1), type = "l", col = "gold", bty = "l", xlab = "", ylab = "", axes = F)
lines(D$t, D$Iobs, col = rgb(0, 0, 255, max = 255, alpha = 125))
axis(2, seq(0,ceiling(max(D$I$k1)+0.2), by = 100), las = 1)
plot(D$t, lagvec(D$Ta$k1,1), type = "l", col = rgb(240, 37, 37, max = 255, alpha = 150), bty = "l", xlab = "", ylab = "", axes = F, ylim = c(min(D$Ta$k1) - 4,max(D$Ta$k1)))
axis(2, seq(floor(min(D$Ta$k1)),ceiling(max(D$Ta$k1)+0.2), by = 10), las = 1)
axis.POSIXct(side = 1, x = D$t, xaxt = "s",
at = seq(D$t[1], D$t[length(D$t)], by = "1 month"),
format = "%Y-%m-%d", mgp = c(4,-1,-2))
# Make a training set of 3 months, and a then test set with 1 month
tstart <- "2009-06-01"
ttest <- "2009-03-31"
tend <- "2009-07-01"
# Cut only the necessary period
D$scoreperiod <- in_range("2009-06-10", D$t)
Dtrain <- subset(D, in_range(tstart,D$t,tend))
model <- qmodel$new(N1 = 50)
model$output = "Ps"
model$add_inputs(I = "bspline(tday, df=5, Boundary.knots=c(5,18)) %**% I")
model$add_regprm("rls_prm(lambda=0.9)")
model$add_prmbounds(lambda = c(0.99, 0.999, 0.9999))
## First 5 columns are the input Matrix
model$IX <- 0:4
## The output column is the 6th one
model$Iy <- 5
model$K <- 5
model$datatr <- model$transform_data(Dtrain)
model$tau <- c(0.05,0.2, 0.5, 0.8,0.95)
model$kseq <- 1:24
#opt_model <- quantile_optim(model = model, data = Dnew)
plot(Dtrain$I$k1[1:100], type = "l")
lines(model$datatr$I.bs1$k1, col = "red")
lines(model$datatr$I.bs2$k1, col = "red")
lines(model$datatr$I.bs3$k1, col = "red")
lines(model$datatr$I.bs4$k1, col = "red")
lines(model$datatr$I.bs5$k1, col = "red")
PAR <- c("lambda" = 0.999)
quantile_fit(prm = PAR, model = model, data = Dtrain, q = model$tau[1])
PAR <- c("lambda" = 0.999)
quantile_fit(prm = PAR, model = model, data = Dtrain, q = model$tau[2])
PAR <- c("lambda" = 0.999)
quantile_fit(prm = PAR, model = model, data = Dtrain, q = model$tau[3])
PAR <- c("lambda" = 0.999)
quantile_fit(prm = PAR, model = model, data = Dtrain, q = model$tau[4])
PAR <- c("lambda" = 0.999)
quantile_fit(prm = PAR, model = model, data = Dtrain, q = model$tau[5])
Pred_model <- quantile_predict(model = model, datatr = model$datatr)
str(Pred_model)
k <- 24
colSeq <- grey(seq(0.9,0.1,len=3))
#colSeq <- colorJet(nPolygons)
require( RColorBrewer )
colSeq <- brewer.pal(11 , "Spectral" )
for(i in k:(length(Dtrain$t)-1)){
par(mfrow = c(2,1))
par(mar = c(2,4,2,2)) # bottom left top right
with(Pred_model, {
plot(Dtrain$t[(i-10):(i+k)], Dtrain$Ps[(i-10):(i+k)], bty = "l", lwd = 2, col = "black", pch=19, cex=0.5, axes = FALSE, xaxt = "n",
type="n", ylim = range(Dtrain$Ps[(i-10):(i+k)], q0.5[i,], q0.95[i,], q0.05[i,], na.rm = T), main = "TimeAdaptive with Weights", xlab = "Time", ylab = "Ps")
axis(2)
polygon(c(Dtrain$t[(i+1):(i+k)], rev(Dtrain$t[(i+1):(i+k)])),
c(Pred_model$q0.05[i,],rev(Pred_model$q0.2[i,])), col= colSeq[3], border = NA)
polygon(c(Dtrain$t[(i+1):(i+k)], rev(Dtrain$t[(i+1):(i+k)])),
c(Pred_model$q0.8[i,],rev(Pred_model$q0.95[i,])), col= colSeq[3], border = NA)
polygon(c(Dtrain$t[(i+1):(i+k)], rev(Dtrain$t[(i+1):(i+k)])),
c(Pred_model$q0.2[i,],rev(Pred_model$q0.5[i,])), col= colSeq[4], border = NA)
polygon(c(Dtrain$t[(i+1):(i+k)], rev(Dtrain$t[(i+1):(i+k)])),
c(Pred_model$q0.5[i,],rev(Pred_model$q0.8[i,])), col= colSeq[4], border = NA)
lines(Dtrain$t[(i-10):(i+k)], Dtrain$Ps[(i-10):(i+k)], col = "black", type = "b", lwd = 2)
lines(Dtrain$t[(i+1):(i+k)], q0.5[i,], type = "b", col = "grey", lwd = 2)
axis.POSIXct(side = 1, x = Dtrain$t[(i-10):(i+k)],
at = seq(from = Dtrain$t[(i-10):(i+k)][1],
to = Dtrain$t[(i-10):(i+k)][length(Dtrain$t[(i-10):(i+k)])],
by = "1 hour"), format = "%Y/%m/%d %H:%M \n %a",
las = 1, cex.axis = 1, srt = 45)
#lines(Pred_model$q0.05[i,], type = "b", col = "blue")
#lines(Pred_model$q0.95[i,], type = "b", col = "blue")
plot(Dtrain$t[(i-10):(i+k)], c(rep(NA,11), as.numeric(Dtrain$I[i, 1:k])), type = "b", col = "steelblue", axes = FALSE, xlab = "Time", ylab = "Temp", lwd = 2, ylim = range(Dtrain$I[i, 1:k], Dtrain$Iobs[(i+1):(i+k)]))
#lines(Dtrain$t[i], Dtrain$Iobs[i+1,1], type = "b", col = "red", axes = FALSE)
lines(Dtrain$t[(i+1):(i+k)], Dtrain$Iobs[(i+1):(i+k)], type = "b", col = "red", lwd = 2)
axis(2)
abline(h=0,v=Dtrain$t[i],lty=2, col = "lightgrey", lwd = 2)
legend("topleft", legend=c("I Obs", "I Pred"),
col=c("red", "steelblue"), lty=1, cex=1, lwd = 2)
i
axis.POSIXct(side = 1, x = Dtrain$t[(i-10):(i+k)],
at = seq(from = Dtrain$t[(i-10):(i+k)][1],
to = Dtrain$t[(i-10):(i+k)][length(Dtrain$t[(i-10):(i+k)])],
by = "1 hour"), format = "%Y/%m/%d %H:%M \n %a",
las = 1, cex.axis = 1, srt = 45)
})
}