apply 语句中的滞后在 R 中不起作用
我正在尝试“应用”一个对 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里有一些数据:
如果你想滞后这个多元时间序列,只需调用 lag (即不需要 apply):
如果你想跨行应用函数,它需要是明智的。例如,要获取行值的平均值:
您无法应用动物园对象并取回动物园对象。 apply 的输出是“向量、数组或值列表”。在上面的示例中:
您需要将其重新创建为动物园对象,然后对其进行滞后:
您需要稍微小心输出的方向。但如果需要,您可以使用
t()
函数转置它。例如:您也可以将其包装在一个函数中。或者,您可以使用 xts 时间序列库中的应用函数之一(这将在进程中保留时间序列对象):
Here's some data:
If you want to lag this multivariate time series, just call lag (i.e. no need for apply):
If you want to apply a function across the rows, it needs to be sensible. For instance, to get mean of the row values:
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:
You need to recreate it as a zoo object and then lag it:
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: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):为什么不尝试使用
quantmod::Lag
函数来生成一个由不同滞后值的序列的各种滞后序列组成的矩阵?例如将返回
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 examplewill return
@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.