https://github.com/cran/aster
Raw File
Tip revision: caf80bbb933639f6a5334d03486d97e18fb725ec authored by Charles J. Geyer on 29 March 2013, 00:00:00 UTC
version 0.8-21
Tip revision: caf80bb
mlogl-cond.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)
>  print(pred)
[1] 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
> 
>  theta <- matrix(modmat, ncol = ncoef) %*% beta
>  theta <- matrix(theta, ncol = nnode)
> 
>  root <- sample(1:3, nind * nnode, replace = TRUE)
>  root <- matrix(root, nind, nnode)
> 
>  x <- raster(theta, pred, fam, root)
>  
>  out <- mlogl(beta, pred, fam, x, root, modmat, deriv = 2,
+      type = "conditional")
>  print(out)
$value
[1] 72.75

$gradient
[1] 13.2402  2.5559  5.7239  1.3889  0.5841 -3.5044

$hessian
       [,1]  [,2]  [,3]   [,4]    [,5]    [,6]
[1,] 42.563 4.989 15.72 3.7952  6.3260  2.0684
[2,]  4.989 4.989  0.00 0.0000  0.0000  1.3476
[3,] 15.724 0.000 15.72 0.0000  0.0000  1.6702
[4,]  3.795 0.000  0.00 3.7952  0.0000  0.1751
[5,]  6.326 0.000  0.00 0.0000  6.3260 -0.9735
[6,]  2.068 1.348  1.67 0.1751 -0.9735 43.7936

> 
>  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 = "conditional")
+      my.grad[i] <- (out.eps$value - out$value) / epsilon
+  }
> 
>  all.equal(out$gradient, my.grad, tolerance = sqrt(epsilon))
[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 = "conditional")
+      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 = 1,
+          type = "conditional")
+      result <- out$value
+      attr(result, "gradient") <- out$gradient
+      return(result)
+  }
>  out <- nlm(objfun, beta, fscale = nind)
>  print(out)
$minimum
[1] 69.86

$estimate
[1] -0.3018 -0.3692 -0.4161 -0.3519  0.1824  0.1868

$gradient
[1]  1.246e-05 -1.209e-05  3.638e-06  8.298e-06 -7.505e-06  5.915e-07

$code
[1] 1

$iterations
[1] 22

> 
>  ##########
> 
>  objfun <- function(beta) {
+      out <- mlogl(beta, pred, fam, x, root, modmat, deriv = 2,
+          type = "conditional")
+      result <- out$value
+      attr(result, "gradient") <- out$gradient
+      attr(result, "hessian") <- out$hessian
+      return(result)
+  }
>  out <- nlm(objfun, beta, fscale = nind)
>  print(out)
$minimum
[1] 69.86

$estimate
[1] -0.3018 -0.3692 -0.4161 -0.3519  0.1825  0.1868

$gradient
[1] -1.704e-05  5.650e-06  4.207e-05  1.844e-06  1.731e-05 -2.905e-05

$code
[1] 1

$iterations
[1] 18

> 
>  ########## expected Fisher information ##########
> 
>  aster:::setfam(fam.default())
> 
>  fout <- .C("aster_fish_cond",
+      nind = as.integer(nind),
+      nnode = as.integer(nnode),
+      ncoef = as.integer(ncoef),
+      pred = as.integer(pred),
+      fam = as.integer(fam),
+      beta = as.double(out$estimate),
+      root = as.double(root),
+      x = as.double(x),
+      modmat = as.double(modmat),
+      fish = matrix(as.double(0), ncoef, ncoef))
> 
>  mout <- mlogl(out$estimate, pred, fam, x, root, modmat,
+      deriv = 2, type = "conditional")
> 
>  aster:::setfam(fam.default())
> 
>  theta <- .C("aster_mat_vec_mult",
+     nrow = as.integer(nind * nnode),
+     ncol = as.integer(ncoef),
+     a = as.double(modmat),
+     b = as.double(out$estimate),
+     c = matrix(as.double(0), nind, nnode))$c
>  ctau <- .C("aster_theta2ctau",
+      nind = as.integer(nind),
+      nnode = as.integer(nnode),
+      pred = as.integer(pred),
+      fam = as.integer(fam),
+      theta = as.double(theta),
+      ctau = matrix(as.double(0), nind, nnode))$ctau
>  tau <- .C("aster_ctau2tau",
+      nind = as.integer(nind),
+      nnode = as.integer(nnode),
+      pred = as.integer(pred),
+      fam = as.integer(fam),
+      root = as.double(root),
+      ctau = as.double(ctau),
+      tau = matrix(as.double(0), nind, nnode))$tau
>  psiDoublePrime <- .C("aster_theta2whatsis",
+      nind = as.integer(nind),
+      nnode = as.integer(nnode),
+      pred = as.integer(pred),
+      fam = as.integer(fam),
+      deriv = as.integer(2),
+      theta = as.double(theta),
+      result = matrix(as.double(0), nind, nnode))$result
> 
>  my.hessian <- theta * NaN
>  my.fish <- theta * NaN
> 
>  for (i in 1:nind) {
+      for (j in 1:nnode) {
+          k <- pred[j]
+          if (k > 0) {
+              my.hessian[i, j] <- x[i, k] * psiDoublePrime[i, j]
+              my.fish[i, j] <- tau[i, k] * psiDoublePrime[i, j]
+          } else {
+              my.hessian[i, j] <- root[i, j] * psiDoublePrime[i, j]
+              my.fish[i, j] <- root[i, j] * psiDoublePrime[i, j]
+          }
+      }
+  }
> 
>  modmat <- matrix(as.double(modmat), ncol = ncoef)
>  my.hessian <- as.numeric(my.hessian)
>  my.fish <- as.numeric(my.fish)
>  my.hessian <- t(modmat) %*% diag(my.hessian) %*% modmat
>  my.fish <- t(modmat) %*% diag(my.fish) %*% modmat
> 
>  all.equal(my.hessian, mout$hessian)
[1] TRUE
>  all.equal(my.fish, fout$fish)
[1] TRUE
> 
> 
> proc.time()
   user  system elapsed 
  0.277   0.022   0.407 
back to top