From 39e65882097332d2dfbf794a3d6d8afa223751aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul-Christian=20B=C3=BCrkner?= Date: Wed, 2 Oct 2024 14:02:09 +0200 Subject: [PATCH] allow argument surface in conditional_smooths --- DESCRIPTION | 4 ++-- R/conditional_smooths.R | 16 +++++++++++++--- man/conditional_smooths.brmsfit.Rd | 6 ++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index a3086e9b2..16bc6715e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,8 +2,8 @@ Package: brms Encoding: UTF-8 Type: Package Title: Bayesian Regression Models using 'Stan' -Version: 2.22.1 -Date: 2024-09-24 +Version: 2.22.2 +Date: 2024-10-02 Authors@R: c(person("Paul-Christian", "Bürkner", email = "paul.buerkner@gmail.com", role = c("aut", "cre")), diff --git a/R/conditional_smooths.R b/R/conditional_smooths.R index 74ba1e39e..e656fb2d4 100644 --- a/R/conditional_smooths.R +++ b/R/conditional_smooths.R @@ -9,6 +9,10 @@ #' @param smooths Optional character vector of smooth terms #' to display. If \code{NULL} (the default) all smooth terms #' are shown. +#' @param surface Logical. Indicates if interactions or +#' two-dimensional smooths should be visualized as a surface. +#' Defaults to \code{TRUE}. The surface type can be controlled +#' via argument \code{stype} of the related plotting method. #' @param ndraws Positive integer indicating how many #' posterior draws should be used. #' If \code{NULL} (the default) all draws are used. @@ -49,12 +53,14 @@ conditional_smooths.brmsfit <- function(x, smooths = NULL, int_conditions = NULL, prob = 0.95, spaghetti = FALSE, + surface = TRUE, resolution = 100, too_far = 0, ndraws = NULL, draw_ids = NULL, nsamples = NULL, subset = NULL, probs = NULL, ...) { probs <- validate_ci_bounds(prob, probs = probs) spaghetti <- as_one_logical(spaghetti) + surface <- as_one_logical(surface) draw_ids <- use_alias(draw_ids, subset) ndraws <- use_alias(ndraws, nsamples) contains_draws(x) @@ -68,7 +74,7 @@ conditional_smooths.brmsfit <- function(x, smooths = NULL, bterms, fit = x, smooths = smooths, conditions = conditions, int_conditions = int_conditions, too_far = too_far, resolution = resolution, probs = probs, - spaghetti = spaghetti, draw_ids = draw_ids + spaghetti = spaghetti, surface = surface, draw_ids = draw_ids ) if (!length(out)) { stop2("No valid smooth terms found in the model.") @@ -118,7 +124,7 @@ conditional_smooths.brmsterms <- function(x, ...) { #' @export conditional_smooths.btl <- function(x, fit, smooths, conditions, int_conditions, probs, resolution, too_far, spaghetti, - ...) { + surface, ...) { stopifnot(is.brmsfit(fit)) out <- list() mf <- model.frame(fit) @@ -139,7 +145,11 @@ conditional_smooths.btl <- function(x, fit, smooths, conditions, int_conditions, covars <- all_vars(sub_smframe$covars[[1]]) byvars <- all_vars(sub_smframe$byvars[[1]]) ncovars <- length(covars) - if (ncovars > 2L) { + if (!surface && ncovars > 1L) { + byvars <- c(covars[2:ncovars], byvars) + covars <- covars[1] + ncovars <- 1L + } else if (ncovars > 2L) { byvars <- c(covars[3:ncovars], byvars) covars <- covars[1:2] ncovars <- 2L diff --git a/man/conditional_smooths.brmsfit.Rd b/man/conditional_smooths.brmsfit.Rd index 440163022..7caa1fcd0 100644 --- a/man/conditional_smooths.brmsfit.Rd +++ b/man/conditional_smooths.brmsfit.Rd @@ -13,6 +13,7 @@ int_conditions = NULL, prob = 0.95, spaghetti = FALSE, + surface = TRUE, resolution = 100, too_far = 0, ndraws = NULL, @@ -53,6 +54,11 @@ predictors. If \code{TRUE}, it is recommended to set argument \code{ndraws} to a relatively small value (e.g., \code{100}) in order to reduce computation time.} +\item{surface}{Logical. Indicates if interactions or +two-dimensional smooths should be visualized as a surface. +Defaults to \code{TRUE}. The surface type can be controlled +via argument \code{stype} of the related plotting method.} + \item{resolution}{Number of support points used to generate the plots. Higher resolution leads to smoother plots. Defaults to \code{100}. If \code{surface} is \code{TRUE},