2013-07-02 67 views
0

如何計算例如5分鐘的xts對象的時間加權平均值: 我知道weighted.mean(),但不知道如何處理日期和時間。 感謝xts或動物園中的時間加權平均值

library(xts) 
    structure(c(28.2, 28.2, 28.2, 28.2, 28.1, 28.1, 28.1, 28.1, 28.1, 
28.2, 28.3, 28.2, 28.2, 28.1, 28.1, 28.1, 28.1, 28.1, 28, 28, 
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 27.9, 27.9, 27.9, 27.9, 
27.9, 27.9, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 
27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 
27.7, 27.7, 27.7, 27.8), class = c("xts", "zoo"), .indexCLASS = c("POSIXct", 
"POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "", index = structure(c(1356998400, 
1356998460, 1356998520, 1356998580, 1356998640, 1356998700, 1356998760, 
1356998880, 1356998940, 1356999000, 1356999060, 1356999120, 1356999180, 
1356999240, 1356999300, 1356999360, 1356999420, 1356999480, 1356999540, 
1356999600, 1356999660, 1356999720, 1356999780, 1356999840, 1356999900, 
1356999960, 1357000020, 1357000080, 1357000140, 1357000200, 1357000260, 
1357000320, 1357000380, 1357000440, 1357000500, 1357000560, 1357000620, 
1357000680, 1357000740, 1357000800, 1357000860, 1357000920, 1357000980, 
1357001040, 1357001100, 1357001160, 1357001220, 1357001280, 1357001340, 
1357001400, 1357001460, 1357001520, 1357001580, 1357001640, 1357001700, 
1357001760, 1357001820, 1357001880, 1357001940, 1357002000), tzone = "", tclass = c("POSIXct", 
"POSIXt")), .Dim = c(60L, 1L)) 
+1

這並不比你刪除的問題好多少。你提供了一些樣本數據很好,但是輸出應該是什麼?你的數據是有規律的,所以常規平均值和時間加權平均值是相同的。目前還不清楚你在問什麼。 –

+0

對不起,我錯過了刪除之間的一個項目,現在它不再經常 - 這只是一個例子,有時5個值在1小時內丟失。 –

回答

2

我還是不知道你要問什麼,但下面的代碼將計算在每個非重疊的10分鐘間隔的時間加權平均。除第一個和第八個觀測值之外,所有行(除第一個外)都與常規平均值相同,因爲您的數據每分鐘發生一次。

x <- structure(c(28.2, 28.2, 28.2, 28.2, 28.1, 28.1, 28.1, 28.1, 28.1, 28.2, 
    28.3, 28.2, 28.2, 28.1, 28.1, 28.1, 28.1, 28.1, 28, 28, 28, 28, 28, 28, 28, 
    28, 28, 28, 28, 28, 27.9, 27.9, 27.9, 27.9, 27.9, 27.9, 27.8, 27.8, 27.8, 
    27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 
    27.8, 27.8, 27.8, 27.8, 27.7, 27.7, 27.7, 27.8), class = c("xts", "zoo"), 
    .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct", "POSIXt"), 
    .indexTZ = "", tzone = "", index = structure(c(1356998400, 1356998460, 
    1356998520, 1356998580, 1356998640, 1356998700, 1356998760, 1356998880, 
    1356998940, 1356999000, 1356999060, 1356999120, 1356999180, 1356999240, 
    1356999300, 1356999360, 1356999420, 1356999480, 1356999540, 1356999600, 
    1356999660, 1356999720, 1356999780, 1356999840, 1356999900, 1356999960, 
    1357000020, 1357000080, 1357000140, 1357000200, 1357000260, 1357000320, 
    1357000380, 1357000440, 1357000500, 1357000560, 1357000620, 1357000680, 
    1357000740, 1357000800, 1357000860, 1357000920, 1357000980, 1357001040, 
    1357001100, 1357001160, 1357001220, 1357001280, 1357001340, 1357001400, 
    1357001460, 1357001520, 1357001580, 1357001640, 1357001700, 1357001760, 
    1357001820, 1357001880, 1357001940, 1357002000), tzone = "", 
    tclass = c("POSIXct", "POSIXt")), .Dim = c(60L, 1L)) 
y <- cbind(x, c(0,diff(.index(x)))) 
f <- function(z) weighted.mean(z[,1],z[,2]) 
period.apply(y, endpoints(y, "minutes", 10), f) 
#       [,1] 
# 2012-12-31 18:09:00 28.13333 
# 2012-12-31 18:19:00 28.14000 
# 2012-12-31 18:29:00 28.00000 
# 2012-12-31 18:39:00 27.88000 
# 2012-12-31 18:49:00 27.80000 
# 2012-12-31 18:59:00 27.77000 
# 2012-12-31 19:00:00 27.80000 
+0

如果我應用你的代碼,並用5代替10分鐘,我會得到相同的結果......這純粹是巧合嗎? – dickoa

+0

@dickoa:不一樣。第一行是不同的,正如我在我的回答中解釋的那樣。 –

+0

+1我現在看到,我明白了,所以我會刪除我的答案。謝謝 – dickoa