R-Listers:
I am doing a quasi-maximum likelihood estimation and I get a "subscript
out of bound" error message, Typically I would think this means that a
subscript used in the function is literally out of bounds however I don't
think this is the case. All I change in the code is a constant, that is
hard-wired in (not data dependent and not parameter dependent),
furthermore, the constant is not used in any subscripting.
Sorry I cannot provide a toy example, but the likelihood function looks
like this:
lnL <- function(theta, gsvr, gsvR) {
# theta[1]= mu, theta[2]=gamma, theta[3]=kappa1,
theta[4]=kappa2
nn <- 252
d <- 0:nn
wd <- exp((theta[3] * d + theta[4] * d^2))/(sum(exp(theta[3] *
d + theta[4] * d^2)))
sigsq <- numeric(length(gsvR$ret))
x <- numeric(length(gsvR$ret)
# Below this line can be specified differently
x[1] <- 1
lenR <- length(gsvR$ret)
for (i in 1:(lenR-1)) {
x[i+1] <- sum(gsvR$rw[1:i]) + 1
rsq <- (gsvr$ret[(x[i]):(nn+x[i])])^2
sigsq[i] <- 22 * sum(wd * rsq)
}
if((nn+x[lenR]) < length(gsvr$ret)) rsq <-
(gsvr$ret[(x[lenR]):(nn+x[lenR])])^2 else rsq <- NA
sigsq[length(gsvR$ret)] <- 22 * sum(wd * rsq)
sigsq <- na.omit(sigsq)
sigsq <- sigsq[-1]
# Above this line can be specified differently
Ret <- gsvR$ret[1:length(sigsq)]
mymu <- (theta[1]+theta[2]*(sigsq))
n <- length(wd)#/22
ll <- numeric(length(sigsq))
for (j in 1:length(sigsq)) {
ll[j] <- -(n/2) * log(2*pi) - (n/2) * log(sigsq[j]) - 0.5 *
sum(((Ret - mymu[j])^2)/(sigsq[j]))
}
l <- mean(ll)
-l
}
#########Alternative specitication################
for (i in 1:(length(gsvR$ret))) {
x[i] <- sum(gsvR$rw[1:i]) + 1
rsq <- (gsvr$ret[(x[i]):(nn+x[i])])^2
sigsq[i] <- 22 * sum(wd * rsq)
}
sigsq <- na.omit(sigsq)
###############################################
The constant that is changed is "n" when the n <- length(wd)/22 the
optimization converges using both nlm and optim(with the default method),
however with n <- length(wd) the functions returns the subscript out of
bounds error message. Maximizing the likelihood with the alternative
specification replacing the code marked in the original function allows
the optimization to converge. It should be noted that the original
likelihood function and the alternative specification return the same
likelihood value when evaluated at the initial values.
I realize that in the original specification the variable sigsq could be
all NAs and na.omit(sigsq) would produce a zero length vector, upon which
sigsq[-1] would be out of bounds, however the original specification
converges when n<-length(wd)/22, in which case the aforementioned case
would still be true. Plus both specifications evaluate the initial values
when submitted line by line (rather than in the function), using both
n<-length(wd)/22 and n<-length(wd).
Could the error "subscript out of bounds" mean something different? Is
there likely to be a bug?
I am using:
> version
_
platform i386-pc-mingw32
arch i386
os mingw32
system i386, mingw32
status
major 1
minor 8.0
year 2003
month 10
day 08
language R
Thanks for reading, and/or replying to this.
Jason Higbee
Research Associate
Federal Reserve Bank of St. Louis
T: 314.444.7316
F:314.444.8731
[[alternative HTML version deleted]]
______________________________________________
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html