Skip to content

Commit

Permalink
docs: Updated examples and vignettes
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiaslj committed Dec 9, 2024
1 parent f535970 commit 9ddb63e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 30 deletions.
26 changes: 15 additions & 11 deletions R/measure_performance.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ generate_and_compare_eta_comptime <- function(n_vars,
#' @examples
#' # Compare the runtime for 2, 20, 40, 60 variables in the model
#' compare_eta_comptime_across_nvars(n_vars = c(2, seq(from = 20, to = 60, by = 20)),
#' n_samples = 100,
#' burnin = 20)
#' n_samples = 1,
#' burnin = 0)
compare_eta_comptime_across_nvars <- function(n_vars,
n = 100,
beta_prior = distributional::dist_normal(0, 1),
Expand Down Expand Up @@ -156,9 +156,10 @@ compare_eta_comptime_across_nvars <- function(n_vars,
#'
#' @param eta_comptime_data a `data.frame` with results of computation times. Result of a call to
#' [compare_eta_comptime_across_nvars]
#' @param facet_by a `character` with the variable to facet the plots by. Default is "qslice_fun",
#' enabling the user to combine results of [compare_eta_comptime_across_nvars] across different slice
#' functions and plot them easily. Other options are fx. running
#' @param facet_by a `character` with the variable to facet the plots by. Default is NULL,
#' which does not produce any facets. However, a user could specify fx. "qslice_fun" after having
#' run [compare_eta_comptime_across_nvars] across different slice
#' functions. Other options are fx. running
#' [compare_eta_comptime_across_nvars] for different values of a tuning parameter and seeing how that
#' affects runtime
#'
Expand All @@ -168,16 +169,19 @@ compare_eta_comptime_across_nvars <- function(n_vars,
#' @examples
#' # Compare the runtime for 2, 20, 40, 60 variables in the model
#' res <- compare_eta_comptime_across_nvars(n_vars = c(2, seq(from = 20, to = 60, by = 20)),
#' n_samples = 100,
#' burnin = 20)
#' n_samples = 1,
#' burnin = 0)
#' plot_eta_comptime(res)
plot_eta_comptime <- function(eta_comptime_data, facet_by = "qslice_fun") {
eta_comptime_data %>%
plot_eta_comptime <- function(eta_comptime_data, facet_by = NULL) {
p <- eta_comptime_data %>%
dplyr::mutate(time = as.numeric(time)) %>%
ggplot2::ggplot(ggplot2::aes(x = n_vars, y = time, col = linear_predictor_calc)) +
ggplot2::geom_line() +
ggplot2::geom_point() +
ggplot2::theme_bw() +
ggplot2::labs(y = "Computation time (seconds)", x = "Dimension of parameter vector") +
ggplot2::facet_wrap(facet_by, labeller = "label_both")
ggplot2::labs(y = "Computation time (seconds)", x = "Dimension of parameter vector")

if (!is.null(facet_by)) p <- p + ggplot2::facet_wrap(facet_by, labeller = "label_both")

return(p)
}
4 changes: 2 additions & 2 deletions man/compare_eta_comptime_across_nvars.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions man/plot_eta_comptime.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 16 additions & 7 deletions vignettes/performance.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,31 @@ withr::local_seed(42)

## Graph of compute time as function of dimension

### Using default slice function
We can use the functions `compare_eta_comptime_across_nvars()` and `plot_eta_comptime()` to investigate computation time of the algorithm for varying dimension of the parameter vector $\beta$. This can also be used to investigate computation time for different sample methods, different tuning parameters, or whatever the user might be interested in.

The basic usage of the function can be used to investigate the computational gain for high dimensions of the CGGibbs sampler compared to the naive approach of doing $d$ computation for each coordinate update. For this purpose, the function call is:
```{r run-compare-nvars}
res <- compare_eta_comptime_across_nvars(
n_vars = c(2, seq(from = 40, to = 400, by = 40)),
n_vars = c(2, seq(from = 50, to = 1000, by = 50)),
n_samples = 1,
burnin = 0,
w = 0.5)
```

The results can be plotted by simply calling
```{r show-compare}
plot_eta_comptime(res, facet_by = "w")
plot_eta_comptime(res)
```

Could run over different slice widths if interested
### Runtime across dimensions for different sampling methods

The user can easily just iterate the function across different sampling procedures or tuning parameters for a single sample procedure, bind the results, and then use the `facet_by` argument in `plot_eta_comptime()`.

An example of this could look like:
> We won't run this code for this vignette to reduce rendering time, but it provides an idea to what is possible using the functionalities.
```{r run-compare_ws, eval = FALSE}
ws <- c(0.5, 10)
ws <- c(0.5, 5, 20, 100)
res <- lapply(ws, function(w) {
compare_eta_comptime_across_nvars(
w = w,
Expand All @@ -49,6 +58,6 @@ res <- lapply(ws, function(w) {
parallelise = TRUE)
}) %>%
dplyr::bind_rows()
```
## Versus naive computation
plot_eta_comptime(res, facet_by = "w")
```
8 changes: 4 additions & 4 deletions vignettes/pospkg.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,9 @@ The utility functions `mcmcglm_across_tuningparams()` and `plot_mcmcglm_across_t

Fx. for the default choice of slice sampler, `qslice::slice_stepping_out()`, the tuning parameter is the slice width `w`. A way to create a plot of samples across different values of the slice width is seen below:

```{r vary-w, fig.height = 5}
```{r vary-w, fig.height = 4}
w05_mcmcglms <- mcmcglm_across_tuningparams(
seq(from = 0.5, by = 0.5, length.out = 4),
c(0.5, 5, 20, 100),
tuning_parameter_name = "w",
formula = Y ~ .,
family = "gaussian",
Expand All @@ -321,9 +321,9 @@ Since the utility function work generally for any single tuning parameter, below

> Note that only a single tuning parameter can be "varied" at a time. So in the below example, we have to keep `sigma` and `mu` constant, while we vary over `df`
```{r vary-df, fig.height = 5}
```{r vary-df, fig.height = 4}
df1_mcmcglms <- mcmcglm_across_tuningparams(
c(1, seq(from = 10, by = 10, length.out = 3)),
c(1, 10, 50, 200),
tuning_parameter_name = "df",
formula = Y ~ .,
family = "gaussian",
Expand Down

0 comments on commit 9ddb63e

Please sign in to comment.