簡體   English   中英

R中的子集xts時間序列對象

[英]Subset xts time-series object in R

我有像這樣某些月份的時間序列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進行子集化! 鏈接上找到了一個相關的問題,但不足以解決這個問題。

更新預期輸出顯示為

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

這是一個一般很難解決的問題,因此您需要推出自己的解決方案。 最簡單的方法是通過重疊 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)

我們可以將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)

基於對“預期輸出”問題的更新和 RS 的評論:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM