[R] ts's in lm()

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Apr 25 18:15:43 CEST 2004


On Sun, 25 Apr 2004 kjetil at acelerate.com wrote:

> Is this a bug?

Without even a traceback() nor reproducible code it is hard to say.
But for lm, you don't want time series and it would be better to drop the 
ts class and tsp attributes.

> seasonal.dummies <-
> function(x, contr=NULL) {
> # takes a time series and returns a matrix of seasonal dummies for 
> # x. This is almost cycle(x), we only have to make it into a factor
> # and add suitable level names.
> # return a matrix which includes a constant!
> # level names here assumes frequency is 12!
> cyc <- factor( cycle(x), labels=c("ene","feb", "mar", "abr", "may",
>                "jun","jul", "ago", "sep",  "oct","nov", "dic") )
> if(!is.null(contr)) cyc <- C(cyc, contr)
> model.matrix( ~  cyc )
> }
> 
> 
> > modelo1 <- function(xx, yy) {
>  # xx es serie explicativa (economia)
>  # yy es serie para predecir --- gasolina.eq
>  name.x <- deparse(substitute(xx))
>  name.y <- deparse(substitute(yy))
>  xx <- ts(rep(xx,rep(3, length(xx))), frequency=12, start=
>             c(start(xx)[1], (start(xx)[2]-1)*3+1) )
>  common <- ts.intersect(xx, yy)
>  colnames(common) <- c(name.x, name.y)
>  xx <- common[,1]
>  yy <- common[,2]
>  sea <- seasonal.dummies(xx)
>  return(lm(yy  ~ 0+sea+xx))
>  }
> > mod1 <- modelo1(PIBtrimestral, gasolina.eq)
> Error in "storage.mode<-"(`*tmp*`, value = "double") : 
>         invalid time series parameters specified
> 
> lm() works with time series objects made directly with ts(), and the 
> arguments to modelo1() is of this type.
> 
> Kjetil Halvorsen
> 
> ______________________________________________
> 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
> 
> 

-- 
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




More information about the R-help mailing list