[R] Time Series information in formulae
Gabor Grothendieck
ggrothendieck at gmail.com
Wed Apr 12 13:07:26 CEST 2006
You might want to look at the dyn package. It allows
time series in model formulae, e.g. this aligns UKgas
and diff(UKgas) properly:
dyn$lm(UKgas ~ diff(UKgas))
dyn$ transforms the above to
dyn(lm(dyn(UKgas ~ diff(UKgas))))
and the inner dyn adds class "dyn" to the formula's class vector
so that subsequently model.formula.dyn will intercept it and pick off
the class of the response and add some additional info to the result
including the class. This leaves na.action= open for the usual
uses. Modifying your example, by removing the last two lines
(but otherwise leaving it unchanged):
ssm <- function(formula, data = list(),subset=NULL) {
cl <- match.call()
if (missing(data))
data <- environment(formula)
mf <- match.call(expand.dots = FALSE)
mf$drop.unused.levels <- TRUE
mf[[1]] <- as.name("model.frame")
mf <- eval(mf, .GlobalEnv)
}
library(dyn)
out <- dyn$ssm(UKgas ~ 1)
attr(out, ".Class") # "ts"
# the same
out <- ssm(dyn(UKgas ~ 1))
attr(out, ".Class") # "ts"
On 4/12/06, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
> The problem here is that you called model.frame() with (I presume, the
> 'factory-fresh' default) na.action=na.omit, and model.frame is documented
> to remove tsp attributes in that case.
>
> Use model.frame(..., na.action = NULL), e.g.
>
> model.frame(~UKgas, na.action=NULL)[[1]]
>
> is a time series but
>
> model.frame(~UKgas, na.action=na.omit)[[1]]
>
> is not. See ?model.frame.
>
>
> On Tue, 11 Apr 2006, Claus Dethlefsen wrote:
>
> > Dear List
> >
> > The UKgas data is stored as an object of class 'ts'. I am trying to use "UKgas"
> > in a formula as argument to a function. However, I do not know how to access
> > the 'time series' information in the response (such as start() end() etc.).
> >
> > Here is a boiled down example.
> >
> > ssm <- function(formula, data = list(),subset=NULL) {
> > cl <- match.call()
> > if (missing(data))
> > data <- environment(formula)
> > mf <- match.call(expand.dots = FALSE)
> > mf$drop.unused.levels <- TRUE
> > mf[[1]] <- as.name("model.frame")
> > mf <- eval(mf, .GlobalEnv)
> > mt <- attr(mf, "terms")
> > y <- model.response(mf, "numeric")
> > print(class(y))
> > }
> >
> > R> class(UKgas)
> > [1] "ts"
> > R> ssm(UKgas~1)
> > [1] "numeric"
> > R> ssm(log10(UKgas)~1)
> > [1] "numeric"
> >
> > I want the latter two to be "ts". I have tried putting "any" in place
> > of "numeric" in the call to model.response, but it does not change anything for
> > me.
> >
> > How do I retrieve the response from a formula without loosing its "ts" status?
> >
> > Best,
> > Claus
> > ----------------------------------------------------
> > Claus Dethlefsen, MSc, PhD
> > Biostatistician at Center for Cardiovascular Research
> > Aalborg Hospital, Aarhus University Hospital, Denmark
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> >
>
> --
> Brian D. Ripley, ripley at stats.ox.ac.uk
> Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
> University of Oxford, Tel: +44 1865 272861 (self)
> 1 South Parks Road, +44 1865 272866 (PA)
> Oxford OX1 3TG, UK Fax: +44 1865 272595
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.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