Skip to content

Commit 7f7c3e1

Browse files
split into two functions
1 parent f3f694e commit 7f7c3e1

14 files changed

+118
-105
lines changed

NAMESPACE

+5-3
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@ 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)
123121
S3method(check_residuals,DHARMa)
124122
S3method(check_residuals,default)
125123
S3method(check_residuals,performance_simres)
@@ -245,6 +243,8 @@ S3method(performance_aicc,default)
245243
S3method(performance_aicc,lmerMod)
246244
S3method(performance_aicc,rma)
247245
S3method(performance_aicc,vglm)
246+
S3method(performance_dvour,default)
247+
S3method(performance_dvour,estimate_grouplevel)
248248
S3method(performance_logloss,brmsfit)
249249
S3method(performance_logloss,default)
250250
S3method(performance_logloss,logitmfx)
@@ -272,6 +272,7 @@ S3method(performance_mse,negbinmfx)
272272
S3method(performance_mse,poissonirr)
273273
S3method(performance_mse,poissonmfx)
274274
S3method(performance_mse,probitmfx)
275+
S3method(performance_reliability,default)
275276
S3method(plot,binned_residuals)
276277
S3method(plot,check_autocorrelation)
277278
S3method(plot,check_clusterstructure)
@@ -572,7 +573,6 @@ export(check_normality)
572573
export(check_outliers)
573574
export(check_overdispersion)
574575
export(check_predictions)
575-
export(check_reliability)
576576
export(check_residuals)
577577
export(check_singularity)
578578
export(check_sphericity)
@@ -598,11 +598,13 @@ export(performance_accuracy)
598598
export(performance_aic)
599599
export(performance_aicc)
600600
export(performance_cv)
601+
export(performance_dvour)
601602
export(performance_hosmer)
602603
export(performance_logloss)
603604
export(performance_mae)
604605
export(performance_mse)
605606
export(performance_pcp)
607+
export(performance_reliability)
606608
export(performance_rmse)
607609
export(performance_roc)
608610
export(performance_rse)

R/check_reliability.R R/performance_reliability.R

