Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use examplesIf in roxygen comments #615

Merged
merged 11 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Suggests:
boot,
brms,
car,
carData,
CompQuadForm,
correlation,
cplm,
Expand Down
2 changes: 1 addition & 1 deletion R/check_convergence.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#' cbpp$x <- rnorm(nrow(cbpp))
#' cbpp$x2 <- runif(nrow(cbpp))
#'
#' model <- lm4::glmer(
#' model <- lme4::glmer(
#' cbind(incidence, size - incidence) ~ period + x + x2 + (1 + x | herd),
#' data = cbpp,
#' family = binomial()
Expand Down
48 changes: 22 additions & 26 deletions R/check_distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -196,23 +192,23 @@ check_distribution.numeric <- function(model) {
x <- x[!is.na(x)]

data.frame(
"SD" = stats::sd(x),
"MAD" = stats::mad(x, constant = 1),
"Mean_Median_Distance" = mean(x) - stats::median(x),
"Mean_Mode_Distance" = mean(x) - as.numeric(bayestestR::map_estimate(x, bw = "nrd0")),
"SD_MAD_Distance" = stats::sd(x) - stats::mad(x, constant = 1),
"Var_Mean_Distance" = stats::var(x) - mean(x),
"Range_SD" = diff(range(x)) / stats::sd(x),
"Range" = diff(range(x)),
"IQR" = stats::IQR(x),
"Skewness" = .skewness(x),
"Kurtosis" = .kurtosis(x),
"Uniques" = length(unique(x)) / length(x),
"N_Uniques" = length(unique(x)),
"Min" = min(x),
"Max" = max(x),
"Proportion_Positive" = sum(x >= 0) / length(x),
"Integer" = all(.is_integer(x))
SD = stats::sd(x),
MAD = stats::mad(x, constant = 1),
Mean_Median_Distance = mean(x) - stats::median(x),
Mean_Mode_Distance = mean(x) - as.numeric(bayestestR::map_estimate(x, bw = "nrd0")),
SD_MAD_Distance = stats::sd(x) - stats::mad(x, constant = 1),
Var_Mean_Distance = stats::var(x) - mean(x),
Range_SD = diff(range(x)) / stats::sd(x),
Range = diff(range(x)),
IQR = stats::IQR(x),
Skewness = .skewness(x),
Kurtosis = .kurtosis(x),
Uniques = length(unique(x)) / length(x),
N_Uniques = length(unique(x)),
Min = min(x),
Max = max(x),
Proportion_Positive = sum(x >= 0) / length(x),
Integer = all(.is_integer(x))
)
}

