简体   繁体   中英

What is the difference between lag and zlag function in r?

在使用时间序列或任何数据帧时,取列的滞后或列的滞后有什么区别?

lag of time-series is shifting underlying time period without affecting time series values. Eg ldeaths time-series:

      Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
1974 3035 2552 2704 2554 2014 1655 1721 1524 1596 2074 2199 2512
1975 2933 2889 2938 2497 1870 1726 1607 1545 1396 1787 2076 2837
1976 2787 3891 3179 2011 1636 1580 1489 1300 1356 1653 2013 2823
1977 3102 2294 2385 2444 1748 1554 1498 1361 1346 1564 1640 2293
1978 2815 3137 2679 1969 1870 1633 1529 1366 1357 1570 1535 2491
1979 3084 2605 2573 2143 1693 1504 1461 1354 1333 1492 1781 1915

After lag(ldeaths, 12) 1-year shift (12 months), values of time series are not changing. Only period of time changes from 1974-1979 to 1973-1978:

      Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
1973 3035 2552 2704 2554 2014 1655 1721 1524 1596 2074 2199 2512
1974 2933 2889 2938 2497 1870 1726 1607 1545 1396 1787 2076 2837
1975 2787 3891 3179 2011 1636 1580 1489 1300 1356 1653 2013 2823
1976 3102 2294 2385 2444 1748 1554 1498 1361 1346 1564 1640 2293
1977 2815 3137 2679 1969 1870 1633 1529 1366 1357 1570 1535 2491
1978 3084 2605 2573 2143 1693 1504 1461 1354 1333 1492 1781 1915

After execution of library(TSA); zlag(ldeath, 12) library(TSA); zlag(ldeath, 12) the output is a vector, where last 12 values are truncated and in the beginning 12 NA are added:

 [1]   NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   NA 3035 2552 2704 2554 2014 1655 1721 1524 1596 2074 2199 2512 2933 2889
[27] 2938 2497 1870 1726 1607 1545 1396 1787 2076 2837 2787 3891 3179 2011 1636 1580 1489 1300 1356 1653 2013 2823 3102 2294 2385 2444
[53] 1748 1554 1498 1361 1346 1564 1640 2293 2815 3137 2679 1969 1870 1633 1529 1366 1357 1570 1535 2491

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM