R-alpha: tsp

Paul Gilbert (pgilbert@bank-banque-canada.ca)
Thu, 6 Jun 1996 16:11:05 -0400


Date: Thu, 6 Jun 1996 16:11:05 -0400
From: pgilbert@bank-banque-canada.ca (Paul Gilbert)
To: ihaka@stat.auckland.ac.nz
Subject: R-alpha: tsp
Message-Id: <96Jun6.180053edt.29492@mailgate.bank-banque-canada.ca>


Ross

In R 0.7 and 0.8 I define two functions


"tsp<-" <-function(x,tf)
{if (is.null(tf)) 
   {attr(x, "tsp") <- tf
    if(inherits(x,"ts")) class(x) <- NULL
    return(x)
   }
 attr(x, "tsp") <- tf
 class(x) <- "ts"
 x
}

window.ts <-function(x, start=NULL, end=NULL)
    {f <- tsp(x)[3]
     if (is.null(start)) start <- tsp(x)[1]
     if (is.null(end))   end   <- tsp(x)[2]
     if (2 == length(start)) start <- start[1] + (start[2]-1)/f
     if (2 == length(end))    end  <-   end[1] + (  end[2]-1)/f
     leave <- (time(x) >= start) & (time(x) <= end)
     if (is.matrix(x)) z <- x[leave,,drop=F]
     else  z <- x[leave]
     tsp(z) <- c(start, end, f)
     class(z) <- class(x)
     z
    }

Then I get an error:

>  tspvector <- ts(1:100, start=c(1981,3), frequency=4)
> zz <- window.ts(tspvector, start=c(1983,2))
Error: invalid time series parameters specified
> 

which seems to happen in "tsp<-", but I can't see why. My eyes are
getting a bit glazed, so I may be missing something simple.

Also, it would help if start, end frequency, time, and window were
generic functions as in S, with the functions you now have as the
defaults (and window.ts above if you can get it to work):

start     <- function(x, ...) UseMethod("start", x, ...)
end       <- function(x, ...) UseMethod("end", x, ...)
frequency <- function(x, ...) UseMethod("frequency", x, ...)
time      <- function(x, ...) UseMethod("time", x, ...)
window    <- function(x, ...) UseMethod("window", x, ...)

My tframe programs actually replace these, but it would be a little
simpler if I could assume I am starting with the same generic functions
as in Splus.


Finally, as I previously mentioned, it would helpful if "[<-" stripped
out most attributes. I have managed a bit of a kludge to get around
some of the problems caused by the attributes remaining, but I don't
think it will get me very far.


Paul Gilbert
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-testers mailing list -- To (un)subscribe, send
subscribe	or	unsubscribe
(in the "body", not the subject !)  To: r-testers-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-