0

Given that I am forcing, via the for loops, for Hessian to be an 8x8, why do i get error that it is a 2x1?

Running an 8x8 Hessian to generate maxLik on nx8 regressor dataset. Getting an error that Hessian is 2x1 instead of 8x8, even though this is forced in Hessian[i,j]<-.

x <- independent.variable.matrix.nby8
param <- c(1,1,1,1,1,1,1,1)

Hessian <- matrix(ncol=8,nrow=8)
hess.lik <- function(param) {
  loglambd <- x%*%param       
  lambd <- exp(loglambd)
  for (i in 1:8){
    for (j in 1:8){
      moment <- x[,i]*x[,j]
      return(dim(moment))
      Hessian[i,j] <- -sum(moment%*%lambd)
    } 
  }
}
  • The dataset, x, is nxk (4406x8).
  • The param (initial iteration of estimators, start= argument under maxLik) vector is 8 numeric string, so loglambda is (nxk)%*%(8x1) a nx1 vector. Right?
  • The moment is then nx1 * nx1 i.e. nx1 as element multiplication. Right?
  • and each element of the Hessian should be scalar as it's a summed kx1 vector (result from moment %*% lambda). This part of the code is non conformable.

The main qn is, even if parts are not the right dimension (likely), the Hessian[i,j <- -sum(vector) should form a scalar, which through {i,j} in 1:8 loops should force an 8x8. Why does that not work?

Amir
  • 10,600
  • 9
  • 48
  • 75
rrg
  • 655
  • 2
  • 6
  • 24
  • 1
    Can you make the problem reproducible. The code needs to define `x` and `param`. – Matt Brigida Sep 28 '15 at 21:32
  • If the error you're getting is complaining about you hessian being `2 x 1`, it may be because you're doing `return(dim(moment))`- the return value of your `hess.lik` function will just be the dimensions of `moment`, when you probably want to be returning `Hessian`, after running through the two nested for loops. – Marius Sep 29 '15 at 05:31
  • You're right Marius, this should be print(dim(moment)) – rrg Oct 03 '15 at 01:30

0 Answers0