[R] Error in f(x, ...) : subscript out of bounds
Uwe Ligges
ligges at statistik.uni-dortmund.de
Wed Feb 4 08:57:22 CET 2004
Jason.L.Higbee at stls.frb.org wrote:
> 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)
Obviously, this is not the original code you have got a problem with,
since a delimeter is missing the line above. Please don't send code you
have not tested yourself ....
>
> # 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'm pretty sure it's a big in your code.
I haven't checked the rest of your code, since it doesn't seem to make
sense to look into code that is buggy by copy&paste or whatever (see above).
There are *many* lines where your code might result in invalid
subscripts, depending on the data.
Say length(gsvR$ret) == 1, then:
lenR <- length(gsvR$ret) # lenR == 1
for (i in 1:(lenR-1)) { # 1:(lenR-1) == 1:0 !!!!!
x[i+1] <- sum(gsvR$rw[1:i]) + 1 # 1:i == 1:0 !!!!!
in for() you might want to use seq(along = gsvR$ret)
Uwe Ligges
> 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]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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
More information about the R-help
mailing list