使用 zoo 包输入每日时间序列时出错
Error in imputing daily time series with zoo package
na.StructTS()
-函数,根据其文档,用于使用季节性卡尔曼滤波器(不熟悉这背后的数学原理)填充NA
时间序列中的值。
但是我有一个问题:
当我想替换 zooreg
对象中的 NA
s 时,它只有在指定频率(每个时间单位的观察)大于 1 时才有效。
示例:
#with frequency = 1 appearantly being the default
t <- zooreg(c(34, 12, 45, 56, 34, NA, 57, 59),
start = as.Date("2019-01-01"))
> t
2019-01-01 2019-01-02 2019-01-03 2019-01-04 2019-01-05 2019-01-06 2019-01-07 2019-01-08
34 12 45 56 34 NA 57 59
na.StructTS(t, na.rm = TRUE)
Returns错误:
Error in rowSums(tsSmooth(StructTS(y))[, -2]) :
'x' must be an array of at least two dimensions
变化中:
t <- zooreg(c(34, 12, 45, 56, 34, NA, 57, 59),
start = as.Date("2019-01-01"), frequency = 2)
> na.StructTS(t, na.rm = TRUE)
2019-01-01 2019-01-01 2019-01-02 2019-01-02 2019-01-03 2019-01-03 2019-01-04 2019-01-04
34.00000 12.00000 45.00000 56.00000 34.00000 49.84633 57.00000 59.00000
对任何事情 > 1
都有效,但每次的归因都不同,并且花费的时间越来越长。
为什么会这样?如何用 na.StructTS()
估算每日时间序列?
很想知道您想要什么样的系列。
na.Struct()
可能不是非常短的系列的最佳选择。
还有很多其他的插补方法(有些更容易理解)。
例如。 zoo
还提供 na.approx
, na.spline
, na.StructTS
, na.locf
还有 imputeTS
包,它专门用于时间序列插补。 (并且还应该与 zoo
时间序列兼容,因此您可以在动物园系列上使用它)
在 imputeTS 包中,您有:na_interpolation()
、na_locf()
、na_ma()
、na_kalman()
、na_seadec()
、na_seasplit()
甚至更多 functions.
如果您想继续使用卡尔曼滤波器,可以查看 na_kalman()
函数
na_kalman(x, model = "StructTS", smooth = TRUE, nit = -1, ...)
它有几个额外的选项,因此您可以使用 ARIMA 模型而不是来自 StructTS 的模型,或者您可以选择是否要使用 KalmanRun 或 KalmanSmoothing。
na_kalman(x, model = "StructTS", smooth = TRUE)
最相似
na.StructTS。但仍然有不同的实现——所以它们不会给出完全相同的结果。因此,如果您坚持在 State Space 模型上使用 KalmanSmoothing,则可以使用 imputeTS 中的此函数(频率 = 1 时不会出错)
na.StructTS()
-函数,根据其文档,用于使用季节性卡尔曼滤波器(不熟悉这背后的数学原理)填充NA
时间序列中的值。
但是我有一个问题:
当我想替换 zooreg
对象中的 NA
s 时,它只有在指定频率(每个时间单位的观察)大于 1 时才有效。
示例:
#with frequency = 1 appearantly being the default
t <- zooreg(c(34, 12, 45, 56, 34, NA, 57, 59),
start = as.Date("2019-01-01"))
> t
2019-01-01 2019-01-02 2019-01-03 2019-01-04 2019-01-05 2019-01-06 2019-01-07 2019-01-08
34 12 45 56 34 NA 57 59
na.StructTS(t, na.rm = TRUE)
Returns错误:
Error in rowSums(tsSmooth(StructTS(y))[, -2]) : 'x' must be an array of at least two dimensions
变化中:
t <- zooreg(c(34, 12, 45, 56, 34, NA, 57, 59),
start = as.Date("2019-01-01"), frequency = 2)
> na.StructTS(t, na.rm = TRUE)
2019-01-01 2019-01-01 2019-01-02 2019-01-02 2019-01-03 2019-01-03 2019-01-04 2019-01-04
34.00000 12.00000 45.00000 56.00000 34.00000 49.84633 57.00000 59.00000
对任何事情 > 1
都有效,但每次的归因都不同,并且花费的时间越来越长。
为什么会这样?如何用 na.StructTS()
估算每日时间序列?
很想知道您想要什么样的系列。
na.Struct()
可能不是非常短的系列的最佳选择。
还有很多其他的插补方法(有些更容易理解)。
例如。 zoo
还提供 na.approx
, na.spline
, na.StructTS
, na.locf
还有 imputeTS
包,它专门用于时间序列插补。 (并且还应该与 zoo
时间序列兼容,因此您可以在动物园系列上使用它)
在 imputeTS 包中,您有:na_interpolation()
、na_locf()
、na_ma()
、na_kalman()
、na_seadec()
、na_seasplit()
甚至更多 functions.
如果您想继续使用卡尔曼滤波器,可以查看 na_kalman()
函数
na_kalman(x, model = "StructTS", smooth = TRUE, nit = -1, ...)
它有几个额外的选项,因此您可以使用 ARIMA 模型而不是来自 StructTS 的模型,或者您可以选择是否要使用 KalmanRun 或 KalmanSmoothing。
na_kalman(x, model = "StructTS", smooth = TRUE)
最相似 na.StructTS。但仍然有不同的实现——所以它们不会给出完全相同的结果。因此,如果您坚持在 State Space 模型上使用 KalmanSmoothing,则可以使用 imputeTS 中的此函数(频率 = 1 时不会出错)