From 9805166fd62044b9bab0a17239db824c2f695064 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 14 Sep 2023 18:48:03 +0200 Subject: [PATCH] use examplesIf --- R/check_distribution.R | 14 +++++-------- R/check_itemscale.R | 11 +++++----- R/check_multimodal.R | 42 +++++++++++++++++-------------------- R/check_predictions.R | 29 +++++++++++--------------- R/check_sphericity.R | 15 +++++++------ R/icc.R | 36 ++++++++++++++------------------ R/r2_nakagawa.R | 10 ++++----- man/check_distribution.Rd | 14 ++++++------- man/check_itemscale.Rd | 11 +++++----- man/check_multimodal.Rd | 44 +++++++++++++++++++-------------------- man/check_predictions.Rd | 30 ++++++++++++-------------- man/check_sphericity.Rd | 15 ++++++------- man/icc.Rd | 38 ++++++++++++++++----------------- man/r2_nakagawa.Rd | 10 ++++----- 14 files changed, 145 insertions(+), 174 deletions(-) diff --git a/R/check_distribution.R b/R/check_distribution.R index 976fedf7c..77cc19db6 100644 --- a/R/check_distribution.R +++ b/R/check_distribution.R @@ -48,15 +48,11 @@ NULL #' There is a `plot()` method, which shows the probabilities of all predicted #' distributions, however, only if the probability is greater than zero. #' -#' @examples -#' if (require("lme4") && require("parameters") && -#' require("see") && require("patchwork") && require("randomForest")) { -#' data(sleepstudy) -#' -#' model <<- lmer(Reaction ~ Days + (Days | Subject), sleepstudy) -#' check_distribution(model) -#' plot(check_distribution(model)) -#' } +#' @examplesIf require("lme4") && require("parameters") && require("see") && require("patchwork") && require("randomForest") +#' data(sleepstudy, package = "lme4") +#' model <<- lme4::lmer(Reaction ~ Days + (Days | Subject), sleepstudy) +#' check_distribution(model) +#' plot(check_distribution(model)) #' @export check_distribution <- function(model) { UseMethod("check_distribution") diff --git a/R/check_itemscale.R b/R/check_itemscale.R index 182b681bc..8d8b71082 100644 --- a/R/check_itemscale.R +++ b/R/check_itemscale.R @@ -43,17 +43,16 @@ #' - Trochim WMK (2008) Types of Reliability. #' ([web](https://conjointly.com/kb/types-of-reliability/)) #' -#' @examples +#' @examplesIf require("parameters") && require("psych") #' # data generation from '?prcomp', slightly modified #' C <- chol(S <- toeplitz(0.9^(0:15))) #' set.seed(17) #' X <- matrix(rnorm(1600), 100, 16) #' Z <- X %*% C -#' if (require("parameters") && require("psych")) { -#' pca <- principal_components(as.data.frame(Z), rotation = "varimax", n = 3) -#' pca -#' check_itemscale(pca) -#' } +#' +#' pca <- principal_components(as.data.frame(Z), rotation = "varimax", n = 3) +#' pca +#' check_itemscale(pca) #' @export check_itemscale <- function(x) { if (!inherits(x, "parameters_pca")) { diff --git a/R/check_multimodal.R b/R/check_multimodal.R index 9223ee59a..6285af02f 100644 --- a/R/check_multimodal.R +++ b/R/check_multimodal.R @@ -10,33 +10,29 @@ #' @param x A numeric vector or a data frame. #' @param ... Arguments passed to or from other methods. #' -#' @examples +#' @examplesIf require("multimode") && require("mclust") #' \dontrun{ -#' if (require("multimode")) { -#' # Univariate -#' x <- rnorm(1000) -#' check_multimodal(x) -#' } +#' # Univariate +#' x <- rnorm(1000) +#' check_multimodal(x) #' -#' if (require("multimode") && require("mclust")) { -#' x <- c(rnorm(1000), rnorm(1000, 2)) -#' check_multimodal(x) +#' x <- c(rnorm(1000), rnorm(1000, 2)) +#' check_multimodal(x) #' -#' # Multivariate -#' m <- data.frame( -#' x = rnorm(200), -#' y = rbeta(200, 2, 1) -#' ) -#' plot(m$x, m$y) -#' check_multimodal(m) +#' # Multivariate +#' m <- data.frame( +#' x = rnorm(200), +#' y = rbeta(200, 2, 1) +#' ) +#' plot(m$x, m$y) +#' check_multimodal(m) #' -#' m <- data.frame( -#' x = c(rnorm(100), rnorm(100, 4)), -#' y = c(rbeta(100, 2, 1), rbeta(100, 1, 4)) -#' ) -#' plot(m$x, m$y) -#' check_multimodal(m) -#' } +#' m <- data.frame( +#' x = c(rnorm(100), rnorm(100, 4)), +#' y = c(rbeta(100, 2, 1), rbeta(100, 1, 4)) +#' ) +#' plot(m$x, m$y) +#' check_multimodal(m) #' } #' @references #' - Ameijeiras-Alonso, J., Crujeiras, R. M., and Rodríguez-Casal, A. (2019). diff --git a/R/check_predictions.R b/R/check_predictions.R index 2e3bc6462..d52b8378a 100644 --- a/R/check_predictions.R +++ b/R/check_predictions.R @@ -66,26 +66,21 @@ #' - Gelman, A., Hill, J., and Vehtari, A. (2020). Regression and Other Stories. #' Cambridge University Press. #' -#' @examples -#' library(performance) +#' @examplesIf require("see") #' # linear model -#' if (require("see")) { -#' model <- lm(mpg ~ disp, data = mtcars) -#' check_predictions(model) -#' } +#' model <- lm(mpg ~ disp, data = mtcars) +#' check_predictions(model) #' #' # discrete/integer outcome -#' if (require("see")) { -#' set.seed(99) -#' d <- iris -#' d$skewed <- rpois(150, 1) -#' model <- glm( -#' skewed ~ Species + Petal.Length + Petal.Width, -#' family = poisson(), -#' data = d -#' ) -#' check_predictions(model, type = "discrete_both") -#' } +#' set.seed(99) +#' d <- iris +#' d$skewed <- rpois(150, 1) +#' model <- glm( +#' skewed ~ Species + Petal.Length + Petal.Width, +#' family = poisson(), +#' data = d +#' ) +#' check_predictions(model, type = "discrete_both") #' #' @export check_predictions <- function(object, ...) { diff --git a/R/check_sphericity.R b/R/check_sphericity.R index 5a9ebba95..a087a1a5f 100644 --- a/R/check_sphericity.R +++ b/R/check_sphericity.R @@ -10,15 +10,14 @@ #' @return Invisibly returns the p-values of the test statistics. A p-value < #' 0.05 indicates a violation of sphericity. #' -#' @examples -#' if (require("car")) { -#' soils.mod <- lm( -#' cbind(pH, N, Dens, P, Ca, Mg, K, Na, Conduc) ~ Block + Contour * Depth, -#' data = Soils -#' ) +#' @examplesIf require("car") && require("carData") +#' data(Soils, package = "carData") +#' soils.mod <- lm( +#' cbind(pH, N, Dens, P, Ca, Mg, K, Na, Conduc) ~ Block + Contour * Depth, +#' data = Soils +#' ) #' -#' check_sphericity(Manova(soils.mod)) -#' } +#' check_sphericity(Manova(soils.mod)) #' @export check_sphericity <- function(x, ...) { UseMethod("check_sphericity") diff --git a/R/icc.R b/R/icc.R index 8ce19288e..9155a7f40 100644 --- a/R/icc.R +++ b/R/icc.R @@ -143,29 +143,25 @@ #' very large, the variance ratio in the output makes no sense, e.g. because #' it is negative. In such cases, it might help to use `robust = TRUE`. #' -#' @examples -#' if (require("lme4")) { -#' model <- lmer(Sepal.Length ~ Petal.Length + (1 | Species), data = iris) -#' icc(model) -#' } +#' @examplesIf require("lme4") +#' model <- lme4::lmer(Sepal.Length ~ Petal.Length + (1 | Species), data = iris) +#' icc(model) #' #' # ICC for specific group-levels -#' if (require("lme4")) { -#' data(sleepstudy) -#' set.seed(12345) -#' sleepstudy$grp <- sample(1:5, size = 180, replace = TRUE) -#' sleepstudy$subgrp <- NA -#' for (i in 1:5) { -#' filter_group <- sleepstudy$grp == i -#' sleepstudy$subgrp[filter_group] <- -#' sample(1:30, size = sum(filter_group), replace = TRUE) -#' } -#' model <- lmer( -#' Reaction ~ Days + (1 | grp / subgrp) + (1 | Subject), -#' data = sleepstudy -#' ) -#' icc(model, by_group = TRUE) +#' data(sleepstudy, package = "lme4") +#' set.seed(12345) +#' sleepstudy$grp <- sample(1:5, size = 180, replace = TRUE) +#' sleepstudy$subgrp <- NA +#' for (i in 1:5) { +#' filter_group <- sleepstudy$grp == i +#' sleepstudy$subgrp[filter_group] <- +#' sample(1:30, size = sum(filter_group), replace = TRUE) #' } +#' model <- lme4::lmer( +#' Reaction ~ Days + (1 | grp / subgrp) + (1 | Subject), +#' data = sleepstudy +#' ) +#' icc(model, by_group = TRUE) #' @export icc <- function(model, by_group = FALSE, diff --git a/R/r2_nakagawa.R b/R/r2_nakagawa.R index cdf1c8e02..9b751c843 100644 --- a/R/r2_nakagawa.R +++ b/R/r2_nakagawa.R @@ -46,12 +46,10 @@ #' generalized linear mixed-effects models revisited and expanded. Journal of #' The Royal Society Interface, 14(134), 20170213. #' -#' @examples -#' if (require("lme4")) { -#' model <- lmer(Sepal.Length ~ Petal.Length + (1 | Species), data = iris) -#' r2_nakagawa(model) -#' r2_nakagawa(model, by_group = TRUE) -#' } +#' @examplesIf require("lme4") +#' model <- lme4::lmer(Sepal.Length ~ Petal.Length + (1 | Species), data = iris) +#' r2_nakagawa(model) +#' r2_nakagawa(model, by_group = TRUE) #' @export r2_nakagawa <- function(model, by_group = FALSE, diff --git a/man/check_distribution.Rd b/man/check_distribution.Rd index 83ece551a..9ccc8ccc1 100644 --- a/man/check_distribution.Rd +++ b/man/check_distribution.Rd @@ -45,12 +45,10 @@ implemented in the \href{https://easystats.github.io/see/}{\pkg{see}-package}. } \examples{ -if (require("lme4") && require("parameters") && - require("see") && require("patchwork") && require("randomForest")) { - data(sleepstudy) - - model <<- lmer(Reaction ~ Days + (Days | Subject), sleepstudy) - check_distribution(model) - plot(check_distribution(model)) -} +\dontshow{if (require("lme4") && require("parameters") && require("see") && require("patchwork") && require("randomForest")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +data(sleepstudy, package = "lme4") +model <<- lme4::lmer(Reaction ~ Days + (Days | Subject), sleepstudy) +check_distribution(model) +plot(check_distribution(model)) +\dontshow{\}) # examplesIf} } diff --git a/man/check_itemscale.Rd b/man/check_itemscale.Rd index dc6128bbf..7fa487ab5 100644 --- a/man/check_itemscale.Rd +++ b/man/check_itemscale.Rd @@ -44,16 +44,17 @@ acceptability. Satisfactory range lies between 0.2 and 0.4. See also } } \examples{ +\dontshow{if (require("parameters") && require("psych")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} # data generation from '?prcomp', slightly modified C <- chol(S <- toeplitz(0.9^(0:15))) set.seed(17) X <- matrix(rnorm(1600), 100, 16) Z <- X \%*\% C -if (require("parameters") && require("psych")) { - pca <- principal_components(as.data.frame(Z), rotation = "varimax", n = 3) - pca - check_itemscale(pca) -} + +pca <- principal_components(as.data.frame(Z), rotation = "varimax", n = 3) +pca +check_itemscale(pca) +\dontshow{\}) # examplesIf} } \references{ \itemize{ diff --git a/man/check_multimodal.Rd b/man/check_multimodal.Rd index 43153734a..a53546a28 100644 --- a/man/check_multimodal.Rd +++ b/man/check_multimodal.Rd @@ -19,33 +19,31 @@ it always returns a significant result (suggesting that the distribution is multimodal). A better method might be needed here. } \examples{ +\dontshow{if (require("multimode") && require("mclust")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \dontrun{ -if (require("multimode")) { - # Univariate - x <- rnorm(1000) - check_multimodal(x) -} +# Univariate +x <- rnorm(1000) +check_multimodal(x) -if (require("multimode") && require("mclust")) { - x <- c(rnorm(1000), rnorm(1000, 2)) - check_multimodal(x) +x <- c(rnorm(1000), rnorm(1000, 2)) +check_multimodal(x) - # Multivariate - m <- data.frame( - x = rnorm(200), - y = rbeta(200, 2, 1) - ) - plot(m$x, m$y) - check_multimodal(m) +# Multivariate +m <- data.frame( + x = rnorm(200), + y = rbeta(200, 2, 1) +) +plot(m$x, m$y) +check_multimodal(m) - m <- data.frame( - x = c(rnorm(100), rnorm(100, 4)), - y = c(rbeta(100, 2, 1), rbeta(100, 1, 4)) - ) - plot(m$x, m$y) - check_multimodal(m) -} -} +m <- data.frame( + x = c(rnorm(100), rnorm(100, 4)), + y = c(rbeta(100, 2, 1), rbeta(100, 1, 4)) +) +plot(m$x, m$y) +check_multimodal(m) +} +\dontshow{\}) # examplesIf} } \references{ \itemize{ diff --git a/man/check_predictions.Rd b/man/check_predictions.Rd index a2a15ab50..591c813da 100644 --- a/man/check_predictions.Rd +++ b/man/check_predictions.Rd @@ -86,26 +86,22 @@ package that imports \strong{bayesplot} such as \strong{rstanarm} or \strong{brm is loaded, \code{pp_check()} is also available as an alias for \code{check_predictions()}. } \examples{ -library(performance) +\dontshow{if (require("see")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} # linear model -if (require("see")) { - model <- lm(mpg ~ disp, data = mtcars) - check_predictions(model) -} +model <- lm(mpg ~ disp, data = mtcars) +check_predictions(model) # discrete/integer outcome -if (require("see")) { - set.seed(99) - d <- iris - d$skewed <- rpois(150, 1) - model <- glm( - skewed ~ Species + Petal.Length + Petal.Width, - family = poisson(), - data = d - ) - check_predictions(model, type = "discrete_both") -} - +set.seed(99) +d <- iris +d$skewed <- rpois(150, 1) +model <- glm( + skewed ~ Species + Petal.Length + Petal.Width, + family = poisson(), + data = d +) +check_predictions(model, type = "discrete_both") +\dontshow{\}) # examplesIf} } \references{ \itemize{ diff --git a/man/check_sphericity.Rd b/man/check_sphericity.Rd index 531b745a2..6aaa53b3b 100644 --- a/man/check_sphericity.Rd +++ b/man/check_sphericity.Rd @@ -20,12 +20,13 @@ Check model for violation of sphericity. For \link[=check_factorstructure]{Bartl (used for correlation matrices and factor analyses), see \link{check_sphericity_bartlett}. } \examples{ -if (require("car")) { - soils.mod <- lm( - cbind(pH, N, Dens, P, Ca, Mg, K, Na, Conduc) ~ Block + Contour * Depth, - data = Soils - ) +\dontshow{if (require("car") && require("carData")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +data(Soils, package = "carData") +soils.mod <- lm( + cbind(pH, N, Dens, P, Ca, Mg, K, Na, Conduc) ~ Block + Contour * Depth, + data = Soils +) - check_sphericity(Manova(soils.mod)) -} +check_sphericity(Manova(soils.mod)) +\dontshow{\}) # examplesIf} } diff --git a/man/icc.Rd b/man/icc.Rd index facc40653..d25004e07 100644 --- a/man/icc.Rd +++ b/man/icc.Rd @@ -176,28 +176,26 @@ it is negative. In such cases, it might help to use \code{robust = TRUE}. } } \examples{ -if (require("lme4")) { - model <- lmer(Sepal.Length ~ Petal.Length + (1 | Species), data = iris) - icc(model) -} +\dontshow{if (require("lme4")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +model <- lme4::lmer(Sepal.Length ~ Petal.Length + (1 | Species), data = iris) +icc(model) # ICC for specific group-levels -if (require("lme4")) { - data(sleepstudy) - set.seed(12345) - sleepstudy$grp <- sample(1:5, size = 180, replace = TRUE) - sleepstudy$subgrp <- NA - for (i in 1:5) { - filter_group <- sleepstudy$grp == i - sleepstudy$subgrp[filter_group] <- - sample(1:30, size = sum(filter_group), replace = TRUE) - } - model <- lmer( - Reaction ~ Days + (1 | grp / subgrp) + (1 | Subject), - data = sleepstudy - ) - icc(model, by_group = TRUE) -} +data(sleepstudy, package = "lme4") +set.seed(12345) +sleepstudy$grp <- sample(1:5, size = 180, replace = TRUE) +sleepstudy$subgrp <- NA +for (i in 1:5) { + filter_group <- sleepstudy$grp == i + sleepstudy$subgrp[filter_group] <- + sample(1:30, size = sum(filter_group), replace = TRUE) +} +model <- lme4::lmer( + Reaction ~ Days + (1 | grp / subgrp) + (1 | Subject), + data = sleepstudy +) +icc(model, by_group = TRUE) +\dontshow{\}) # examplesIf} } \references{ \itemize{ diff --git a/man/r2_nakagawa.Rd b/man/r2_nakagawa.Rd index 3bfa31fed..357c2a3e9 100644 --- a/man/r2_nakagawa.Rd +++ b/man/r2_nakagawa.Rd @@ -74,11 +74,11 @@ The contribution of random effects can be deduced by subtracting the marginal R2 from the conditional R2 or by computing the \code{\link[=icc]{icc()}}. } \examples{ -if (require("lme4")) { - model <- lmer(Sepal.Length ~ Petal.Length + (1 | Species), data = iris) - r2_nakagawa(model) - r2_nakagawa(model, by_group = TRUE) -} +\dontshow{if (require("lme4")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +model <- lme4::lmer(Sepal.Length ~ Petal.Length + (1 | Species), data = iris) +r2_nakagawa(model) +r2_nakagawa(model, by_group = TRUE) +\dontshow{\}) # examplesIf} } \references{ \itemize{