|
| 1 | +skip_on_cran() |
| 2 | +skip_if_not_installed("marginaleffects", minimum_version = "0.29.0") |
| 3 | + |
| 4 | +# Focal variables can have names that insight, parameters or modelbased use |
| 5 | +# for their own columns. Each test renames the focal variable and compares the |
| 6 | +# output with the output for a neutral name (`safe_var`). |
| 7 | + |
| 8 | +# names that `insight::standardize_names()` changes, or that modelbased drops |
| 9 | +# by name |
| 10 | +focal_reported_names <- c( |
| 11 | + "type", "p", "CI", "S", "s.value", "predicted", "conf.level", "df_residual", |
| 12 | + "df_error", "response", "n", "value", "component", "effect", "weight", |
| 13 | + "weights", "sumsq", "meansq", "std.dev", "y.level", "hypothesis", "Median", |
| 14 | + "Mean", "MAP", "Dxy", "rho", "r", "tau", "Difference", "Estimate", "F", |
| 15 | + "Chi2", "chisq", "Chisq", "W" |
| 16 | +) |
| 17 | + |
| 18 | +focal_data <- function(name, numeric = FALSE) { |
| 19 | + d <- datasets::mtcars |
| 20 | + d$am_f <- factor(d$am) |
| 21 | + d[[name]] <- if (numeric) d$wt else factor(d$cyl) |
| 22 | + d |
| 23 | +} |
| 24 | + |
| 25 | +# the formula environment is the calling function, so that the data `d` is |
| 26 | +# found there and not in the global environment |
| 27 | +focal_formula <- function(rhs, env = parent.frame()) { |
| 28 | + stats::as.formula(paste("mpg ~", rhs), env = env) |
| 29 | +} |
| 30 | + |
| 31 | +focal_calls <- list( |
| 32 | + means = function(name) { |
| 33 | + d <- focal_data(name) |
| 34 | + m <- stats::lm(focal_formula(paste0("`", name, "` + hp")), data = d) |
| 35 | + estimate_means(m, name) |
| 36 | + }, |
| 37 | + means_two = function(name) { |
| 38 | + d <- focal_data(name) |
| 39 | + m <- stats::lm(focal_formula(paste0("`", name, "` * am_f")), data = d) |
| 40 | + estimate_means(m, c("am_f", name)) |
| 41 | + }, |
| 42 | + slopes_by = function(name) { |
| 43 | + d <- focal_data(name) |
| 44 | + m <- stats::lm(focal_formula(paste0("`", name, "` * wt")), data = d) |
| 45 | + estimate_slopes(m, "wt", by = name) |
| 46 | + }, |
| 47 | + contrasts_by = function(name) { |
| 48 | + d <- focal_data(name) |
| 49 | + m <- stats::lm(focal_formula(paste0("`", name, "` * am_f")), data = d) |
| 50 | + estimate_contrasts(m, "am_f", by = name) |
| 51 | + }, |
| 52 | + contrasts = function(name) { |
| 53 | + d <- focal_data(name) |
| 54 | + m <- stats::lm(focal_formula(paste0("`", name, "` + hp")), data = d) |
| 55 | + estimate_contrasts(m, name) |
| 56 | + } |
| 57 | +) |
| 58 | + |
| 59 | +# Returns NULL when `out` equals `ref` with "safe_var" replaced by `name`, |
| 60 | +# otherwise a short description of the first difference. |
| 61 | +focal_difference <- function(ref, out, name) { |
| 62 | + if (inherits(out, "error")) { |
| 63 | + return(paste("error:", conditionMessage(out))) |
| 64 | + } |
| 65 | + swap <- function(x) { |
| 66 | + if (is.character(x)) gsub("safe_var", name, x, fixed = TRUE) else x |
| 67 | + } |
| 68 | + ref_names <- swap(colnames(ref)) |
| 69 | + if (!identical(ref_names, colnames(out))) { |
| 70 | + return(paste0( |
| 71 | + "columns [", toString(colnames(out)), "], expected [", |
| 72 | + toString(ref_names), "]" |
| 73 | + )) |
| 74 | + } |
| 75 | + for (i in seq_along(ref)) { |
| 76 | + r <- ref[[i]] |
| 77 | + o <- out[[i]] |
| 78 | + if (!identical(class(r), class(o))) { |
| 79 | + return(paste0("column ", i, " has class ", class(o)[1])) |
| 80 | + } |
| 81 | + if (is.factor(r)) { |
| 82 | + levels(r) <- swap(levels(r)) |
| 83 | + } |
| 84 | + r <- swap(r) |
| 85 | + if (!isTRUE(all.equal(r, o, check.attributes = FALSE))) { |
| 86 | + return(paste0("column ", i, " has other values")) |
| 87 | + } |
| 88 | + } |
| 89 | + for (a in c("focal_terms", "by", "trend", "contrast")) { |
| 90 | + if (!identical(swap(attr(ref, a, exact = TRUE)), attr(out, a, exact = TRUE))) { |
| 91 | + return(paste0("attribute ", a, " differs")) |
| 92 | + } |
| 93 | + } |
| 94 | + NULL |
| 95 | +} |
| 96 | + |
| 97 | +expect_focal_names_match <- function(call_name, names) { |
| 98 | + call <- focal_calls[[call_name]] |
| 99 | + ref <- call("safe_var") |
| 100 | + for (name in names) { |
| 101 | + out <- tryCatch(suppressWarnings(call(name)), error = function(e) e) |
| 102 | + diff <- focal_difference(ref, out, name) |
| 103 | + expect_null(diff, label = paste0(call_name, "(\"", name, "\"): ", diff)) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | +test_that("estimate_means() keeps focal variables named like renamed columns", { |
| 109 | + names <- setdiff(focal_reported_names, c("Mean", "s.value")) |
| 110 | + expect_focal_names_match("means", names) |
| 111 | + expect_focal_names_match("means_two", names) |
| 112 | +}) |
| 113 | + |
| 114 | + |
| 115 | +test_that("estimate_slopes() keeps by-variables named like renamed columns", { |
| 116 | + names <- setdiff(focal_reported_names, c("p", "s.value", "predicted")) |
| 117 | + expect_focal_names_match("slopes_by", names) |
| 118 | +}) |
| 119 | + |
| 120 | + |
| 121 | +test_that("estimate_contrasts() keeps by-variables named like renamed columns", { |
| 122 | + names <- setdiff( |
| 123 | + focal_reported_names, |
| 124 | + c("p", "s.value", "hypothesis", "Difference") |
| 125 | + ) |
| 126 | + expect_focal_names_match("contrasts_by", names) |
| 127 | +}) |
| 128 | + |
| 129 | + |
| 130 | +test_that("focal variable names that worked before still work", { |
| 131 | + # names that matched on upstream main b964ea75, computed once from a list |
| 132 | + # of 99 candidate names (the reported names and other column, argument and |
| 133 | + # output names) |
| 134 | + matched_before <- list( |
| 135 | + means = c( |
| 136 | + "z", "rowidcf", "wts", "predicted_lo", "predicted_hi", "emmean", |
| 137 | + "prob", "rate", "lower.CL", "upper.CL", "asymp.LCL", "asymp.UCL", |
| 138 | + "Level1", "Level2", "Parameter", "Comparison", "Response", "Threshold", |
| 139 | + "Row", "Residuals", "Group", "Level", "Slope", "Coefficient", |
| 140 | + "Probability", "Contrast", "Component", "Effects", "Type", "Ratio", |
| 141 | + "iter_1", "x", "model", "data", "at", "ci", "trend", "predict", |
| 142 | + "comparison", "length", "range", "fun", "i", "out", "means", "grid" |
| 143 | + ), |
| 144 | + means_two = c( |
| 145 | + "z", "rowidcf", "wts", "predicted_lo", "predicted_hi", "emmean", |
| 146 | + "prob", "rate", "lower.CL", "upper.CL", "asymp.LCL", "asymp.UCL", |
| 147 | + "Level1", "Level2", "Parameter", "Comparison", "Response", "Threshold", |
| 148 | + "Row", "Residuals", "Group", "Level", "Slope", "Coefficient", |
| 149 | + "Probability", "Contrast", "Component", "Effects", "Type", "Ratio", |
| 150 | + "iter_1", "x", "model", "data", "at", "ci", "trend", "predict", |
| 151 | + "comparison", "length", "range", "fun", "i", "out", "means", "grid" |
| 152 | + ), |
| 153 | + slopes_by = c( |
| 154 | + "rowidcf", "wts", "emmean", "prob", "rate", "lower.CL", "upper.CL", |
| 155 | + "asymp.LCL", "asymp.UCL", "Level1", "Level2", "Response", "Threshold", |
| 156 | + "Row", "Residuals", "Group", "Level", "Probability", "Contrast", |
| 157 | + "Component", "Effects", "Type", "Ratio", "iter_1", "x", "model", |
| 158 | + "data", "at", "ci", "trend", "predict", "comparison", "length", |
| 159 | + "range", "fun", "i", "out", "means", "grid" |
| 160 | + ), |
| 161 | + contrasts_by = c( |
| 162 | + "z", "rowidcf", "wts", "predicted_lo", "predicted_hi", "emmean", |
| 163 | + "prob", "rate", "lower.CL", "upper.CL", "asymp.LCL", "asymp.UCL", |
| 164 | + "Level1", "Level2", "Comparison", "Response", "Threshold", "Row", |
| 165 | + "Residuals", "Group", "Level", "Probability", "Contrast", "Component", |
| 166 | + "Effects", "Type", "Ratio", "iter_1", "x", "model", "data", "at", "ci", |
| 167 | + "trend", "predict", "comparison", "length", "range", "fun", "i", "out", |
| 168 | + "means", "grid" |
| 169 | + ), |
| 170 | + contrasts = c( |
| 171 | + "z", "F", "Chi2", "chisq", "Chisq", "W", "Median", "Mean", "MAP", |
| 172 | + "Dxy", "rho", "r", "tau", "df_residual", "df_error", "std.dev", |
| 173 | + "s.value", "component", "effect", "predicted", "response", "y.level", |
| 174 | + "conf.level", "n", "type", "weight", "weights", "hypothesis", "value", |
| 175 | + "sumsq", "meansq", "Estimate", "rowidcf", "wts", "predicted_lo", |
| 176 | + "predicted_hi", "emmean", "prob", "rate", "lower.CL", "upper.CL", |
| 177 | + "asymp.LCL", "asymp.UCL", "Predicted", "CI", "Level1", "Level2", |
| 178 | + "Comparison", "Response", "Threshold", "Row", "Residuals", "Group", |
| 179 | + "Level", "Slope", "Coefficient", "Probability", "Contrast", |
| 180 | + "Component", "Effects", "Type", "Ratio", "S", "iter_1", "x", "model", |
| 181 | + "data", "at", "ci", "trend", "predict", "comparison", "length", |
| 182 | + "range", "fun", "i", "out", "means", "grid" |
| 183 | + ) |
| 184 | + ) |
| 185 | + for (call_name in names(matched_before)) { |
| 186 | + expect_focal_names_match(call_name, matched_before[[call_name]]) |
| 187 | + } |
| 188 | +}) |
| 189 | + |
| 190 | + |
| 191 | +test_that("estimate_means() keeps numeric focal variables for glm", { |
| 192 | + names <- setdiff(focal_reported_names, c("Mean", "s.value")) |
| 193 | + call <- function(name) { |
| 194 | + d <- focal_data(name, numeric = TRUE) |
| 195 | + d$vs <- datasets::mtcars$vs |
| 196 | + m <- stats::glm( |
| 197 | + stats::as.formula(paste0("vs ~ am_f * `", name, "`")), |
| 198 | + family = stats::binomial(), |
| 199 | + data = d |
| 200 | + ) |
| 201 | + estimate_means(m, c("am_f", name)) |
| 202 | + } |
| 203 | + ref <- suppressWarnings(call("safe_var")) |
| 204 | + for (name in names) { |
| 205 | + out <- tryCatch(suppressWarnings(call(name)), error = function(e) e) |
| 206 | + diff <- focal_difference(ref, out, name) |
| 207 | + expect_null(diff, label = paste0("glm(\"", name, "\"): ", diff)) |
| 208 | + } |
| 209 | +}) |
| 210 | + |
| 211 | + |
| 212 | +test_that("estimate_means() keeps focal variables with custom newdata", { |
| 213 | + # the values in `newdata` are not part of the generated data grid |
| 214 | + names <- setdiff(focal_reported_names, c("Mean", "s.value")) |
| 215 | + call <- function(name) { |
| 216 | + d <- focal_data(name, numeric = TRUE) |
| 217 | + m <- stats::lm(focal_formula(paste0("`", name, "` + hp")), data = d) |
| 218 | + newdata <- data.frame(x = c(2.123, 4.567), hp = 150) |
| 219 | + colnames(newdata)[1] <- name |
| 220 | + estimate_means(m, by = name, newdata = newdata) |
| 221 | + } |
| 222 | + ref <- call("safe_var") |
| 223 | + for (name in names) { |
| 224 | + out <- tryCatch(suppressWarnings(call(name)), error = function(e) e) |
| 225 | + diff <- focal_difference(ref, out, name) |
| 226 | + expect_null(diff, label = paste0("newdata(\"", name, "\"): ", diff)) |
| 227 | + } |
| 228 | +}) |
0 commit comments