使用 Return.Calculate 函数复合 returns 的价格
Price to compounded returns using the Return.Calculate Function
我有以下样本数据,并试图根据以下价格计算复合 returns。
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))
我这样使用 Return.calculate 包 :
Return.calculate(sample_data$Price, method = "compound")
但出现以下错误:
no applicable method for 'xtsAttributes<-' applied to an object of
class "zoo"
您需要将数据作为 xts
对象传递。转换为 xts
以仅包含数字变量时要小心 - reason here - 这就是为什么可以仅转换 sample_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
我有以下样本数据,并试图根据以下价格计算复合 returns。
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))
我这样使用 Return.calculate 包 :
Return.calculate(sample_data$Price, method = "compound")
但出现以下错误:
no applicable method for 'xtsAttributes<-' applied to an object of class "zoo"
您需要将数据作为 xts
对象传递。转换为 xts
以仅包含数字变量时要小心 - reason here - 这就是为什么可以仅转换 sample_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