harvard-ufds / saeczi

Small Area Estimation for Continuous Zero Inflated data
https://harvard-ufds.github.io/saeczi/
Other
4 stars 2 forks source link

speedup bootstrap model fitting by using `start` values from original model fit? #29

Closed joshyam-k closed 3 months ago

joshyam-k commented 6 months ago

Both lmer and glmer allow a user to specify start values for certain parameters involved in the optimization steps that the functions perform. It seems like this does marginally speed things up, but we'll want to be sure that it doesn't constrain the models or bias our results.

library(saeczi)
library(lme4)
#> Loading required package: Matrix
data(samp)

orig_lm <- lmer(DRYBIO_AG_TPA_live_ADJ ~ tcc16 + (1 | COUNTYFIPS), samp)
orig_glm <- glmer(DRYBIO_AG_TPA_live_ADJ != 0 ~ tcc16 + (1 | COUNTYFIPS),
                  family = "binomial", samp)

bench::mark(
  lmer(DRYBIO_AG_TPA_live_ADJ ~ tcc16 + (1 | COUNTYFIPS), samp),
  lmer(DRYBIO_AG_TPA_live_ADJ ~ tcc16 + (1 | COUNTYFIPS),
       start = list(theta = orig_lm@theta), samp),
  check = FALSE,
  iterations = 100
)
#> # A tibble: 2 × 6
#>   expression                             min median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>                         <bch:t> <bch:>     <dbl> <bch:byt>    <dbl>
#> 1 lmer(DRYBIO_AG_TPA_live_ADJ ~ tcc… 11.57ms 12.2ms      77.3    1.98MB     12.6
#> 2 lmer(DRYBIO_AG_TPA_live_ADJ ~ tcc…  9.96ms 10.7ms      92.7    1.87MB     13.8

bench::mark(
  glmer(DRYBIO_AG_TPA_live_ADJ != 0 ~ tcc16 + (1 | COUNTYFIPS),
        family = "binomial", samp),
  glmer(DRYBIO_AG_TPA_live_ADJ != 0 ~ tcc16 + (1 | COUNTYFIPS),
        start = list(theta = orig_glm@theta, fixef = orig_glm@beta), 
        family = "binomial", samp),
  check = FALSE,
  iterations = 100
)
#> # A tibble: 2 × 6
#>   expression                             min median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>                          <bch:> <bch:>     <dbl> <bch:byt>    <dbl>
#> 1 "glmer(DRYBIO_AG_TPA_live_ADJ != 0… 94.1ms   97ms      9.93    6.43MB     6.90
#> 2 "glmer(DRYBIO_AG_TPA_live_ADJ != 0… 88.7ms 90.8ms     10.2     6.18MB     2.88