使用“to.weekly”错误的周末结束日期“xts”中的函数包裹

发布于 2024-12-03 20:01:49 字数 1550 浏览 0 评论 0原文

我有一个非常奇怪的问题...我正在使用 to.weeklyto.period 函数将每日 xts 对象转换为每周数据。在大多数情况下,我得到的周末日期为星期五(day.of.week 函数将返回 5)(例如 "2010-01-08",< code>“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]

I have a really odd issue... I am using the to.weekly and to.period function to convert a daily xts object to weekly data. In most instances, I get the week-ending date as a Friday (day.of.week function will return 5) (e.g. "2010-01-08", "2011-02-11"), but there are a few cases where I get something other than Friday (Saturday/Sunday/Thursday/etc.)

I have tried to.weekly and to.period(x, period = 'weeks') and both return the same problem.

Why is this happening? Is there a work-around for this??

Thanks!!

[EDIT: EXAMPLE BELOW]

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)}

This returns the date(s) that are not Friday: time(to.weekly(test.xts))[dayofweek(time(to.weekly(test.xts))) != 5]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沫离伤花 2024-12-10 20:01:49

您的示例有两个问题:

  1. 您的 dayofweek 函数有点麻烦,而且结果可能不正确。
  2. 您的示例日期缺少一些日期,例如 05-23-2010。

这是代码的清理版本:

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"]

You have 2 problems with your example:

  1. Your dayofweek function is a bit cumbersome, and probably incorrect in its results.
  2. Your example dates is missing some dates, such as 05-23-2010.

Here is a cleaned-up version of your code:

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"]

The only result of this function is

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

The problem of course, is that this week ends on 05-23-2010, but that date is not present in the time series. Therefore, to.weekly uses the next closest date as the end point, which is 05-22-2010. This is the source of your problem.

Here is a better example, which reveals no issue with the to.weekly function.

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"]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文