下面是我用zoo
写的一些代码 - 我还没有使用xts
,所以我不知道是否可以应用相同的函数。希望有所帮助!
功能
下面的函数来计算,对于原始数据中的每个间隔,即以给定的时间间隔(注重叠分数:在下面所有的代码,变量名ta1
和ta2
指一个给定的时间间隔的开始和结束(例如在每次需要作为输出相等的间隔),而tb1
和tb2
指原始数据的(不相等)的间隔)的开始和结束:
frac.overlap <- function(ta1,ta2,tb1,tb2){
if(tb1 <= ta1 & tb2 >= ta2) { # Interval 2 starts earlier and ends later than interval 1
frac <- as.numeric(difftime(ta2,ta1,units="secs"))/as.numeric(difftime(tb2,tb1,units="secs"))
} else if(tb1 >= ta1 & tb2 <= ta2) { # Interval 2 is fully contained within interval 1
frac <- 1
} else if(tb1 <= ta1 & tb2 >= ta1) { # Interval 2 partly overlaps with interval 1 (starts earlier, ends earlier)
frac <- as.numeric(difftime(tb2,ta1,units="secs"))/as.numeric(difftime(tb2,tb1,units="secs"))
} else if (tb1 <= ta2 & tb2 >= ta2){ # Interval 2 partly overlaps with interval 1 (starts later, ends later)
frac <- as.numeric(difftime(ta2,tb1,units="secs"))/as.numeric(difftime(tb2,tb1,units="secs"))
} else { # No overlap
frac <- 0
}
return(frac)
}
下一个函数来确定与当前考虑的间隔ta1
原始数据集重叠的记录 - ta2
:
check.overlap <- function(ta1,ta2,tb1,tb2){
ov <- vector("logical",4)
ov[1] <- (tb1 <= ta1 & tb2 >= ta2) # Interval 2 starts earlier and ends later than interval 1
ov[2] <- (tb1 >= ta1 & tb2 <= ta2) # Interval 2 is fully contained within interval 1
ov[3] <- (tb1 <= ta1 & tb2 >= ta1) # Interval 2 partly overlaps with interval 1 (starts earlier, ends earlier)
ov[4] <- (tb1 <= ta2 & tb2 >= ta2) # Interval 2 partly overlaps with interval 1 (starts later, ends later)
return(as.logical(sum(ov))) # Gives TRUE if at least one element of ov is TRUE, otherwise FALSE
}
(注:这工作得很好,您所提供的样本数据,但在更大的数据集,我发现它过于缓慢。由于我编写的这段代码是用一个固定的时间步重新采样时间序列,所以我通常使用一个固定的时间间隔来完成这个步骤,这个过程要快得多。可能很容易修改代码(查看下一个函数的代码),以便根据原始数据的间隔加快此步骤。)
下一个函数使用前两个函数来计算间隔的重采样值ta1
- ta2
:
fracres <- function(tstart,interval,input){
# tstart: POSIX object
# interval: length of interval in seconds
# input: zoo object
ta1 <- tstart
ta2 <- tstart + interval
# First, determine which records of the original data (input) overlap with the current
# interval, to avoid going through the whole object at every iteration
ind <- index(input)
ind1 <- index(lag(input,-1))
recs <- which(sapply(1:length(ind),function(x) check.overlap(ta1,ta2,ind[x],ind1[x])))
#recs <- which(abs(as.numeric(difftime(ind,ta1,units="secs"))) < 601)
# For each record overlapping with the current interval, return the fraction of the input data interval contained in the current interval
if(length(recs) > 0){
fracs <- sapply(1:length(recs), function(x) frac.overlap(ta1,ta2,ind[recs[x]],ind1[recs[x]]))
return(sum(coredata(input)[recs]*fracs))
} else {
return(0)
}
}
(被注释掉的行显示如何获取相关记录,如果原来的和新的时间步长之间的最大时间差是已知的)
应用
首先,让我们在您的样本数据读取为zoo
对象:
sample_zoo <- read.zoo(text='
2016-07-01 00:00:20, 0.0
2016-07-01 00:01:20, 60.0
2016-07-01 00:01:50, 30.0
2016-07-01 00:02:30, 40.0
2016-07-01 00:04:20, 110.0
2016-07-01 00:05:30, 140.0
2016-07-01 00:06:00, 97.0
2016-07-01 00:07:12, 144.0
2016-07-01 00:08:09, 0.0
', sep=',', index=1, tz='', format="%Y-%m-%d %H:%M:%S")
它看起来像你的数据集包含瞬时值(“在01:20
的x
值60”)。由于我为汇总值编写了此代码,因此时间戳的含义不同(“起始于01:20
的记录的值为60”)。为了纠正这一点,记录需要转移:
sample_zoo <- lag(sample_zoo,1)
然后,我们定义对应所需的分辨率POSIXct
对象序列:
time.out <- seq.POSIXt(from=as.POSIXct("2016-07-01"),to=(as.POSIXct("2016-07-01")+(60*9)),by="1 min")
然后,我们可以应用功能fracres
,描述以上:
data.out <- sapply(1:length(time.out), function(x) fracres(tstart=time.out[x],interval=60,input=sample_zoo))
索引和数据被组合到一个zoo
对象:
zoo.out <- read.zoo(data.frame(time.out,data.out))
最后,时间序列又一步像以前那样移动,向相反的方向:
zoo.out <- lag(zoo.out,-1)
2016-07-01 00:01:00 2016-07-01 00:02:00 2016-07-01 00:03:00 2016-07-01 00:04:00 2016-07-01 00:05:00 2016-07-01 00:06:00 2016-07-01 00:07:00 2016-07-01 00:08:00 2016-07-01 00:09:00
40 60 60 60 100 157 120 24 0
Thanks @ m.chips!最后在我的实时系列中尝试了这一点。完美的作品,但是,是的,正如你所指出的那样,即使是很短的系列,它也会变得“过于缓慢”。看起来,执行时间与系列的长度不成比例地增长 - 按指数或2^N。我的系列有30万到1万。观察灵感来自你的算法,我决定尝试别的。在下面的帖子中回答问题。 –