将 tibble 转换为 xts 以使用 Performanceanalytics 包进行分析

发布于 2025-01-14 11:33:20 字数 910 浏览 5 评论 0原文

我有一个带有日期和返回列的 tibble,如下所示:

> head(return_series)
# A tibble: 6 x 2
  date      return
  <chr>      <dbl>
1 2002-01  0.0292 
2 2002-02  0.0439 
3 2002-03  0.0240 
4 2002-04  0.00585
5 2002-05 -0.0169 
6 2002-06 -0.0686 

我首先使用以下代码将日期添加到日期列:

return_series$date <- as.Date(as.yearmon(return_series$date))

# A tibble: 6 x 2
  date         return
  <date>        <dbl>
1 2002-01-01  0.0292 
2 2002-02-01  0.0439 
3 2002-03-01  0.0240 
4 2002-04-01  0.00585
5 2002-05-01 -0.0169 
6 2002-06-01 -0.0686 

我的目标是将 return_series tibble 转换为 xts 数据,以使用 PerformanceAnalytics 进行进一步分析包裹。但是,当我使用命令 as.xts 时,我收到以下错误:

Error in as.POSIXlt.character(x, tz, ...) : 
  character string is not in a standard unambiguous format

如何将格式更改为 xts,或者是否有其他可能使用 PerformanceAnalytics 包而不是转换为 xts?

非常感谢您的帮助!

I have a tibble with a date and return column, that looks as follows:

> head(return_series)
# A tibble: 6 x 2
  date      return
  <chr>      <dbl>
1 2002-01  0.0292 
2 2002-02  0.0439 
3 2002-03  0.0240 
4 2002-04  0.00585
5 2002-05 -0.0169 
6 2002-06 -0.0686 

I first add the day to the date column with the following code:

return_series$date <- as.Date(as.yearmon(return_series$date))

# A tibble: 6 x 2
  date         return
  <date>        <dbl>
1 2002-01-01  0.0292 
2 2002-02-01  0.0439 
3 2002-03-01  0.0240 
4 2002-04-01  0.00585
5 2002-05-01 -0.0169 
6 2002-06-01 -0.0686 

My goal is to convert the return_series tibble to xts data to use it for further analysis with the PerformanceAnalytics package. But when I use the command as.xts I receive the following error:

Error in as.POSIXlt.character(x, tz, ...) : 
  character string is not in a standard unambiguous format

How can I change the format to xts or is there an other possibility to work with the PerformanceAnalytics package instead of converting to xts?

Thank you very much for your help!

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

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

发布评论

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

评论(1

生来就爱笑 2025-01-21 11:33:20

您需要更仔细地遵循 xts 文档:

> tb <- as_tibble(data.frame(date=as.Date("2002-01-01") + (0:5)*30, 
+                            return=rnorm(6)))
> tb
# A tibble: 6 × 2
  date       return
  <date>      <dbl>
1 2002-01-01  0.223
2 2002-01-31 -0.352
3 2002-03-02  0.149
4 2002-04-01  1.42 
5 2002-05-01 -1.04 
6 2002-05-31  0.507
> 
> x <- xts(tb[,-1], order.by=as.POSIXct(tb[[1]]))
> x
                       return
2001-12-31 18:00:00  0.222619
2002-01-30 18:00:00 -0.352288
2002-03-01 18:00:00  0.149319
2002-03-31 18:00:00  1.421967
2002-04-30 19:00:00 -1.035087
2002-05-30 19:00:00  0.507046
> 

xts 对象更喜欢 POSIXct 日期时间对象,您可以从 Date 转换它对象。对于(密切相关的)zoo 对象,您可以保留Date

You need to follow the xts documentation more closely:

> tb <- as_tibble(data.frame(date=as.Date("2002-01-01") + (0:5)*30, 
+                            return=rnorm(6)))
> tb
# A tibble: 6 × 2
  date       return
  <date>      <dbl>
1 2002-01-01  0.223
2 2002-01-31 -0.352
3 2002-03-02  0.149
4 2002-04-01  1.42 
5 2002-05-01 -1.04 
6 2002-05-31  0.507
> 
> x <- xts(tb[,-1], order.by=as.POSIXct(tb[[1]]))
> x
                       return
2001-12-31 18:00:00  0.222619
2002-01-30 18:00:00 -0.352288
2002-03-01 18:00:00  0.149319
2002-03-31 18:00:00  1.421967
2002-04-30 19:00:00 -1.035087
2002-05-30 19:00:00  0.507046
> 

An xts object prefers a POSIXct datetime object, which you can convert from a Date object. For a (closely-related) zoo object you could keep Date.

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