-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mixed-Penalties.Rmd
276 lines (228 loc) · 12.8 KB
/
Mixed-Penalties.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
---
title: "Mixed Penalties"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Mixed Penalties}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
> **This is an experimental feature. There may be bugs; use carefully!**
```r
library(lessSEM)
```
The `mixedPenalty` function allows you to add multiple penalties to a single model.
For instance, you may want to regularize both loadings and regressions in a SEM.
In this case, using the same penalty (e.g., lasso) for both types of penalties may
actually not be what you want to use because the penalty function is sensitive to
the scales of the parameters. Instead, you may want to use two separate lasso
penalties for loadings and regressions. Similarly, separate penalties for
different parameters have, for instance, been proposed in multi-group models
(Geminiani et al., 2021).
> Important: You cannot impose two penalties on the same parameter!
Models are fitted with the glmnet or ista optimizer. Note that the
optimizers differ in which penalties they support. The following table provides
an overview:
| Penalty | Function | glmnet | ista |
| --- | ---- | ---- | ---- |
| lasso | addLasso | x | x |
| elastic net | addElasticNet | x* | - |
| cappedL1 | addCappedL1 | x | x |
| lsp | addLsp | x | x |
| scad | addScad | x | x |
| mcp | addMcp | x | x |
By default, glmnet will be used. Note that the elastic net penalty can
only be combined with other elastic net penalties.
## Getting Started
In the following model, we will allow for cross-loadings (`c2`-`c4`).
We want to regularize both, cross-loadings and regression coefficients (`r1` - `r3`)
```r
model <- '
# latent variable definitions
ind60 =~ x1 + x2 + x3 + c2*y2 + c3*y3 + c4*y4
dem60 =~ y1 + y2 + y3 + y4
dem65 =~ y5 + y6 + y7 + c*y8
# regressions
dem60 ~ r1*ind60
dem65 ~ r2*ind60 + r3*dem60
'
lavaanModel <- sem(model,
data = PoliticalDemocracy)
```
Next, we add separate lasso penalties for the loadings and the regressions:
```r
mp <- lavaanModel |>
mixedPenalty() |>
addLasso(regularized = c("c2", "c3", "c4"),
lambdas = seq(0,1,.1)) |>
addLasso(regularized = c("r1", "r2", "r3"),
lambdas = seq(0,1,.2))
```
Note that we can use the pipe-operator to add multiple penalties. They
don't have to be the same; the following would also work:
```r
mp <- lavaanModel |>
mixedPenalty() |>
addLasso(regularized = c("c2", "c3", "c4"),
lambdas = seq(0,1,.1)) |>
addScad(regularized = c("r1", "r2", "r3"),
lambdas = seq(0,1,.2),
thetas = 3.7)
```
To fit the model, we use the `fit`- function:
```r
fitMp <- fit(mp)
```
To check which parameter has be regularized with which penalty, we can look
at the `penalty` statement in the resulting object:
```r
fitMp@penalty
#> ind60=~x2 ind60=~x3 c2 c3 c4 dem60=~y2 dem60=~y3 dem60=~y4 dem65=~y6
#> "none" "none" "lasso" "lasso" "lasso" "none" "none" "none" "none"
#> dem65=~y7 c r1 r2 r3 x1~~x1 x2~~x2 x3~~x3 y2~~y2
#> "none" "none" "scad" "scad" "scad" "none" "none" "none" "none"
#> y3~~y3 y4~~y4 y1~~y1 y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 dem60~~dem60
#> "none" "none" "none" "none" "none" "none" "none" "none" "none"
#> dem65~~dem65
#> "none"
```
We can access the best parameters according to the BIC with:
```r
coef(fitMp, criterion = "BIC")
#>
#> Tuning ||--|| Estimates
#> ---------------------------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
#> tuningParameterConfiguration ||--|| ind60=~x2 ind60=~x3 c2 c3 c4 dem60=~y2 dem60=~y3 dem60=~y4
#> ============================ ||--|| ========== ========== ========== ========== ========== ========== ========== ==========
#> 11.0000 ||--|| 2.1817 1.8188 . . . 1.3540 1.0440 1.2995
#>
#>
#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
#> dem65=~y6 dem65=~y7 c r1 r2 r3 x1~~x1 x2~~x2 x3~~x3 y2~~y2 y3~~y3
#> ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ==========
#> 1.2585 1.2825 1.3098 1.4738 0.4533 0.8644 0.0818 0.1184 0.4673 6.4896 5.3399
#>
#>
#> ---------- ---------- ---------- ---------- ---------- ---------- ------------ ------------ ------------
#> y4~~y4 y1~~y1 y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 dem60~~dem60 dem65~~dem65
#> ========== ========== ========== ========== ========== ========== ============ ============ ============
#> 2.8871 1.9419 2.3901 4.3428 3.5096 2.9403 0.4482 3.8717 0.1149
```
The `tuningParameterConfiguration` refers to the rows in the lambda, theta, and alpha
matrices that resulted in the best fit:
```r
getTuningParameterConfiguration(regularizedSEMMixedPenalty = fitMp,
tuningParameterConfiguration = 11)
#> parameter penalty lambda alpha
#> ind60=~x2 ind60=~x2 none 0 0
#> ind60=~x3 ind60=~x3 none 0 0
#> c2 c2 lasso 1 1
#> c3 c3 lasso 1 1
#> c4 c4 lasso 1 1
#> dem60=~y2 dem60=~y2 none 0 0
#> dem60=~y3 dem60=~y3 none 0 0
#> dem60=~y4 dem60=~y4 none 0 0
#> dem65=~y6 dem65=~y6 none 0 0
#> dem65=~y7 dem65=~y7 none 0 0
#> c c none 0 0
#> r1 r1 scad 0 0
#> r2 r2 scad 0 0
#> r3 r3 scad 0 0
#> x1~~x1 x1~~x1 none 0 0
#> x2~~x2 x2~~x2 none 0 0
#> x3~~x3 x3~~x3 none 0 0
#> y2~~y2 y2~~y2 none 0 0
#> y3~~y3 y3~~y3 none 0 0
#> y4~~y4 y4~~y4 none 0 0
#> y1~~y1 y1~~y1 none 0 0
#> y5~~y5 y5~~y5 none 0 0
#> y6~~y6 y6~~y6 none 0 0
#> y7~~y7 y7~~y7 none 0 0
#> y8~~y8 y8~~y8 none 0 0
#> ind60~~ind60 ind60~~ind60 none 0 0
#> dem60~~dem60 dem60~~dem60 none 0 0
#> dem65~~dem65 dem65~~dem65 none 0 0
```
In this case, the best model has no cross-loadings, but the regressions remained
unregularized: The lambda for the cross-loadings is large (1), while the
lambda for the regressions is 0 (no regularization).
## Using ista
The glmnet optimizer is typically considerably faster than ista. However, sometimes
glmnet may run into issues. In that case, it can help to switch to ista:
```r
mp <- lavaanModel |>
# Change the optimizer and the control object:
mixedPenalty(method = "ista",
control = controlIsta()) |>
addLasso(regularized = c("c2", "c3", "c4"),
lambdas = seq(0,1,.1)) |>
addLasso(regularized = c("r1", "r2", "r3"),
lambdas = seq(0,1,.2))
```
To fit the model, we use the `fit`- function:
```r
fitMp <- fit(mp)
```
```r
coef(fitMp, criterion = "BIC")
#>
#> Tuning ||--|| Estimates
#> ---------------------------- ||--|| ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
#> tuningParameterConfiguration ||--|| ind60=~x2 ind60=~x3 c2 c3 c4 dem60=~y2 dem60=~y3 dem60=~y4
#> ============================ ||--|| ========== ========== ========== ========== ========== ========== ========== ==========
#> 11.0000 ||--|| 2.1818 1.8188 . . . 1.3541 1.0441 1.2997
#>
#>
#> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
#> dem65=~y6 dem65=~y7 c r1 r2 r3 x1~~x1 x2~~x2 x3~~x3 y2~~y2 y3~~y3
#> ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ==========
#> 1.2586 1.2825 1.3099 1.4740 0.4532 0.8643 0.0818 0.1184 0.4672 6.4892 5.3396
#>
#>
#> ---------- ---------- ---------- ---------- ---------- ---------- ------------ ------------ ------------
#> y4~~y4 y1~~y1 y5~~y5 y6~~y6 y7~~y7 y8~~y8 ind60~~ind60 dem60~~dem60 dem65~~dem65
#> ========== ========== ========== ========== ========== ========== ============ ============ ============
#> 2.8861 1.9420 2.3904 4.3421 3.5090 2.9392 0.4482 3.8710 0.1159
```
The `tuningParameterConfiguration` refers to the rows in the lambda, theta, and alpha
matrices that resulted in the best fit:
```r
getTuningParameterConfiguration(regularizedSEMMixedPenalty = fitMp,
tuningParameterConfiguration = 11)
#> parameter penalty lambda theta alpha
#> ind60=~x2 ind60=~x2 none 0 0 0
#> ind60=~x3 ind60=~x3 none 0 0 0
#> c2 c2 lasso 1 0 1
#> c3 c3 lasso 1 0 1
#> c4 c4 lasso 1 0 1
#> dem60=~y2 dem60=~y2 none 0 0 0
#> dem60=~y3 dem60=~y3 none 0 0 0
#> dem60=~y4 dem60=~y4 none 0 0 0
#> dem65=~y6 dem65=~y6 none 0 0 0
#> dem65=~y7 dem65=~y7 none 0 0 0
#> c c none 0 0 0
#> r1 r1 lasso 0 0 1
#> r2 r2 lasso 0 0 1
#> r3 r3 lasso 0 0 1
#> x1~~x1 x1~~x1 none 0 0 0
#> x2~~x2 x2~~x2 none 0 0 0
#> x3~~x3 x3~~x3 none 0 0 0
#> y2~~y2 y2~~y2 none 0 0 0
#> y3~~y3 y3~~y3 none 0 0 0
#> y4~~y4 y4~~y4 none 0 0 0
#> y1~~y1 y1~~y1 none 0 0 0
#> y5~~y5 y5~~y5 none 0 0 0
#> y6~~y6 y6~~y6 none 0 0 0
#> y7~~y7 y7~~y7 none 0 0 0
#> y8~~y8 y8~~y8 none 0 0 0
#> ind60~~ind60 ind60~~ind60 none 0 0 0
#> dem60~~dem60 dem60~~dem60 none 0 0 0
#> dem65~~dem65 dem65~~dem65 none 0 0 0
```
Here is a short run-time comparison of ista and glmnet with the lasso-regularized
model from above: Five repetitions using ista took 24.509 seconds,
while glmnet took 1.131 seconds. That is, if you
can use glmnet with your model, we recommend that you do.
## Bibliography
- Geminiani, E., Marra, G., & Moustaki, I. (2021). Single- and multiple-group penalized factor analysis:
A trust-region algorithm approach with integrated automatic multiple tuning parameter selection.
Psychometrika, 86(1), 65–95. https://doi.org/10.1007/s11336-021-09751-8