CVXR:as.vector(data)中的错误:没有用于将此S4类强制转换为向量的方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CVXR:as.vector(data)中的错误:没有用于将此S4类强制转换为向量的方法相关的知识,希望对你有一定的参考价值。
我正在尝试最小化R的以下问题:
min sum_ {t = 1} ^ T | y_t- delta | _2
其中y_t for t = 1,...,T和 delta是向量这是我尝试过的:
deltaHat <- Variable(p)
objective <- sum(col.norm(y - matrix(deltaHat, ncol=obs,
nrow=p, byrow=FALSE)))
problem <- Problem(Minimize(objective))
result <- solve(problem)
delta <- matrix(result$getValue(deltaHat), ncol = 1)
这是编译目标时收到的错误消息:
Error in as.vector(data) :
no method for coercing this S4 class to a vector
我知道在这里,betaHat不是一个数字值,这就是为什么会出现错误的原因。那么,如何编写具有相同列的矩阵?如果y有2个列,我尝试将其作为多个约束。
delta_1 <- Variable(p)
delta_2 <- Variable(p)
objective <- norm(y[,1]-delta_1, type="2") + norm(y[,2]-delta_2, type="2")
constraints <- list( delta_1 == delta_2)
problem <- Problem(Minimize(objective), constraints)
result <- solve(problem)
这有效,但是如何在T列中重复呢?是否可以在循环中创建多个变量?然后对它们求和?
答案
您最上面的代码摘录使用col.norm
,这是我不知道的功能。因此,我将通过一个可重现的示例(其中delta有5列)进行第二篇。
library(CVXR)
set.seed(123)
n <- 10
p <- 5
delta <- Variable(n, p) # n by p matrix
A <- matrix(rnorm(n * p), nrow = n)
col_diffs <- lapply(X = seq_len(p), FUN = function(j) delta[, j] - A[, j])
col_diff_norms <- lapply(X = col_diffs, FUN = cvxr_norm, p = 2)
objective <- Reduce(f = sum, x = col_diff_norms)
constraints <- lapply(2:p, function(j) delta[, j] == delta[, 1])
problem <- Problem(Minimize(objective), constraints)
## Specify solver to avoid automatic use of commercial solvers I have
system.time(result <- solve(problem, solver = "ECOS", verbose = TRUE))
result$value
result$getValue(delta)
## Always post session info to help others
sessionInfo()
结果如下。
ECOS 2.0.7 - (C) embotech GmbH, Zurich Switzerland, 2012-15. Web: www.embotech.com/ECOS
It pcost dcost gap pres dres k/t mu step sigma IR | BT
0 +0.000e+00 -0.000e+00 +6e+01 5e-01 3e-04 1e+00 1e+01 --- --- 1 1 - | - -
1 +4.960e+00 +5.047e+00 +1e+01 1e-01 5e-05 3e-01 2e+00 0.8458 1e-02 2 2 2 | 0 0
2 +1.242e+01 +1.246e+01 +8e-01 9e-03 4e-06 6e-02 1e-01 0.9369 3e-02 2 2 2 | 0 0
3 +1.284e+01 +1.284e+01 +3e-02 4e-04 2e-07 3e-03 6e-03 0.9580 4e-04 2 2 2 | 0 0
4 +1.286e+01 +1.286e+01 +2e-03 2e-05 9e-09 2e-04 4e-04 0.9410 1e-03 2 2 2 | 0 0
5 +1.286e+01 +1.286e+01 +2e-04 2e-06 1e-09 2e-05 4e-05 0.8911 2e-03 2 1 1 | 0 0
6 +1.286e+01 +1.286e+01 +5e-05 5e-07 2e-10 4e-06 9e-06 0.7863 1e-02 2 1 1 | 0 0
7 +1.286e+01 +1.286e+01 +3e-06 3e-08 1e-11 5e-07 5e-07 0.9890 5e-02 2 1 1 | 0 0
8 +1.286e+01 +1.286e+01 +2e-07 2e-09 2e-12 3e-08 3e-08 0.9387 7e-04 2 1 1 | 0 0
9 +1.286e+01 +1.286e+01 +1e-08 1e-10 8e-13 2e-09 2e-09 0.9890 6e-02 2 1 1 | 0 0
OPTIMAL (within feastol=1.3e-10, reltol=9.3e-10, abstol=1.2e-08).
Runtime: 0.000648 seconds.
> result$value
[1] 12.85991
> result$getValue(delta)
[,1] [,2] [,3] [,4] [,5]
[1,] 0.05115646 0.05115646 0.05115646 0.05115646 0.05115646
[2,] -0.15827438 -0.15827438 -0.15827438 -0.15827438 -0.15827438
[3,] 0.49632013 0.49632013 0.49632013 0.49632013 0.49632013
[4,] 0.57015571 0.57015571 0.57015571 0.57015571 0.57015571
[5,] 0.34898918 0.34898918 0.34898918 0.34898918 0.34898918
[6,] 0.61539678 0.61539678 0.61539678 0.61539678 0.61539678
[7,] 0.43976476 0.43976476 0.43976476 0.43976476 0.43976476
[8,] -0.64837389 -0.64837389 -0.64837389 -0.64837389 -0.64837389
[9,] -0.18336475 -0.18336475 -0.18336475 -0.18336475 -0.18336475
[10,] -0.20182226 -0.20182226 -0.20182226 -0.20182226 -0.20182226
> ## Always post session info to help others
> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-apple-darwin19.3.0 (64-bit)
Running under: macOS Catalina 10.15.4
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices datasets utils methods base
other attached packages:
[1] CVXR_1.0-1 rmarkdown_2.1 knitr_1.28 pkgdown_1.5.1 devtools_2.3.0
[6] usethis_1.6.0
loaded via a namespace (and not attached):
[1] gmp_0.5-13.6 Rcpp_1.0.4.6 compiler_3.6.3 prettyunits_1.1.1
[5] remotes_2.1.1 tools_3.6.3 bit_1.1-15.2 testthat_2.3.2
[9] digest_0.6.25 pkgbuild_1.0.6 pkgload_1.0.2 memoise_1.1.0
[13] evaluate_0.14 lattice_0.20-41 rlang_0.4.5 Matrix_1.2-18
[17] gurobi_9.0-1 cli_2.0.2 Rglpk_0.6-4 xfun_0.13
[21] ECOSolveR_0.5.3 Rmpfr_0.8-1 withr_2.1.2 desc_1.2.0
[25] fs_1.4.1 rprojroot_1.3-2 bit64_0.9-7 grid_3.6.3
[29] glue_1.4.0 R6_2.4.1 processx_3.4.2 fansi_0.4.1
[33] sessioninfo_1.1.1 callr_3.4.3 magrittr_1.5 rcbc_0.1.0.9001
[37] backports_1.1.6 ps_1.3.2 ellipsis_0.3.0 htmltools_0.4.0
[41] MASS_7.3-51.5 assertthat_0.2.1 Rcplex_0.3-3 Rmosek_9.1.0
[45] slam_0.1-47 crayon_1.3.4
另一答案
我找到了解决问题的方法:
delta <- Variable(p)
objective <- norm(y[,1]-delta, type = "2")
for (s in 2:obs){
objective <- objective + norm(y[,s]-delta, type = "2")
}
problem <- Problem(Minimize(objective))
result <- solve(problem)
delta_one <- matrix(result$getValue(delta), ncol = 1)
``
以上是关于CVXR:as.vector(data)中的错误:没有用于将此S4类强制转换为向量的方法的主要内容,如果未能解决你的问题,请参考以下文章
r 闪亮错误 as.vector(x, "character") 中的错误:无法将类型“闭包”强制转换为“字符”类型的向量