Expand Down
11 changes: 5 additions & 6 deletions R/check_itemscale.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down
14 changes: 4 additions & 10 deletions R/check_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,14 @@
#'
#' @family functions to check model assumptions and and assess model quality
#'
#' @examples
#' @examplesIf require("lme4")
#' \dontrun{
#' m <- lm(mpg ~ wt + cyl + gear + disp, data = mtcars)
#' check_model(m)
#'
#' if (require("lme4")) {
#' m <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
#' check_model(m, panel = FALSE)
#' }
#'
#' if (require("rstanarm")) {
#' m <- stan_glm(mpg ~ wt + gear, data = mtcars, chains = 2, iter = 200)
#' check_model(m)
#' }
#' data(sleepstudy, package = "lme4")
#' m <- lme4::lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
#' check_model(m, panel = FALSE)
#' }
#' @export
check_model <- function(x, ...) {
Expand Down
42 changes: 19 additions & 23 deletions R/check_multimodal.R
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
9 changes: 4 additions & 5 deletions R/check_normality.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@
#' standardized deviance residuals is shown (in line with changes in
#' `plot.lm()` for R 4.3+).
#'
#' @examples
#' @examplesIf require("see")
#' m <<- lm(mpg ~ wt + cyl + gear + disp, data = mtcars)
#' check_normality(m)
#'
#' # plot results
#' if (require("see")) {
#' x <- check_normality(m)
#' plot(x)
#' }
#' x <- check_normality(m)
#' plot(x)
#'
#' \dontrun{
#' # QQ-plot
#' plot(check_normality(m), type = "qq")
Expand Down
29 changes: 12 additions & 17 deletions R/check_predictions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...) {
Expand Down
15 changes: 7 additions & 8 deletions R/check_sphericity.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion R/check_symmetry.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#' @param ... Not used.
#'
#' @examples
#' V <- wilcox.test(mtcars$mpg)
#' V <- suppressWarnings(wilcox.test(mtcars$mpg))
#' check_symmetry(V)
#'
#' @export
Expand Down
10 changes: 4 additions & 6 deletions R/check_zeroinflation.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@
#'
#' @family functions to check model assumptions and and assess model quality
#'
#' @examples
#' if (require("glmmTMB")) {
#' data(Salamanders)
#' m <- glm(count ~ spp + mined, family = poisson, data = Salamanders)
#' check_zeroinflation(m)
#' }
#' @examplesIf require("glmmTMB")
#' data(Salamanders, package = "glmmTMB")
#' m <- glm(count ~ spp + mined, family = poisson, data = Salamanders)
#' check_zeroinflation(m)
#' @export
check_zeroinflation <- function(x, tolerance = 0.05) {
# check if we have poisson
Expand Down
12 changes: 5 additions & 7 deletions R/compare_performance.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,18 @@
#' _Model selection and multimodel inference: A practical information-theoretic approach_ (2nd ed.).
#' Springer-Verlag. \doi{10.1007/b97636}
#'
#' @examples
#' @examplesIf require("lme4")
#' data(iris)
#' lm1 <- lm(Sepal.Length ~ Species, data = iris)
#' lm2 <- lm(Sepal.Length ~ Species + Petal.Length, data = iris)
#' lm3 <- lm(Sepal.Length ~ Species * Petal.Length, data = iris)
#' compare_performance(lm1, lm2, lm3)
#' compare_performance(lm1, lm2, lm3, rank = TRUE)
#'
#' if (require("lme4")) {
#' m1 <- lm(mpg ~ wt + cyl, data = mtcars)
#' m2 <- glm(vs ~ wt + mpg, data = mtcars, family = "binomial")
#' m3 <- lmer(Petal.Length ~ Sepal.Length + (1 | Species), data = iris)
#' compare_performance(m1, m2, m3)
#' }
#' m1 <- lm(mpg ~ wt + cyl, data = mtcars)
#' m2 <- glm(vs ~ wt + mpg, data = mtcars, family = "binomial")
#' m3 <- lme4::lmer(Petal.Length ~ Sepal.Length + (1 | Species), data = iris)
#' compare_performance(m1, m2, m3)
#' @inheritParams model_performance.lm
#' @export
compare_performance <- function(..., metrics = "all", rank = FALSE, estimator = "ML", verbose = TRUE) {
Expand Down
40 changes: 18 additions & 22 deletions R/icc.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -357,8 +353,8 @@ variance_decomposition <- function(model,
result <- structure(
class = "icc_decomposed",
list(
"ICC_decomposed" = 1 - fun(var_icc),
"ICC_CI" = ci_icc
ICC_decomposed = 1 - fun(var_icc),
ICC_CI = ci_icc
)
)

Expand Down
14 changes: 9 additions & 5 deletions R/looic.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
#'
#' @return A list with four elements, the ELPD, LOOIC and their standard errors.
#'
#' @examples
#' if (require("rstanarm")) {
#' model <- stan_glm(mpg ~ wt + cyl, data = mtcars, chains = 1, iter = 500, refresh = 0)
#' looic(model)
#' }
#' @examplesIf require("rstanarm")
#' model <- rstanarm::stan_glm(
#' mpg ~ wt + cyl,
#' data = mtcars,
#' chains = 1,
#' iter = 500,
#' refresh = 0
#' )
#' looic(model)
#' @export
looic <- function(model, verbose = TRUE) {
insight::check_if_installed("loo")
Expand Down
Loading