+91-60
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
#' Variability-Over-Uncertainty Ratio (d-vour) for Random Effects Reliability
1+
#' Random Effects Reliability
2+
#'
3+
#' Variability-Over-Uncertainty Ratio (d-vour).
24
#'
35
#' @description TODO: Add description.
46
#'
57
#' @param x A model object (or from [`modelbased::estimate_grouplevel()`]).
6-
#' @param n_trials to do...
78
#' @param ... Currently not used.
89
#'
910
#
@@ -18,52 +19,117 @@
1819
#'
1920
#' @references TODO.
2021
#'
21-
#' @family functions to check model assumptions and and assess model quality
2222
#'
23-
#' @examplesIf require("lme4") & require("glmmTMB")
23+
#' @examplesIf require("lme4") && require("glmmTMB")
2424
#' df <- read.csv("https://raw.githubusercontent.com/easystats/circus/refs/heads/main/data/illusiongame.csv")
2525
#'
2626
#' m <- lme4::lmer(RT ~ (1 | Participant), data = df)
27-
#' check_reliability(m)
27+
#' performance_reliability(m)
28+
#' performance_dvour(m)
2829
#' m <- glmmTMB::glmmTMB(RT ~ (1 | Participant), data = df)
29-
#' check_reliability(m)
30+
#' performance_reliability(m)
31+
#' performance_dvour(m)
3032
#'
3133
#' m <- lme4::lmer(RT ~ (1 | Participant) + (1 | Trial), data = df)
32-
#' check_reliability(m)
34+
#' performance_reliability(m)
35+
#' performance_dvour(m)
3336
#' m <- glmmTMB::glmmTMB(RT ~ (1 | Participant) + (1 | Trial), data = df)
34-
#' check_reliability(m)
37+
#' performance_reliability(m)
38+
#' performance_dvour(m)
3539
#'
3640
#' m <- lme4::lmer(RT ~ Illusion_Difference + (Illusion_Difference | Participant) + (1 | Trial), data = df)
37-
#' check_reliability(m)
41+
#' performance_reliability(m)
42+
#' performance_dvour(m)
3843
#' m <- glmmTMB::glmmTMB(RT ~ Illusion_Difference + (Illusion_Difference | Participant) + (1 | Trial), data = df)
39-
#' check_reliability(m)
44+
#' performance_reliability(m)
45+
#' performance_dvour(m)
4046
#' @export
41-
check_reliability <- function(x, ...) {
42-
UseMethod("check_reliability")
47+
performance_reliability <- function(x, ...) {
48+
UseMethod("performance_reliability")
4349
}
4450

4551

4652
#' @export
47-
check_reliability.default <- function(x, ...) {
48-
insight::check_if_installed("modelbased", minimum_version = "0.10.0")
49-
check_reliability(modelbased::estimate_grouplevel(x, ...), ...)
53+
performance_reliability.default <- function(x, ...) {
54+
# Find how many observations per random effect (n-trials)
55+
random <- lapply(insight::get_random(x), function(z) min(table(z)))
56+
v <- insight::get_variance(x) # Extract variance components
57+
58+
params <- as.data.frame(parameters::parameters(x, effects = "random", group_level = TRUE))
59+
60+
reliability <- data.frame()
61+
for (grp in unique(params$Group)) {
62+
for (param in unique(params$Parameter)) {
63+
# Store group-level results
64+
rez <- data.frame(
65+
Group = grp,
66+
Parameter = param
67+
)
68+
69+
70+
# Based on Rouder's (2024) paper https://journals.sagepub.com/doi/10.1177/09637214231220923
71+
# "What part of reliability is invariant to trial size? Consider the ratio sigma_B^2 / sigma_W^2.
72+
# This is a signal-to-noise variance ratio - it is how much more variable people are relative to
73+
# trial noise. Let gamma2 denote this ratio. With it, the reliability coefficient follows (eq. 1):
74+
# E(r) = gamma2 / (gamma2 + 2/L)" (or 1/L for non-contrast tasks, see annotation 4)
75+
76+
# Number of trials per group
77+
L <- random[[grp]]
78+
79+
# Extract variances
80+
if(param %in% c("(Intercept)", "Intercept")) {
81+
var_between <- v$var.intercept[grp]
82+
} else {
83+
var_between <- v$var.slope[paste0(grp, ".", param)]
84+
}
85+
86+
# Non-adjusted index
87+
# rez$Reliability <- var_between / (var_between + v$var.residual)
88+
89+
# Adjusted index:
90+
# Rouder & Mehrvarz suggest 1/L for non-contrast tasks and 2/L for contrast tasks.
91+
rez$Reliability <- var_between / (var_between + v$var.residual + 1 / L)
92+
93+
# The parameter γ is the signal-to-noise standard-deviation ratio. It is often convenient for
94+
# communication as standard deviations are sometimes more convenient than variances.
95+
# rez$Reliability_adjusted <- sqrt(rez$Reliability_adjusted)
96+
97+
reliability <- rbind(reliability, rez)
98+
}
99+
}
100+
101+
reliability
50102
}
51103

52104

53-
#' @rdname check_reliability
105+
106+
107+
108+
109+
110+
# d-vour ------------------------------------------------------------------
111+
112+
113+
114+
#' @rdname performance_reliability
54115
#' @export
55-
check_reliability.estimate_grouplevel <- function(x, ...) {
116+
performance_dvour <- function(x, ...) {
117+
UseMethod("performance_dvour")
118+
}
56119

57-
coefname <- attributes(x)$coef_name
58-
dispname <- grep("SE|SD|MAD", colnames(x), value = TRUE)
59120

60-
# Extract model information
61-
model <- attributes(x)$model
121+
#' @export
122+
performance_dvour.default <- function(x, ...) {
123+
insight::check_if_installed("modelbased", minimum_version = "0.10.0")
124+
performance_dvour(modelbased::estimate_grouplevel(x, ...), ...)
125+
}
62126

63-
# Find how many observations per random effect (n-trials)
64-
random <- lapply(insight::get_random(model), function(x) min(table(x)))
65-
v <- insight::get_variance(model) # Extract variance components
66127

128+
#' @export
129+
performance_dvour.estimate_grouplevel <- function(x, ...) {
130+
131+
coefname <- attributes(x)$coef_name
132+
dispname <- grep("SE|SD|MAD", colnames(x), value = TRUE)
67133

68134
# Sanity checks
69135
if (insight::n_unique(x$Level) <= 3) {
@@ -97,11 +163,6 @@ check_reliability.estimate_grouplevel <- function(x, ...) {
97163

98164
reliability <- data.frame()
99165

100-
# TODO: need to decide on which indices we want to use.
101-
102-
# we need these nested loops only if we need to calculate the reliability
103-
# index for the different random effects parameters. If we want an "overall"
104-
# reliability index, we can simply call ".expected_reliability()".
105166
for (comp in unique(x$Component)) {
106167
for (grp in unique(x$Group)) {
107168
for (param in unique(x$Parameter)) {
@@ -115,42 +176,12 @@ check_reliability.estimate_grouplevel <- function(x, ...) {
115176
Parameter = param
116177
)
117178

118-
119-
# Rouder (2024) --------------------------------------------------------
120-
# Based on Rouder's (2024) paper https://journals.sagepub.com/doi/10.1177/09637214231220923
121-
# "What part of reliability is invariant to trial size? Consider the ratio sigma_B^2 / sigma_W^2.
122-
# This is a signal-to-noise variance ratio - it is how much more variable people are relative to
123-
# trial noise. Let gamma2 denote this ratio. With it, the reliability coefficient follows (eq. 1):
124-
# E(r) = gamma2 / (gamma2 + 2/L)" (or 1/L for non-contrast tasks, see annotation 4)
125-
126-
# Number of trials per group
127-
L <- random[[grp]]
128-
129-
# Extract variances
130-
if(param %in% c("(Intercept)", "Intercept")) {
131-
var_between <- v$var.intercept[grp]
132-
} else {
133-
var_between <- v$var.slope[paste0(grp, ".", param)]
134-
}
135-
136-
# Non-adjusted index
137-
# rez$Reliability <- var_between / (var_between + v$var.residual)
138-
139-
# Adjusted index:
140-
# Rouder & Mehrvarz suggest 1/L for non-contrast tasks and 2/L for contrast tasks.
141-
rez$Reliability <- var_between / (var_between + v$var.residual + 1 / L)
142-
143-
# The parameter γ is the signal-to-noise standard-deviation ratio. It is often convenient for
144-
# communication as standard deviations are sometimes more convenient than variances.
145-
# rez$Reliability_adjusted <- sqrt(rez$Reliability_adjusted)
146-
147-
# d-vour ------------------------------------------------------------------
148179
# Variability-Over-Uncertainty Ratio (d-vour)
149180
# This index is based on the information contained in the group-level estimates.
150181
var_between <- stats::sd(d[[coefname]]) # Variability
151182
var_within <- mean(d[[dispname]]) # Average Uncertainty
152183

153-
rez$Dvour <- var_between^2 / (var_between^2 + var_within^2)
184+
rez$D_vour <- var_between^2 / (var_between^2 + var_within^2)
154185

155186
# Alternative 1: average of level-specific reliability
156187
# Inspired by the hlmer package (R version of HLM7 by Raudenbush et al., 2014)

man/check_autocorrelation.Rd

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/check_collinearity.Rd

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/check_convergence.Rd

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/check_heteroscedasticity.Rd

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/check_homogeneity.Rd

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/check_model.Rd

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/check_outliers.Rd

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/check_overdispersion.Rd

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/check_predictions.Rd

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/check_singularity.Rd

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/check_zeroinflation.Rd

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)