将 tibble 转换为 xts 以使用 Performanceanalytics 包进行分析
我有一个带有日期和返回列的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要更仔细地遵循
xts
文档:xts
对象更喜欢POSIXct
日期时间对象,您可以从Date 转换它对象。对于(密切相关的)
zoo
对象,您可以保留Date
。You need to follow the
xts
documentation more closely:An
xts
object prefers aPOSIXct
datetime object, which you can convert from aDate
object. For a (closely-related)zoo
object you could keepDate
.