apply 语句中的滞后在 R 中不起作用

发布于 2024-08-05 16:34:25 字数 253 浏览 10 评论 0原文

我正在尝试“应用”一个对 R 中的动物园对象执行“滞后”的函数。

如果我传递单个动物园向量,该函数就会正常工作 - 它会应用滞后并且一切正常。

但是,如果我 apply( data, 1, function ) 那么滞后不起作用。没有错误,只是相当于零滞后。

简单的 apply( data, 1, lag ) 也是如此。

谁能解释为什么会这样?我可以做些什么来使延迟发生吗?

I'm trying to "apply" a function that does "lag"s on zoo objects in R.

The function works correctly if I pass a single zoo vector - it applys the lag and everything works.

However, if I apply( data, 1, function ) then the lag doesn't work. There is no error, just the equivalent of a zero lag.

This is also the case with a simple apply( data, 1, lag ).

Can anyone explain why this should be the case? Is there anything I can do to make the lag to occur?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

驱逐舰岛风号 2024-08-12 16:34:25

这里有一些数据:

 > x <- zoo(matrix(1:12, 4, 3), as.Date("2003-01-01") + 0:3)
 > x
 2003-01-01 1 5  9
 2003-01-02 2 6 10
 2003-01-03 3 7 11
 2003-01-04 4 8 12

如果你想滞后这个多元时间序列,只需调用 lag (即不需要 apply):

 > lag(x)                 
 2003-01-01 2 6 10
 2003-01-02 3 7 11
 2003-01-03 4 8 12

如果你想跨行应用函数,它需要是明智的。例如,要获取行值的平均值:

> apply(x, 1, mean)
2003-01-01 2003-01-02 2003-01-03 2003-01-04 
         5          6          7          8 

您无法应用动物园对象并取回动物园对象。 apply 的输出是“向量、数组或值列表”。在上面的示例中:

> class(apply(x, 1, mean))
[1] "numeric"

您需要将其重新创建为动物园对象,然后对其进行滞后:

> lag(zoo(apply(coredata(x), 1, mean), index(x)))
 2003-01-01 2003-01-02 2003-01-03 
         6          7          8 

您需要稍微小心输出的方向。但如果需要,您可以使用 t() 函数转置它。例如:

> zoo(t(apply(coredata(x), 1, quantile)), index(x))
           0% 25% 50% 75% 100%
2003-01-01  1   3   5   7    9
2003-01-02  2   4   6   8   10
2003-01-03  3   5   7   9   11
2003-01-04  4   6   8  10   12

您也可以将其包装在一个函数中。或者,您可以使用 xts 时间序列库中的应用函数之一(这将在进程中保留时间序列对象):

> x <- as.xts(x)
> apply.daily(x, mean)
           [,1]
2003-01-01    5
2003-01-02    6
2003-01-03    7
2003-01-04    8

Here's some data:

 > x <- zoo(matrix(1:12, 4, 3), as.Date("2003-01-01") + 0:3)
 > x
 2003-01-01 1 5  9
 2003-01-02 2 6 10
 2003-01-03 3 7 11
 2003-01-04 4 8 12

If you want to lag this multivariate time series, just call lag (i.e. no need for apply):

 > lag(x)                 
 2003-01-01 2 6 10
 2003-01-02 3 7 11
 2003-01-03 4 8 12

If you want to apply a function across the rows, it needs to be sensible. For instance, to get mean of the row values:

> apply(x, 1, mean)
2003-01-01 2003-01-02 2003-01-03 2003-01-04 
         5          6          7          8 

You can't apply a zoo object and get a zoo object back. The output of apply is "a vector or array or list of values". In the example above:

> class(apply(x, 1, mean))
[1] "numeric"

You need to recreate it as a zoo object and then lag it:

> lag(zoo(apply(coredata(x), 1, mean), index(x)))
 2003-01-01 2003-01-02 2003-01-03 
         6          7          8 

You need to be slightly careful of the direction of your output. But you can transpose it if necessary with the t() function. For instance:

> zoo(t(apply(coredata(x), 1, quantile)), index(x))
           0% 25% 50% 75% 100%
2003-01-01  1   3   5   7    9
2003-01-02  2   4   6   8   10
2003-01-03  3   5   7   9   11
2003-01-04  4   6   8  10   12

You could also wrap this in a function. Alternatively you can use one of the apply functions in the xts time series library (this will retain the time series object in the process):

> x <- as.xts(x)
> apply.daily(x, mean)
           [,1]
2003-01-01    5
2003-01-02    6
2003-01-03    7
2003-01-04    8
撩心不撩汉 2024-08-12 16:34:25

为什么不尝试使用 quantmod::Lag 函数来生成一个由不同滞后值的序列的各种滞后序列组成的矩阵?例如

> quantmod::Lag (1:10, k=c(0,5,2))

将返回

Lag.0 Lag.5 Lag.2
 [1,] 1 不适用 不适用
 [2,] 2 不适用 不适用
 [3,] 3 不适用 1
 [4,] 4 不适用 2
 [5,] 5 不适用 3
 [6、] 6 1 4
 [7、] 7 2 5
 [8、]8 3 6
 [9、] 9 4 7
[10、]10 5 8

Why don't you try quantmod::Lag function for generating a matrix consisting of various lagged-series of a series, at different lag values? For example

> quantmod::Lag (1:10, k=c(0,5,2))

will return

Lag.0 Lag.5 Lag.2
 [1,]     1    NA    NA
 [2,]     2    NA    NA
 [3,]     3    NA     1
 [4,]     4    NA     2
 [5,]     5    NA     3
 [6,]     6     1     4
 [7,]     7     2     5
 [8,]     8     3     6
 [9,]     9     4     7
[10,]    10     5     8
毁梦 2024-08-12 16:34:25

@Marek - lag(data) 确实做了我想要的事情,但我希望能够将其用作“应用”构造的一部分,以使向量->矩阵抽象更容易一些。

@Marek - lag(data) does do what I want, but I wanted to be able to use this as part of an "apply" construct to make the vector->matrix abstraction a little easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文