Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document how to run in parallel #20

Merged
merged 6 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/multi.R
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ qmulti_est <- function(x, dist, p) {

qmulti_ranges <- function(p, list) {
elist <- purrr::imap(list, qmulti_est, p = p)
tlist <- purrr::transpose(elist)
tlist <- purrr::list_transpose(elist)
tlist <- purrr::map(tlist, unlist)
min <- purrr::map_dbl(tlist, min)
max <- purrr::map_dbl(tlist, max)
Expand Down
8 changes: 4 additions & 4 deletions R/test-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

local_multisession <- function(.local_envir = parent.frame()) {
local_multisession <- function(.local_envir = parent.frame(), workers = 2) {
oldDoPar <- doFuture::registerDoFuture()
withr::defer(with(oldDoPar, foreach::setDoPar(fun = fun, data = data, info = info)))
oldPlan <- future::plan("future::multisession")
withr::defer(future::plan(oldPlan))
withr::defer_parent(with(oldDoPar, foreach::setDoPar(fun = fun, data = data, info = info)))
oldPlan <- future::plan("future::multisession", workers = workers)
withr::defer_parent(future::plan(oldPlan))
invisible(oldDoPar)
}

Expand Down
10 changes: 8 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ ssddata::ccme_boron

Distributions are fit using `ssd_fit_dists()`
```{r}
fits <- ssd_fit_dists(ssddata::ccme_boron)
fits <- ssd_fit_dists(ssddata::ccme_boron, dists = c("lnorm", "llogis"))
```

and can be quickly plotted using `autoplot`
Expand All @@ -70,13 +70,19 @@ The goodness of fit can be assessed using `ssd_gof`
ssd_gof(fits)
```

and the model-averaged 5% hazard concentration estimated by parametric bootstrapping using `ssd_hc`
and the model-averaged 5% hazard concentration estimated by parametric bootstrapping using `ssd_hc`.
```{r}
set.seed(99)
hc5 <- ssd_hc(fits, ci = TRUE, nboot = 100) # 100 bootstrap samples for speed
print(hc5)
```

To bootstrap in parallel set `future::plan()`. For example:
```{r, eval=FALSE}
future::multisession(workers = 2)
hc5 <- ssd_hc(fits, ci = TRUE, nboot = 100)
```

Model-averaged predictions complete with confidence intervals can also be estimated by parametric bootstrapping using the `stats` generic `predict`.
To perform bootstrapping for each distribution in parallel register the future backend
and then select the evaluation strategy.
Expand Down
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ ssddata::ccme_boron
Distributions are fit using `ssd_fit_dists()`

``` r
fits <- ssd_fit_dists(ssddata::ccme_boron)
fits <- ssd_fit_dists(ssddata::ccme_boron, dists = c("lnorm", "llogis"))
```

and can be quickly plotted using `autoplot`
Expand All @@ -88,19 +88,15 @@ The goodness of fit can be assessed using `ssd_gof`

``` r
ssd_gof(fits)
#> # A tibble: 6 × 9
#> dist ad ks cvm aic aicc bic delta weight
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 gamma 0.440 0.117 0.0554 238. 238. 240. 0.005 0.357
#> 2 lgumbel 0.829 0.158 0.134 244. 245. 247. 6.56 0.013
#> 3 llogis 0.487 0.0994 0.0595 241. 241. 244. 3.39 0.066
#> 4 lnorm 0.507 0.107 0.0703 239. 240. 242. 1.40 0.177
#> 5 lnorm_lnorm 0.320 0.116 0.0414 240. 243. 247. 4.98 0.03
#> 6 weibull 0.434 0.117 0.0542 238. 238. 240. 0 0.357
#> # A tibble: 2 × 9
#> dist ad ks cvm aic aicc bic delta weight
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 lnorm 0.507 0.107 0.0703 239. 240. 242. 0 0.73
#> 2 llogis 0.487 0.0994 0.0595 241. 241. 244. 1.99 0.27
```

and the model-averaged 5% hazard concentration estimated by parametric
bootstrapping using `ssd_hc`
bootstrapping using `ssd_hc`.

``` r
set.seed(99)
Expand All @@ -109,7 +105,14 @@ print(hc5)
#> # A tibble: 1 × 10
#> dist percent est se lcl ucl wt method nboot pboot
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <dbl> <dbl>
#> 1 average 5 1.24 0.741 0.510 3.35 1 parametric 100 1
#> 1 average 5 1.65 0.599 0.923 3.34 1 parametric 100 1
```

To bootstrap in parallel set `future::plan()`. For example:

``` r
future::multisession(workers = 2)
hc5 <- ssd_hc(fits, ci = TRUE, nboot = 100)
```

Model-averaged predictions complete with confidence intervals can also
Expand All @@ -136,7 +139,7 @@ ssd_plot(ssddata::ccme_boron, boron_pred,
scale_colour_ssd()
```

![](man/figures/README-unnamed-chunk-9-1.png)<!-- -->
![](man/figures/README-unnamed-chunk-10-1.png)<!-- -->

## References

Expand Down
Binary file added man/figures/README-unnamed-chunk-10-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-5-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions tests/testthat/_snaps/hc/hcici.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist,percent,est,se,lcl,ucl,wt,method,nboot,pboot
average,5,1.68117,0.455819,0.998397,2.43078,1,parametric,10,1
2 changes: 2 additions & 0 deletions tests/testthat/_snaps/hc/hcici_multi.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist,percent,est,se,lcl,ucl,wt,method,nboot,pboot
average,5,1.68117,0.455819,0.998397,2.43078,1,parametric,10,1
15 changes: 13 additions & 2 deletions tests/testthat/test-hc.R
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ test_that("ssd_hc calculates cis with equally weighted data", {
fits <- ssd_fit_dists(data, weight = "Weight", dists = "lnorm")
set.seed(10)
hc <- ssd_hc(fits, ci = TRUE, nboot = 10, multi = FALSE)
expect_equal(hc$se, 0.455819097122445)
expect_snapshot_data(hc, "hcici")
})

test_that("ssd_hc calculates cis in parallel but one distribution", {
Expand All @@ -330,7 +330,7 @@ test_that("ssd_hc calculates cis in parallel but one distribution", {
fits <- ssd_fit_dists(data, dists = "lnorm")
set.seed(10)
hc <- ssd_hc(fits, ci = TRUE, nboot = 10, multi = FALSE)
expect_equal(hc$se, 0.455819097122445)
expect_snapshot_data(hc, "hcici_multi")
})

test_that("ssd_hc calculates cis with two distributions", {
Expand Down Expand Up @@ -675,3 +675,14 @@ test_that("not all estimates if fail", {
"estimates_000000005_multi.rds", "estimates_000000006_multi.rds",
"estimates_000000007_multi.rds", "estimates_000000009_multi.rds", "estimates_000000010_multi.rds"))
})

test_that("ssd_hc identical if in parallel", {
data <- ssddata::ccme_boron
fits <- ssd_fit_dists(data, dists = c("lnorm", "llogis"))
set.seed(10)
hc <- ssd_hc(fits, ci = TRUE, nboot = 500)
local_multisession(workers = 2)
set.seed(10)
hc2 <- ssd_hc(fits, ci = TRUE, nboot = 500)
expect_identical(hc, hc2)
})
Loading