【发布时间】:2021-04-21 11:09:32
【问题描述】:
我的data.table如下
dt <- structure(list(x = c(-0.888888888888886, -0.588235294117648,
0.630952380952381, 0.0769230769230788, 0.250000000000003, -0.615384615384616,
0.888888888888891, 0.924528301886792, -0.477326968973745, 0),
ema = c(-0.121833534531943, -0.148485063651126, -0.103945781102354,
-0.0936104177866151, -0.0739755367702369, -0.104913198405344,
-0.0481245077028166, NA, NA,
NA)), row.names = c(NA, -10L), class = c("data.table",
"data.frame"))
看起来像
x ema
1: -0.88888889 -0.121833535
2: -0.58823529 -0.148485064
3: 0.63095238 -0.103945781
4: 0.07692308 -0.093610418
5: 0.25000000 -0.073975537
6: -0.61538462 -0.104913198
7: 0.88888889 -0.048124508
8: 0.9245283 NA
9: -0.4773270 NA
10: 0.0000000 NA
在此 data.table 中,x 列是每天更新的连续变量,ema 列是 x 列的 EMA(指数移动平均线)。由于某种原因,过去 3 天我无法更新 x 的 EMA(在列 ema 中),现在我需要使用下面给出的函数 ema_add 更新它 -
ema_add <- function(newx, lasty){
ratio <- 2 / (34+1)
lasty * (1 - ratio) + ratio * newx
}
正如帖子中所建议的 - Rolling a function on two columns in data.table ,我正在使用以下代码来查找最后三个值的 EMA,但它没有给出预期的结果。以下是我得到的结果。
dt$updated_ema = Reduce(ema_add, x = dt$x[-1], init = first(dt$ema), accumulate = T)
dt$updated_ema
[1] -0.12183353 -0.43276804 0.27637891 0.14340835 0.21446945 -0.33876659 0.47967039 0.77624233 -0.05947054 -0.01982351
预期结果是-
x ema
1: -0.88888889 -0.121833535
2: -0.58823529 -0.148485064
3: 0.63095238 -0.103945781
4: 0.07692308 -0.093610418
5: 0.25000000 -0.073975537
6: -0.61538462 -0.104913198
7: 0.88888889 -0.048124508
8: 0.92452830 0.007455653
9: -0.47732697 -0.020246211
10: 0.00000000 -0.019089285
在应用上面的Reduce 函数时,有人能发现我做错了什么吗?
提前致谢。
【问题讨论】:
-
你的代码抛出
Error in match.fun(f) : object 'ema_update' not found。如果我将函数名称更正为发布的函数Error in f(init, x[[i]]) : object 'ratio' not found. -
感谢您指出。我已经修复了这些错误。
-
还是没有
ratio。 -
变量
ratio在ema_add函数内部定义。
标签: r data.table rolling-computation