2

データへのリンク: http://dl.dropbox.com/u/56075871/data.txt

その時間の各観測値を平均で割りたいと思います。例:

2012-01-02 10:00:00     5.23
2012-01-03 10:00:00     5.28
2012-01-04 10:00:00     5.29
2012-01-05 10:00:00     5.29
2012-01-09 10:00:00     5.28
2012-01-10 10:00:00     5.33
2012-01-11 10:00:00     5.42
2012-01-12 10:00:00     5.55
2012-01-13 10:00:00     5.68
2012-01-16 10:00:00     5.53

その平均は 5.388 です。次に、各観測値をその平均で割りたいので、... 5.23/5.388、5.28/5.388、... 最後まで 5.53/5.388

私は10株の時系列を1時間ごとに持っています:

                        S1.1h   S2.1h     S3.1h   S4.1h  S5.1h       S6.1h    S7.1h  S8.1h      S9.1h       S10.1h
2012-01-02 10:00:00     64.00   110.7     5.23    142.0  20.75       34.12    32.53  311.9      7.82        5.31
2012-01-02 11:00:00     64.00   110.8     5.30    143.2  20.90       34.27    32.81  312.0      7.97        5.34
2012-01-02 12:00:00     64.00   111.1     5.30    142.8  20.90       34.28    32.70  312.4      7.98        5.33
2012-01-02 13:00:00     61.45   114.7     5.30    143.1  21.01       34.35    32.85  313.0      7.96        5.35
2012-01-02 14:00:00     61.45   116.2     5.26    143.7  21.10       34.60    32.99  312.9      7.95        5.36
2012-01-02 15:00:00     63.95   116.2     5.26    143.2  21.26       34.72    33.00  312.6      7.99        5.37
2012-01-02 16:00:00     63.95   117.3     5.25    143.3  21.27       35.08    33.04  312.7      7.99        5.36
2012-01-02 17:00:00     63.95   117.8     5.24    144.7  21.25       35.40    33.10  313.6      7.99        5.40
2012-01-02 18:00:00     63.95   117.9     5.23    145.0  21.20       35.50    33.17  312.5      7.98        5.35
2012-01-03 10:00:00     63.95   115.5     5.28    143.5  21.15       35.31    33.05  311.7      7.94        5.37
...

そして、各観測値を時間の平均(定期的)で割りたいと思います。いくつかのコードがあります。意味を作るコード:

#10:00:00, 11:00:00, ... 18:00:00
times <- paste(seq(10, 18),":00:00", sep="")
#means - matrix of means for timeseries and hour
means <- matrix(ncol= ncol(time_series), nrow = length(times))
for (t in 1:length(times)) {
  #t is time 10 to 18
  for(i in 1:ncol(time_series)) {
    #i is stock 1 to 10
    # hour mean for each observation in data
    means[t,i] <- mean(time_series[grep(times[t], index(time_series)), i])
  }
}

そして、「物事を成し遂げる」ための私の機能:

for (t in 1:length(times)) {
  # get all dates with times[t] hour
  hours <- time_series[grep(times[t], index(time_series))]
  ep <- endpoints(hours, "hours")

  out <- rbind(out, period.apply(hours, INDEX=ep, FUN=function(x) {
    x/means[t,]
  }))
}

私はこれがひどいことを知っていますが、うまくいきます。コードを単純化するにはどうすればよいですか?

4

2 に答える 2

3

これを行う1つの方法は次のとおりです。

# Split the xts object into chunks by hour
# .indexhour() returns the hourly portion for each timestamp
s <- split(time_series, .indexhour(time_series))
# Use sweep to divide each value of x by colMeans(x) for each group of hours
l <- lapply(s, function(x) sweep(x, 2, colMeans(x), FUN="/"))
# rbind everything back together
r <- do.call(rbind, l)
于 2012-12-14T21:21:22.393 に答える
0

scale関数はそれを行うことができます。一緒に使用すると、ave数時間以内の計算に制限できます。その xts/zoo オブジェクトの結果を投稿dputすると、迅速な返信が得られます。

于 2012-12-14T20:36:35.983 に答える