【发布时间】:2015-12-12 07:42:14
【问题描述】:
我从投资组合分析包中收到以下错误。
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 是单期收益的模拟。所以它们都是 100% 或 0%,并且在同一时期。如果需要,我可以添加一个日期变量作为行名,但我不知道如何。我试过了
> rownames(df) = as.Date(c("Jan", rep(nrow(df))))
Error in charToDate(x) :
character string is not in a standard unambiguous format
有人可以帮我解决这个错误吗?谢谢
【问题讨论】:
标签: r optimization portfolio