From 227c7a6eaecdc8775f01bdd84d545315a2712942 Mon Sep 17 00:00:00 2001 From: Nicole Hill Date: Tue, 17 Sep 2024 10:03:18 -0700 Subject: [PATCH] Use rlang::is_installed() instead of requireNamespace for conditional examples and vignette code chunks --- R/dev.R | 2 +- R/log-lik.R | 2 +- R/numericise.R | 2 +- R/ran.R | 2 +- R/res.R | 2 +- R/sens.R | 2 +- R/skewnorm.R | 2 +- man/dev_skewnorm.Rd | 2 +- man/log_lik_skewnorm.Rd | 2 +- man/numericise.Rd | 2 +- man/ran_skewnorm.Rd | 2 +- man/res_skewnorm.Rd | 2 +- man/sens_skewnorm.Rd | 2 +- man/skewnorm.Rd | 2 +- vignettes/beta-binomial-deviance-residuals.Rmd | 8 ++++---- 15 files changed, 18 insertions(+), 18 deletions(-) diff --git a/R/dev.R b/R/dev.R index a9b24266..715a9613 100644 --- a/R/dev.R +++ b/R/dev.R @@ -288,7 +288,7 @@ dev_pois_zi <- function(x, lambda, prob = 0, res = FALSE) { #' @family dev_dist #' @export #' -#' @examplesIf requireNamespace("sn") +#' @examplesIf rlang::is_installed("sn") #' dev_skewnorm(c(-2:2)) #' dev_skewnorm(-2:2, 0, 1, 5) #' dev_skewnorm(-2:2, 0, 1, 5, res = TRUE) diff --git a/R/log-lik.R b/R/log-lik.R index 81906df6..4d84dbb2 100644 --- a/R/log-lik.R +++ b/R/log-lik.R @@ -214,7 +214,7 @@ log_lik_pois_zi <- function(x, lambda = 1, prob = 0) { #' @family log_lik_dist #' @export #' -#' @examplesIf requireNamespace("sn") +#' @examplesIf rlang::is_installed("sn") #' log_lik_skewnorm(c(-2:2)) #' log_lik_skewnorm(c(-2:2), shape = -2) #' log_lik_skewnorm(c(-2:2), shape = 2) diff --git a/R/numericise.R b/R/numericise.R index 8f7e6ccd..786bd5b7 100644 --- a/R/numericise.R +++ b/R/numericise.R @@ -77,7 +77,7 @@ numericise.POSIXct <- function(x, ...) { #' @examples #' #' # hms -#' @examplesIf requireNamespace("hms") +#' @examplesIf rlang::is_installed("hms") #' numericise(hms::as_hms("00:01:03")) numericise.hms <- function(x, ...) { rlang::check_installed("hms") diff --git a/R/ran.R b/R/ran.R index ae7c76ca..8c659585 100644 --- a/R/ran.R +++ b/R/ran.R @@ -180,7 +180,7 @@ ran_pois_zi <- function(n = 1, lambda = 1, prob = 0) { #' @family ran_dist #' @export #' -#' @examplesIf requireNamespace("sn") +#' @examplesIf rlang::is_installed("sn") #' ran_skewnorm(10, shape = -1) #' ran_skewnorm(10, shape = 0) #' ran_skewnorm(10, shape = 1) diff --git a/R/res.R b/R/res.R index cf05e38d..a1f1496a 100644 --- a/R/res.R +++ b/R/res.R @@ -335,7 +335,7 @@ res_student_standardized <- function(x, mean, sd, theta) { #' @family res_dist #' @export #' -#' @examplesIf requireNamespace("sn") +#' @examplesIf rlang::is_installed("sn") #' res_skewnorm(c(-2:2)) res_skewnorm <- function(x, mean = 0, sd = 1, shape = 0, type = "dev", simulate = FALSE) { rlang::check_installed("sn") diff --git a/R/sens.R b/R/sens.R index f76854c8..1f5bcef5 100644 --- a/R/sens.R +++ b/R/sens.R @@ -62,7 +62,7 @@ sens_student <- function(mean, sd, theta, sd_mult = 2) { #' @family sens_dist #' @export #' -#' @examplesIf requireNamespace("sn") +#' @examplesIf rlang::is_installed("sn") #' sens_skewnorm(10, 3, -1, 2) #' sens_skewnorm(10, 3, 3, 0.8) sens_skewnorm <- function(mean, sd, shape, sd_mult = 2) { diff --git a/R/skewnorm.R b/R/skewnorm.R index bc28f505..f38bb2c1 100644 --- a/R/skewnorm.R +++ b/R/skewnorm.R @@ -10,7 +10,7 @@ #' @rdname skewnorm #' @export #' -#' @examplesIf requireNamespace("sn") +#' @examplesIf rlang::is_installed("sn") #' dskewnorm(x = -2:2, mean = 0, sd = 1, shape = 0.1) #' dskewnorm(x = -2:2, mean = 0, sd = 1, shape = -1) #' qskewnorm(p = c(0.1, 0.4), mean = 0, sd = 1, shape = 0.1) diff --git a/man/dev_skewnorm.Rd b/man/dev_skewnorm.Rd index 4b2acc3b..9983b41c 100644 --- a/man/dev_skewnorm.Rd +++ b/man/dev_skewnorm.Rd @@ -25,7 +25,7 @@ An numeric vector of the corresponding deviances or deviance residuals. Skew Normal Deviances } \examples{ -\dontshow{if (requireNamespace("sn")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (rlang::is_installed("sn")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} dev_skewnorm(c(-2:2)) dev_skewnorm(-2:2, 0, 1, 5) dev_skewnorm(-2:2, 0, 1, 5, res = TRUE) diff --git a/man/log_lik_skewnorm.Rd b/man/log_lik_skewnorm.Rd index 98353ba1..ea6eae50 100644 --- a/man/log_lik_skewnorm.Rd +++ b/man/log_lik_skewnorm.Rd @@ -22,7 +22,7 @@ An numeric vector of the corresponding log-likelihoods. Skew Normal Log-Likelihood } \examples{ -\dontshow{if (requireNamespace("sn")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (rlang::is_installed("sn")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} log_lik_skewnorm(c(-2:2)) log_lik_skewnorm(c(-2:2), shape = -2) log_lik_skewnorm(c(-2:2), shape = 2) diff --git a/man/numericise.Rd b/man/numericise.Rd index 06703f43..1d7215b3 100644 --- a/man/numericise.Rd +++ b/man/numericise.Rd @@ -100,7 +100,7 @@ numericise(as.Date("1972-01-01")) numericise(as.POSIXct("1972-01-01", tz = "UTC")) # hms -\dontshow{if (requireNamespace("hms")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (rlang::is_installed("hms")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} numericise(hms::as_hms("00:01:03")) \dontshow{\}) # examplesIf} diff --git a/man/ran_skewnorm.Rd b/man/ran_skewnorm.Rd index ba1baba9..b20f753e 100644 --- a/man/ran_skewnorm.Rd +++ b/man/ran_skewnorm.Rd @@ -23,7 +23,7 @@ A numeric vector of the random samples. Skew Normal Random Samples } \examples{ -\dontshow{if (requireNamespace("sn")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (rlang::is_installed("sn")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} ran_skewnorm(10, shape = -1) ran_skewnorm(10, shape = 0) ran_skewnorm(10, shape = 1) diff --git a/man/res_skewnorm.Rd b/man/res_skewnorm.Rd index 6185dd12..ecee901e 100644 --- a/man/res_skewnorm.Rd +++ b/man/res_skewnorm.Rd @@ -27,7 +27,7 @@ An numeric vector of the corresponding residuals. Skew Normal Residuals } \examples{ -\dontshow{if (requireNamespace("sn")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (rlang::is_installed("sn")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} res_skewnorm(c(-2:2)) \dontshow{\}) # examplesIf} } diff --git a/man/sens_skewnorm.Rd b/man/sens_skewnorm.Rd index e5dc2a03..262637a7 100644 --- a/man/sens_skewnorm.Rd +++ b/man/sens_skewnorm.Rd @@ -24,7 +24,7 @@ Expands (\code{sd_mult > 1}) or reduces (\code{sd_mult < 1}) the standard deviat of the Skew Normal distribution without changing the mean. } \examples{ -\dontshow{if (requireNamespace("sn")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (rlang::is_installed("sn")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} sens_skewnorm(10, 3, -1, 2) sens_skewnorm(10, 3, 3, 0.8) \dontshow{\}) # examplesIf} diff --git a/man/skewnorm.Rd b/man/skewnorm.Rd index 258047dd..77747585 100644 --- a/man/skewnorm.Rd +++ b/man/skewnorm.Rd @@ -41,7 +41,7 @@ generate.} Skew-Normal Distribution } \examples{ -\dontshow{if (requireNamespace("sn")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (rlang::is_installed("sn")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} dskewnorm(x = -2:2, mean = 0, sd = 1, shape = 0.1) dskewnorm(x = -2:2, mean = 0, sd = 1, shape = -1) qskewnorm(p = c(0.1, 0.4), mean = 0, sd = 1, shape = 0.1) diff --git a/vignettes/beta-binomial-deviance-residuals.Rmd b/vignettes/beta-binomial-deviance-residuals.Rmd index 6a54b254..35745e60 100644 --- a/vignettes/beta-binomial-deviance-residuals.Rmd +++ b/vignettes/beta-binomial-deviance-residuals.Rmd @@ -16,7 +16,7 @@ knitr::opts_chunk$set( library(extras) ``` -```{r eval = requireNamespace("tidyr") & requireNamespace("ggplot2") & requireNamespace("viridis") & requireNamespace("scales"), include = FALSE} +```{r eval = rlang::is_installed("tidyr") & rlang::is_installed("ggplot2") & rlang::is_installed("viridis") & rlang::is_installed("scales"), include = FALSE} library(tidyr) library(ggplot2) library(viridis) @@ -77,7 +77,7 @@ The problem with the first approach is that likelihood profile of the beta-binom Consequently, increasing $\theta$ increases the deviance even if the expected value of the fitted model is the same as the saturated binomial model. -```{r eval = requireNamespace("tidyr") & requireNamespace("ggplot2") & requireNamespace("viridis") & requireNamespace("scales"), echo = FALSE, fig.width = 7, fig.height = 4, fig.cap = "Fig. 1: Beta-binomial likelihood profile for $x = 1$ and $n = 5$, for different values of $\\theta$. $\\theta = 0$ corresponds to the binomial case."} +```{r eval = rlang::is_installed("tidyr") & rlang::is_installed("ggplot2") & rlang::is_installed("viridis") & rlang::is_installed("scales"), echo = FALSE, fig.width = 7, fig.height = 4, fig.cap = "Fig. 1: Beta-binomial likelihood profile for $x = 1$ and $n = 5$, for different values of $\\theta$. $\\theta = 0$ corresponds to the binomial case."} n_samp <- 1000 x <- 1 size <- 5 @@ -144,7 +144,7 @@ We will refer to the optimized $p$ value for the $i^{th}$ data point as $p_i^*$, We use $p^*$ to calculate the saturated log-likelihood that produces deviances that are (a) strictly positive, and (b) relative to the value of $\theta$. The value of $p_i^*$ for the example case with $x = 1$, $n = 5$, and $\theta = 0.5$ is shown in Figure 2. -```{r eval = requireNamespace("tidyr") & requireNamespace("ggplot2") & requireNamespace("viridis") & requireNamespace("scales"), echo = FALSE, fig.width = 7, fig.height = 4, fig.cap = "Fig. 2: Beta-binomial likelihood profile for $x = 1$, $n = 5$, and $\\theta = 0.5$. The dashed vertical line shows the likelihood at the expected value of the beta-binomial distribution ($n \\cdot p$) where $p = \\frac{x}{n} = \\frac{1}{5} = 0.2$. As you can see, this is not the $p$ for which the likelihood is maximized. The solid vertical line shows the likelihood at its maximum point. In this case, $p^* = 0.26$."} +```{r eval = rlang::is_installed("tidyr") & rlang::is_installed("ggplot2") & rlang::is_installed("viridis") & rlang::is_installed("scales"), echo = FALSE, fig.width = 7, fig.height = 4, fig.cap = "Fig. 2: Beta-binomial likelihood profile for $x = 1$, $n = 5$, and $\\theta = 0.5$. The dashed vertical line shows the likelihood at the expected value of the beta-binomial distribution ($n \\cdot p$) where $p = \\frac{x}{n} = \\frac{1}{5} = 0.2$. As you can see, this is not the $p$ for which the likelihood is maximized. The solid vertical line shows the likelihood at its maximum point. In this case, $p^* = 0.26$."} n_samp <- 1000 x <- 1 size <- 5 @@ -187,7 +187,7 @@ $$r_i = \text{sign}(x_i - (n p_i^*)) \sqrt{d_i}.$$ The following plot shows histograms of deviance residuals for 10,000 randomly generated data points with $n = 50$ and various values of $p$ and $\theta$. -```{r eval = requireNamespace("tidyr") & requireNamespace("ggplot2") & requireNamespace("viridis") & requireNamespace("scales"), echo = FALSE, fig.height = 8, fig.width = 7, fig.cap = "Fig. 4: Histograms of deviance residuals for 10,000 beta-binomial data points simulated with $n = 50$, $p = \\{0.3, 0.5, 0.9 \\}$, and $\\theta = \\{0.0, 0.1, 0.5, 1.0 \\}$. The percentages within each panel (i.e., each combination of $p$ and $\\theta$) sum to 100%."} +```{r eval = rlang::is_installed("tidyr") & rlang::is_installed("ggplot2") & rlang::is_installed("viridis") & rlang::is_installed("scales"), echo = FALSE, fig.height = 8, fig.width = 7, fig.cap = "Fig. 4: Histograms of deviance residuals for 10,000 beta-binomial data points simulated with $n = 50$, $p = \\{0.3, 0.5, 0.9 \\}$, and $\\theta = \\{0.0, 0.1, 0.5, 1.0 \\}$. The percentages within each panel (i.e., each combination of $p$ and $\\theta$) sum to 100%."} n_samp <- 10000 size <- 50 prob <- c(0.3, 0.5, 0.9)