首页 > 解决方案 > 通过从 imputeTS 函数中提取数据来计算时间序列中的平均间隙大小

问题描述

我需要计算单变量时间序列数据集的平均间隙大小。imputeTS包使用这些数据生成图。statsNA是否可以从或中提取“间隙大小”和“出现次数” ggplot_na_gapsize?或者有没有其他方法可以找到时间序列数据集中差距的平均大小?(您可以使用imputeTStsNH4中的数据集)

(这是我第一次在这里提问,我对'r'还很陌生)

标签: time-seriesimputets

解决方案


目前,您只能通过使用 CRAN 版本的imputeTS的一些额外工作间接获得平均间隙大小。

但我在 GitHub 上快速更新了开发版本。现在您还可以使用该statsNA函数获得平均间隙大小。

因此,您必须先从 GitHub 安装新版本(因为它还没有在 CRAN 上):

library("devtools")
install_github("SteffenMoritz/imputeTS")

如果你没有安装“devtools”,那么一开始也要安装这个库

install.packages("devtools")

然后像往常一样使用imputeTS包。

library("imputeTS")

#Example with the tsNH4 dataset
statsNA(tsNH4)

这将打印您以下内容:

> statsNA(tsNH4)

[1] "Length of time series:"
[1] 4552
[1] "-------------------------"
[1] "Number of Missing Values:"
[1] 883
[1] "-------------------------"
[1] "Percentage of Missing Values:"
[1] "19.4%"
[1] "-------------------------"
[1] "Number of Gaps:"
[1] 155
[1] "-------------------------"
[1] "Average Gap Size:"
[1] 5.696774
[1] "-------------------------"
[1] "Stats for Bins"
[1] "  Bin 1 (1138 values from 1 to 1138) :      233 NAs (20.5%)"
[1] "  Bin 2 (1138 values from 1139 to 2276) :      433 NAs (38%)"
[1] "  Bin 3 (1138 values from 2277 to 3414) :      135 NAs (11.9%)"
[1] "  Bin 4 (1138 values from 3415 to 4552) :      82 NAs (7.21%)"
[1] "-------------------------"
[1] "Longest NA gap (series of consecutive NAs)"
[1] "157 in a row"
[1] "-------------------------"
[1] "Most frequent gap size (series of consecutive NA series)"
[1] "1 NA in a row (occuring 68 times)"
[1] "-------------------------"
[1] "Gap size accounting for most NAs"
[1] "157 NA in a row (occuring 1 times, making up for overall 157 NAs)"

如您所见,“间隙数”和“平均间隙大小”现在新添加到输出中。

您还可以将输出作为变量访问:

library("imputeTS")

#To actually get a output object, set print_only to false

out <- statsNA(tsNH4, print_only = F)

# Average gap size
out$average_size_na_gaps

# Number of Gaps
out$number_na_gaps

#Number of NAs
out$number_NAs

更新也将在下一次 CRAN 更新中。(感谢您的建议)请小心一点,因为它是一个开发版本 - 因此没有像 CRAN 版本那样经过全面测试。


推荐阅读