Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic Lag TS Object in R not "working"

Tags:

r

time-series

Here is some basic info about my data

> prod.ts
Time Series:
Start = 2009.26027397260 
End = 2010.83719704953 
Frequency = 52 
 [1]  895 1780 1989 1996 1660 1860 1921 2188 1789 1996 2085 2185 2205 2232 2062
[16] 2257 2000 2055 2084 1977 2051 1999 2428 2220 2385 2174 2307 2549 2211 2224
[31] 1922 2091 2318 1986 2080 2069 2106 1998 1480 1841 1819 2119 2109 2072 2206
[46] 1965 2017 2296 1866 2262 2088 2157 2582 2398 2325 1393 2577 2375 2452 2534
[61] 2586 2032 2781 2423 2575 2362 2132 2375 2105 2425 2346 2495 2908 2301 2918
[76] 2426 2633 2312 2472 2305 2622 2662 2626

I am stumped as to why these two are equivlane

> length(prod.ts)
[1] 83
> length(lag(prod.ts, 1))
[1] 83

This probably shows that I am new to R and Time series, but what am I missing?

Thanks in advance

like image 773
Btibert3 Avatar asked Feb 21 '26 07:02

Btibert3


2 Answers

lag.ts lags the time index. The data is unchanged. Compare the series g and lag(g) below. Note that the data is the same but g goes from 1 to 5 and lag(g) goes from 0 to 4:

> g <- ts(101:105)
> g
Time Series:
Start = 1 
End = 5 
Frequency = 1 
[1] 101 102 103 104 105
> lag(g)
Time Series:
Start = 0 
End = 4 
Frequency = 1 
[1] 101 102 103 104 105
like image 200
G. Grothendieck Avatar answered Feb 25 '26 13:02

G. Grothendieck


That is just the way lag() for ts objects is defined. From help(lag):

lag                   package:stats                    R
Documentation

Lag a Time Series

Description:

     Compute a lagged version of a time series, shifting the time base
     back by a given number of observations.

Use xts() or zoo() if you'd rather have NAs added while keeping start and end times.

like image 39
Dirk Eddelbuettel Avatar answered Feb 25 '26 14:02

Dirk Eddelbuettel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!