R中的错误消息:尝试在小于两个维度的对象上设置'colnames'

xiozqbni  于 2023-11-14  发布在  其他
关注(0)|答案(2)|浏览(278)

我有代码,当我不把它太多的约束工作.然而,我想运行大量的约束.无论如何,有帮助解决问题,或围绕这个错误工作.我试图绘制一个有效的边界.谢谢.我有dput下面的数据时间序列.

library("fPortfolio")
library('fPortfolio')
data = SPISECTOR.RET
asset = dim(data)[2]

# I'm putting a lot of constraints here. I want to do this. 
# If I take away some of the constraints, the formula works. 
# But I don't want to take away the constraints.
constraints <- c('minW[1:asset]=0','maxW[1:asset]=0.3', 'minsumW[c("ACWI", "ACWX", "AGED", "CEMA", "CEMG", "CMXC", "CNYA", "COMF", "CSJP", "CSKR", "CSRU", "CSUK", "DGTL", "DLTM", "ECAR", "EEM", "EFA", "EIDO", "EIRL", "EMB", "ENOR", "EPHE", "EPOL", "EUSA", "GUNR", "HEAL", "ICDU", "ICSU", "IESU", "IGAA", "IH2O", "IHCU", "IHYA", "IIND", "IISU", "IITU", "IJS", "IJT", "IMSU", "INRG", "IQLT", "IUSU", "KWEB", "LAND", "LOCK", "MGK", "OPEN", "QQQ", "RBOT", "SAUS", "SPAG", "UIFS", "UNG", "USMV", "VB", "VNQ", "VNQI", "VTV", "VUG", "WOOD", "XFVT")]=0.01', 'maxsumW[c("ACWI", "ACWX", "AGED", "CEMA", "CEMG", "CMXC", "CNYA", "COMF", "CSJP", "CSKR", "CSRU", "CSUK", "DGTL", "DLTM", "ECAR", "EEM", "EFA", "EIDO", "EIRL", "EMB", "ENOR", "EPHE", "EPOL", "EUSA", "GUNR", "HEAL", "ICDU", "ICSU", "IESU", "IGAA", "IH2O", "IHCU", "IHYA", "IIND", "IISU", "IITU", "IJS", "IJT", "IMSU", "INRG", "IQLT", "IUSU", "KWEB", "LAND", "LOCK", "MGK", "OPEN", "QQQ", "RBOT", "SAUS", "SPAG", "UIFS", "UNG", "USMV", "VB", "VNQ", "VNQI", "VTV", "VUG", "WOOD", "XFVT")]=0.3', 'minsumW[c("BND", "CBON", "CRPA", "GLD", "SLV", "SPGP", "SWAN")]=0.01', 'maxsumW[c("BND", "CBON", "CRPA", "GLD", "SLV", "SPGP", "SWAN")]=0.3', 'minsumW[c("GLTL", "GLTS", "IBGE", "IBGY", "IBGZ", "IEF", "IGLT", "SEGA", "SHY", "TIP", "TLT", "VVUILG")]=0.01', 'maxsumW[c("GLTL", "GLTS", "IBGE", "IBGY", "IBGZ", "IEF", "IGLT", "SEGA", "SHY", "TIP", "TLT", "VVUILG")]=0.3')

spec <- portfolioSpec()
setNFrontierPoints(spec) <- 25
setSolver(spec)<- "solveRquadprog"

frontier <-portfolioFrontier(data, spec, constraints)

字符串
当我运行最后一行frontier <-portfolioFrontier(data, spec, constraints)时,我得到错误:

Error in `colnames<-`(`*tmp*`, value = names(getMu(Data))) : 
attempt to set 'colnames' on an object with less than two dimensions

iyfamqjs

iyfamqjs1#

好的,我让它工作了。我没有把约束加到一个,我放进了其他的库。现在一切都好了。

eh57zj3b

eh57zj3b2#

导致我在生成两个资产行时出现相同错误的原因是由于portfolioFrontier()中的编码不小心,如果一对中的任何资产具有负平均回报,则由于双浮点精度问题,无法识别有效投资组合解决方案。为了找到内部解决方案,portfolioFrontier()的内部编码引入了公差项.Machine$double.eps,旨在仅在每个资产的平均回报形成的限制内调整目标回报。但函数定义脚本规定调整因子的方式仅在两种资产都具有正平均回报时缩小解决方案边界。但是,如果其中任何一种资产具有负回报,调整因子将目标回报扩展到每个资产的平均回报之外,使问题无法解决。这导致返回空的权重矩阵NULL,从而导致在从资产数据分配列名时出错。
解决方案是在函数的脚本中更改两行,如下所示,使调整因子取决于资产回报的符号。

