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 @@ -32,7 +32,7 @@
#' how to resolve convergence issues. Another clue might be large parameter
#' values, e.g. estimates (on the scale of the linear predictor) larger than
#' 10 in (non-identity link) generalized linear model *might* indicate
#' [complete separation](https://stats.oarc.ucla.edu/other/mult-pkg/faq/general/faqwhat-is-complete-or-quasi-complete-separation-in-logisticprobit-regression-and-how-do-we-deal-with-them/).

Check warning on line 35 in R/check_convergence.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/check_convergence.R,line=35,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 189 characters.

Check warning on line 35 in R/check_convergence.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/check_convergence.R,line=35,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 189 characters.
#' Complete separation can be addressed by regularization, e.g. penalized
#' regression or Bayesian regression with appropriate priors on the fixed effects.
#'
Expand All @@ -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 @@
#' 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")

Check warning on line 51 in R/check_distribution.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/check_distribution.R,line=51,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 124 characters.

Check warning on line 51 in R/check_distribution.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/check_distribution.R,line=51,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 124 characters.
#' 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 All @@ -78,7 +74,7 @@
} else {
x <- stats::residuals(model)
}
# x_scaled <- .normalize(x)

Check warning on line 77 in R/check_distribution.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/check_distribution.R,line=77,col=5,[commented_code_linter] Commented code should be removed.
dat <- .extract_features(x)

dist_residuals <- as.data.frame(t(stats::predict(classify_distribution, dat, type = "prob")))
Expand Down Expand Up @@ -196,23 +192,23 @@
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 Expand Up @@ -83,8 +82,8 @@
Mean = vapply(items, mean, numeric(1), na.rm = TRUE),
SD = vapply(items, stats::sd, numeric(1), na.rm = TRUE),
Skewness = vapply(items, function(i) as.numeric(datawizard::skewness(i)), numeric(1)),
"Difficulty" = item_difficulty(items)$Difficulty,

Check warning on line 85 in R/check_itemscale.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/check_itemscale.R,line=85,col=7,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
"Discrimination" = .item_discr,

Check warning on line 86 in R/check_itemscale.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/check_itemscale.R,line=86,col=7,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
"alpha if deleted" = .item_alpha,
stringsAsFactors = FALSE,
check.names = FALSE
Expand Down
14 changes: 4 additions & 10 deletions R/check_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#'
#' @details For Bayesian models from packages **rstanarm** or **brms**,
#' models will be "converted" to their frequentist counterpart, using
#' [`bayestestR::bayesian_as_frequentist`](https://easystats.github.io/bayestestR/reference/convert_bayesian_as_frequentist.html).

Check warning on line 51 in R/check_model.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/check_model.R,line=51,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 130 characters.
#' A more advanced model-check for Bayesian models will be implemented at a
#' later stage.
#'
Expand Down Expand Up @@ -76,7 +76,7 @@
#' plots are helpful to check model assumptions, they do not necessarily indicate
#' so-called "lack of fit", e.g. missed non-linear relationships or interactions.
#' Thus, it is always recommended to also look at
#' [effect plots, including partial residuals](https://strengejacke.github.io/ggeffects/articles/introduction_partial_residuals.html).

Check warning on line 79 in R/check_model.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/check_model.R,line=79,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 134 characters.
#'
#' @section Homogeneity of Variance:
#' This plot checks the assumption of equal variance (homoscedasticity). The
Expand Down 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 Expand Up @@ -208,7 +202,7 @@
}

# try to find sensible default for "type" argument
suggest_dots <- (minfo$is_bernoulli || minfo$is_count || minfo$is_ordinal || minfo$is_categorical || minfo$is_multinomial)

Check warning on line 205 in R/check_model.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/check_model.R,line=205,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 124 characters.
if (missing(type) && suggest_dots) {
type <- "discrete_interval"
}
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 All @@ -109,7 +104,7 @@
minfo <- insight::model_info(object, verbose = FALSE)

# try to find sensible default for "type" argument
suggest_dots <- (minfo$is_bernoulli || minfo$is_count || minfo$is_ordinal || minfo$is_categorical || minfo$is_multinomial)

Check warning on line 107 in R/check_predictions.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/check_predictions.R,line=107,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 124 characters.
if (missing(type) && suggest_dots) {
type <- "discrete_interval"
}
Expand Down Expand Up @@ -221,7 +216,7 @@

if (is.null(out)) {
insight::format_error(
sprintf("Could not simulate responses. Maybe there is no `simulate()` for objects of class `%s`?", class(object)[1])

Check warning on line 219 in R/check_predictions.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/check_predictions.R,line=219,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 122 characters.
)
}

Expand Down Expand Up @@ -268,7 +263,7 @@
out <- tryCatch(
{
matrix_sim <- stats::simulate(object, nsim = iterations, re.form = re_formula, ...)
as.data.frame(sapply(matrix_sim, function(i) i[, 1] / i[, 2], simplify = TRUE))

Check warning on line 266 in R/check_predictions.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/check_predictions.R,line=266,col=21,[undesirable_function_linter] Function "sapply" is undesirable.
},
error = function(e) {
NULL
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
Loading