1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
get_comparisons_data <- function(
mfx,
variables,
cross,
...) {
newdata <- mfx@newdata
model <- mfx@model
modeldata <- mfx@modeldata
lo <- hi <- ter <- lab <- original <- rowid <- list()
# sometimes needed for extensions when get_data doesn't work
if (is.null(modeldata) || nrow(modeldata) == 0) {
modeldata <- newdata
}
# safety need for extensions not supported by `insight`
if (any(c("factor", "character") %in% mfx@variable_class)) {
idx <- mfx@variable_class %in% c("factor", "character")
first_cross <- names(mfx@variable_class[idx])[1]
} else {
first_cross <- NULL
}
# must use `as.data.table()` because `setDT()` does not handle columns with
# more dimensions (e.g., "idx" in {mlogit})
newdata <- as.data.table(newdata)
for (v in variables) {
args <- list(
model = model,
newdata = newdata,
variable = v,
cross = cross,
first_cross = identical(v$name, first_cross),
modeldata = modeldata,
mfx = mfx
)
args <- append(args, list(...))
# logical and character before factor used to be important; but I don't think so anymore
if (check_variable_class(mfx, v$name, "logical")) {
fun <- get_comparisons_data_logical
} else if (check_variable_class(mfx, v$name, "character")) {
fun <- get_comparisons_data_character
} else if (check_variable_class(mfx, v$name, "categorical")) {
fun <- get_comparisons_data_factor
} else if (check_variable_class(mfx, v$name, "numeric")) {
fun <- get_comparisons_data_numeric
} else if (check_variable_class(mfx, v$name, "integer")) {
fun <- get_comparisons_data_numeric
} else {
msg <- sprintf(
"Class of the `%s` variable is class is not supported.",
v$name
)
stop(msg, call. = FALSE)
}
tmp <- do_call(fun, args)
lo[[v$name]] <- tmp$lo
if (isTRUE(cross)) {
lo[[v$name]][[paste0("null_contrast_", v$name)]] <- tmp$contrast_null
}
hi[[v$name]] <- tmp$hi
ter[[v$name]] <- tmp$ter
lab[[v$name]] <- tmp$lab
original[[v$name]] <- tmp$original
rowid[[v$name]] <- tmp$rowid
}
clean <- function(x) {
for (col in colnames(x)) {
# tobit1 introduces AsIs columns
if (inherits(x[[col]], "AsIs")) {
x[[col]] <- as.numeric(x[[col]])
}
# plm creates c("pseries", "numeric"), but when get_comparisons_data
# assigns +1 numeric, we lose the inheritance
if (inherits(x[[col]], "pseries")) {
x[[col]] <- as.numeric(x[[col]])
}
# strip labelled data which break rbindlist()
cl <- class(x[[col]])
if (length(cl) == 2 && cl[1] == "labelled") {
class(x[[col]]) <- class(x[[col]])[2]
}
}
return(x)
}
lo <- lapply(lo, clean)
hi <- lapply(hi, clean)
original <- lapply(original, clean)
# single contrast
if (!isTRUE(cross)) {
lo <- rbindlist(lo, fill = TRUE, ignore.attr = TRUE)
hi <- rbindlist(hi, fill = TRUE, ignore.attr = TRUE)
original <- rbindlist(original, fill = TRUE, ignore.attr = TRUE)
# long names to avoid user-supplied colname conflict
marginaleffects_ter <- unlist(ter, use.names = FALSE)
marginaleffects_lab <- unlist(lab, use.names = FALSE)
lo[, "term" := marginaleffects_ter]
hi[, "term" := marginaleffects_ter]
original[, "term" := marginaleffects_ter]
lo[, "contrast" := marginaleffects_lab]
hi[, "contrast" := marginaleffects_lab]
original[, "contrast" := marginaleffects_lab]
# cross contrast
} else {
# drop variables for which we have contrasts
for (i in seq_along(lo)) {
if (i == 1) {
# keep rowid and original data only in one of the datasets
idx_lo <- setdiff(names(variables), names(lo)[i])
idx_hi <- setdiff(names(variables), names(hi)[i])
idx_or <- setdiff(names(variables), names(hi)[i])
} else {
# exclude rowid and variables excluded from `variables`, for
# which we do not compute cross-contrasts
contrast_null <- grep(
"rowid|^null_contrast_",
colnames(lo[[i]]),
value = TRUE
)
idx_lo <- c(
setdiff(names(lo[[i]]), c(contrast_null, names(variables))),
setdiff(names(variables), names(lo)[[i]])
)
idx_hi <- c(
setdiff(names(hi[[i]]), c(contrast_null, names(variables))),
setdiff(names(variables), names(hi)[[i]])
)
idx_or <- c(
setdiff(names(original[[i]]), c(contrast_null, names(variables))),
setdiff(names(variables), names(original)[[i]])
)
}
lo[[i]] <- data.table(lo[[i]])[, !..idx_lo]
hi[[i]] <- data.table(hi[[i]])[, !..idx_hi]
original[[i]] <- data.table(original[[i]])[, !..idx_or]
lo[[i]][[paste0("contrast_", names(lo)[i])]] <- lab[[i]]
hi[[i]][[paste0("contrast_", names(hi)[i])]] <- lab[[i]]
original[[i]][[paste0("contrast_", names(original)[i])]] <- lab[[i]]
}
fun <- function(x, y) merge(x, y, all = TRUE, allow.cartesian = TRUE, sort = FALSE)
lo <- Reduce("fun", lo)
hi <- Reduce("fun", hi)
original <- Reduce("fun", original)
# faster to rbind, but creates massive datasets. need cartesian join on rowid
# lo <- cjdt(lo)
# hi <- cjdt(hi)
# if there are fewer null_contrast_* columns, then there is at least
# one always non-null variable type, so we keep everything
idx <- grepl("^null_contrast_", colnames(lo))
idx_df <- lo[, ..idx]
lo <- lo[, !..idx]
if (sum(idx) == length(variables)) {
idx <- rowSums(idx_df) < ncol(idx_df)
lo <- lo[idx]
hi <- hi[idx]
original <- original[idx]
}
}
# get_predict() is much faster if we only build the model matrix once
lo <- add_model_matrix_attribute(mfx, lo)
hi <- add_model_matrix_attribute(mfx, hi)
original <- add_model_matrix_attribute(mfx, original)
out <- list(lo = lo, hi = hi, original = original)
return(out)
}
|