Skip to content

Commit

Permalink
fix default plot parameter selection logic, fix n_params for plot_typ…
Browse files Browse the repository at this point in the history
…e trace
  • Loading branch information
santikka committed May 9, 2024
1 parent 65c77b8 commit e2602ac
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
27 changes: 15 additions & 12 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ plot_dynamiteformula_ggplot <- function(g, vertex_size, label_size) {
#' is provided, the same limit is used for all parameters. If a vector is
#' supplied, the first element defines the maximum number of time-invariant
#' parameters to plot and the second the maximum number of time-varying
#' parameters to plot. The defaults values are 50 for time-invariant
#' parameters to plot. The defaults values are 20 for time-invariant
#' parameters and 3 for time-varying parameters. The default value is 5
#' for `plot_type == "trace"`.
#' @param ... Not used..
Expand Down Expand Up @@ -372,6 +372,11 @@ plot.dynamitefit <- function(x, plot_type = c("default", "trace"),
groups = groups,
probs = c(level, 1 - level)
)
coefs <- ifelse_(
is.null(types) && is.null(parameters),
coefs[coefs$type %in% default_types, ],
coefs
)
p_fixed <- plot_fixed(
coefs,
level,
Expand Down Expand Up @@ -463,12 +468,11 @@ plot_trace <- function(x, types, parameters, responses,
#' @inheritParams plot.dynamitefit
#' @noRd
plot_fixed <- function(coefs, level, alpha, scales, n_params) {
coefs <- coefs[coefs$type %in% intersect(fixed_types, default_types), ]
coefs <- coefs[is.na(coefs$time), ]
if (nrow(coefs) == 0L) {
return(NULL)
}
coefs <- filter_params(coefs, n_params, 50)
coefs <- filter_params(coefs, n_params, 20)
n_coefs <- nrow(coefs)
coefs$parameter <- glue::glue(
"{coefs$parameter}_{coefs$category}_{coefs$group}"
Expand Down Expand Up @@ -526,7 +530,6 @@ plot_fixed <- function(coefs, level, alpha, scales, n_params) {
#' @inheritParams plot.dynamitefit
#' @noRd
plot_varying <- function(coefs, level, alpha, scales, n_params) {
coefs <- coefs[coefs$type %in% intersect(varying_types, default_types), ]
coefs <- coefs[!is.na(coefs$time), ]
if (nrow(coefs) == 0L) {
return(NULL)
Expand Down Expand Up @@ -603,10 +606,10 @@ filter_params <- function(x, n_params, n_params_default) {
onlyif(
!n_params_set && !all(keep_params),
warning_(c(
"Number of parameters to be plotted ({n_u_params}) exceeds the
maximum number of parameters ({n_params}). The remaining parameters will
not be plotted.",
`i` = "Please increase {.arg n_params} to plot more parameters."
"Number of parameters to be plotted ({n_vars}) exceeds the
maximum number of parameters ({n_params}). The remaining parameters
will not be plotted.",
`i` = "Please increase {.arg n_params} to plot more parameters."
))
)
} else {
Expand All @@ -625,10 +628,10 @@ filter_params <- function(x, n_params, n_params_default) {
!n_params_set && !all(keep_type_params),
warning_(c(
"Number of parameters to be plotted ({n_u_params}) exceeds the
maximum number of parameters ({max_params}) for parameters
of type {.var type}. The remaining parameters of this type will
not be plotted.",
`i` = "Please increase {.arg n_params} to plot more parameters."
maximum number of parameters ({max_params}) for parameters
of type {.var {type}}. The remaining parameters of this type will
not be plotted.",
`i` = "Please increase {.arg n_params} to plot more parameters."
))
)
}
Expand Down
2 changes: 1 addition & 1 deletion man/plot.dynamitefit.Rd

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

3 changes: 1 addition & 2 deletions tests/testthat/test-output.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ test_that("trace type plot works", {
)
})


test_that("combining plots works", {
expect_error(
plot(gaussian_example_fit, type = c("alpha", "beta")),
Expand Down Expand Up @@ -230,7 +229,7 @@ test_that("deltas can be plotted", {

test_that("nus can be plotted", {
expect_error(
plot(gaussian_example_fit, types = "nu"),
plot(gaussian_example_fit, types = "nu", n_params = 10),
NA
)
})
Expand Down
25 changes: 24 additions & 1 deletion tests/testthat/test-warnings.R
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,29 @@ test_that("windows and old rstan warns on attach", {
)
})

# Plot warnings -----------------------------------------------------------

test_that("too many parameters warns in plot", {
expect_warning(
plot(gaussian_example_fit, types = "nu"),
paste0(
"Number of parameters to be plotted \\(50\\) exceeds the maximum ",
"number of parameters \\(20\\) for parameters of type `nu`\\. ",
"The remaining parameters of this type will not be plotted\\.\n",
"i Please increase `n_params` to plot more parameters\\."
)
)
expect_warning(
plot(gaussian_example_fit, types = "nu", plot_type = "trace"),
paste0(
"Number of parameters to be plotted \\(50\\) exceeds the maximum ",
"number of parameters \\(5\\)\\. ",
"The remaining parameters will not be plotted\\.\n",
"i Please increase `n_params` to plot more parameters\\."
)
)
})

# Deprecated --------------------------------------------------------------

test_that("deprecated warn", {
Expand All @@ -304,7 +327,7 @@ test_that("deprecated warn", {
"'plot_deltas' is deprecated"
)
expect_warning(
plot_nus(gaussian_example_fit),
plot_nus(gaussian_example_fit, n_params = 10),
"'plot_nus' is deprecated"
)
expect_warning(
Expand Down

0 comments on commit e2602ac

Please sign in to comment.