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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
#############################################################################
## Copyright (c) 2010-2022 Rune Haubo Bojesen Christensen
##
## This file is part of the ordinal package for R (*ordinal*)
##
## *ordinal* is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 2 of the License, or
## (at your option) any later version.
##
## *ordinal* is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## A copy of the GNU General Public License is available at
## <https://www.r-project.org/Licenses/> and/or
## <http://www.gnu.org/licenses/>.
#############################################################################
## This file contains:
## Utility functions for fitting CLMMs with clmm2().
### OPTION: Could make use of getFittedC throughout this file...
.negLogLikMfastR <- function(rho) { ## negative log-likelihood
### .negLogLikMfast in R
with(rho, {
o21 <- u[grFac] * stDev
o11 <- o1 - o21
o21 <- o2 - o21
eta1 <- (eta1Fix + o11)/sigma
eta2 <- (eta2Fix + o21)/sigma
pr <-
if(nlambda) pfun(eta1, lambda) - pfun(eta2, lambda)
else pfun(eta1) - pfun(eta2)
if(any(is.na(pr)) || any(pr <= 0))
nll <- Inf
else
nll <- -sum(weights * log(pr)) -
sum(dnorm(x=u, mean=0, sd=1, log=TRUE))
nll
})
}
.negLogLikM <- function(rho) { ## negative log-likelihood
with(rho, {
if(estimStDev)
stDev <- exp(par[p+nxi+k+estimLambda+ 1:s])
o21 <- u[grFac] * stDev
o11 <- o1 - o21
o21 <- o2 - o21
if(estimLambda > 0)
lambda <- par[nxi + p + k + 1:estimLambda]
sigma <-
if(k > 0) expSoffset * exp(drop(Z %*% par[nxi+p + 1:k]))
else expSoffset
eta1Fix <- drop(B1 %*% par[1:(nxi + p)])
eta2Fix <- drop(B2 %*% par[1:(nxi + p)])
eta1 <- (eta1Fix + o11)/sigma
eta2 <- (eta2Fix + o21)/sigma
pr <-
if(nlambda) pfun(eta1, lambda) - pfun(eta2, lambda)
else pfun(eta1) - pfun(eta2)
if(any(is.na(pr)) || any(pr <= 0))
nll <- Inf
else
nll <- -sum(weights * log(pr)) -
sum(dnorm(x=u, mean=0, sd=1, log=TRUE))
nll
})
}
.gradM <- function(rho) { ## gradient of the negative log-likelihood
with(rho, {
if(nlambda) {
p1 <- dfun(eta1, lambda)
p2 <- dfun(eta2, lambda)
}
else {
p1 <- dfun(eta1)
p2 <- dfun(eta2)
}
wtprSig <- weights/pr/sigma
.C("gradC",
as.double(stDev),
as.double(p1),
as.double(p2),
as.double(wtprSig),
as.integer(grFac),
length(wtprSig),
u = as.double(u),
length(u))$u
## tapply(stDev * wtprSig * (p1 - p2), grFac, sum) + u
})
}
.gradC <- function(rho) {
tmp <- with(rho, {
.C("grad_C",
as.double(stDev),
p1 = double(length(pr)),
p2 = double(length(pr)),
as.double(pr),
as.double(weights),
as.double(sigma),
wtprSig = double(length(pr)),
as.double(eta1),
as.double(eta2),
gradValues = double(length(u)),
as.double(u),
as.integer(grFac),
length(pr),
length(u),
as.double(lambda),
as.integer(linkInt))[c("p1", "p2", "wtprSig", "gradValues")]
})
rho$wtprSig <- tmp$wtprSig
rho$p1 <- tmp$p1
rho$p2 <- tmp$p2
tmp$gradValues
}
.hessC <- function(rho) {
with(rho, {
.C("hess",
as.double(stDev),
as.double(p1),
as.double(p2),
as.double(pr),
as.double(wtprSig),
as.double(eta1),
as.double(eta2),
as.integer(linkInt),
as.integer(grFac),
length(pr),
hessValues = double(length(u)),
as.double(lambda),
length(u))$hessValues
})
}
.hessianM <- function(rho) ## hessian of the negative log-likelihood
with(rho,{
if(nlambda) {
g1 <- gfun(eta1, lambda)
g2 <- gfun(eta2, lambda)
}
else {
g1 <- gfun(eta1)
g2 <- gfun(eta2)
}
.C("hessC",
as.double(stDev),
as.double(p1),
as.double(p2),
as.double(pr),
as.double(g1),
as.double(g2),
as.double(wtprSig),
as.integer(grFac),
length(pr),
z = double(length(u)),
length(u))$z
## tapply(((p1 - p2)^2 / pr - g1 + g2) * wtprSig, grFac, sum) *
## stDev^2 + 1
})
update.u2.v2 <- function(rho) {
### second version: C-implementation of NR-algorithm.
.negLogLikBase(rho) ## update: par, stDev, eta1Fix, eta2Fix eta2Fix, sigma
fit <- with(rho,
.C("NRalg",
as.integer(ctrl$trace),
as.integer(ctrl$maxIter),
as.double(ctrl$gradTol),
as.integer(ctrl$maxLineIter),
as.integer(grFac),
as.double(stDev),
as.double(o1),
as.double(o2),
as.double(eta1Fix),
as.double(eta2Fix),
as.double(eta1),
as.double(eta2),
as.double(sigma),
as.integer(linkInt),
as.double(weights),
u = as.double(uStart),
pr = as.double(pr),
funValue = as.double(nll),
gradValues = as.double(uStart),
hessValues = as.double(uStart),
length(pr),
length(uStart),
maxGrad = double(1),
conv = 0L,
double(length(pr)), # p1
double(length(pr)), # p2
double(length(pr)), # wtprSig
as.double(lambda),
Niter = as.integer(Niter)
)[c("u", "pr", "funValue", "gradValues",
"hessValues", "maxGrad", "conv", "Niter")] )
## Get message:
message <- switch(as.character(fit$conv),
"1" = "max|gradient| < tol, so current iterate is probably solution",
"0" = "Non finite negative log-likelihood",
"-1" = "iteration limit reached when updating the random effects",
"-2" = "step factor reduced below minimum when updating the random effects")
## check for convergence and report warning/error:
if(rho$ctrl$trace > 0 && fit$conv == 1)
cat("\nOptimizer converged! ", "max|grad|:",
fit$maxGrad, message, fill = TRUE)
if(fit$conv != 1 && rho$ctrl$innerCtrl == "warnOnly")
warning(message, "\n at iteration ", rho$Niter)
else if(fit$conv != 1 && rho$ctrl$innerCtrl == "giveError")
stop(message, "\n at iteration ", rho$Niter)
## Store values and return:
rho$Niter <- fit$Niter
rho$u <- fit$u
rho$D <- fit$hessValue
rho$gradient <- fit$gradValue
if(!is.finite(rho$negLogLik <- fit$funValue))
return(FALSE)
return(TRUE)
}
update.u2 <- function(rho)
{
stepFactor <- 1
innerIter <- 0
rho$u <- rho$uStart
rho$negLogLik <- .negLogLikM(rho)
if(!is.finite(rho$negLogLik)) return(FALSE)
rho$gradient <- .gradC(rho)
maxGrad <- max(abs(rho$gradient))
conv <- -1 ## Convergence flag
message <- "iteration limit reached when updating the random effects"
if(rho$ctrl$trace > 0)
Trace(iter=0, stepFactor, rho$negLogLik, maxGrad, rho$u, first=TRUE)
## Newton-Raphson algorithm:
for(i in 1:rho$ctrl$maxIter) {
if(maxGrad < rho$ctrl$gradTol) {
message <- "max|gradient| < tol, so current iterate is probably solution"
if(rho$ctrl$trace > 0)
cat("\nOptimizer converged! ", "max|grad|:",
maxGrad, message, fill = TRUE)
conv <- 0
break
}
rho$D <- .hessC(rho)
## rho$D <- .hessianM(rho)
step <- rho$gradient / rho$D
rho$u <- rho$u - stepFactor * step
negLogLikTry <- .negLogLikMfast(rho)
lineIter <- 0
## simple line search, i.e. step halfing:
while(negLogLikTry > rho$negLogLik) {
stepFactor <- stepFactor/2
rho$u <- rho$u + stepFactor * step
negLogLikTry <- .negLogLikMfast(rho)
lineIter <- lineIter + 1
if(rho$ctrl$trace > 0)
Trace(i+innerIter, stepFactor, rho$negLogLik, maxGrad,
rho$u, first=FALSE)
if(lineIter > rho$ctrl$maxLineIter){
message <- "step factor reduced below minimum when updating the random effects"
conv <- 1
break
}
innerIter <- innerIter + 1
}
rho$negLogLik <- negLogLikTry
rho$gradient <- .gradC(rho)
maxGrad <- max(abs(rho$gradient))
if(rho$ctrl$trace > 0)
Trace(i+innerIter, stepFactor, rho$negLogLik, maxGrad, rho$u, first=FALSE)
stepFactor <- min(1, 2 * stepFactor)
}
if(conv != 0 && rho$ctrl$innerCtrl == "warnOnly") {
warning(message, "\n at iteration ", rho$Niter)
utils::flush.console()
}
else if(conv != 0 && rho$ctrl$innerCtrl == "giveError")
stop(message, "\n at iteration ", rho$Niter)
rho$Niter <- rho$Niter + i
rho$D <- .hessC(rho)
if(!is.finite(rho$negLogLik))
return(FALSE)
else
return(TRUE)
}
.hessMinC <- function(rho) {
with(rho,{
if(nlambda) {
g1 <- gfun(eta1, lambda)
g2 <- gfun(eta2, lambda)
}
else {
g1 <- gfun(eta1)
g2 <- gfun(eta2)
}
.C("hessC",
as.double(stDev),
as.double(p1),
as.double(p2),
as.double(pr),
as.double(g1),
as.double(g2),
as.double(wtprSig),
as.integer(grFac),
length(pr),
z = double(length(u)),
length(u))$z
})
}
.gradMinC <- function(stDev, p1, p2, wtprSig, grFac, u)
.C("gradC",
as.double(stDev),
as.double(p1),
as.double(p2),
as.double(wtprSig),
as.integer(unclass(grFac)),
as.integer(length(wtprSig)),
u = as.double(u),
as.integer(length(u)))$u
.gradMinC <- function(rho) {
with(rho, {
if(nlambda) {
p1 <- dfun(eta1, lambda)
p2 <- dfun(eta2, lambda)
}
else {
p1 <- dfun(eta1)
p2 <- dfun(eta2)
}
wtprSig <- weights/pr/sigma
.C("gradC",
as.double(stDev),
as.double(p1),
as.double(p2),
as.double(wtprSig),
as.integer(grFac),
length(wtprSig),
u = as.double(u),
length(u))$u
})
}
grFacSumC <- function(x, grFac, u)
.C("grFacSum_C",
as.double(x),
as.integer(grFac),
as.integer(length(x)),
u = as.double(u),
as.integer(length(u)))$u
grFacSum <- function(x, grFac, n.x, u, n.u) {
## i, j, z
z <- 0
for (i in 1:n.u) {
for (j in 1:n.x)
if(grFac[j] == i)
z <- z + x[j]
u[i] <- z + u[i]
z <- 0
}
u
}
getNAGQ2 <- function(rho, par) {
### Not in use
if(!missing(par))
rho$par <- par
if(!update.u2(rho)) return(Inf)
if(any(rho$D < 0)) return(Inf)
with(rho, {
K <- sqrt(2/D)
agqws <- K %*% t(ghqws)
agqns <- apply(K %*% t(ghqns), 2, function(x) x + u)
ranNew <- apply(agqns, 2, function(x) x[grFac] * stDev)
eta1Tmp <- (eta1Fix + o1 - ranNew) / sigma
eta2Tmp <- (eta2Fix + o2 - ranNew) / sigma
if(nlambda)
## PRnn <- (pfun(eta1Tmp, lambda) - pfun(eta2Tmp, lambda))^weights
## This is likely a computationally more safe solution:
PRnn <- exp(weights * log(pfun(eta1Tmp, lambda) - pfun(eta2Tmp, lambda)))
else
## PRnn <- (pfun(eta1Tmp) - pfun(eta2Tmp))^weights
PRnn <- exp(weights * log(pfun(eta1Tmp) - pfun(eta2Tmp)))
for(i in 1:r)
## PRrn[i,] <- apply(PRnn[grFac == i, ], 2, prod)
PRrn[i,] <- apply(PRnn[grFac == i, ,drop = FALSE], 2, prod)
PRrn <- PRrn * agqws * dnorm(x=agqns, mean=0, sd=1)
### OPTION: Could this be optimized by essentially computing dnorm 'by hand'?
})
-sum(log(rowSums(rho$PRrn)))
}
getNGHQ <- function(rho, par) {
### Not in use
if(!missing(par))
rho$par <- par
.negLogLikM(rho) ## Update lambda, stDev, sigma and eta*Fix
with(rho, {
eta1Tmp <- (eta1Fix + o1 - ranNew * stDev) / sigma
eta2Tmp <- (eta2Fix + o2 - ranNew * stDev) / sigma
if(nlambda)
PRnn <- exp(weights * log(pfun(eta1Tmp, lambda) - pfun(eta2Tmp, lambda)))
else
PRnn <- exp(weights * log(pfun(eta1Tmp) - pfun(eta2Tmp)))
for(i in 1:r)
PRrn[i,] <- apply(PRnn[grFac == i, ,drop = FALSE], 2, prod)
PRrn <- PRrn * agqws * dnorm(x=agqns, mean=0, sd=1)
})
-sum(log(rowSums(rho$PRrn)))
}
|