子集x中的时间序列对象

时间:2016-04-02 09:51:14

标签: r time-series xts

我有像这样的特定月份的时间序列xts对象

library(xts)
  seq<- seq(as.POSIXct("2015-09-01"),as.POSIXct("2015-09-04"), by = "30 mins")
  ob<- xts(data.frame(power=1:(length(seq))),seq)

现在,对应每个观察(比如说A),我想计算过去两小时观察的平均值。因此,对应于每个观察(A),我需要计算在A两小时之前发生的观察指数,比如B。然后我可以计算AB之间观测值的平均值。因此,

i=10 # dummy
ind_cur<- index(ob[i,]) # index of current observation
ind_back <- ind_cur - 3600 * 2 # index of 2 hours back observation

使用这些索引,我将ob作为

的子集
 ob['ind_cur/ind_back']

导致以下错误:

Error in if (length(c(year, month, day, hour, min, sec)) == 6 && c(year,  : 
  missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In as_numeric(YYYY) : NAs introduced by coercion
2: In as_numeric(MM) : NAs introduced by coercion
3: In as_numeric(DD) : NAs introduced by coercion
4: In as_numeric(YYYY) : NAs introduced by coercion
5: In as_numeric(MM) : NAs introduced by coercion
6: In as_numeric(DD) : NAs introduced by coercion

任何人都可以帮我分组ob!在link找到了相关问题,但还不足以解决此问题。

更新预期输出显示为

2015-09-01 00:00:00     1   NA # as I don't have previous data
2015-09-01 00:30:00     2   NA
2015-09-01 01:00:00     3   NA
2015-09-01 01:30:00     4   NA
2015-09-01 02:00:00     5   10/4 # mean of prevous 4 observations (last two hours)
2015-09-01 02:30:00     6   14/4  
2015-09-01 03:00:00     7   18/4

4 个答案:

答案 0 :(得分:2)

这是一个难以解决的问题,因此您需要推出自己的解决方案。最简单的方法是通过重叠2小时的间隔来使用window进行子集化。

# initialize a result object
ob2 <- ob * NA_real_
# loop over all rows and calculate 2-hour mean
for(i in 2:nrow(ob)) {
  ix <- index(ob)[i]
  ob2[i] <- mean(window(ob, start=ix-3600*2, end=ix))
}
# set incomplete 2-hour intervals to NA
is.na(ob2) <- which(index(ob2) < start(ob2)+3600*2)

答案 1 :(得分:1)

我们可以将rollapply()包与lag()结合使用,将生成的滚动mean偏移一行。

rollapply(lag(ob), 4, mean)
#                    power
#2015-09-01 00:00:00    NA
#2015-09-01 00:30:00    NA
#2015-09-01 01:00:00    NA
#2015-09-01 01:30:00    NA
#2015-09-01 02:00:00   2.5
#2015-09-01 02:30:00   3.5
#2015-09-01 03:00:00   4.5

# Or if you want it as new variable in your xts object
ob$mean <- rollapply(lag(ob),4,mean)

答案 2 :(得分:0)

基于对问题的更新&#34;预期输出&#34;以及R.S。的评论:

library(TTR)
head(SMA(ob$power, 4))  # 2 hour moving average

结果

                    SMA
2015-09-01 00:00:00  NA
2015-09-01 00:30:00  NA
2015-09-01 01:00:00  NA
2015-09-01 01:30:00 2.5
2015-09-01 02:00:00 3.5
2015-09-01 02:30:00 4.5

这假设有问题的30分钟间隔。

看起来更像预期输出:

lag(head(SMA(ob$power, 4),7))

                    SMA
2015-09-01 00:00:00  NA
2015-09-01 00:30:00  NA
2015-09-01 01:00:00  NA
2015-09-01 01:30:00  NA
2015-09-01 02:00:00 2.5
2015-09-01 02:30:00 3.5
2015-09-01 03:00:00 4.5

答案 3 :(得分:0)

data.table提供了滚动功能,可用于单个以及多个时间序列:

head(

    as.data.table(ob)[, roll_power := frollmean(power, 4, align = 'right')]
)

# at the end of a 4 1/2 hour lag

                 index power roll_power
1: 2015-09-01 00:00:00     1         NA
2: 2015-09-01 00:30:00     2         NA
3: 2015-09-01 01:00:00     3         NA
4: 2015-09-01 01:30:00     4        2.5 # the rolling mean covers this, and preceding rows
5: 2015-09-01 02:00:00     5        3.5
6: 2015-09-01 02:30:00     6        4.5