Newer
Older
#' @title update_rq
#'
#' @description This function plots the quantiles
#' @param Lfor List of the data needed to plot
#' @export
#' @examples
#' plotprob()
update_rq <- function(Xny, tau, k, model = NULL, debug = TRUE){
if(is.null(model)) stop("Model needs to be initialized before update")
N <- nrow(Xny)
res <- model$info[[paste0("q", tau)]][[paste0("k", k)]]
## Maybe this is no correct - we should put weights on this!
Xold <- res$X
# Prepare for keeping for the parameter estimates
BETA <- matrix(as.numeric(NA), nrow = N, ncol = model$K)
i <- 1
counter <- 1
Ypred <- matrix(as.numeric(NA), nrow = N, ncol = 1)
if(debug) {
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
list_idx <- matrix(as.numeric(NA), nrow = 1, ncol = length(res$Ih)+1)
list_idx[1,] <- c(0,res$Ih)
}
while(counter <= N){
#cat("counter", counter, "\n")
beta <- res$xB[1:model$K]
BETA[counter,] <- t(beta)
j <- 0
res <- rq_update_cpp(newX = matrix(Xny[counter,], nrow = 1),
X = Xold,
IX = model$IX,
Iy = model$Iy,
Ih = res$Ih,
Ihc = res$Ihc,
beta = beta,
Rny = res$R,
K = model$K,
n = nrow(Xold),
xB = res$xB,
P = res$P,
tau = tau,
k = 0,
kk = 0,
i = i,
n_in_bin = model$n_in_bin,
W = model$regprm$lambda)
Ypred[counter] <- res$Ypred
i = res$i
Xold <- res$X
res$IH <- res$Ih
#cat("Here", "\n")
#if(counter == 119) browser()
if(debug) {
list_idx <- rbind(list_idx, c(counter, res$Ih))
}
# if(FALSE){
# invXh <- solve(Xold[res$Ih, 1:model$K])
# cB <- as.numeric(res$P < 0) + res$P * tau
# cC <- c(rep(tau, model$K), rep(1 - tau, model$K))
# #IB2 <- -(t(res$P %*% t(rep(1, model$K))) %*% Xold[res$Ihc+1, 1:model$K]) %*% invXh
# g <- t(IB2) %*% cB
# d <- cC - c(g, -g)
# d[abs(d) < 1e-15] <- 0
# s <- order(d)
# md <- sort(d)
# }
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
resAlg <- rq_simplex_cpp(X = Xold[,1:model$K],
Ih = res$Ih,
Ihc = res$Ihc,
IH = as.matrix(res$Ih),
K = model$K,
n = nrow(Xold),
xB = res$xB,
P = res$P,
tau = tau)
res$CON <- resAlg$CON
res$s <- resAlg$s
res$g <- resAlg$g
res$gain <- resAlg$gain
res$md <- resAlg$md
res$alpha <- resAlg$alpha
res$h <- resAlg$h
res$IH <- resAlg$IH
res$cq <- resAlg$cq
res$q <- resAlg$q
if(length(res$md) == 0) res$md <- 1
while(res$gain <= 0 & res$md < 0 & j < 24 & res$CON < 10^6){
j <- j + 1
z = res$xB - as.numeric(res$alpha) * res$h
IhM <- res$Ih[res$s + 1]
IhcM <- res$Ihc[res$q + 1]
res$Ih[res$s + 1] <- IhcM
res$Ihc[res$q+1] <- IhM
res$P[res$q+1] <- res$cq
res$xB <- z
res$xB[res$q + 1 + model$K] <- res$alpha
dummy <- sortIdx(res$Ih)
res$Ih <- dummy$newVal; IndexIh <- dummy$idxVal
dummy <- sortIdx(res$Ihc)
res$Ihc <- dummy$newVal; IndexIhc <- dummy$idxVal
if(debug) {
list_idx <- rbind(list_idx, c(counter, res$Ih))
}
res$P <- res$P[IndexIhc]
xBm <- res$xB[((model$K+1):length(res$xB))]
xBm <- xBm[IndexIhc]
res$xB[((model$K+1):length(res$xB))] <- xBm
#cat("Here2", "\n")
resAlg <- rq_simplex_cpp(X = Xold[,1:model$K],
Ih = res$Ih,
Ihc = res$Ihc,
IH = as.matrix(res$Ih),
K = model$K,
n = nrow(Xold),
xB = res$xB,
P = res$P,
tau = tau)
res$CON <- resAlg$CON
res$s <- resAlg$s
res$g <- resAlg$g
res$gain <- resAlg$gain
res$md <- resAlg$md
res$alpha <- resAlg$alpha
res$h <- resAlg$h
res$IH <- resAlg$IH
res$cq <- resAlg$cq
res$q <- resAlg$q
if(length(res$md) == 0) res$md <- 1
}
counter = counter + 1
}
model$info[[paste0("q", tau)]][[paste0("k", k)]] <- res
model$beta[[paste0("q", tau)]][[paste0("k", k)]] <- rbind(model$beta[[paste0("q", tau)]][[paste0("k", k)]], BETA)
if(debug) model$listIH[[paste0("q", tau)]][[paste0("k", k)]] <- list_idx
return(Ypred)
}