Skip to content

Commit 297692f

Browse files
init
1 parent f1ffa8c commit 297692f

File tree

4 files changed

+194
-0
lines changed

4 files changed

+194
-0
lines changed

NAMESPACE

+3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ S3method(check_predictions,brmsfit)
118118
S3method(check_predictions,default)
119119
S3method(check_predictions,lme)
120120
S3method(check_predictions,stanreg)
121+
S3method(check_reliability,default)
122+
S3method(check_reliability,estimate_grouplevel)
121123
S3method(check_residuals,DHARMa)
122124
S3method(check_residuals,default)
123125
S3method(check_residuals,performance_simres)
@@ -570,6 +572,7 @@ export(check_normality)
570572
export(check_outliers)
571573
export(check_overdispersion)
572574
export(check_predictions)
575+
export(check_reliability)
573576
export(check_residuals)
574577
export(check_singularity)
575578
export(check_sphericity)

R/check_reliability.R

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#' Variability-Over-Uncertainty Ratio (D-vour) for Random Effects Reliability
2+
#'
3+
#' @description TODO: Add description.
4+
#'
5+
#' @param x A model object.
6+
#' @param ... Currently not used.
7+
#'
8+
#
9+
#'
10+
#' @details TODO: Add details.
11+
#'
12+
#' @references TODO.
13+
#'
14+
#' @family functions to check model assumptions and and assess model quality
15+
#'
16+
#' @examplesIf require("lme4")
17+
#' # Add groups to the data
18+
#' data <- iris
19+
#' data$Place <- as.factor(rep(c("P1", "P2", "P3", "P4", "P5", "P6"), each = 25))
20+
#'
21+
#' # lme4
22+
#' m <- lme4::lmer(Sepal.Width ~ Petal.Width + (Petal.Width | Place), data = data)
23+
#' check_reliability(m)
24+
#'
25+
#' @export
26+
check_reliability <- function(x, ...) {
27+
UseMethod("check_reliability")
28+
}
29+
30+
31+
32+
33+
#' @export
34+
check_reliability.default <- function(x, ...) {
35+
check_reliability(modelbased::estimate_grouplevel(x, ...), ...)
36+
}
37+
38+
39+
#' @export
40+
check_reliability.estimate_grouplevel <- function(x, ...) {
41+
42+
coefname <- attributes(x)$coef_name
43+
dispname <- names(x)[grep("SE|SD|MAD", names(x))]
44+
45+
# Sanity checks
46+
if(length(unique(x$Level)) <= 3) {
47+
warning(paste0("The number of random levels (N = ",
48+
length(unique(x$Level)),
49+
") might be too low to reliably estimate the variability."))
50+
}
51+
52+
if(length(dispname) == 0) {
53+
stop(paste0("This function requires an index of variability of each random ",
54+
"effect (e.g., SE) but none was found. Try running check_reliability() on the",
55+
" output of modelbased::estimate_grouplevel(model), and make sure the latter ",
56+
"returns a table with an index of dispersion."))
57+
}
58+
59+
if(length(dispname) > 1) {
60+
warning(paste0("Multiple indices of variability were found (",
61+
paste(dispname, collapse = ", "),
62+
"). Using the first one."))
63+
dispname <- dispname[1]
64+
}
65+
66+
67+
# Compute reliability
68+
if (!"Component" %in% names(x)) x$Component <- "TEMP"
69+
70+
reliability <- data.frame()
71+
for(c in unique(x$Component)) {
72+
for(g in unique(x$Group)) {
73+
for(p in unique(x$Parameter)) {
74+
d <- x[x$Component == c & x$Group == g & x$Parameter == p,]
75+
rez <- data.frame(
76+
Component = c,
77+
Group = g,
78+
Parameter = p,
79+
Variability = sd(d[[coefname]]),
80+
Uncertainty = mean(d[[dispname]])
81+
)
82+
rez$Reliability <- rez$Variability / rez$Uncertainty
83+
84+
reliability <- rbind(reliability, rez)
85+
}
86+
}
87+
}
88+
89+
# Clean-up output
90+
if(length(unique(reliability$Component)) == 1 && unique(reliability$Component) == "TEMP") {
91+
reliability$Component <- NULL
92+
}
93+
94+
reliability
95+
}

man/check_reliability.Rd

+48
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
test_that("check_reliability - frequentist", {
2+
3+
skip_if_not_installed("lme4")
4+
skip_if_not_installed("glmmTMB")
5+
6+
data <- iris
7+
data$Place <- as.factor(rep(c("P1", "P2", "P3", "P4", "P5", "P6"), each = 25))
8+
9+
m <- lme4::lmer(Sepal.Width ~ Petal.Width + (Petal.Width | Place), data = data)
10+
out <- check_reliability(m)
11+
testthat::expect_true(all(dim(out) == c(2, 5)))
12+
13+
m <- glmmTMB::glmmTMB(Sepal.Width ~ Petal.Width + (Petal.Width | Place), data = data)
14+
out <- check_reliability(m)
15+
testthat::expect_true(all(dim(out) == c(2, 5)))
16+
})
17+
18+
test_that("check_reliability - Bayesian", {
19+
20+
skip_if_offline()
21+
skip_if_not_installed("curl")
22+
skip_if_not_installed("httr2")
23+
skip_if_not_installed("rstanarm")
24+
skip_if_not_installed("rstantools")
25+
skip_if_not_installed("brms")
26+
27+
library(rstanarm)
28+
29+
30+
# m <- rstanarm::stan_lmer(
31+
# Sepal.Width ~ Petal.Width + (Petal.Width | Group),
32+
# data = d,
33+
# refresh = 0
34+
# )
35+
# m <- brms::brm(
36+
# Sepal.Width ~ Petal.Width + (Petal.Width | Group),
37+
# data = d,
38+
# refresh = 0,
39+
# backend="cmdstanr"
40+
# )
41+
m <- insight::download_model("brms_mixed_10")
42+
out <- check_reliability(m)
43+
testthat::expect_true(all(dim(out) == c(2, 5)))
44+
45+
m <- insight::download_model("brms_sigma_3")
46+
out <- check_reliability(m)
47+
testthat::expect_true(all(dim(out) == c(4, 6)))
48+
})

0 commit comments

Comments
 (0)