| 1 |
#' @templateVar class lsmobj |
| 2 |
#' @template title_desc_tidy |
| 3 |
#' |
| 4 |
#' @param x An `lsmobj` object. |
| 5 |
#' @template param_confint |
| 6 |
#' @param ... Additional arguments passed to [emmeans::summary.emmGrid()] or |
| 7 |
#' [lsmeans::summary.ref.grid()]. **Cautionary note**: misspecified arguments |
| 8 |
#' may be silently ignored! |
| 9 |
#' |
| 10 |
#' @evalRd return_tidy( |
| 11 |
#' "contrast", |
| 12 |
#' "null.value", |
| 13 |
#' estimate = "Expected marginal mean", |
| 14 |
#' "std.error", |
| 15 |
#' "df", |
| 16 |
#' "conf.low", |
| 17 |
#' "conf.high", |
| 18 |
#' statistic = "T-ratio statistic", |
| 19 |
#' "p.value" |
| 20 |
#' ) |
| 21 |
#' |
| 22 |
#' @details Returns a data frame with one observation for each estimated marginal |
| 23 |
#' mean, and one column for each combination of factors. When the input is a |
| 24 |
#' contrast, each row will contain one estimated contrast. |
| 25 |
#' |
| 26 |
#' There are a large number of arguments that can be |
| 27 |
#' passed on to [emmeans::summary.emmGrid()] or [lsmeans::summary.ref.grid()]. |
| 28 |
#' |
| 29 |
#' @examplesIf rlang::is_installed(c("emmeans", "ggplot2")) |
| 30 |
#' |
| 31 |
#' # load libraries for models and data |
| 32 |
#' library(emmeans) |
| 33 |
#' |
| 34 |
#' # linear model for sales of oranges per day |
| 35 |
#' oranges_lm1 <- lm(sales1 ~ price1 + price2 + day + store, data = oranges) |
| 36 |
#' |
| 37 |
#' # reference grid; see vignette("basics", package = "emmeans") |
| 38 |
#' oranges_rg1 <- ref_grid(oranges_lm1) |
| 39 |
#' td <- tidy(oranges_rg1) |
| 40 |
#' td |
| 41 |
#' |
| 42 |
#' # marginal averages |
| 43 |
#' marginal <- emmeans(oranges_rg1, "day") |
| 44 |
#' tidy(marginal) |
| 45 |
#' |
| 46 |
#' # contrasts |
| 47 |
#' tidy(contrast(marginal)) |
| 48 |
#' tidy(contrast(marginal, method = "pairwise")) |
| 49 |
#' |
| 50 |
#' # plot confidence intervals |
| 51 |
#' library(ggplot2) |
| 52 |
#' |
| 53 |
#' ggplot(tidy(marginal, conf.int = TRUE), aes(day, estimate)) + |
| 54 |
#' geom_point() + |
| 55 |
#' geom_errorbar(aes(ymin = conf.low, ymax = conf.high)) |
| 56 |
#' |
| 57 |
#' # by multiple prices |
| 58 |
#' by_price <- emmeans(oranges_lm1, "day", |
| 59 |
#' by = "price2", |
| 60 |
#' at = list( |
| 61 |
#' price1 = 50, price2 = c(40, 60, 80), |
| 62 |
#' day = c("2", "3", "4") |
| 63 |
#' ) |
| 64 |
#' ) |
| 65 |
#' |
| 66 |
#' by_price |
| 67 |
#' |
| 68 |
#' tidy(by_price) |
| 69 |
#' |
| 70 |
#' ggplot(tidy(by_price, conf.int = TRUE), aes(price2, estimate, color = day)) + |
| 71 |
#' geom_line() + |
| 72 |
#' geom_errorbar(aes(ymin = conf.low, ymax = conf.high)) |
| 73 |
#' |
| 74 |
#' # joint_tests |
| 75 |
#' tidy(joint_tests(oranges_lm1)) |
| 76 |
#' |
| 77 |
#' @aliases emmeans_tidiers |
| 78 |
#' @export |
| 79 |
#' @family emmeans tidiers |
| 80 |
#' @seealso [tidy()], [emmeans::ref_grid()], [emmeans::emmeans()], |
| 81 |
#' [emmeans::contrast()] |
| 82 |
tidy.lsmobj <- function(x, conf.int = FALSE, conf.level = .95, ...) { |
| 83 |
check_ellipses("exponentiate", "tidy", "lsmobj", ...) |
| 84 |
|
| 85 |
tidy_emmeans(x, infer = c(conf.int, TRUE), level = conf.level, ...) |
| 86 |
} |
| 87 |
|
| 88 |
#' @templateVar class ref.grid |
| 89 |
#' @template title_desc_tidy |
| 90 |
#' |
| 91 |
#' @param x A `ref.grid` object created by [emmeans::ref_grid()]. |
| 92 |
#' @inherit tidy.lsmobj params examples details |
| 93 |
#' |
| 94 |
#' @evalRd return_tidy( |
| 95 |
#' estimate = "Expected marginal mean", |
| 96 |
#' "std.error", |
| 97 |
#' "df", |
| 98 |
#' "conf.low", |
| 99 |
#' "conf.high", |
| 100 |
#' statistic = "T-ratio statistic", |
| 101 |
#' "p.value" |
| 102 |
#' ) |
| 103 |
#' |
| 104 |
#' @export |
| 105 |
#' @family emmeans tidiers |
| 106 |
#' @seealso [tidy()], [emmeans::ref_grid()], [emmeans::emmeans()], |
| 107 |
#' [emmeans::contrast()] |
| 108 |
tidy.ref.grid <- function(x, conf.int = FALSE, conf.level = .95, ...) { |
| 109 |
check_ellipses("exponentiate", "tidy", "ref.grid", ...) |
| 110 |
|
| 111 |
tidy_emmeans(x, infer = c(conf.int, TRUE), level = conf.level, ...) |
| 112 |
} |
| 113 |
|
| 114 |
#' @templateVar class emmGrid |
| 115 |
#' @template title_desc_tidy |
| 116 |
#' |
| 117 |
#' @param x An `emmGrid` object. |
| 118 |
#' @inherit tidy.lsmobj params examples details |
| 119 |
#' |
| 120 |
#' @evalRd return_tidy( |
| 121 |
#' estimate = "Expected marginal mean", |
| 122 |
#' "std.error", |
| 123 |
#' "df", |
| 124 |
#' "conf.low", |
| 125 |
#' "conf.high", |
| 126 |
#' statistic = "T-ratio statistic", |
| 127 |
#' "p.value" |
| 128 |
#' ) |
| 129 |
#' |
| 130 |
#' @export |
| 131 |
#' @family emmeans tidiers |
| 132 |
#' @seealso [tidy()], [emmeans::ref_grid()], [emmeans::emmeans()], |
| 133 |
#' [emmeans::contrast()] |
| 134 |
tidy.emmGrid <- function(x, conf.int = FALSE, conf.level = .95, ...) { |
| 135 |
check_ellipses("exponentiate", "tidy", "emmGrid", ...) |
| 136 |
|
| 137 |
tidy_emmeans(x, infer = c(conf.int, TRUE), level = conf.level, ...) |
| 138 |
} |
| 139 |
|
| 140 |
#' @templateVar class summary_emm |
| 141 |
#' @template title_desc_tidy |
| 142 |
#' |
| 143 |
#' @param x A `summary_emm` object. |
| 144 |
#' @param null.value Value to which estimate is compared. |
| 145 |
#' @inherit tidy.lsmobj params examples details |
| 146 |
#' |
| 147 |
#' @evalRd return_tidy( |
| 148 |
#' "contrast", |
| 149 |
#' level1 = "One level of the factor being contrasted", |
| 150 |
#' level2 = "The other level of the factor being contrasted", |
| 151 |
#' term = "Model term in joint tests", |
| 152 |
#' "null.value", |
| 153 |
#' estimate = "Expected marginal mean", |
| 154 |
#' "std.error", |
| 155 |
#' "df", |
| 156 |
#' "num.df", |
| 157 |
#' "den.df", |
| 158 |
#' "conf.low", |
| 159 |
#' "conf.high", |
| 160 |
#' statistic = "T-ratio statistic or F-ratio statistic", |
| 161 |
#' "p.value" |
| 162 |
#' ) |
| 163 |
#' |
| 164 |
#' @export |
| 165 |
#' @family emmeans tidiers |
| 166 |
#' @seealso [tidy()], [emmeans::ref_grid()], [emmeans::emmeans()], |
| 167 |
#' [emmeans::contrast()] |
| 168 |
|
| 169 |
tidy.summary_emm <- function(x, null.value = NULL, ...) { |
| 170 |
check_ellipses("exponentiate", "tidy", "summary_emm", ...) |
| 171 |
|
| 172 |
tidy_emmeans_summary(x, null.value = null.value) |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
tidy_emmeans <- function(x, ...) { |
| 177 |
s <- summary(x, ...) |
| 178 |
|
| 179 |
# Get null.value |
| 180 |
if (".offset." %in% colnames(x@grid)) { |
| 181 |
null.value <- x@grid[, ".offset."] |
| 182 |
} else { |
| 183 |
null.value <- 0 |
| 184 |
} |
| 185 |
|
| 186 |
# Get term names |
| 187 |
term_names <- names(x@misc$orig.grid) |
| 188 |
|
| 189 |
tidy_emmeans_summary(s, null.value = null.value, term_names = term_names) |
| 190 |
} |
| 191 |
|
| 192 |
tidy_emmeans_summary <- function(x, null.value = NULL, term_names = NULL) { |
| 193 |
ret <- as.data.frame(x) |
| 194 |
repl <- list( |
| 195 |
"lsmean" = "estimate", |
| 196 |
"emmean" = "estimate", |
| 197 |
"pmmean" = "estimate", |
| 198 |
"prediction" = "estimate", |
| 199 |
"effect.size" = "estimate", |
| 200 |
"SE" = "std.error", |
| 201 |
"lower.CL" = "conf.low", |
| 202 |
"asymp.LCL" = "conf.low", |
| 203 |
"upper.CL" = "conf.high", |
| 204 |
"asymp.UCL" = "conf.high", |
| 205 |
"z.ratio" = "statistic", |
| 206 |
"t.ratio" = "statistic", |
| 207 |
"F.ratio" = "statistic", |
| 208 |
"df1" = "num.df", |
| 209 |
"df2" = "den.df", |
| 210 |
"model term" = "term" |
| 211 |
) |
| 212 |
|
| 213 |
mc_adjusted <- any( |
| 214 |
grepl( |
| 215 |
"conf-level adjustment|p value adjustment", |
| 216 |
attr(x, "mesg"), |
| 217 |
ignore.case = TRUE |
| 218 |
) |
| 219 |
) |
| 220 |
|
| 221 |
if (mc_adjusted) { |
| 222 |
repl <- c(repl, "p.value" = "adj.p.value") |
| 223 |
} |
| 224 |
|
| 225 |
colnames(ret) <- dplyr::recode(colnames(ret), !!!(repl)) |
| 226 |
|
| 227 |
# If contrast column exists, add null.value column |
| 228 |
if ("contrast" %in% colnames(ret)) { |
| 229 |
if (length(null.value) < nrow(ret)) |
| 230 |
null.value <- rep_len(null.value, nrow(ret)) |
| 231 |
ret <- bind_cols( |
| 232 |
contrast = ret[, "contrast"], |
| 233 |
null.value = null.value, |
| 234 |
select(ret, -contrast) |
| 235 |
) |
| 236 |
} |
| 237 |
|
| 238 |
# add term column, if appropriate, unless it exists |
| 239 |
if ("term" %in% colnames(ret)) { |
| 240 |
ret <- ret |> |
| 241 |
mutate(term = stringr::str_trim(term)) |
| 242 |
} else if (!is.null(term_names)) { |
| 243 |
term <- term_names[!term_names %in% colnames(ret)] |
| 244 |
|
| 245 |
if (length(term) != 0) { |
| 246 |
term <- paste( |
| 247 |
term_names[!term_names %in% colnames(ret)], |
| 248 |
collapse = "*" |
| 249 |
) |> |
| 250 |
rep_len(nrow(ret)) |
| 251 |
} else { |
| 252 |
# No missing term names, because combine = TRUE? |
| 253 |
term <- apply(ret, 1, function(x) colnames(ret)[which(x == ".")]) |
| 254 |
} |
| 255 |
|
| 256 |
ret <- bind_cols( |
| 257 |
ret[, colnames(ret) %in% term_names, drop = FALSE], |
| 258 |
term = term, |
| 259 |
ret[, !colnames(ret) %in% term_names, drop = FALSE] |
| 260 |
) |
| 261 |
} |
| 262 |
|
| 263 |
as_tibble(ret) |> |
| 264 |
mutate_if(is.factor, as.character) |
| 265 |
} |