简体   繁体   English

R上/下R中缺少的时间序列数据

[英]Ramp up/down missing time-series data in R

I have a set of time-series data (GPS speed data, specifically), which includes gaps of missing values where the signal was lost. 我有一组时间序列数据(特别是GPS速度数据),其中包括丢失信号的丢失值的间隙。 For missing periods of short durations I am about to fill simply using a na.spline, however this is inappropriate with longer time periods. 对于短时间的缺失时段,我将仅使用na.spline进行填充,但是这对于较长的时间段是不合适的。 I would like to ramp the values from the last true value down to zero, based on predefined acceleration limits. 我想根据预定义的加速度限制将值从最后一个真实值降低到零。

#create sample data frame
test <- as.data.frame(c(6,5.7,5.4,5.14,4.89,4.64,4.41,4.19,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,5,5.1,5.3,5.4,5.5))
names(test)[1] <- "speed"

#set rate of acceleration for ramp
ramp <- 6

#set sampling rate of receiver
Hz <- 1/10

So for missing data the ramp would use the previous value and the rate of acceleration to get the next data point, until speed reached zero (ie last speed [4.19] + (Hz * ramp)), yielding the following values: 因此,对于丢失的数据,斜坡将使用先前的值和加速度来获得下一个数据点,直到速度达到零(即最后速度[4.19] +(Hz *斜坡)),得出以下值:

3.59
2.99
2.39
1.79
1.19
0.59
0

Lastly, I need to do this in the reverse fashion, to ramp up from zero when the signal picks back up again. 最后,我需要以相反的方式执行此操作,以在信号再次变回零时从零开始上升。

Hope this is clear. 希望这很清楚。

Cheers 干杯

It's not really elegant, but you can do it in a loop. 它不是很优雅,但是您可以循环执行。

na.pos <- which(is.na(test$speed))

acc = FALSE
for (i in na.pos) {
    if (acc) {
        speed <- test$speed[i-1]+(Hz*ramp)
    }
    else {
        speed <- test$speed[i-1]-(Hz*ramp)
        if (round(speed,1) < 0) {
            acc <- TRUE
            speed <- test$speed[i-1]+(Hz*ramp)
        }

    }
    test[i,] <- speed
}

The result is: 结果是:

   speed
1   6.00
2   5.70
3   5.40
4   5.14
5   4.89
6   4.64
7   4.41
8   4.19
9   3.59
10  2.99
11  2.39
12  1.79
13  1.19
14  0.59
15 -0.01
16  0.59
17  1.19
18  1.79
19  2.39
20  2.99
21  3.59
22  4.19
23  4.79
24  5.00
25  5.10
26  5.30
27  5.40
28  5.50

Note that '-0.01', because 0.59-(6*10) is -0.01, not 0. You can round it later, I decided not to. 请注意,“-0.01”是因为0.59-(6 * 10)是-0.01,而不是0。您可以稍后对其进行舍入,我决定不这样做。

When the question says "ramp the values from the last true value down to zero" in each run of NAs I assume that that means that any remaining NAs in the run after reaching zero are also to be replaced by zero. 当问题在每次运行的NA中都说“将值从最后一个真实值降低到零”时,我认为那意味着在达到零后,运行中任何剩余的NA也将被零替换。

Now, use rleid from data.table to create a grouping vector the same length as test$speed identifying each run in is.na(test$speed) and use ave to create sequence numbers within such groups, seqno . 现在,使用rleid的rleid创建一个长度与test$speed相同的分组矢量, test$speed标识is.na(test$speed)每个运行,并使用ave在此类组中创建序列号seqno Then calculate the declining sequences, ramp_down by combining na.locf(test$speed) and seqno . 然后通过组合na.locf(test$speed)seqno来计算下降序列ramp_down Finally replace the NAs. 最后更换NA。

library(data.table)
library(zoo)

test_speed <- test$speed
seqno <- ave(test_speed, rleid(is.na(test_speed)), FUN = seq_along)
ramp_down <- pmax(na.locf(test_speed) - seqno * ramp * Hz, 0)
result <- ifelse(is.na(test_speed), ramp_down, test_speed)

giving: 给予:

> result     
 [1] 6.00 5.70 5.40 5.14 4.89 4.64 4.41 4.19 3.59 2.99 2.39 1.79 1.19 0.59 0.00
[16] 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 5.00 5.10 5.30 5.40 5.50

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

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