使用 glmnet 从插入符号中提取系数的协方差

Extracting Coefficients' Covariance from caret with glmnet

提问人:Spätzle 提问时间:9/5/2023 最后编辑:Spätzle 更新时间:9/5/2023 访问量:20

问:

当使用该函数构建线性回归模型时,我们可以很容易地得到回归系数及其协方差矩阵:lm

reg <- lm(mtcars)
beta <- coef(reg)
cov_beta <- summary(reg)$cov

所以,我们得到了

> beta
(Intercept)         cyl        disp          hp        drat          wt        qsec          vs          am        gear        carb 
12.30337416 -0.11144048  0.01333524 -0.02148212  0.78711097 -3.71530393  0.82104075  0.31776281  2.52022689  0.65541302 -0.19941925 
> cov_beta
              (Intercept)           cyl          disp            hp          drat           wt          qsec            vs            am          gear         carb
(Intercept) 49.8835321876 -1.8742423910 -8.414814e-04 -3.788688e-03 -1.8426349117  0.449345889 -1.5015939690  0.5191416655 -1.2649727601 -1.6171191755  0.258652641
cyl         -1.8742423910  0.1554875692 -7.071840e-04 -5.953951e-04  0.0670914657  0.031805873  0.0298592741  0.1004400830  0.0798098197  0.0783313749 -0.028133739
disp        -0.0008414814 -0.0007071840  4.540305e-05 -2.905034e-05 -0.0004818652 -0.003705589  0.0005304819  0.0005359447  0.0001409838 -0.0002981977  0.001421265
hp          -0.0037886881 -0.0005953951 -2.905034e-05  6.746893e-05  0.0004406994  0.001411614  0.0002455543 -0.0017760496 -0.0003026473 -0.0004140029 -0.001352658
drat        -1.8426349117  0.0670914657 -4.818652e-04  4.406994e-04  0.3807828305  0.074287190  0.0061847567 -0.0145767070 -0.0748973106 -0.0260486727 -0.039823235
wt           0.4493458888  0.0318058726 -3.705589e-03  1.411614e-03  0.0742871900  0.510967881 -0.0999519610  0.0481275585  0.0523321257  0.0730403152 -0.155368805
qsec        -1.5015939690  0.0298592741  5.304819e-04  2.455543e-04  0.0061847567 -0.099951961  0.0760490849 -0.0799725516  0.0586241152  0.0125790869  0.023167374
vs           0.5191416655  0.1004400830  5.359447e-04 -1.776050e-03 -0.0145767070  0.048127558 -0.0799725516  0.6305871069  0.1292468075 -0.0194917069  0.023018829
am          -1.2649727601  0.0798098197  1.409838e-04 -3.026473e-04 -0.0748973106  0.052332126  0.0586241152  0.1292468075  0.6022331926 -0.1371903335  0.015055228
gear        -1.6171191755  0.0783313749 -2.981977e-04 -4.140029e-04 -0.0260486727  0.073040315  0.0125790869 -0.0194917069 -0.1371903335  0.3174786433 -0.074770481
carb         0.2586526408 -0.0281337389  1.421265e-03 -1.352658e-03 -0.0398232345 -0.155368805  0.0231673742  0.0230188288  0.0150552284 -0.0747704807  0.097789759

现在,我正在尝试获得相同的估计值,但这次是针对惩罚回归模型,该模型使用和 - 例如,岭回归构建:caretglmnet

lambdas <- 2^seq(-7, 3, by = 0.25)
ridge_lambda.grid <- expand.grid(alpha = 0, lambda = lambdas)
ridge_obj2 <- caret::train(mpg~., data = mtcars, method = 'glmnet', tuneGrid = ridge_lambda.grid, control = list(maxit = 1000))

我可以很容易地提取最佳拟合 lambda 的系数向量:

> coef(ridge_obj2$finalModel, ridge_obj2$bestTune$lambda)
11 x 1 sparse Matrix of class "dgCMatrix"
                       1
(Intercept) 20.597142002
cyl         -0.379302833
disp        -0.005470599
hp          -0.010885177
drat         1.047794464
wt          -1.064653057
qsec         0.155283172
vs           0.847266052
am           1.428152518
gear         0.528886751
carb        -0.468132959

这相当于我们的.问题是 - 如何提取等价物?betacov_beta

有这个简历帖子说这是不可能的,但它已经有 8 年多的历史了,可能已经过时了,(显然)不能满足我的需求(我需要整个矩阵,而不仅仅是单个 SD)。

R-Caret 插入符号 glmnet

评论


答: 暂无答案