statisfactions / simpr

Tidyverse-friendly simulations and power analysis
42 stars 6 forks source link

Vignette: speed optimization in simpr #48

Closed statisfactions closed 2 years ago

statisfactions commented 2 years ago

Worked example, etc. for working with futures in simpr.

library(simpr)
#> 
#> Attaching package: 'simpr'
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(future)
library(tictoc)

tic()
plan(sequential)
sim_ref = specify(x1 = ~ 2 + b + rnorm(n)) %>%
  define(n = 1:100,
         b = 1:10) %>%
  generate(10) %>%
  fit(dumb_model = ~ lm(x1 ~ 1)) %>% 
  tidy_fits()
toc()
#> 141.199 sec elapsed

availableCores()
#> system 
#>      8
plan(multisession, workers = availableCores() - 1)
tic()
sim_ref = specify(x1 = ~ 2 + b + rnorm(n)) %>%
  define(n = 1:100,
         b = 1:10) %>%
  fit(dumb_model = ~ lm(x1 ~ 1)) %>% 
  tidy_fits() %>% 
  generate(10) 
toc()
#> 188.586 sec elapsed

Created on 2021-11-17 by the reprex package (v2.0.1)