-
Notifications
You must be signed in to change notification settings - Fork 2
/
scaling.Rmd
447 lines (315 loc) · 10.2 KB
/
scaling.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
---
title: "Computational Scaling"
output:
rmarkdown::html_vignette:
fig_width: 6
fig_height: 4
bibliography: ../inst/REFERENCES.bib
link-citations: yes
vignette: >
%\VignetteIndexEntry{Computational Scaling}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```r
library(galamm)
```
This vignette describes the computational scaling properties of `galamm` for selected example models. We focus on the relationship between the size of the input data and the speed of the algorithm.
We will fit models to different random subsets of the input data, and here specify the number of such random subsets:
```r
n_replicates <- 20
```
## Linear Mixed Models with Factor Structures
We start with the linear mixed model with factor structures described in [another vignette](https://lcbc-uio.github.io/galamm/articles/lmm_factor.html). Our goal here is to study how the computing time scales with the number of observations.
```r
data(KYPSsim, package = "PLmixed")
```
We start by defining the necessary arguments to provide to galamm.
```r
loading_matrix <- rbind(
c(1, 0),
c(NA, 0),
c(NA, 1),
c(NA, NA)
)
factors <- c("ms", "hs")
load.var <- "time"
form <- esteem ~ time + (0 + ms | mid) + (0 + hs | hid) + (1 | sid)
```
The KYPSsim dataset has 11,494 rows.
```r
nrow(KYPSsim)
#> [1] 11494
```
There are 2,924 unique student IDs, and we will fit models by extracting subsets of these students of increasing size.
```r
all_sids <- unique(KYPSsim$sid)
length(all_sids)
#> [1] 2924
```
We set up a simulation grid in `sim_params`, with five different samples sizes, and specifying that we want ten random replicates for each sample size.
```r
sim_params <- expand.grid(
n_students = c(100, 500, 1000, 2000, 2924),
replicate = seq(from = 1, to = n_replicates, by = 1)
)
```
```r
set.seed(11)
times_to_convergence <- Map(function(n, r) {
sids <- sample(all_sids, size = n)
dat <- KYPSsim[KYPSsim$sid %in% sids, ]
t1 <- Sys.time()
mod <- galamm(
formula = form,
data = dat,
factor = factors,
load.var = load.var,
lambda = loading_matrix
)
t2 <- Sys.time()
data.frame(
n = n,
nrow = nrow(dat),
time = t2 - t1
)
},
n = sim_params$n_students,
r = sim_params$replicate
)
```
The plot below suggests that for this model, the algorithm scales linearly in the number of IDs.
```r
plot_dat <- do.call(rbind, times_to_convergence)
fit <- lm(time ~ n, data = plot_dat)
plot(plot_dat$n, plot_dat$time, xlab = "Number of IDs",
ylab = "Time until convergence")
abline(fit)
```
![Time until convergence for different model sizes of linear mixed model with factor structures. Black line shows linear model fit to the data.](scaling-lmm-plot-1.png)
## Model with Group-Wise Heteroscedasticity
We now consider the model with group-wise heteroscedasticity consider in [the vignette on heteroscedastic linear mixed models](https://lcbc-uio.github.io/galamm/articles/lmm_heteroscedastic.html).
There 1200 rows and 200 unique IDs in the dataset. We take subset of IDs, while keeping the remaining model structure unchanged.
```r
nrow(hsced)
#> [1] 1200
all_ids <- unique(hsced$id)
length(all_ids)
#> [1] 200
```
```r
sim_params <- expand.grid(
n_ids = c(100, 150, 200),
replicate = seq(from = 1, to = n_replicates, by = 1)
)
```
```r
set.seed(11)
times_to_convergence <- Map(function(n, r) {
ids <- sample(all_ids, size = n)
dat <- hsced[hsced$id %in% ids, ]
t1 <- Sys.time()
mod <- galamm(
formula = y ~ x + (1 | id),
weights = ~ (1 | item),
data = dat
)
t2 <- Sys.time()
data.frame(
n = n,
nrow = nrow(dat),
time = t2 - t1
)
},
n = sim_params$n_ids,
r = sim_params$replicate
)
```
A plot of the time until convergence suggests that also in this case the algorithm scales linearly with the unput size.
```r
plot_dat <- do.call(rbind, times_to_convergence)
fit <- lm(time ~ n, data = plot_dat)
plot(plot_dat$n, plot_dat$time, xlab = "Number of IDs",
ylab = "Time until convergence")
abline(fit)
```
![Time until convergence as a function of input data size for heteroscedastic linear mixed model. Black line shows linear model fit to the data.](scaling-hsced-plot-1.png)
## Generalized Linear Mixed Models with Factor Structures
We now consider an example described in the [vignette on generalized linear mixed models with factor structures](https://lcbc-uio.github.io/galamm/articles/glmm_factor.html).
```r
data(IRTsim, package = "PLmixed")
```
This dataset has 2,500 rows and 500 student IDs.
```r
nrow(IRTsim)
#> [1] 2500
all_sids <- unique(IRTsim$sid)
length(all_sids)
#> [1] 500
```
We again sample subsets of student IDs.
```r
sim_params <- expand.grid(
n_students = seq(from = 100, to = 500, by = 100),
replicate = seq(from = 1, to = n_replicates, by = 1)
)
```
```r
set.seed(11)
times_to_convergence <- Map(function(n, r) {
sids <- sample(all_sids, size = n)
dat <- IRTsim[IRTsim$sid %in% sids, ]
t1 <- Sys.time()
mod <- galamm(
formula = y ~ item + (0 + ability | school / sid),
data = IRTsim,
family = binomial,
load.var = "item",
factor = "ability",
lambda = matrix(c(1, NA, NA, NA, NA), ncol = 1)
)
t2 <- Sys.time()
data.frame(
n = n,
nrow = nrow(dat),
time = t2 - t1
)
},
n = sim_params$n_students,
r = sim_params$replicate
)
```
The plot below suggests that for this model and for the range of IDs considered here, there is almost no relationship between the input data size and the time until convergence.
```r
plot_dat <- do.call(rbind, times_to_convergence)
fit <- lm(time ~ n, data = plot_dat)
plot(plot_dat$n, plot_dat$time, xlab = "Number of IDs",
ylab = "Time until convergence")
abline(fit)
```
![Time until convergence for different model sizes of generalized linear mixed model with factor structures. Black line shows linear model fit to the data.](scaling-glmm-plot-1.png)
We speculate that this is caused by the fact that for non-Gaussian models, a penalized iteratively reweighted least squares algorithm has to be run to find the posterior modes of the random effect for each candidate value of the model parameters, whereas for models with Gaussian responses, the linear system is solved exactly in a single iteration. There might hence be a trade-off: by increasing the number of observations, fewer iterations are required to reach convergence, since uncertainty is reduced, while on the other hand, the time taken to solve a single system increases with the sample size.
## Semiparametric Model with Gaussian Responses
We can study the scaling issue further in semiparametric models with factor structures, which are described in [a separate vignette](https://lcbc-uio.github.io/galamm/articles/semiparametric.html).
The data for domain 1 has 4,800 rows and 200 unique IDs.
```r
dat <- subset(cognition, domain == 1)
nrow(dat)
#> [1] 4800
all_ids <- unique(dat$id)
length(all_ids)
#> [1] 200
```
We define the simulation parameters as before.
```r
sim_params <- expand.grid(
n_ids = seq(from = 50, to = 200, by = 50),
replicate = seq(from = 1, to = n_replicates, by = 1)
)
```
Then we fit the model for different subsets of the data.
```r
set.seed(11)
times_to_convergence <- Map(function(n, r) {
ids <- sample(all_ids, size = n)
dat <- dat[dat$id %in% ids, ]
t1 <- Sys.time()
mod <- galamm(
formula = y ~ 0 + item + sl(x, factor = "loading") +
(0 + loading | id / timepoint),
data = dat,
load.var = "item",
lambda = matrix(c(1, NA, NA), ncol = 1),
factor = "loading"
)
t2 <- Sys.time()
data.frame(
n = n,
nrow = nrow(dat),
time = t2 - t1
)
},
n = sim_params$n_ids,
r = sim_params$replicate
)
```
The plot below suggests that for this model, the algorithm scales linearly in the number of IDs.
```r
plot_dat <- do.call(rbind, times_to_convergence)
fit <- lm(time ~ n, data = plot_dat)
plot(plot_dat$n, plot_dat$time, xlab = "Number of IDs",
ylab = "Time until convergence")
abline(fit)
```
![Time until convergence for different model sizes of semiparametric model with factor structures. Black line shows linear model fit to the data.](scaling-semiparametric-gaussian-plot-1.png)
## Semiparametric Model with Binomial Responses
We now consider domain 2, which has binomial responses.
The data for domain 2 has 3,200 rows and 200 unique IDs.
```r
dat <- subset(cognition, domain == 2)
nrow(dat)
#> [1] 3200
all_ids <- unique(dat$id)
length(all_ids)
#> [1] 200
```
We define the simulation parameters as before.
```r
sim_params <- expand.grid(
n_ids = seq(from = 50, to = 200, by = 50),
replicate = seq(from = 1, to = n_replicates, by = 1)
)
```
Then we fit the model for different subsets of the data.
```r
set.seed(11)
times_to_convergence <- Map(function(n, r) {
ids <- sample(all_ids, size = n)
dat <- dat[dat$id %in% ids, ]
dt <- tryCatch({
t1 <- Sys.time()
mod <- galamm(
formula = y ~ 0 + item + sl(x, factor = "loading") +
(0 + loading | id / timepoint),
data = dat,
family = binomial,
load.var = "item",
lambda = matrix(c(1, NA), ncol = 1),
factor = "loading"
)
t2 <- Sys.time()
t2 - t1
},
error = function(e) {
NA
})
data.frame(
n = n,
nrow = nrow(dat),
time = dt
)
},
n = sim_params$n_ids,
r = sim_params$replicate
)
```
```r
plot_dat <- do.call(rbind, times_to_convergence)
```
We check how many models failed to converge, and then remove those rows:
```r
sum(is.na(plot_dat))
#> [1] 1
plot_dat <- na.omit(plot_dat)
```
The plot below suggests that for this model, the algorithm scales linearly in the number of IDs.
```r
fit <- lm(time ~ n, data = plot_dat)
plot(plot_dat$n, plot_dat$time, xlab = "Number of IDs",
ylab = "Time until convergence")
abline(fit)
```
![Time until convergence for different model sizes of semiparametric model with factor structures. Black line shows linear model fit to the data.](scaling-semiparametric-binomial-plot-1.png)
## Conclusion
The algorithm used by galamm, which is described in detail in @sorensenLongitudinalModelingAgeDependent2023, seems to scale linearly with the input data size in the tests considered in this vignette, with the exception of the the generalized linear mixed model with factor structures, for which the relationship was almost flat.
# References