使用Return.Calculate函数将价格转换为复合收益

使用Return.Calculate函数将价格转换为复合收益,r,xts,zoo,performanceanalytics,R,Xts,Zoo,Performanceanalytics,我有以下样本数据,并试图从以下价格计算复合回报 sample_data <- data.frame(Date = c ("2017-01-31", "2017-02-28", "2017-03-31", "2017-04-30", "2017-05-31", "2017-06-30"), stock = c("a", "a", "a","a", "a", "a"),

我有以下样本数据,并试图从以下价格计算复合回报

sample_data <- data.frame(Date = c ("2017-01-31", "2017-02-28", "2017-03-31", 
                                "2017-04-30", "2017-05-31", "2017-06-30"), 
                      stock = c("a", "a", "a","a", "a", "a"), 
                      Price = c(10, 11, 17, 12, 13, 14))
但是得到以下错误:


没有适用于“xtsAttributes”的方法您需要将数据作为
xts
对象传递。转换到
xts
时要小心,因为它只包含数值变量——这就是为什么可以只转换
sample\u data$Price

library(xts)
library(PerformanceAnalytics)
sample_data$Date <- as.Date(sample_data$Date)
sample_data_xts <- xts(sample_data$Price, order.by = sample_data$Date) 

Return.calculate(sample_data_xts, method = "compound")

#                   [,1]
# 2017-01-31          NA
# 2017-02-28  0.09531018
# 2017-03-31  0.43531807
# 2017-04-30 -0.34830669
# 2017-05-31  0.08004271
# 2017-06-30  0.07410797
库(xts)
库(性能分析)

示例_data$Date您需要将数据作为
xts
对象传递。转换到
xts
时要小心,因为它只包含数值变量——这就是为什么可以只转换
sample\u data$Price

library(xts)
library(PerformanceAnalytics)
sample_data$Date <- as.Date(sample_data$Date)
sample_data_xts <- xts(sample_data$Price, order.by = sample_data$Date) 

Return.calculate(sample_data_xts, method = "compound")

#                   [,1]
# 2017-01-31          NA
# 2017-02-28  0.09531018
# 2017-03-31  0.43531807
# 2017-04-30 -0.34830669
# 2017-05-31  0.08004271
# 2017-06-30  0.07410797
库(xts)
库(性能分析)
示例数据$Date