portfolioFrontier <- function (
    data, spec = portfolioSpec(), constraints = "LongOnly", 
    include.mvl = TRUE, title = NULL, description = NULL) {
  DEBUG <- TRUE
  fPortfolio:::.checkSpecVsConstraints(spec, constraints)
  Data <- portfolioData(data, spec)
  nFrontierPoints = getNFrontierPoints(spec)
  mu <- getMu(Data)
  targetReturns <- seq(min(mu), max(mu), length = nFrontierPoints)
  eps <- .Machine$double.eps^0.5 
  # !!! changed below
  targetReturns[1] = targetReturns[1] * (1 + sign(targetReturns[1]) * eps)
  targetReturns[nFrontierPoints] = targetReturns[nFrontierPoints] * 
    (1 - sign(targetReturns[nFrontierPoints]) *eps) # make less extreme
  mvPortfolio <- minvariancePortfolio(Data, spec, constraints)
  mvReturn <- getTargetReturn(mvPortfolio@portfolio)["mean"]
  minIndex <- which.min(abs(mvReturn - targetReturns))
  Status <- 0
  IDX <- minIndex
  weights <- targetReturn <- targetRisk <- covRiskBudgets <- maxDD <- NULL
  while (Status == 0 & IDX <= nFrontierPoints) {
    setTargetReturn(spec) = targetReturns[IDX]
    ans = try(efficientPortfolio(Data, spec, constraints), silent = TRUE)
    if (inherits(ans, "try-error")) {Status = 1} else {
      portfolio = ans
      Status = getStatus(portfolio)
    }
    if (Status == 0) {
      Weights = getWeights(portfolio)
      weights = rbind(weights, Weights)
      targetReturn = rbind(
        targetReturn, getTargetReturn(portfolio@portfolio))
      targetRisk = rbind(
        targetRisk, getTargetRisk(portfolio@portfolio))
      covRiskBudgets = rbind(
        covRiskBudgets, getCovRiskBudgets(portfolio@portfolio))
    }
    IDX = IDX + 1
  }
  if (include.mvl) {
    if (minIndex > 1) {
      weights2 = targetReturn2 = targetRisk2 = covRiskBudgets2 = maxDD2 = NULL
      Status = 0
      IDX = minIndex - 1
      while (Status == 0 & IDX > 0) {
        setTargetReturn(spec) = targetReturns[IDX]
        ans = try(efficientPortfolio(Data, spec, constraints), silent = TRUE)
        if (inherits(ans, "try-error")) {Status = 1} else {
          portfolio = ans
          Status = getStatus(portfolio) }
        if (Status == 0) {
          Weights2 = getWeights(portfolio)
          weights2 = rbind(Weights2, weights2)
          targetReturn2 = rbind(
            getTargetReturn(portfolio@portfolio), targetReturn2)
          targetRisk2 = rbind(
            getTargetRisk(portfolio@portfolio), targetRisk2)
          covRiskBudgets2 = rbind(
            getCovRiskBudgets(portfolio@portfolio), covRiskBudgets2)
        }
        IDX = IDX - 1
      }
      weights = rbind(weights2, weights)
      targetReturn = rbind(targetReturn2, targetReturn)
      targetRisk = rbind(targetRisk2, targetRisk)
      covRiskBudgets = rbind(covRiskBudgets2, covRiskBudgets)
    }
  }
  colnames(weights) <- names(getMu(Data))
  rownames(weights) <- NULL
  rownames(covRiskBudgets) <- NULL
  rownames(targetReturn) <- NULL
  rownames(targetRisk) <- NULL
  if (is.null(weights)) {
    portfolio <- mvPortfolio
    return(portfolio)
  }
  setTargetReturn(spec) <- NULL
  portfolio@call <- match.call()
  portfolio@portfolio <- new(
    "fPFOLIOVAL", portfolio = list(
      weights = weights, covRiskBudgets = covRiskBudgets, 
      targetReturn = targetReturn, targetRisk = targetRisk, 
      targetAlpha = getAlpha(spec), 
      minriskPortfolio = mvPortfolio, status = 0))
  portfolio@title = "Portfolio Frontier"
  portfolio
}

字符串
在控制台中创建此函数对象,然后使用is替换{fPortfolio}包中的原始portfolioFrontier()

相关问题