Introduction to gglasso

Yi Yang

2024-03-24

Install the package

# on CRAN
install.packages("gglasso")

# dev version on GitHub
pacman::p_load_gh('emeryyi/gglasso')

Least squares regression

library(gglasso)

# load bardet data set
data(bardet)

group1 <- rep(1:20, each = 5)

fit_ls <- gglasso(x = bardet$x, y = bardet$y, group = group1, loss = "ls")

plot(fit_ls)

coef(fit_ls)[1:5,90:100]
##                      s89          s90          s91          s92          s93
## (Intercept)  8.099354325  8.098922472  8.098531366  8.098175719  8.097849146
## V1          -0.119580203 -0.120877799 -0.122079683 -0.123223779 -0.124310183
## V2          -0.113742329 -0.114834411 -0.115853997 -0.116837630 -0.117782854
## V3          -0.002584792 -0.003487571 -0.004328519 -0.005134215 -0.005904892
## V4          -0.084771705 -0.088304073 -0.091674509 -0.094978960 -0.098212775
##                      s94          s95          s96          s97          s98
## (Intercept)  8.097574095  8.097295166  8.097058895  8.096833259  8.096637676
## V1          -0.125274109 -0.126284595 -0.127173301 -0.128011016 -0.128738414
## V2          -0.118630121 -0.119526679 -0.120326451 -0.121086672 -0.121754134
## V3          -0.006593702 -0.007323107 -0.007970047 -0.008583011 -0.009116809
## V4          -0.101161988 -0.104349829 -0.107241330 -0.110045942 -0.112543755
##                      s99
## (Intercept)  8.096455264
## V1          -0.129453437
## V2          -0.122415680
## V3          -0.009645386
## V4          -0.115058449

Cross-Validation

cvfit_ls <- cv.gglasso(x = bardet$x, y = bardet$y, group = group1, loss = "ls")

plot(cvfit_ls)

coef(cvfit_ls, s = "lambda.min")
##                         1
## (Intercept)  8.197773e+00
## V1          -2.672925e-02
## V2          -6.183860e-02
## V3           3.687290e-02
## V4           8.694734e-03
## V5          -8.829105e-02
## V6          -8.456807e-05
## V7           6.822711e-04
## V8           6.317361e-04
## V9           6.848111e-05
## V10         -1.123480e-03
## V11          2.820666e-02
## V12         -3.695297e-02
## V13         -4.326627e-03
## V14          6.422049e-03
## V15         -5.692340e-02
## V16         -2.198106e-04
## V17          4.512853e-02
## V18         -2.560941e-02
## V19         -5.128320e-03
## V20         -6.528665e-02
## V21          4.313356e-02
## V22          8.934235e-02
## V23         -4.545772e-02
## V24         -2.241544e-02
## V25         -1.662608e-01
## V26          1.207781e-01
## V27          2.789045e-02
## V28         -3.449442e-02
## V29          1.260556e-02
## V30         -1.873208e-01
## V31         -1.844027e-02
## V32          8.501327e-03
## V33          3.583815e-03
## V34          1.191608e-02
## V35          1.866050e-02
## V36          3.895912e-02
## V37         -1.437030e-02
## V38          6.427634e-03
## V39          1.168648e-02
## V40         -5.657613e-02
## V41         -2.330551e-03
## V42         -3.064803e-03
## V43          1.191571e-03
## V44          7.216732e-03
## V45          3.345442e-03
## V46         -9.474428e-02
## V47          7.760270e-02
## V48          9.471451e-02
## V49          1.466030e-02
## V50          2.249739e-02
## V51         -1.008583e-02
## V52          1.112999e-02
## V53          9.524593e-02
## V54          1.597186e-01
## V55          3.856741e-02
## V56          5.048018e-05
## V57         -1.063163e-05
## V58         -1.613658e-04
## V59          2.666697e-04
## V60          8.414123e-05
## V61         -5.692919e-02
## V62          3.423488e-02
## V63          1.331030e-03
## V64          8.528575e-02
## V65          2.249628e-01
## V66         -3.302048e-02
## V67          1.017548e-02
## V68          2.540641e-02
## V69          4.124517e-02
## V70          3.031728e-03
## V71         -8.444857e-03
## V72         -1.748408e-03
## V73          2.958499e-02
## V74         -4.612260e-03
## V75          8.148859e-03
## V76         -1.818270e-02
## V77          2.363876e-02
## V78          5.281023e-02
## V79         -8.057897e-03
## V80          1.797602e-02
## V81          5.307318e-03
## V82         -1.623854e-02
## V83          8.314143e-03
## V84          8.236939e-03
## V85         -2.402224e-02
## V86          5.402119e-03
## V87         -6.397635e-02
## V88          9.012578e-04
## V89          4.500677e-02
## V90         -7.815789e-02
## V91          1.665534e-02
## V92         -5.759135e-03
## V93         -6.432123e-03
## V94          1.552064e-02
## V95         -2.118880e-02
## V96         -2.735230e-03
## V97          2.547208e-03
## V98         -5.347579e-04
## V99          5.831298e-04
## V100         4.605191e-03

Weight Least squares regression

We can also perform weighted least-squares regression by specifying loss='wls', and providing a \(n \times n\) weight matrix in the weights argument, where \(n\) is the number of observations. Note that cross-validation is NOT IMPLEMENTED for loss='wls'.

# generate weight matrix
times <- seq_along(bardet$y)
rho <- 0.5
sigma <- 1
H <- abs(outer(times, times, "-"))
V <- sigma * rho^H
p <- nrow(V)
V[cbind(1:p, 1:p)] <- V[cbind(1:p, 1:p)] * sigma

# reduce eps to speed up convergence for vignette build
fit_wls <- gglasso(x = bardet$x, y = bardet$y, group = group1, loss = "wls", 
                   weight = V, eps = 1e-4)

plot(fit_wls)

coef(fit_wls)[1:5,90:100]
##                     s89         s90         s91         s92         s93
## (Intercept)  8.09429262  8.09340481  8.09254573  8.09170743  8.09089247
## V1          -0.13922372 -0.14077803 -0.14222609 -0.14359110 -0.14487482
## V2          -0.15966042 -0.16117772 -0.16261019 -0.16397683 -0.16527730
## V3           0.03917529  0.03880296  0.03847035  0.03816594  0.03788642
## V4          -0.16548208 -0.17057112 -0.17546237 -0.18021267 -0.18481370
##                     s94         s95         s96         s97         s98
## (Intercept)  8.09011527  8.08935394  8.08862146  8.08793054  8.08727098
## V1          -0.14606257 -0.14719352 -0.14824987 -0.14921624 -0.15011520
## V2          -0.16649386 -0.16766459 -0.16877053 -0.16979410 -0.17075592
## V3           0.03763241  0.03739356  0.03717453  0.03697953  0.03680099
## V4          -0.18919074 -0.19347289 -0.19758499 -0.20145156 -0.20513769
##                     s99
## (Intercept)  8.08664325
## V1          -0.15094837
## V2          -0.17165664
## V3           0.03663899
## V4          -0.20863833