[R] ts's in lm()

kjetil@acelerate.com kjetil at acelerate.com
Sun Apr 25 17:29:50 CEST 2004


Is this a bug?

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




More information about the R-help mailing list