Skip to content
Prev 2269 / 63424 Next

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

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:
----------------------------------
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._