[英]CVXR: Problem with solve() - Error in as.vector(data): no method for coercing this s4 class to a vector
I'm trying to minimize this function:我试图最小化这个 function:
\min_{\mu} \sum_{t=T}^T \| \min_{\mu} \sum_{t=T}^T \| y_t - \mu_t \|
y_t - \mu_t \| 2 + \lambda \sum {t=1}^{T-1} \|mu_{t+1}-\mu_{t}\|_2
2 + \lambda \sum {t=1}^{T-1} \|mu_{t+1}-\mu_{t}\|_2
where: y and mu are p*T matraxis.其中: y 和 mu 是 p*T 矩阵。 Everything compiles well until I use the solve() function.
在我使用solve() function之前,一切都编译得很好。
Here is what I coded with y being ap*obs matrix这是我用 y 编码的 ap*obs 矩阵
library(CVXR)
mu <- Variable(p, obs)
# group lasso ----
total_var <- lapply(X = seq_len(obs-1), FUN = function(j) mu[,j+1] - mu[,j])
total_var_norm <- lapply(X = total_var, FUN = cvxr_norm, p=2)
group_lasso <- Reduce(f = sum, x = total_var_norm)
# loss function ----
col_diff <- lapply( X = seq_len(obs), FUN = function(j) y[,j] - mu[,j])
col_diff_norm <- lapply( X = col_diff, FUN = cvxr_norm, p=2)
loss <- Reduce(f = sum, x = col_diff_norm)
# convex optimization ----
objective_mu <- loss + lambda * group_lasso
problem_mu <- Minimize(objective_mu)
result_mu <- solve(problem_mu)
All executes well until the result_mu <- solve(problem_mu)
.在
result_mu <- solve(problem_mu)
之前,一切都执行得很好。 Where I get the following Error message:我在哪里收到以下错误消息:
> result_mu <- solve(problem_mu)
Error in as.vector(data) :
no method for coercing this S4 class to a vector
Everything is fine until this point.到目前为止一切都很好。
I also tried with the following formulation:我还尝试了以下公式:
# group lasso ----
group_lasso <- norm(mu[,2] - mu[,1], type = "2")
for (s in 2:obs-1){
group_lasso <- group_lasso + norm(mu[,s] - mu[,s+1], type = "2")
}
# loss function ----
loss <- norm(y[,1] - mu[,1], type = "2")
for (s in 2:obs){
loss <- group_lasso_2 + norm(y[,s] - mu[,s], type = "2")
}
with the same objective and problem function.具有相同的目标和问题 function。 And here again I get exactly the same error message at the same point.
在这里,我再次在同一点收到完全相同的错误消息。
I can't see at what point the code is wrong... Any pointers?我看不出代码在什么时候出错了......任何指针?
Thanks谢谢
Problem
is missing in your code:您的代码中缺少
Problem
:
problem_mu <- Problem(Minimize(objective_mu))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.