Skip to content

Commit

Permalink
examplesIf
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Sep 14, 2023
1 parent 9a6ead4 commit a85fa62
Show file tree
Hide file tree
Showing 16 changed files with 161 additions and 154 deletions.
9 changes: 4 additions & 5 deletions R/check_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...) {
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
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
111 changes: 55 additions & 56 deletions R/model_performance.lavaan.R
Original file line number Diff line number Diff line change
@@ -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.
#'
Expand Down Expand Up @@ -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
#'
Expand Down Expand Up @@ -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")) {
Expand Down Expand Up @@ -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")) {
Expand Down
19 changes: 12 additions & 7 deletions R/model_performance.rma.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down
8 changes: 3 additions & 5 deletions R/r2.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down
22 changes: 10 additions & 12 deletions R/r2_loo.R
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...)
Expand Down
9 changes: 5 additions & 4 deletions man/check_model.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions man/check_normality.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions man/check_zeroinflation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions man/compare_performance.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a85fa62

Please sign in to comment.