From plummer@iarc.fr  Fri Jul  2 17:21:37 1999
From: plummer@iarc.fr (Martyn Plummer)
Date: Fri, 02 Jul 1999 18:21:37 +0200 (CEST)
Subject: Bug in "[.ts" for multivariate ts {Problem with plot.ts, "["
In-Reply-To: <199907021644.SAA21974@pubhealth.ku.dk>
Message-ID: <XFMail.990702182137.plummer@iarc.fr>

This message is in MIME format
--_=XFMail.1.3.p0.Linux:990702182137:16900=_
Content-Type: text/plain; charset=us-ascii

There was some discussion a while back on R-devel between Ross Ihaka,
Paul Gilbert and myself about row subsetting in time series.  I think
the consensus was that "[.ts" should not try to coerce its result
back to a time series object (which is underlying the problem here).
If you really do want to take a subseries from a regular time series,
a modified version of window.ts would do. I am attaching my
suggested modifications.
Martyn


On 02-Jul-99 maechler@stat.math.ethz.ch wrote:
> 
>>>>>> On Fri, 02 Jul 1999, Adrian Trapletti <Adrian.Trapletti@wu-wien.ac.at>
>>>>>> said:
> 
>     Adrian> There seems to be a problem with plot.ts (R Version 0.64.2)
>     > x<-cbind(1:10,2:11)
>     > x<-as.ts(x)
>     > plot(x)
>     Adrian> Error: subscript (20) out of bounds, should be at most 10
> 
> This is definitely a bug
> --> CC: R-bugs
> 
> ALL NOTE : This is *not* new in 0.64.2
> 
> 
> 
>     Adrian> The problem is located in "[.ts":
> 
>     > x<-cbind(1:10,2:11)
>     > x[is.finite(x)]
>     Adrian> [1]  1  2  3  4  5  6  7  8  9 10  2  3  4  5  6  7  8  9 10 11
>     > x<-as.ts(x)
>     > x[is.finite(x)]
>     Adrian> Error: subscript (20) out of bounds, should be at most 10
> 
> 
>     Adrian> and there (in "[.ts") the following statement gives the error:
> 
>     Adrian> Browse[1]>
>     Adrian> debug: li <- length(ind <- (1:n)[i])
> 
>     Adrian> However I don't understand what this part exactly does?
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -
> r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> _

----------------------------------
E-Mail: Martyn Plummer <plummer@iarc.fr>
Date: 02-Jul-99
Time: 18:02:32

This message was sent by XFMail
----------------------------------

--_=XFMail.1.3.p0.Linux:990702182137:16900=_
Content-Disposition: attachment; filename="tsfix"
Content-Transfer-Encoding: 7bit
Content-Description: tsfix
Content-Type: text/plain; charset=us-ascii; name=tsfix; SizeOnDisk=1838

"[.ts" <- function (x, i, j, drop = TRUE) 
{
    y <- NextMethod("[")
    if (missing(i)) 
        ts(y, start = start(x), freq = frequency(x))
    else y
}

"window.ts" <-
function (x, start, end, frequency, deltat) 
{
    x <- as.ts(x)
    xtsp <- tsp(x)
    xfreq <- xtsp[3]
    xtime <- time(x)
    ts.eps <- .Options$ts.eps
    if (missing(frequency) && missing(deltat)) 
        yfreq <- xfreq
    else if (missing(deltat)) 
        yfreq <- frequency
    else if (missing(frequency)) 
        yfreq <- 1/deltat
    if (xfreq%%yfreq < ts.eps) {
        thin <- round(xfreq/yfreq)
        yfreq <- xfreq/thin
    }
    else {
        thin <- 1
        yfreq <- xfreq
        warning("Frequency not changed")
    }
    start <- if (missing(start)) 
        xtsp[1]
    else switch(length(start), start, start[1] + (start[2] - 
        1)/xfreq, stop("Bad value for start"))
    if (start < xtsp[1]) {
        start <- xtsp[1]
        warning("start value not changed")
    }
    end <- if (missing(end)) 
        xtsp[2]
    else switch(length(end), end, end[1] + (end[2] - 1)/xfreq, 
        stop("Bad value for end"))
    if (end > xtsp[2]) {
        end <- xtsp[2]
        warning("end value not changed")
    }
    if (start > end) 
        stop("start cannot be after end")
    if (all(abs(start - xtime) > abs(start) * ts.eps)) {
        start <- xtime[(xtime > start) & ((start + 1/xfreq) > 
            xtime)]
    }
    if (all(abs(end - xtime) > abs(end) * ts.eps)) {
        end <- xtime[(xtime < end) & ((end - 1/xfreq) < xtime)]
    }
    i <- seq(trunc((start - xtsp[1]) * xfreq + 1.5), trunc((end - 
        xtsp[1]) * xfreq + 1.5), by = thin)
    y <- if (is.matrix(x)) 
        x[i, , drop = FALSE]
    else x[i]
    ystart <- xtime[i[1]]
    yend <- xtime[i[length(i)]]
    tsp(y) <- c(ystart, yend, yfreq)
    y
}


--_=XFMail.1.3.p0.Linux:990702182137:16900=_--
End of MIME message
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


Bug in "[.ts" for multivariate ts {Problem with plot.ts, "["
Martyn Plummer