「XTS」パッケージで「to.weekly」機能を使用した間違った週末の日付

StackOverflow https://stackoverflow.com/questions/7366011

  •  28-10-2019
  •  | 
  •  

質問

私は本当に奇妙な問題を抱えています...私は to.weeklyto.period 毎日変換する機能 xts 毎週のデータに反対します。ほとんどの場合、私は金曜日として週末に日付を取得します(day.of.week 関数は5を返します)(例 "2010-01-08", "2011-02-11")、しかし、金曜日(土曜日/日曜日/木曜日など)以外の何かを手に入れるいくつかのケースがあります。

私が試してみました to.weeklyto.period(x, period = 'weeks') どちらも同じ問題を返します。

なぜこうなった?これの回避策はありますか?

ありがとう!!

編集:以下の例

test.dates <- as.Date(c("2010-04-27","2010-04-28","2010-04-29","2010-04-30","2010-05-03","2010-05-04","2010-05-05","2010-05-06","2010-05-07","2010-05-10","2010-05-11","2010-05-12","2010-05-13","2010-05-14","2010-05-17","2010-05-18","2010-05-19","2010-05-20","2010-05-21","2010-05-22","2010-05-24","2010-05-25","2010-05-26","2010-05-27","2010-05-28","2010-06-01","2010-06-02","2010-06-03","2010-06-04"))

test.data <- rnorm(length(test.dates),mean=1,sd=2)

test.xts <- xts(x=test.data,order.by=test.dates)

#Function that takes in a vector of zoo/xts objects (e.g. "2010-01-08") and returns the day of the week for each
dayofweek <- function(x) {
 placeholder <- vector("list",length=length(x))
 names(placeholder) <- x

 for(i in 1:length(x)) {placeholder[[i]] <- month.day.year(x[i])}
 placeholder2 <- rep(NA,times=length(x))

 for(i in 1:length(x)) {placeholder2[i] <- day.of.week(placeholder[[i]][[1]],placeholder[[i]][[2]],placeholder[[i]][[3]])}
 return(placeholder2)}

これは金曜日ではない日付を返します: time(to.weekly(test.xts))[dayofweek(time(to.weekly(test.xts))) != 5]

役に立ちましたか?

解決

例には2つの問題があります。

  1. 君の dayofweek 機能は少し面倒で、おそらくその結果が間違っています。
  2. あなたの例の日付には、2010年5月5日などの日付がありません。

これがあなたのコードのクリーンアップバージョンです:

library(xts)
test.dates <- as.Date(c("2010-04-27","2010-04-28","2010-04-29","2010-04-30","2010-05-03","2010-05-04","2010-05-05","2010-05-06","2010-05-07","2010-05-10","2010-05-11","2010-05-12","2010-05-13","2010-05-14","2010-05-17","2010-05-18","2010-05-19","2010-05-20","2010-05-21","2010-05-22","2010-05-24","2010-05-25","2010-05-26","2010-05-27","2010-05-28","2010-06-01","2010-06-02","2010-06-03","2010-06-04"))
test.data <- rnorm(length(test.dates),mean=1,sd=2)
test.xts <- xts(x=test.data,order.by=test.dates)
test.weekly <- to.weekly(test.xts)

library(lubridate)
test.weekly[wday(test.weekly, label = TRUE, abbr = TRUE) != "Fri"]

この関数の唯一の結果は次のとおりです

           test.xts.Open test.xts.High test.xts.Low test.xts.Close
2010-05-22     -1.705749      1.273982    -2.084203      -1.502611

もちろん問題は、今週が終わることです 05-23-2010, 、しかし、その日付は時系列に存在しません。したがって、 to.weekly 次の最も近い日付をエンドポイントとして使用します。 05-22-2010. 。これがあなたの問題の原因です。

これがより良い例です。 to.weekly 関数。

library(lubridate); library(xts)   
test.dates <- seq(as.Date("1900-01-01"),as.Date("2011-10-01"),by='days')
test.dates <- test.dates[wday(test.dates)!=1 & wday(test.dates)!=7] #Remove weekends
test.data <- rnorm(length(test.dates),mean=1,sd=2)
test.xts <- xts(x=test.data,order.by=test.dates)
test.weekly <- to.weekly(test.xts)
test.weekly[wday(test.weekly, label = TRUE, abbr = TRUE) != "Fri"]
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top