From 8a19dce36f5b907d2334f204e00e3924e061cee1 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 29 Oct 2023 12:31:58 +0100 Subject: [PATCH] examples --- R/looic.R | 2 + R/r2_bayes.R | 98 ++++++++++++++++++++++------------------------ man/looic.Rd | 2 + man/r2_bayes.Rd | 102 ++++++++++++++++++++++++------------------------ 4 files changed, 101 insertions(+), 103 deletions(-) diff --git a/R/looic.R b/R/looic.R index 8f0a0c66e..4ded6ccd7 100644 --- a/R/looic.R +++ b/R/looic.R @@ -12,6 +12,7 @@ #' @return A list with four elements, the ELPD, LOOIC and their standard errors. #' #' @examplesIf require("rstanarm") +#' \donttest{ #' model <- suppressWarnings(rstanarm::stan_glm( #' mpg ~ wt + cyl, #' data = mtcars, @@ -20,6 +21,7 @@ #' refresh = 0 #' )) #' looic(model) +#' } #' @export looic <- function(model, verbose = TRUE) { insight::check_if_installed("loo") diff --git a/R/r2_bayes.R b/R/r2_bayes.R index fc007489c..4adf80da7 100644 --- a/R/r2_bayes.R +++ b/R/r2_bayes.R @@ -30,67 +30,63 @@ #' `r2_posterior()` is the actual workhorse for `r2_bayes()` and #' returns a posterior sample of Bayesian R2 values. #' -#' @examples +#' @examplesIf require("rstanarm") && require("rstantools") && require("BayesFactor") && require("brms") #' library(performance) -#' if (require("rstanarm") && require("rstantools")) { -#' model <- suppressWarnings(stan_glm( -#' mpg ~ wt + cyl, -#' data = mtcars, -#' chains = 1, -#' iter = 500, -#' refresh = 0, -#' show_messages = FALSE -#' )) -#' r2_bayes(model) +#' \donttest{ +#' model <- suppressWarnings(rstanarm::stan_glm( +#' mpg ~ wt + cyl, +#' data = mtcars, +#' chains = 1, +#' iter = 500, +#' refresh = 0, +#' show_messages = FALSE +#' )) +#' r2_bayes(model) #' -#' model <- suppressWarnings(stan_lmer( -#' Petal.Length ~ Petal.Width + (1 | Species), -#' data = iris, -#' chains = 1, -#' iter = 500, -#' refresh = 0 -#' )) -#' r2_bayes(model) +#' model <- suppressWarnings(rstanarm::stan_lmer( +#' Petal.Length ~ Petal.Width + (1 | Species), +#' data = iris, +#' chains = 1, +#' iter = 500, +#' refresh = 0 +#' )) +#' r2_bayes(model) #' } #' -#' if (require("BayesFactor")) { -#' BFM <- generalTestBF(mpg ~ qsec + gear, data = mtcars, progress = FALSE) -#' FM <- lmBF(mpg ~ qsec + gear, data = mtcars) +#' BFM <- BayesFactor::generalTestBF(mpg ~ qsec + gear, data = mtcars, progress = FALSE) +#' FM <- BayesFactor::lmBF(mpg ~ qsec + gear, data = mtcars) #' -#' r2_bayes(FM) -#' r2_bayes(BFM[3]) -#' r2_bayes(BFM, average = TRUE) # across all models +#' r2_bayes(FM) +#' r2_bayes(BFM[3]) +#' r2_bayes(BFM, average = TRUE) # across all models #' -#' # with random effects: -#' mtcars$gear <- factor(mtcars$gear) -#' model <- lmBF( -#' mpg ~ hp + cyl + gear + gear:wt, -#' mtcars, -#' progress = FALSE, -#' whichRandom = c("gear", "gear:wt") -#' ) +#' # with random effects: +#' mtcars$gear <- factor(mtcars$gear) +#' model <- BayesFactor::lmBF( +#' mpg ~ hp + cyl + gear + gear:wt, +#' mtcars, +#' progress = FALSE, +#' whichRandom = c("gear", "gear:wt") +#' ) #' -#' r2_bayes(model) -#' } +#' r2_bayes(model) #' #' \donttest{ -#' if (require("brms")) { -#' model <- suppressWarnings(brms::brm( -#' mpg ~ wt + cyl, -#' data = mtcars, -#' silent = 2, -#' refresh = 0 -#' )) -#' r2_bayes(model) +#' model <- suppressWarnings(brms::brm( +#' mpg ~ wt + cyl, +#' data = mtcars, +#' silent = 2, +#' refresh = 0 +#' )) +#' r2_bayes(model) #' -#' model <- suppressWarnings(brms::brm( -#' Petal.Length ~ Petal.Width + (1 | Species), -#' data = iris, -#' silent = 2, -#' refresh = 0 -#' )) -#' r2_bayes(model) -#' } +#' model <- suppressWarnings(brms::brm( +#' Petal.Length ~ Petal.Width + (1 | Species), +#' data = iris, +#' silent = 2, +#' refresh = 0 +#' )) +#' r2_bayes(model) #' } #' @references #' Gelman, A., Goodrich, B., Gabry, J., and Vehtari, A. (2018). diff --git a/man/looic.Rd b/man/looic.Rd index 742ac3482..7f985f4a2 100644 --- a/man/looic.Rd +++ b/man/looic.Rd @@ -22,6 +22,7 @@ indicative of a better fit. } \examples{ \dontshow{if (require("rstanarm")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\donttest{ model <- suppressWarnings(rstanarm::stan_glm( mpg ~ wt + cyl, data = mtcars, @@ -30,5 +31,6 @@ model <- suppressWarnings(rstanarm::stan_glm( refresh = 0 )) looic(model) +} \dontshow{\}) # examplesIf} } diff --git a/man/r2_bayes.Rd b/man/r2_bayes.Rd index 63b082e08..edc78150e 100644 --- a/man/r2_bayes.Rd +++ b/man/r2_bayes.Rd @@ -62,67 +62,65 @@ R2 takes both the fixed and random effects into account. returns a posterior sample of Bayesian R2 values. } \examples{ +\dontshow{if (require("rstanarm") && require("rstantools") && require("BayesFactor") && require("brms")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} library(performance) -if (require("rstanarm") && require("rstantools")) { - model <- suppressWarnings(stan_glm( - mpg ~ wt + cyl, - data = mtcars, - chains = 1, - iter = 500, - refresh = 0, - show_messages = FALSE - )) - r2_bayes(model) - - model <- suppressWarnings(stan_lmer( - Petal.Length ~ Petal.Width + (1 | Species), - data = iris, - chains = 1, - iter = 500, - refresh = 0 - )) - r2_bayes(model) +\donttest{ +model <- suppressWarnings(rstanarm::stan_glm( + mpg ~ wt + cyl, + data = mtcars, + chains = 1, + iter = 500, + refresh = 0, + show_messages = FALSE +)) +r2_bayes(model) + +model <- suppressWarnings(rstanarm::stan_lmer( + Petal.Length ~ Petal.Width + (1 | Species), + data = iris, + chains = 1, + iter = 500, + refresh = 0 +)) +r2_bayes(model) } -if (require("BayesFactor")) { - BFM <- generalTestBF(mpg ~ qsec + gear, data = mtcars, progress = FALSE) - FM <- lmBF(mpg ~ qsec + gear, data = mtcars) +BFM <- BayesFactor::generalTestBF(mpg ~ qsec + gear, data = mtcars, progress = FALSE) +FM <- BayesFactor::lmBF(mpg ~ qsec + gear, data = mtcars) - r2_bayes(FM) - r2_bayes(BFM[3]) - r2_bayes(BFM, average = TRUE) # across all models +r2_bayes(FM) +r2_bayes(BFM[3]) +r2_bayes(BFM, average = TRUE) # across all models - # with random effects: - mtcars$gear <- factor(mtcars$gear) - model <- lmBF( - mpg ~ hp + cyl + gear + gear:wt, - mtcars, - progress = FALSE, - whichRandom = c("gear", "gear:wt") - ) +# with random effects: +mtcars$gear <- factor(mtcars$gear) +model <- BayesFactor::lmBF( + mpg ~ hp + cyl + gear + gear:wt, + mtcars, + progress = FALSE, + whichRandom = c("gear", "gear:wt") +) - r2_bayes(model) -} +r2_bayes(model) \donttest{ -if (require("brms")) { - model <- suppressWarnings(brms::brm( - mpg ~ wt + cyl, - data = mtcars, - silent = 2, - refresh = 0 - )) - r2_bayes(model) - - model <- suppressWarnings(brms::brm( - Petal.Length ~ Petal.Width + (1 | Species), - data = iris, - silent = 2, - refresh = 0 - )) - r2_bayes(model) -} +model <- suppressWarnings(brms::brm( + mpg ~ wt + cyl, + data = mtcars, + silent = 2, + refresh = 0 +)) +r2_bayes(model) + +model <- suppressWarnings(brms::brm( + Petal.Length ~ Petal.Width + (1 | Species), + data = iris, + silent = 2, + refresh = 0 +)) +r2_bayes(model) } +\dontshow{\}) # examplesIf} } \references{ Gelman, A., Goodrich, B., Gabry, J., and Vehtari, A. (2018).