简体   繁体   English

使用 R 对不均匀间隔的单变量时间序列进行缺失值插补

[英]missing value imputation for unevenly spaced univariate time series using R

I have the following dataset:我有以下数据集:

timestamp   value

1           90

3           78

6           87

8           NA

12          98

15          100

18          NA

24          88

27          101

As you can see, the gaps between the consecutive timestamps are not equi-spaced.如您所见,连续时间戳之间的间隔不是等距的。 Is there a way to imputate values to replace the NA using a timestamp dependend method?有没有一种方法可以使用时间戳相关方法来估算值以替换 NA?

All packages I found are only suitable for equi-spaced time series... Thanks!我找到的所有包都只适用于等间隔时间序列......谢谢!

The zoo R package can be used to handle irregular spaced / unevenly spaced time series.动物园R package 可用于处理不规则间隔/不均匀间隔时间序列。

First you have to create a zoo ts object. You can either specify indices or use POSIXct timestamps.首先,您必须创建一个动物园 ts object。您可以指定索引或使用 POSIXct 时间戳。

Afterwards you can use a imputation method on this object. Zoo's imputation methods are limited, but they also work on irregular speced time series.之后你可以在这个 object 上使用插补方法。Zoo 的插补方法是有限的,但它们也适用于不规则的特定时间序列。 You can use linear interpolation (na.approx) or spline interpolation (na.spline), which also account for the uneven time stamps.您可以使用线性插值 (na.approx) 或样条插值 (na.spline),这也说明了不均匀的时间戳。

# First create a unevenly spaced zoo time series object
# First vector with values, second with your indices
zoo_ts <- zoo(c(90,78,87,NA,98,100,NA,88,101), c(1, 3, 6,8,12,15,18,24,27))

# Perform the imputation
na.approx(zoo_ts)

Your zoo object looks like this:你的动物园 object 看起来像这样:

>  1   3   6   8  12  15  18  24  27 
> 90  78  87  NA  98 100  NA  88 101 

Your imputed series like this afterwards:之后你的推算系列是这样的:

> 1         3         6         8        12        15        18        24        27 
> 90.00000  78.00000  87.00000  90.66667  98.00000 100.00000  96.00000  88.00000 101.00000 

When you have time stamps and the series is only slightly / few seconds off for each time stamp, you could also try to transform the series into a regular time series by mapping your values to the correct regular intervals.当您有时间戳并且每个时间戳的序列仅略微/几秒时,您还可以尝试通过将您的值映射到正确的规则间隔来将该序列转换为常规时间序列。 (only reasonably if the differences are small). (只有在差异很小的情况下才合理)。 By doing this you could also use additional imputation methods eg by the imputeTS package (which only works for regular spaced data).通过这样做,您还可以使用其他插补方法,例如imputeTS package(仅适用于常规间隔数据)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM