PortfolioAnalytics 错误行名称不是日期
PortfolioAnalytics Error row names not Dates
我从投资组合分析包中收到以下错误。
Error in checkData(R) :
The data cannot be converted into a time series. If you are trying to pass in names from a data object with one column, you should use the form 'data[rows, columns, drop = FALSE]'. Rownames should have standard date formats, such as '1985-03-15'.
我使用的数据集是模拟数据
> df
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
[1,] 0 1 0 1 0 0 0 1 1 0
[2,] 0 1 0 0 1 1 1 1 1 1
[3,] 1 0 0 0 0 0 0 1 1 0
[4,] 1 0 1 1 1 0 0 1 0 1
[5,] 0 0 1 0 1 0 1 1 1 0
[6,] 0 1 0 1 0 1 1 0 1 1
[7,] 1 0 0 0 0 1 1 1 1 0
[8,] 0 0 1 1 0 0 1 1 0 1
[9,] 1 0 0 0 0 1 1 1 1 0
[10,] 0 1 1 0 0 1 0 1 0 0
我将投资组合约束设置为
returns = as.matrix(df)
> funds = colnames(df)
> init.portfolio <- portfolio.spec(assets = funds)
> init.portfolio <- add.constraint(portfolio = init.portfolio, type = "full_investment")
> init.portfolio <- add.constraint(portfolio = init.portfolio, type = "long_only")
> minSD.portfolio <- add.objective(portfolio=init.portfolio,
+ type="risk",
+ name="StdDev")
> minSD.opt <- optimize.portfolio(R = df, portfolio = minSD.portfolio,
+ optimize_method = "ROI", trace = TRUE)
Error in checkData(R) :
The data cannot be converted into a time series. If you are trying to pass in names from a data object with one column, you should use the form 'data[rows, columns, drop = FALSE]'. Rownames should have standard date formats, such as '1985-03-15'.
我该如何解决这个错误。 DF是模拟单周期returns。所以它们要么是 100%,要么是 0%,而且是在同一时期。如果需要,我可以添加一个日期变量作为行名,但我不知道如何添加。我试过了
> rownames(df) = as.Date(c("Jan", rep(nrow(df))))
Error in charToDate(x) :
character string is not in a standard unambiguous format
有人可以帮我解决这个错误吗?谢谢
您需要将日期数据添加到 df。假设数据是从今年年初开始的每月 returns,您可以使用
添加行名
rownames(df) <- as.character(seq(as.Date("2015-01-01"), length.out=nrow(df), by = "month"))
或通过
将 df 转换为 xts 时间序列
library(xts)
df <- xts(df, order.by = seq(as.Date("2015-01-01"),
length.out=nrow(df), by = "month"), df)
xts
是金融时间序列的常用格式,与 PortfolioAnalytics 配合使用效果很好,因此您可以考虑使用它。
完成后 运行 optimize.portfolio,您将无法找到解决方案。看起来 df 不是正定的,因此您必须调整 df 中的值。
另外,我不太理解您的评论,即单个句点 returns 要求 returns 为 0 或 1。这通常不是真的。
我从投资组合分析包中收到以下错误。
Error in checkData(R) :
The data cannot be converted into a time series. If you are trying to pass in names from a data object with one column, you should use the form 'data[rows, columns, drop = FALSE]'. Rownames should have standard date formats, such as '1985-03-15'.
我使用的数据集是模拟数据
> df
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
[1,] 0 1 0 1 0 0 0 1 1 0
[2,] 0 1 0 0 1 1 1 1 1 1
[3,] 1 0 0 0 0 0 0 1 1 0
[4,] 1 0 1 1 1 0 0 1 0 1
[5,] 0 0 1 0 1 0 1 1 1 0
[6,] 0 1 0 1 0 1 1 0 1 1
[7,] 1 0 0 0 0 1 1 1 1 0
[8,] 0 0 1 1 0 0 1 1 0 1
[9,] 1 0 0 0 0 1 1 1 1 0
[10,] 0 1 1 0 0 1 0 1 0 0
我将投资组合约束设置为
returns = as.matrix(df)
> funds = colnames(df)
> init.portfolio <- portfolio.spec(assets = funds)
> init.portfolio <- add.constraint(portfolio = init.portfolio, type = "full_investment")
> init.portfolio <- add.constraint(portfolio = init.portfolio, type = "long_only")
> minSD.portfolio <- add.objective(portfolio=init.portfolio,
+ type="risk",
+ name="StdDev")
> minSD.opt <- optimize.portfolio(R = df, portfolio = minSD.portfolio,
+ optimize_method = "ROI", trace = TRUE)
Error in checkData(R) :
The data cannot be converted into a time series. If you are trying to pass in names from a data object with one column, you should use the form 'data[rows, columns, drop = FALSE]'. Rownames should have standard date formats, such as '1985-03-15'.
我该如何解决这个错误。 DF是模拟单周期returns。所以它们要么是 100%,要么是 0%,而且是在同一时期。如果需要,我可以添加一个日期变量作为行名,但我不知道如何添加。我试过了
> rownames(df) = as.Date(c("Jan", rep(nrow(df))))
Error in charToDate(x) :
character string is not in a standard unambiguous format
有人可以帮我解决这个错误吗?谢谢
您需要将日期数据添加到 df。假设数据是从今年年初开始的每月 returns,您可以使用
添加行名rownames(df) <- as.character(seq(as.Date("2015-01-01"), length.out=nrow(df), by = "month"))
或通过
将 df 转换为 xts 时间序列library(xts)
df <- xts(df, order.by = seq(as.Date("2015-01-01"),
length.out=nrow(df), by = "month"), df)
xts
是金融时间序列的常用格式,与 PortfolioAnalytics 配合使用效果很好,因此您可以考虑使用它。
完成后 运行 optimize.portfolio,您将无法找到解决方案。看起来 df 不是正定的,因此您必须调整 df 中的值。
另外,我不太理解您的评论,即单个句点 returns 要求 returns 为 0 或 1。这通常不是真的。