I don't have much experience in R but I am trying to run a constrained portfolio optimization with R based on fPortfolio package. Using the function portfolioFrontier I get an error message:
Error in `colnames<-`(`*tmp*`, value = names(getMu(Data))) :
attempt to set 'colnames' on an object with less than two dimensions
My code:
#specs
spec <- portfolioSpec()
setNFrontierPoints(spec)<-200
setRiskFreeRate(spec)<-0.001366179
constraints<-c("Long only", "maxW[1]=.05","maxW[2]=.25")
setTargetReturn(Spec) = mean(colMeans(as.timeSeries(portfolioXTS)))
#analysis
portfolioConstraints(as.timeSeries(portfolioXTS), spec, constraints)
portfolioData(as.timeSeries(portfolioXTS), spec = portfolioSpec())
efficientPortfolio(as.timeSeries(portfolioXTS), spec, constraints)
frontier <- portfolioFrontier(as.timeSeries(portfolioXTS),spec,
constraints,include.mvl = TRUE, title="Traditional Asset Mix")
tangencyPortfolio(as.timeSeries(portfolioXTS), spec, constraints)
minvariancePortfolio(as.timeSeries(portfolioXTS), spec, constraints)
print(frontier)
I found on the forum that someone had a similar problem but there was no solution. I know that the number of rows must be greater than the number of columns but this requirement is satisfied.
I'd appreciate your help!
Data Sample:
dput
> dput(head(portfolioXTS))
structure(c(0.00156666666666667, 0.00158333333333333, 0.00169166666666667,
0.0016, 0.00158025, 0.00155, 0.00673371680501522, 0.00900662025078258,
-0.0240575754236355, 0.0248764429466077, 0.00560202723890391,
0.0141166753754614, -0.0301780036878394, -0.00848891285361475,
0.0444382350834758, -0.0335723121416733, 0.00238564176285805,
-0.0603502320765891, 0.032911309501205, 0.0157910651749094, 0.0593749401684385,
0.0061188298291901, -0.0166768949719553, -0.0753692400590273), .indexTZ = "UTC", .indexCLASS = "Date", tclass = "Date", tzone = c(TZ = "UTC"), class = c("xts",
"zoo"), index = structure(c(1009843200, 1012521600, 1014940800,
1017532800, 1020124800, 1022803200), tzone = "UTC", tclass = "Date"), .Dim = c(6L,
4L), .Dimnames = list(NULL, c("Cash", "Bonds", "Equities_dev",
"Equities_em")))