PortfolioAnalytics Error with function gmv_opt - r

I have below script which creates an error. Does anyone know how to solve this. I'm running the latest version of R & RStudio, and all packages are up to date.
library('quantmod')
library('PortfolioAnalytics')
library('PerformanceAnalytics')
ETF_Names <- c("IVV","IJH","IWM","EZU","EEM","SCZ","ILF","EPP")
ETF_All <- lapply(ETF_Names, function(x) getSymbols(x,from="2006-01-01",auto.assign = FALSE))
names(ETF_All) <- ETF_Names
ETF_MR <- do.call(merge,lapply(ETF_All,monthlyReturn))
colnames(ETF_MR) <- ETF_Names
ETF_spec <- portfolio.spec(assets = colnames(ETF_MR))
ETF_spec <- add.constraint(portfolio=ETF_spec, type="full_investment")
ETF_spec <- add.constraint(portfolio=ETF_spec, type="box", min=0, max=1)
ETF.ef <- create.EfficientFrontier(R=ETF_MR['2015'], portfolio=ETF_spec, type="mean-StdDev")
Below the Error message:
Error in gmv_opt(R = R, constraints = constraints, moments = moments, :
No solution found: Error in UseMethod("as.constraint") :
no applicable method for 'as.constraint' applied to an object of class "c('matrix', 'list')"
There have never been issues before (I just recently updated RStudio and the relevant packages). And that is when the error popped up.
Hope someone can help

I got the same error.
Try adding:
library(ROI)
require(ROI.plugin.glpk)
require(ROI.plugin.quadprog)
Have worked for me.

I got the same error.
You could try quadratic optimization (solve.QP). See this answer
https://stackoverflow.com/a/30650972/2824732

Related

I have problems with a function to impose monotonicity

I am new in R. I am trying to impose monotonicity conditions to a translog production function. I am using the function translogMonoRestr. I loaded my data, the coeff and the inv of the var cov matrix. I imported this from excel and all good
read_excel("rtl2.xlsx")
uCoef <- read_excel("matrixbtl2.xlsx")
uCovInv <- read_excel("matrixinvtl2.xlsx")
but when I tried:
monoRestr <- translogMonoRestr( xNames, = c( "lx1", "lx2"), rtl2.xlsx , dataLogged = TRUE)
I got Error in testNames %in% allNames : object 'rtl2' not found.
Then I'll use solve.QP but that is another topic.
I hope someone can help me with this error message
Thanks

I am receiving 'Error in !toroidal : invalid argument type'

I am trying to run a supervised SOM model based on cross-validated values.
The issue seems is in the sup.som line. I receive this error: Error in !toroidal : invalid argument type.
This code has worked fine in the past (within past few days) and I have tried restarting RStudio. There was a very similar question here (Error in !toroidal : invalid argument type in R with som package) but the issue seemed to have resolved itself for the other person. Any help is appreciated!
My code is:
require(kohonen)
set.seed(123)
fitControl <- trainControl(method = "cv",number = 10)
tg <- expand.grid(xdim=c(3:10), ydim=c(3:10), user.weights=seq(0.1,0.9,by=0.1), topo="hexagonal")
somFit1 <- train(train[,1:6], as.factor(train$CORR), method="xyf", trControl=fitControl, tuneLength=20, tuneGrid=tg)
'''sup.som <- xyf(training.sc, classvec2classmat(corrupt.train), grid = somgrid(xdim=somFit1$bestTune$xdim, ydim=somFit1$bestTune$ydim, topo="hexagonal"), user.weights=somFit1$bestTune$user.weights, keep.data=TRUE)
I ended up trying my code in the R console to see if I could get more detailed error messages, and I got the following:
require(class)
Loading required package: class
Attaching package: ‘class’
The following object is masked from ‘package:kohonen’:
somgrid
The solution was to specify kohonen::somgrid before my code as follows:
sup.som <- xyf(training.sc, classvec2classmat(corrupt.train), grid = kohonen::somgrid(xdim=somFit1$bestTune$xdim, ydim=somFit1$bestTune$ydim, topo="hexagonal"), user.weights=somFit1$bestTune$user.weights, keep.data=TRUE)
Hope this helps someone else

getting error in mclust-package while working on univariate fit

While working on a univariate fit using Mclust I am getting following error:
Error in mstepE(data = as.matrix(data)[initialization$subset, ], z = z, :
row dimension of z should equal data length
I am using the code mentioned in:
https://cran.r-project.org/web/packages/mclust/vignettes/mclust.html#initialisation
This is the code section where I am getting error:
df1 <- dataSample
BIC <- NULL
for(j in 1:20){
rBIC <- mclustBIC(df1, verbose = T,
initialization = list(hcPairs = randomPairs(df1)))
BIC <- mclustBICupdate(BIC, rBIC)
}
summary(BIC)
Following link contains data to be passed to variable 'df1' (file name:dataSample.csv)
https://drive.google.com/open?id=0Bzau9RsRnQreYk9XOWVBSm91b2o4NTQ4RlA2UFdWbDBVOVpR
This is the solution I get from one of the Authors (Prof. Luca Scrucca) for 'mclust' library:
"there was a bug due to the use of automatic subset that clash when hcPairs are provided. I have fixed it in the current dev version of mclust.
Since submission to CRAN won't happen shortly, you may use the following code to avoid the error with the current release of mclust:
rBIC <- mclustBIC(df1, verbose = T,
initialization = list(hcPairs = randomPairs(df1),
subset = 1:NROW(df1)))
When the bug fix will be released, the subset argument could be omitted as it is redundant."
Now, the code is working fine.

Error in pnbd.LL(params, x, t.x, T.cal) : T.cal must be numeric and may not contain negative numbers in BTYD in r

I am building a pareto/nbd model using BTYD package and I am getting error while using it.
I have been following the directions from [BTYD walkthrough][1]. Everything was working fine until I got to the calibration customer-by-sufficient-statistic matrix (cal.cbs). I used the following code to generate cal.cbs, as instructed in the tutorial:
library(BTYD)
tot.cbt <- dc.CreateFreqCBT(elog)
cal.cbt <- dc.MergeCustomers(tot.cbt, freq.cbt)
birth.periods <- split.data$cust.data$birth.per
last.dates <- split.data$cust.data$last.date
cal.cbs.dates <- data.frame(birth.periods, last.dates,
end.of.cal.period)
cal.cbs <- dc.BuildCBSFromCBTAndDates(cal.cbt, cal.cbs.dates,
per="month")
But I got a warning message:
In cbind(f, r, T) :
number of rows of result is not a multiple of vector length (arg 2)
Though I overcome the warning by using the following code:
tot.cbt <- dc.CreateFreqCBT(elog.cal)
# instead of
tot.cbt <- dc.CreateFreqCBT(elog)
But I am getting error message while estimating parameters:
params <- pnbd.EstimateParameters(cal.cbs)
Error in pnbd.LL(params, x, t.x, T.cal) :
T.cal must be numeric and may not contain negative numbers.
I have no clue how to fix this problem and why am I getting this error. Can anybody help me please? Many thanks in advance!
Make sure cal.cbs is a matrix, I had similar problem and this fixed it.
cal.cbs <- as.matrix(cal.cbs)

DEoptim - Error in get(as.character(FUN))

I ran into a problem with the DEoptim package. I am trying to minimize the function minF by optimizing vector optVector (see code).
##construct function
minF <- function(x, y, z){
return(mean((z-rowSums(t(x*t(y))))^2))
}
#random matrix and vector
testmrx <- matrix(rnorm(6),38,9)
vctr <- runif(38, min=0, max=50)
#Vector to be optimized and its bounds
optVector = c(20,20,50,30,30,10,3,5,5)
lowr = c(0,0,0,0,0,0,0,0,0)
uppr = c(50,50,200,100,100,50,20,20,20)
#Call of DEoptim
DEoptim(fn = minF(optVector, testmrx, vctr), lower=lowr, upper=uppr)
When I try to do this I get following error:
Error in get(as.character(FUN), mode = "function", envir = envir) :
object 'fn' of mode 'function' was not found
I found a similar error posted (link), but the proposed solution of changing variable names did not work. I have no idea what might be causing the problem here. Any help would be greatly appreciated!

Resources