Mike
Mike

Reputation: 57

lm function on 1min xts time series

I am wondering whether the linear regression function lm should fully work on time series of 1 minute interval. I would expect yes, but seemingly not in this case. I have got the following xts time series z

                         mean
    2016-03-11 08:37:00  10
    2016-03-11 08:38:00  11
    2016-03-11 08:39:00  12

Applying lm( z ~ index(z) ) gives

Coefficients:
(Intercept)     index(z)  
         11           NA  

So the slope of the regression is NA. I wonder why? I don't see any mathematical reason why this couldn't be calculated.

If I change the time of the first row to have a 5 min interval, so z equals

                      mean
2016-03-11 08:33:00   10
2016-03-11 08:38:00   11
2016-03-11 08:39:00   12

then lm( z ~ index(z) ) works as expected and returns a slope of 4.839e-3

Coefficients:
(Intercept)     index(z)  
 -7.053e+06    4.839e-03  

Do I misunderstand anything about how the lm function should work? Or can anybody comment on this behaviour? Is there any other function which can calculate the slope for 1min series?

Upvotes: 3

Views: 175

Answers (1)

Joshua Ulrich
Joshua Ulrich

Reputation: 176688

This isn't specific to xts or the lm function. It's an issue with estimating linear regression coefficients in general. You can't estimate variation in a data series using data that effectively does not vary (within floating point arithmetic precision).

You can see that your first example is computationally singular:

lm(z ~ index(z), singular.ok=FALSE)
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  singular fit encountered

But it would "work" if you lower the tolerance, but that sacrifices numerical stability.

lm(z ~ index(z), singular.ok=FALSE, tol=1e-8)

Call:
lm(formula = z ~ index(z), singular.ok = FALSE, tol = 1e-08)

Coefficients:
(Intercept)     index(z)  
 -2.430e+07    1.667e-02

Your second example works because you created enough variation.

Upvotes: 3

Related Questions