【发布时间】:2015-06-24 10:12:57
【问题描述】:
我有一个自 2012 年以来的股票回报时间序列对象。portfolioFrontier 函数可以很好地创建我的效率边界,直到我将数据集扩展到月数之外。例如,我正在查看 39 个月,38 只股票一切正常。但是,一旦我添加了第 39 支股票(和第 40 支等),就会出现错误。代码和错误如下。
输入:
returns <- new("timeSeries", .Data = structure(c(-0.0108, 0.005, -0.0059, 0.0067, -0.0069, -0.0275,
0.0226, 0.0089, 0.0469, 0.0193, -5e-04, -0.03, -0.0483, 0.0251,
0.013, 0.0202, 0.019, 0.015, 0.0091, 0.002, -0.014, 0.0046, 0.002,
0.025, 0.008, 0.0177, -0.0023, 0.0023, -0.0217, 0.0045, 0.0117,
0.0081, 0.0345, 0.0332, 0.0098, 0.0086, -0.0362, -0.0137, 0.0162,
0.0276, 0.0749, 0.0691, 0.0275, -0.0278, -0.0442, 0.0226, -0.0136,
0.0305, 0.0178, 0.0061, 0.0034, 0.0039, -0.0042, -0.0017, 0.0039,
0.0083, -0.008, -0.0044, 7e-04, 0.0018, 0.008, -0.0057, 0.0044,
0.0043, 0.012, 0.0134, 0.003, 0.0078, -0.0092, 0.0113, 0.0132,
0.0168, 0.032, 0.037, 0.033, 0.017, -0.037, 0.027, 0.026, 0.018
), .Dim = c(8L, 10L), .Dimnames = list(NULL, c("Stock.A", "Stock.B",
"Stock.C", "Stock.D", "Stock.E", "Stock.F", "Stock.G", "Stock.H",
"Stock.I", "Stock.J")))
, units = c("Stock.A", "Stock.B", "Stock.C", "Stock.D", "Stock.E", "Stock.F",
"Stock.G", "Stock.H", "Stock.I", "Stock.J")
, positions = c(1327968000, 1330473600, 1333152000, 1335744000, 1338422400,
1341014400, 1343692800, 1346371200)
, format = "%Y-%m-%d"
, FinCenter = "GMT"
, recordIDs = structure(list(), .Names = character(0), row.names = integer(0), class = "data.frame")
, title = "Time Series Object"
, documentation = "Tue Jun 23 13:48:36 2015"
)
代码:
library(timeSeries)
library(fPortfolio)
# returns <- read.csv("Not Working Excel File.csv")
#lct <- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C")
#returns[,1] = as.Date(as.character(returns[,1]),format="%d-%b-%y")
#Sys.setlocale("LC_TIME", lct)
#returns = timeSeries(returns[,-1], charvec = returns[,1])
Frontier <- portfolioFrontier(returns)
错误:
Error in `colnames<-`(`*tmp*`, value = c("AAPL", :
attempt to set 'colnames' on an object with less than two dimensions
如何解决此问题,以便在不添加日期的情况下添加我想要的股票数量?
编辑:这是数据集外观的快照。 http://i.imgur.com/i0hkBdY.png 因此,在这个包含 8 个月数据且只有四只股票的示例中,我在上面发布的代码运行良好。但是,一旦我再添加 5 只股票(总共 9 只),我就会得到上面列出的错误。
将数据框更改为矩阵会产生相同的错误。 添加更多月份可以让我添加更多股票,但我不想要更多月份。
【问题讨论】:
-
您是否尝试读取错误?
-
我确实阅读了错误。我不知道如何更改colnames,因为它存在于portfolioFrontier(?)中,并且不确定为什么添加另一只股票会使其超过二维。
-
如果我们能有样本数据就好了。
-
添加了数据快照。
-
@user227710 的建议是使用
dput(returns, 10)获取示例输出以插入 R。请将该输出复制并粘贴到您的答案中。