|
| 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 | +} |
0 commit comments