计算时间序列的导数

calculate derivative of time series

我想计算时间序列的一阶和二阶导数,如何在 R 中实现。

 dput(ps)
structure(c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 3L, 2L, 2L, 2L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 5L, 6L), class = c("xts", 
"zoo"), .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct", 
"POSIXt"), .indexTZ = "", tzone = "", index = structure(c(1409522400, 
1409523300, 1409524200, 1409525100, 1409526000, 1409526900, 1409527800, 
1409528700, 1409529600, 1409530500, 1409531400, 1409532300, 1409533200, 
1409534100, 1409535000, 1409535900, 1409536800, 1409537700, 1409538600, 
1409539500, 1409540400, 1409541300, 1409542200, 1409543100), tzone = "", tclass = c("POSIXct", 
"POSIXt")), .Dim = c(24L, 1L))

由于它们以 15 分钟的间隔均匀分布,

d <- as.numeric(diff(ps))/15

给你每分钟找零; diff(d)/15 应该给你二阶导数(以分钟为单位^(-2))

您还可以使用 diff(ps,differences=2) 求二阶差分(您可以将其除以 225 转换为导数 ...)

这会计算样条曲线,然后对其进行一阶导数:

tt <- time(ps)
xts(splinefun(tt, ps)(tt, 1), tt)

用 2 代替 1 得到二阶导数。