微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

R 中的错​​误消息:尝试在小于二维的对象上设置“colnames”

如何解决R 中的错​​误消息:尝试在小于二维的对象上设置“colnames”

当我没有对其施加太多限制时,我的代码可以正常工作。但是,我想运行大量的约束。有没有办法帮助解决问题,或者解决这个错误。我正在尝试绘制一个有效的边界。谢谢。我在下面有数据时间序列的 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","XFVT")]=0.3','minsumW[c("BND","CBON","CRPA","GLD","SLV","SPGP","SWAN")]=0.01','maxsumW[c("BND","SWAN")]=0.3','minsumW[c("GLTL","GLTS","IBGE","IBGY","IBGZ","IEF","IGLT","SEGA","SHY","TIP","TLT","VVUILG")]=0.01','maxsumW[c("GLTL","VVUILG")]=0.3')

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

frontier <-portfolioFrontier(data,spec,constraints)

当我运行最后一行时,frontier <-portfolioFrontier(data,constraints) 出现错误

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

解决方法

好的,我搞定了。我没有将约束添加到一个,而是放入了其他库。现在一切正常。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。