roptimizationr-s4convex-optimizationcvx

Package CVXR: Error in as.vector(data): no method for coercing this S4 class to a vector


I am trying to use the package CVXR to do my optimization. I am following the instructions from this page: https://rviews.rstudio.com/2017/11/27/introduction-to-cvxr/

My problem is a little complicated to I want to put my coefficient variables (the variables I want to optimize) into a matrix to help with coding. However, I could not do it in R

beta = Variable(n)
matrixbeta = matrix(beta,nrow=2)

Error in as.vector(data) : no method for coercing this S4 class to a vector

Really appreciate any help.


Solution

  • beta is a S4 object, not a numeric value, so it cannot be placed in a matrix. What are you trying to do with the coefficient? You can create an arbitrary m by n variable with Variable(rows = m, cols = n) and use it in mathematical operations as you would a matrix.