user3128910
user3128910

Reputation: 23

Error when doing linear regression using zoo objects ... Error in `$<-.zoo`(`*tmp*`

I am new to R and slowly getting acquainted. My question refers to the following piece of code.

I am creating a zoo object with the following headers and then filtering by date. On the filtered dates I am subtracting two columns (Tom from Elena). Everything works fine until here.

Code below:

b <- read.zoo(b1, header = TRUE, index.column = 1, format = "%d/%m/%Y")

startDate = "2013/11/02" endDate = "2013/12/20"

dates <- seq(as.Date(startDate), as.Date(endDate), by=1)

TE = b[dates]$Tom - b[dates]$Elena

However I am then regressing the results from my subtraction (see above TE) on Elena. However i get an error message every time i try and to this regression

TE$model <- lm(TE ~ b[dates]$Elena)

Error in $<-.zoo(*tmp*, "model", value = list(coefficients = c(-0.0597128230859905, : not possible for univariate zoo series

I have tried creating a data frame and then doing the regression but with no avail. Any help would be appreciated. Thanks.

Upvotes: 1

Views: 1232

Answers (1)

Sven Hohenstein
Sven Hohenstein

Reputation: 81683

You can not add the outcome of a regression (a list of class lm) to a time series of class zoo.

I recommend saving the model in a separate object, e.g.,

fit <- lm(TE ~ b[dates]$Elena)

Upvotes: 2

Related Questions