如何提取 R 中 "cv.rq.pen" 函数中的系数?
How to extract coefficients in "cv.rq.pen" function in R?
library(rqPen)
n <- 60
p <- 7
rho <- .5
beta <- c(3,1.5,0,2,0,0,0)
R <- matrix(0,p,p)
for(i in 1:p){
for(j in 1:p){
R[i,j] <- rho^abs(i-j)
}
}
set.seed(1234)
x <- matrix(rnorm(n*p),n,p) %*% t(chol(R))
y <- x %*% beta + rnorm(n)
q.lasso_scad = cv.rq.pen(x, y, tau = 0.5, lambda = NULL, penalty = "SCAD", intercept = FALSE, criteria = "CV", cvFunc = "check", nfolds = 10,
foldid = NULL, nlambda = 100, eps = 1e-04, init.lambda = 1,alg="QICD")
q.lasso_scad
coef1 = q.lasso_scad$models[[which.min(q.lasso_scad$cv[,2])]]
coef1
我有以下输出
Coefficients:
x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
0.0000000 0.3226967 1.8131688 -0.1971847 0.1981571 0.7715635 -0.2289284 -0.1087028 0.9713283 -0.1079333
我只想提取系数。我该怎么做?
提前致谢。
我无权访问 R 程序,因此无法验证它是否有效。但试试这个:
names(coef1) <- NULL
coef1
有点倒退,但你可以这样做:
as.data.frame(as.list.data.frame(coef1)$coefficients)
结果:
as.list.data.frame(coef1)$coefficients
x1 3.17487201
x2 1.15712559
x3 0.05078333
x4 2.27113756
x5 0.24893740
x6 0.00000000
x7 -0.07542964
如果我对问题的理解正确,rqPen
的输出是某种具有附加属性的奇特 list
。 as.list.data.frame
基本上强制 coef1
成为一个 "normal" 列表,这允许我使用 $coefficients
来提取系数值。最后,我使用 as.data.frame
将其转换为更有用的对象。
如果您只想要值,可以将 as.data.frame
替换为 as.vector
:
as.vector(as.list.data.frame(coef1)$coefficients)
结果:
[1] 3.17487201 1.15712559 0.05078333 2.27113756 0.24893740 0.00000000
[7] -0.07542964
library(rqPen)
n <- 60
p <- 7
rho <- .5
beta <- c(3,1.5,0,2,0,0,0)
R <- matrix(0,p,p)
for(i in 1:p){
for(j in 1:p){
R[i,j] <- rho^abs(i-j)
}
}
set.seed(1234)
x <- matrix(rnorm(n*p),n,p) %*% t(chol(R))
y <- x %*% beta + rnorm(n)
q.lasso_scad = cv.rq.pen(x, y, tau = 0.5, lambda = NULL, penalty = "SCAD", intercept = FALSE, criteria = "CV", cvFunc = "check", nfolds = 10,
foldid = NULL, nlambda = 100, eps = 1e-04, init.lambda = 1,alg="QICD")
q.lasso_scad
coef1 = q.lasso_scad$models[[which.min(q.lasso_scad$cv[,2])]]
coef1
我有以下输出
Coefficients:
x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
0.0000000 0.3226967 1.8131688 -0.1971847 0.1981571 0.7715635 -0.2289284 -0.1087028 0.9713283 -0.1079333
我只想提取系数。我该怎么做?
提前致谢。
我无权访问 R 程序,因此无法验证它是否有效。但试试这个:
names(coef1) <- NULL
coef1
有点倒退,但你可以这样做:
as.data.frame(as.list.data.frame(coef1)$coefficients)
结果:
as.list.data.frame(coef1)$coefficients
x1 3.17487201
x2 1.15712559
x3 0.05078333
x4 2.27113756
x5 0.24893740
x6 0.00000000
x7 -0.07542964
如果我对问题的理解正确,rqPen
的输出是某种具有附加属性的奇特 list
。 as.list.data.frame
基本上强制 coef1
成为一个 "normal" 列表,这允许我使用 $coefficients
来提取系数值。最后,我使用 as.data.frame
将其转换为更有用的对象。
如果您只想要值,可以将 as.data.frame
替换为 as.vector
:
as.vector(as.list.data.frame(coef1)$coefficients)
结果:
[1] 3.17487201 1.15712559 0.05078333 2.27113756 0.24893740 0.00000000
[7] -0.07542964