CVXR:solve() 问题 - as.vector(data) 中的错误:没有将此 s4 class 强制转换为向量的方法
CVXR: Problem with solve() - Error in as.vector(data): no method for coercing this s4 class to a vector
我正在尝试最小化此功能:
\min_{\mu} \sum_{t=T}^T \| y_t - \mu_t \|2 + \lambda \sum{t=1}^{T-1} \|mu_{t+1}- \mu_{t}\|_2
其中:y 和 mu 是 p*T 矩阵。
在我使用 solve() 函数之前,一切都编译良好。
这是我用 y 编码的 p*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)
在 result_mu <- solve(problem_mu)
之前一切都很好地执行。我在哪里收到以下错误消息:
> result_mu <- solve(problem_mu)
Error in as.vector(data) :
no method for coercing this S4 class to a vector
到目前为止一切都很好。
我还尝试了以下公式:
# 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")
}
具有相同的objective和问题功能。在这里,我再次在同一点收到完全相同的错误消息。
我看不出代码哪里错了...有任何指示吗?
谢谢
Problem
在您的代码中缺失:
problem_mu <- Problem(Minimize(objective_mu))
我正在尝试最小化此功能:
\min_{\mu} \sum_{t=T}^T \| y_t - \mu_t \|2 + \lambda \sum{t=1}^{T-1} \|mu_{t+1}- \mu_{t}\|_2
其中:y 和 mu 是 p*T 矩阵。 在我使用 solve() 函数之前,一切都编译良好。
这是我用 y 编码的 p*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)
在 result_mu <- solve(problem_mu)
之前一切都很好地执行。我在哪里收到以下错误消息:
> result_mu <- solve(problem_mu)
Error in as.vector(data) :
no method for coercing this S4 class to a vector
到目前为止一切都很好。
我还尝试了以下公式:
# 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")
}
具有相同的objective和问题功能。在这里,我再次在同一点收到完全相同的错误消息。
我看不出代码哪里错了...有任何指示吗?
谢谢
Problem
在您的代码中缺失:
problem_mu <- Problem(Minimize(objective_mu))