https://github.com/cran/aster
Tip revision: c45193827d47cd090900035e43f3188cd3fbc795 authored by Charles J. Geyer on 05 May 2013, 00:00:00 UTC
version 0.8-23
version 0.8-23
Tip revision: c451938
mlogl-unco.Rout.save
R version 2.15.0 (2012-03-30)
Copyright (C) 2012 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-unknown-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
>
> library(aster)
Loading required package: trust
>
> options(digits=4) # avoid rounding differences
>
> set.seed(42)
>
> nind <- 25
> nnode <- 5
> ncoef <- nnode + 1
>
> famlist <- fam.default()
> fam <- c(1, 1, 2, 3, 3)
> pred <- c(0, 1, 1, 2, 3)
>
> modmat <- array(0, c(nind, nnode, ncoef))
> modmat[ , , 1] <- 1
> for (i in 2:nnode)
+ modmat[ , i, i] <- 1
> modmat[ , , ncoef] <- rnorm(nind * nnode)
>
> beta <- rnorm(ncoef) / 10
>
> phi <- matrix(modmat, ncol = ncoef) %*% beta
> phi <- matrix(phi, ncol = nnode)
>
> aster:::setfam(fam.default())
>
> theta <- .C("aster_phi2theta",
+ nind = as.integer(nind),
+ nnode = as.integer(nnode),
+ pred = as.integer(pred),
+ fam = as.integer(fam),
+ phi = as.double(phi),
+ theta = matrix(as.double(0), nind, nnode))$theta
>
> root <- sample(1:3, nind * nnode, replace = TRUE)
> root <- matrix(root, nind, nnode)
>
> x <- raster(theta, pred, fam, root)
>
> zip <- rep(0, nind * nnode)
>
> out <- mlogl(beta, pred, fam, x, root, modmat, deriv = 2,
+ type = "unco", origin = zip)
> print(out)
$value
[1] 124
$gradient
[1] 41.955 0.568 12.940 -1.461 28.377 27.929
$hessian
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 618.59 40.279 173.360 72.122 305.697 26.306
[2,] 40.28 11.703 3.353 17.381 5.231 -2.212
[3,] 173.36 3.353 61.949 4.980 96.896 10.515
[4,] 72.12 17.381 4.980 38.115 7.770 -1.324
[5,] 305.70 5.231 96.896 7.770 186.157 20.456
[6,] 26.31 -2.212 10.515 -1.324 20.456 303.042
>
> aster:::setfam(fam.default())
>
> a <- .C("aster_theta2phi",
+ nind = as.integer(nind),
+ nnode = as.integer(nnode),
+ pred = as.integer(pred),
+ fam = as.integer(fam),
+ theta = as.double(zip),
+ phi = matrix(as.double(0), nind, nnode),
+ PACKAGE = "aster")$phi
>
> M <- matrix(modmat, ncol = ncoef)
>
> alpha <- as.numeric(lm(as.numeric(a) ~ 0 + M)$coefficients)
>
> out.too <- mlogl(beta - alpha, pred, fam, x, root, modmat, deriv = 2,
+ type = "unco")
> all.equal(out, out.too)
[1] TRUE
>
> beta.old <- beta
> beta <- beta - alpha
>
> my.value <- 0
> for (j in 1:nnode) {
+ ifam <- fam[j]
+ k <- pred[j]
+ if (k > 0)
+ xpred <- x[ , k]
+ else
+ xpred <- root[ , j]
+ for (i in 1:nind)
+ my.value <- my.value -
+ sum(x[i, j] * theta[i, j] -
+ xpred[i] * famfun(famlist[[ifam]], 0, theta[i, j]))
+ }
> all.equal(out$value, my.value)
[1] TRUE
>
> my.grad <- NaN * out$gradient
> epsilon <- 1e-9
> for (i in 1:ncoef) {
+ beta.eps <- beta
+ beta.eps[i] <- beta[i] + epsilon
+ out.eps <- mlogl(beta.eps, pred, fam, x, root, modmat, deriv = 0,
+ type = "unco")
+ my.grad[i] <- (out.eps$value - out$value) / epsilon
+ }
>
> all.equal(out$gradient, my.grad, tolerance = sqrt(epsilon))
[1] TRUE
>
> ##########
>
> objfun <- function(beta) {
+ out <- mlogl(beta, pred, fam, x, root, modmat, deriv = 1,
+ type = "unco")
+ result <- out$value
+ attr(result, "gradient") <- out$gradient
+ return(result)
+ }
> nout <- nlm(objfun, beta, fscale = nind)
> print(nout)
$minimum
[1] 119.1
$estimate
[1] 1.62506 -1.45889 -0.96559 -1.61558 -2.06952 -0.08436
$gradient
[1] 8.452e-06 -2.515e-05 -3.569e-06 -2.927e-06 1.135e-05 -6.877e-07
$code
[1] 1
$iterations
[1] 34
> nout <- nlm(objfun, nout$estimate, fscale = nind)
> print(nout)
$minimum
[1] 119.1
$estimate
[1] 1.62506 -1.45889 -0.96559 -1.61558 -2.06952 -0.08436
$gradient
[1] 8.452e-06 -2.515e-05 -3.569e-06 -2.927e-06 1.135e-05 -6.877e-07
$code
[1] 1
$iterations
[1] 0
>
> beta.mle.new <- nout$estimate
> beta.mle.old <- beta.mle.new + alpha
> mout.new <- mlogl(beta.mle.new, pred, fam, x, root, modmat, deriv = 1,
+ type = "unco")
> mout.old <- mlogl(beta.mle.old, pred, fam, x, root, modmat, deriv = 1,
+ type = "unco", origin = zip)
> all.equal(mout.new, mout.old, tol = 1e-7)
[1] TRUE
>
> ##########
>
> my.hess <- matrix(NaN, ncoef, ncoef)
> for (i in 1:ncoef) {
+ beta.eps <- beta
+ beta.eps[i] <- beta[i] + epsilon
+ out.eps <- mlogl(beta.eps, pred, fam, x, root, modmat, deriv = 1,
+ type = "unco")
+ my.hess[ , i] <- (out.eps$gradient - out$gradient) / epsilon
+ }
>
> all.equal(out$hessian, my.hess, tolerance = sqrt(epsilon))
[1] TRUE
>
> ##########
>
> objfun <- function(beta) {
+ out <- mlogl(beta, pred, fam, x, root, modmat, deriv = 2,
+ type = "unco")
+ result <- out$value
+ attr(result, "gradient") <- out$gradient
+ attr(result, "hessian") <- out$hessian
+ return(result)
+ }
> nout <- try(nlm(objfun, beta, fscale = nind))
> print(nout)
$minimum
[1] 119.1
$estimate
[1] 1.61154 -1.43994 -0.97004 -1.60249 -2.04288 -0.08346
$gradient
[1] -0.027849 0.008147 -0.078372 0.004815 0.103718 0.050151
$code
[1] 4
$iterations
[1] 100
> nout <- nlm(objfun, nout$estimate, fscale = nind, iterlim = 1000)
> print(nout)
$minimum
[1] 119.1
$estimate
[1] 1.62500 -1.45880 -0.96551 -1.61551 -2.06945 -0.08436
$gradient
[1] -8.889e-05 1.876e-05 5.314e-05 2.267e-05 6.446e-05 4.977e-05
$code
[1] 2
$iterations
[1] 972
>
> objfun.old <- function(beta) {
+ out <- mlogl(beta, pred, fam, x, root, modmat, deriv = 2,
+ type = "unco", origin = zip)
+ result <- out$value
+ attr(result, "gradient") <- out$gradient
+ attr(result, "hessian") <- out$hessian
+ return(result)
+ }
> nout.old <- nlm(objfun.old, beta.mle.old, fscale = nind, iterlim = 1000)
> print(nout.old)
$minimum
[1] 119.1
$estimate
[1] -0.06808 -0.30707 0.18623 0.07757 -0.37637 -0.08436
$gradient
[1] 8.452e-06 -2.515e-05 -3.569e-06 -2.927e-06 1.135e-05 -6.877e-07
$code
[1] 1
$iterations
[1] 0
> all.equal(nout$minimum, nout.old$minimum)
[1] TRUE
> all.equal(nout$estimate, nout.old$estimate - alpha, tol = 1e-4)
[1] TRUE
>
>
> proc.time()
user system elapsed
1.740 0.031 1.923