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?