滚动滞后差异 [英] Rolling lagged differences

查看:119
本文介绍了滚动滞后差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,所以我希望在R中创建滚动的滞后差异.

Ok so I am looking to create rolling lagged differences in R.

vec <- c(43.79979, 44.04865, 44.17308, 44.54638, 44.79524, 44.79524, 44.79524, 44.42195, 44.54638, 44.79524, 44.42195, 43.30206, 43.30206, 43.17764, 43.30206)

> length(vec)
[1] 15

这是我到目前为止尝试过的:

This is what I have tried so far:

vec1 <- rollapply(vec, width = 2,  fill = NA, FUN = diff)

这将给出以下输出:

[1]  0.24886  0.12443  0.37330  0.24886  0.00000  0.00000 -0.37329  0.12443  0.24886 -0.37329 -1.11989  0.00000 -0.12442  0.12442       NA

> length(vec1)
[1] 15

请注意,元素15中有一个NA值.

Note we have an NA value in element 15.

所以我想对延迟1,2和3进行延迟比较...所以上面的代码无法满足此要求,所以我尝试以下操作:

So I want to do this diff in lags for say lags 1,2 and 3... So the above code doesn't cater for this, so I try the below:

lag1 <- diff(vec, lag = 1, differences = 1, arithmetic = TRUE, na.pad = TRUE)
lag2 <- diff(vec, lag = 2, differences = 1, arithmetic = TRUE, na.pad = TRUE)
lag3 <- diff(vec, lag = 3, differences = 1, arithmetic = TRUE, na.pad = TRUE)
length(lag1)
length(lag2)
length(lag3)

结果:

> lag1
 [1]  0.24886  0.12443  0.37330  0.24886  0.00000  0.00000 -0.37329  0.12443  0.24886 -0.37329 -1.11989  0.00000 -0.12442  0.12442
> lag2
 [1]  0.37329  0.49773  0.62216  0.24886  0.00000 -0.37329 -0.24886  0.37329 -0.12443 -1.49318 -1.11989 -0.12442  0.00000
> lag3
 [1]  0.74659  0.74659  0.62216  0.24886 -0.37329 -0.24886  0.00000  0.00000 -1.24432 -1.49318 -1.24431  0.00000
> length(lag1)
[1] 14
> length(lag2)
[1] 13
> length(lag3)
[1] 12

请注意,当执行上述滞后差值时...将diff结果放在减去了该值的那一行上,因此采用了我们的当前值-滞后值.它将差异结果放置在滞后值位置.然后我们失去向量的长度.我想实际将diff-滞后结果放置在起始编号(diff)上,并放置前导NA以解决数据集起始处的缺失值.

Notice that when do the lagged difference above... it places the diff result on the line that it subtracted the value on... so it took our current value - lagged value. It places the diff result on the lagged value position. We then lose the length of the vector. I want to actually place the diff - lagged result on the start number (diff) and place leading NA's to account for the missing values at the start of the data set.

以滞后2为例,这是我想要的结果:

Using lag 2 as en example, this is my desired result:

> lag2
 [1]  NA    NA 0.37329  0.49773  0.62216  0.24886  0.00000 -0.37329 -0.24886  0.37329 -0.12443 -1.49318 -1.11989 -0.12442  0.00000

有人知道如何纠正此问题吗?

Does anyone know a way on how to correct this??

也许还要解释一下:

这是向量的开始:

vec <- c(43.79979, 44.04865, 44.17308..... 

所以,如果我们做一个滞后的2个差异...

So if we do a lagged 2 difference...

我们取第三个元素... 44.17308-43.79979 = 0.37329的结果.

We take the 3rd element... 44.17308 - 43.79979 = the result of 0.37329.

所以我想拥有NA NA 0.37329

So I want to have NA NA 0.37329

而不是将0.37329放在新的lag2向量的第一个位置上.

Instead of placing 0.37329 on the first position in the new lag2 vector.

推荐答案

就像


2018年5月10日 @thistleknot对我说(谢谢!),dplyr掩盖了stats自己的lag泛型.因此,请确保没有附加dplyr,或者明确地运行stats::lag,否则我的代码将无法运行.


On May 10th 2018 it was pointed to me by @thistleknot (thanks!) that dplyr masks stats's own lag generic. Therefore make sure you don't have dplyr attached, or instead run stats::lag explicitly, otherwise my code won't run.

我想我找到了罪魁祸首:github.com/tidyverse/dplyr/issues/1586 答:这是拥有大量R包的自然结果. 只是明确地使用stats :: lag或dplyr :: lag

I think I found the culprit: github.com/tidyverse/dplyr/issues/1586 answer: This is a natural consequence of having lots of R packages. Just be explicit and use stats::lag or dplyr::lag

这篇关于滚动滞后差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