diff --git a/R/check_model.R b/R/check_model.R index c24da4903..2994455fe 100644 --- a/R/check_model.R +++ b/R/check_model.R @@ -140,15 +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) -#' } +#' data(sleepstudy, package = "lme4") +#' m <- lme4::lmer(Reaction ~ Days + (Days | Subject), sleepstudy) +#' check_model(m, panel = FALSE) #' } #' @export check_model <- function(x, ...) { diff --git a/R/check_normality.R b/R/check_normality.R index b5b658b5e..ff7ce29c4 100644 --- a/R/check_normality.R +++ b/R/check_normality.R @@ -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") diff --git a/R/check_zeroinflation.R b/R/check_zeroinflation.R index fbf399939..f0f19b369 100644 --- a/R/check_zeroinflation.R +++ b/R/check_zeroinflation.R @@ -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 diff --git a/R/compare_performance.R b/R/compare_performance.R index ca1dcf9ff..76b0b329f 100644 --- a/R/compare_performance.R +++ b/R/compare_performance.R @@ -70,7 +70,7 @@ #' _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) @@ -78,12 +78,10 @@ #' 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) { diff --git a/R/model_performance.lavaan.R b/R/model_performance.lavaan.R index 9822c1c2b..050dbd473 100644 --- a/R/model_performance.lavaan.R +++ b/R/model_performance.lavaan.R @@ -1,15 +1,15 @@ #' Performance of lavaan SEM / CFA Models #' #' Compute indices of model performance for SEM or CFA models from the -#' \pkg{lavaan} package. +#' **lavaan** package. #' -#' @param model A \pkg{lavaan} model. +#' @param model A **lavaan** model. #' @param metrics Can be `"all"` or a character vector of metrics to be -#' computed (some of `c("Chi2", "Chi2_df", "p_Chi2", "Baseline", -#' "Baseline_df", "p_Baseline", "GFI", "AGFI", "NFI", "NNFI", "CFI", -#' "RMSEA", "RMSEA_CI_low", "RMSEA_CI_high", "p_RMSEA", "RMR", "SRMR", -#' "RFI", "PNFI", "IFI", "RNI", "Loglikelihood", "AIC", "BIC", -#' "BIC_adjusted")`). +#' computed (some of `"Chi2"`, `"Chi2_df"`, `"p_Chi2"`, `"Baseline"`, +#' `"Baseline_df"`, `"p_Baseline"`, `"GFI"`, `"AGFI"`, `"NFI"`, `"NNFI"`, +#' `"CFI"`, `"RMSEA"`, `"RMSEA_CI_low"`, `"RMSEA_CI_high"`, `"p_RMSEA"`, +#' `"RMR"`, `"SRMR"`, `"RFI"`, `"PNFI"`, `"IFI"`, `"RNI"`, `"Loglikelihood"`, +#' `"AIC"`, `"BIC"`, and `"BIC_adjusted"`. #' @param verbose Toggle off warnings. #' @param ... Arguments passed to or from other methods. #' @@ -70,15 +70,14 @@ #' and the **SRMR**. #' } #' -#' @examples +#' @examplesIf require("lavaan") #' # Confirmatory Factor Analysis (CFA) --------- -#' if (require("lavaan")) { -#' structure <- " visual =~ x1 + x2 + x3 -#' textual =~ x4 + x5 + x6 -#' speed =~ x7 + x8 + x9 " -#' model <- lavaan::cfa(structure, data = HolzingerSwineford1939) -#' model_performance(model) -#' } +#' data(HolzingerSwineford1939, package = "lavaan") +#' structure <- " visual =~ x1 + x2 + x3 +#' textual =~ x4 + x5 + x6 +#' speed =~ x7 + x8 + x9 " +#' model <- lavaan::cfa(structure, data = HolzingerSwineford1939) +#' model_performance(model) #' #' @references #' @@ -113,31 +112,31 @@ model_performance.lavaan <- function(model, metrics = "all", verbose = TRUE, ... row.names(measures) <- NULL out <- data.frame( - "Chi2" = measures$chisq, - "Chi2_df" = measures$df, - "p_Chi2" = measures$pvalue, - "Baseline" = measures$baseline.chisq, - "Baseline_df" = measures$baseline.df, - "p_Baseline" = measures$baseline.pvalue, - "GFI" = measures$gfi, - "AGFI" = measures$agfi, - "NFI" = measures$nfi, - "NNFI" = measures$tli, - "CFI" = measures$cfi, - "RMSEA" = measures$rmsea, - "RMSEA_CI_low" = measures$rmsea.ci.lower, - "RMSEA_CI_high" = measures$rmsea.ci.upper, - "p_RMSEA" = measures$rmsea.pvalue, - "RMR" = measures$rmr, - "SRMR" = measures$srmr, - "RFI" = measures$rfi, - "PNFI" = measures$pnfi, - "IFI" = measures$ifi, - "RNI" = measures$rni, - "Loglikelihood" = measures$logl, - "AIC" = measures$aic, - "BIC" = measures$bic, - "BIC_adjusted" = measures$bic2 + Chi2 = measures$chisq, + Chi2_df = measures$df, + p_Chi2 = measures$pvalue, + Baseline = measures$baseline.chisq, + Baseline_df = measures$baseline.df, + p_Baseline = measures$baseline.pvalue, + GFI = measures$gfi, + AGFI = measures$agfi, + NFI = measures$nfi, + NNFI = measures$tli, + CFI = measures$cfi, + RMSEA = measures$rmsea, + RMSEA_CI_low = measures$rmsea.ci.lower, + RMSEA_CI_high = measures$rmsea.ci.upper, + p_RMSEA = measures$rmsea.pvalue, + RMR = measures$rmr, + SRMR = measures$srmr, + RFI = measures$rfi, + PNFI = measures$pnfi, + IFI = measures$ifi, + RNI = measures$rni, + Loglikelihood = measures$logl, + AIC = measures$aic, + BIC = measures$bic, + BIC_adjusted = measures$bic2 ) if (all(metrics == "all")) { @@ -167,22 +166,22 @@ model_performance.blavaan <- function(model, metrics = "all", verbose = TRUE, .. row.names(measures) <- NULL out <- data.frame( - "BRMSEA" = fitind[1, "EAP"], - "SD_BRMSEA" = fitind[1, "SD"], - "BGammaHat" = fitind[2, "EAP"], - "SD_BGammaHat" = fitind[2, "SD"], - "Adj_BGammaHat" = fitind[3, "EAP"], - "SD_Adj_BGammaHat" = fitind[3, "SD"], - "Loglikelihood" = measures$logl, - "BIC" = measures$bic, - "DIC" = measures$dic, - "p_DIC" = measures$p_dic, - "WAIC" = measures$waic, - "SE_WAIC" = measures$se_waic, - "p_WAIC" = measures$p_waic, - "LOOIC" = measures$looic, - "SE_LOOIC" = measures$se_loo, - "p_LOOIC" = measures$p_loo + BRMSEA = fitind[1, "EAP"], + SD_BRMSEA = fitind[1, "SD"], + BGammaHat = fitind[2, "EAP"], + SD_BGammaHat = fitind[2, "SD"], + Adj_BGammaHat = fitind[3, "EAP"], + SD_Adj_BGammaHat = fitind[3, "SD"], + Loglikelihood = measures$logl, + BIC = measures$bic, + DIC = measures$dic, + p_DIC = measures$p_dic, + WAIC = measures$waic, + SE_WAIC = measures$se_waic, + p_WAIC = measures$p_waic, + LOOIC = measures$looic, + SE_LOOIC = measures$se_loo, + p_LOOIC = measures$p_loo ) if (all(metrics == "all")) { diff --git a/R/model_performance.rma.R b/R/model_performance.rma.R index 2730e5e5b..167eb75bb 100644 --- a/R/model_performance.rma.R +++ b/R/model_performance.rma.R @@ -47,13 +47,18 @@ #' See the documentation for `?metafor::fitstats`. #' } #' -#' @examples -#' if (require("metafor")) { -#' data(dat.bcg) -#' dat <- escalc(measure = "RR", ai = tpos, bi = tneg, ci = cpos, di = cneg, data = dat.bcg) -#' model <- rma(yi, vi, data = dat, method = "REML") -#' model_performance(model) -#' } +#' @examplesIf require("metafor") +#' data(dat.bcg, package = "metafor") +#' dat <- metafor::escalc( +#' measure = "RR", +#' ai = tpos, +#' bi = tneg, +#' ci = cpos, +#' di = cneg, +#' data = dat.bcg +#' ) +#' model <- metafor::rma(yi, vi, data = dat, method = "REML") +#' model_performance(model) #' @export model_performance.rma <- function(model, metrics = "all", estimator = "ML", verbose = TRUE, ...) { if (all(metrics == "all")) { diff --git a/R/r2.R b/R/r2.R index baf3a18a0..26a16f9e6 100644 --- a/R/r2.R +++ b/R/r2.R @@ -32,7 +32,7 @@ #' [`r2_nakagawa()`], [`r2_tjur()`], [`r2_xu()`] and #' [`r2_zeroinflated()`]. #' -#' @examples +#' @examplesIf require("lme4") #' # Pseudo r-quared for GLM #' model <- glm(vs ~ wt + mpg, data = mtcars, family = "binomial") #' r2(model) @@ -41,10 +41,8 @@ #' model <- lm(mpg ~ wt + hp, data = mtcars) #' r2(model, ci = 0.95) #' -#' if (require("lme4")) { -#' model <- lmer(Sepal.Length ~ Petal.Length + (1 | Species), data = iris) -#' r2(model) -#' } +#' model <- lme4::lmer(Sepal.Length ~ Petal.Length + (1 | Species), data = iris) +#' r2(model) #' @export r2 <- function(model, ...) { UseMethod("r2") diff --git a/R/r2_loo.R b/R/r2_loo.R index 857e3834c..040d9b572 100644 --- a/R/r2_loo.R +++ b/R/r2_loo.R @@ -29,18 +29,16 @@ #' @return A list with the LOO-adjusted R2 value. The standard errors #' and credible intervals for the R2 values are saved as attributes. #' -#' @examples -#' if (require("rstanarm")) { -#' model <- suppressWarnings(stan_glm( -#' mpg ~ wt + cyl, -#' data = mtcars, -#' chains = 1, -#' iter = 500, -#' refresh = 0, -#' show_messages = FALSE -#' )) -#' r2_loo(model) -#' } +#' @examplesIf require("rstanarm") && require("rstantools") +#' model <- suppressWarnings(rstanarm::stan_glm( +#' mpg ~ wt + cyl, +#' data = mtcars, +#' chains = 1, +#' iter = 500, +#' refresh = 0, +#' show_messages = FALSE +#' )) +#' r2_loo(model) #' @export r2_loo <- function(model, robust = TRUE, ci = 0.95, verbose = TRUE, ...) { loo_r2 <- r2_loo_posterior(model, verbose = verbose, ...) diff --git a/man/check_model.Rd b/man/check_model.Rd index 6eebeb1e6..309fe1046 100644 --- a/man/check_model.Rd +++ b/man/check_model.Rd @@ -206,15 +206,16 @@ skipped, which also increases performance. } \examples{ +\dontshow{if (require("lme4")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \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) -} +data(sleepstudy, package = "lme4") +m <- lme4::lmer(Reaction ~ Days + (Days | Subject), sleepstudy) +check_model(m, panel = FALSE) } +\dontshow{\}) # examplesIf} } \seealso{ Other functions to check model assumptions and and assess model quality: diff --git a/man/check_normality.Rd b/man/check_normality.Rd index b6591cc1d..93a259e99 100644 --- a/man/check_normality.Rd +++ b/man/check_normality.Rd @@ -43,14 +43,14 @@ standardized residuals, are used for the test. There is also a implemented in the \href{https://easystats.github.io/see/}{\strong{see}-package}. } \examples{ +\dontshow{if (require("see")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} 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") @@ -58,4 +58,5 @@ plot(check_normality(m), type = "qq") # PP-plot plot(check_normality(m), type = "pp") } +\dontshow{\}) # examplesIf} } diff --git a/man/check_zeroinflation.Rd b/man/check_zeroinflation.Rd index d0a62a76c..db9eddd23 100644 --- a/man/check_zeroinflation.Rd +++ b/man/check_zeroinflation.Rd @@ -30,11 +30,11 @@ zero-inflation in the data. In such cases, it is recommended to use negative binomial or zero-inflated models. } \examples{ -if (require("glmmTMB")) { - data(Salamanders) - m <- glm(count ~ spp + mined, family = poisson, data = Salamanders) - check_zeroinflation(m) -} +\dontshow{if (require("glmmTMB")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +data(Salamanders, package = "glmmTMB") +m <- glm(count ~ spp + mined, family = poisson, data = Salamanders) +check_zeroinflation(m) +\dontshow{\}) # examplesIf} } \seealso{ Other functions to check model assumptions and and assess model quality: diff --git a/man/compare_performance.Rd b/man/compare_performance.Rd index cfa80eda0..30c324351 100644 --- a/man/compare_performance.Rd +++ b/man/compare_performance.Rd @@ -91,6 +91,7 @@ same (AIC/...) values as from the defaults in \code{AIC.merMod()}. There is also a \href{https://easystats.github.io/see/articles/performance.html}{\code{plot()}-method} implemented in the \href{https://easystats.github.io/see/}{\pkg{see}-package}. } \examples{ +\dontshow{if (require("lme4")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} data(iris) lm1 <- lm(Sepal.Length ~ Species, data = iris) lm2 <- lm(Sepal.Length ~ Species + Petal.Length, data = iris) @@ -98,12 +99,11 @@ 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) +\dontshow{\}) # examplesIf} } \references{ Burnham, K. P., and Anderson, D. R. (2002). diff --git a/man/model_performance.lavaan.Rd b/man/model_performance.lavaan.Rd index 9ac05771f..b65e9d0f0 100644 --- a/man/model_performance.lavaan.Rd +++ b/man/model_performance.lavaan.Rd @@ -7,10 +7,14 @@ \method{model_performance}{lavaan}(model, metrics = "all", verbose = TRUE, ...) } \arguments{ -\item{model}{A \pkg{lavaan} model.} +\item{model}{A \strong{lavaan} model.} \item{metrics}{Can be \code{"all"} or a character vector of metrics to be -computed (some of \code{c("Chi2", "Chi2_df", "p_Chi2", "Baseline", "Baseline_df", "p_Baseline", "GFI", "AGFI", "NFI", "NNFI", "CFI", "RMSEA", "RMSEA_CI_low", "RMSEA_CI_high", "p_RMSEA", "RMR", "SRMR", "RFI", "PNFI", "IFI", "RNI", "Loglikelihood", "AIC", "BIC", "BIC_adjusted")}).} +computed (some of \code{"Chi2"}, \code{"Chi2_df"}, \code{"p_Chi2"}, \code{"Baseline"}, +\code{"Baseline_df"}, \code{"p_Baseline"}, \code{"GFI"}, \code{"AGFI"}, \code{"NFI"}, \code{"NNFI"}, +\code{"CFI"}, \code{"RMSEA"}, \code{"RMSEA_CI_low"}, \code{"RMSEA_CI_high"}, \code{"p_RMSEA"}, +\code{"RMR"}, \code{"SRMR"}, \code{"RFI"}, \code{"PNFI"}, \code{"IFI"}, \code{"RNI"}, \code{"Loglikelihood"}, +\code{"AIC"}, \code{"BIC"}, and \code{"BIC_adjusted"}.} \item{verbose}{Toggle off warnings.} @@ -22,7 +26,7 @@ A data frame (with one row) and one column per "index" (see } \description{ Compute indices of model performance for SEM or CFA models from the -\pkg{lavaan} package. +\strong{lavaan} package. } \details{ \subsection{Indices of fit}{ @@ -73,15 +77,15 @@ and the \strong{SRMR}. } } \examples{ +\dontshow{if (require("lavaan")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} # Confirmatory Factor Analysis (CFA) --------- -if (require("lavaan")) { - structure <- " visual =~ x1 + x2 + x3 - textual =~ x4 + x5 + x6 - speed =~ x7 + x8 + x9 " - model <- lavaan::cfa(structure, data = HolzingerSwineford1939) - model_performance(model) -} - +data(HolzingerSwineford1939, package = "lavaan") +structure <- " visual =~ x1 + x2 + x3 + textual =~ x4 + x5 + x6 + speed =~ x7 + x8 + x9 " +model <- lavaan::cfa(structure, data = HolzingerSwineford1939) +model_performance(model) +\dontshow{\}) # examplesIf} } \references{ \itemize{ diff --git a/man/model_performance.rma.Rd b/man/model_performance.rma.Rd index 7ace733b1..9f2db24b5 100644 --- a/man/model_performance.rma.Rd +++ b/man/model_performance.rma.Rd @@ -65,10 +65,17 @@ See the documentation for \code{?metafor::fitstats}. } } \examples{ -if (require("metafor")) { - data(dat.bcg) - dat <- escalc(measure = "RR", ai = tpos, bi = tneg, ci = cpos, di = cneg, data = dat.bcg) - model <- rma(yi, vi, data = dat, method = "REML") - model_performance(model) -} +\dontshow{if (require("metafor")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +data(dat.bcg, package = "metafor") +dat <- metafor::escalc( + measure = "RR", + ai = tpos, + bi = tneg, + ci = cpos, + di = cneg, + data = dat.bcg +) +model <- metafor::rma(yi, vi, data = dat, method = "REML") +model_performance(model) +\dontshow{\}) # examplesIf} } diff --git a/man/r2.Rd b/man/r2.Rd index 45169aef2..9c5c648c3 100644 --- a/man/r2.Rd +++ b/man/r2.Rd @@ -54,6 +54,7 @@ If there is no \code{r2()}-method defined for the given model class, \verb{1-sum((y-y_hat)^2)/sum((y-y_bar)^2))} } \examples{ +\dontshow{if (require("lme4")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} # Pseudo r-quared for GLM model <- glm(vs ~ wt + mpg, data = mtcars, family = "binomial") r2(model) @@ -62,10 +63,9 @@ r2(model) model <- lm(mpg ~ wt + hp, data = mtcars) r2(model, ci = 0.95) -if (require("lme4")) { - model <- lmer(Sepal.Length ~ Petal.Length + (1 | Species), data = iris) - r2(model) -} +model <- lme4::lmer(Sepal.Length ~ Petal.Length + (1 | Species), data = iris) +r2(model) +\dontshow{\}) # examplesIf} } \seealso{ \code{\link[=r2_bayes]{r2_bayes()}}, \code{\link[=r2_coxsnell]{r2_coxsnell()}}, \code{\link[=r2_kullback]{r2_kullback()}}, diff --git a/man/r2_loo.Rd b/man/r2_loo.Rd index c418ab359..e6592e08c 100644 --- a/man/r2_loo.Rd +++ b/man/r2_loo.Rd @@ -52,15 +52,15 @@ Mixed models are not currently fully supported. returns a posterior sample of LOO-adjusted Bayesian R2 values. } \examples{ -if (require("rstanarm")) { - model <- suppressWarnings(stan_glm( - mpg ~ wt + cyl, - data = mtcars, - chains = 1, - iter = 500, - refresh = 0, - show_messages = FALSE - )) - r2_loo(model) -} +\dontshow{if (require("rstanarm") && require("rstantools")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +model <- suppressWarnings(rstanarm::stan_glm( + mpg ~ wt + cyl, + data = mtcars, + chains = 1, + iter = 500, + refresh = 0, + show_messages = FALSE +)) +r2_loo(model) +\dontshow{\}) # examplesIf} }