| 1 |
#' @templateVar class epi.2by2 |
| 2 |
#' @template title_desc_tidy |
| 3 |
#' |
| 4 |
#' @param x A `epi.2by2` object produced by a call to [epiR::epi.2by2()] |
| 5 |
#' @param parameters Return measures of association (`moa`) or test statistics (`stat`), |
| 6 |
#' default is `moa` (measures of association) |
| 7 |
#' @template param_unused_dots |
| 8 |
#' |
| 9 |
#' @evalRd return_tidy( |
| 10 |
#' "term", |
| 11 |
#' estimate = "Estimated measure of association", |
| 12 |
#' "conf.low", |
| 13 |
#' "conf.high", |
| 14 |
#' "statistic", |
| 15 |
#' "df", |
| 16 |
#' "p.value" |
| 17 |
#' ) |
| 18 |
#' |
| 19 |
#' @details The tibble has a column for each of the measures of association |
| 20 |
#' or tests contained in `massoc` or `massoc.detail` when [epiR::epi.2by2()] is called. |
| 21 |
#' |
| 22 |
#' @examplesIf rlang::is_installed("epiR") |
| 23 |
#' |
| 24 |
#' # load libraries for models and data |
| 25 |
#' library(epiR) |
| 26 |
#' |
| 27 |
#' # generate data |
| 28 |
#' dat <- matrix(c(13, 2163, 5, 3349), nrow = 2, byrow = TRUE) |
| 29 |
#' |
| 30 |
#' rownames(dat) <- c("DF+", "DF-") |
| 31 |
#' colnames(dat) <- c("FUS+", "FUS-") |
| 32 |
#' |
| 33 |
#' # fit model |
| 34 |
#' fit <- epi.2by2( |
| 35 |
#' dat = as.table(dat), method = "cross.sectional", |
| 36 |
#' conf.level = 0.95, units = 100, outcome = "as.columns" |
| 37 |
#' ) |
| 38 |
#' |
| 39 |
#' # summarize model fit with tidiers |
| 40 |
#' tidy(fit, parameters = "moa") |
| 41 |
#' tidy(fit, parameters = "stat") |
| 42 |
#' |
| 43 |
#' @export |
| 44 |
#' @seealso [tidy()], [epiR::epi.2by2()] |
| 45 |
#' @family epiR tidiers |
| 46 |
#' @aliases epiR_tidiers |
| 47 |
tidy.epi.2by2 <- function(x, parameters = c("moa", "stat"), ...) { |
| 48 |
massoc <- x$massoc.detail |
| 49 |
|
| 50 |
to_bind <- massoc[names(massoc) != "chi2.correction"] |
| 51 |
out <- dplyr::bind_rows( |
| 52 |
lapply(to_bind, replace_dashes), |
| 53 |
.id = "term" |
| 54 |
) |
| 55 |
if (rlang::arg_match(parameters) == "moa") { |
| 56 |
out <- subset(out, !is.na(est), select = c("term", "est", "lower", "upper")) |
| 57 |
colnames(out) <- c("term", "estimate", "conf.low", "conf.high") |
| 58 |
return(tibble::as_tibble(out)) |
| 59 |
} |
| 60 |
|
| 61 |
if ("p.value" %in% colnames(out)) { |
| 62 |
out$p.value <- with(out, dplyr::coalesce(p.value.2s, p.value)) |
| 63 |
} else { |
| 64 |
out$p.value <- out[["p.value.2s"]] |
| 65 |
} |
| 66 |
|
| 67 |
out <- subset( |
| 68 |
out, |
| 69 |
is.na(est), |
| 70 |
select = c("term", "test.statistic", "df", "p.value") |
| 71 |
) |
| 72 |
colnames(out) <- c("term", "statistic", "df", "p.value") |
| 73 |
tibble::as_tibble(out) |
| 74 |
} |
| 75 |
|
| 76 |
# some NA test statistics are represented as dashes--before binding rows, |
| 77 |
# convert them to NA_real_ |
| 78 |
replace_dashes <- function(d) { |
| 79 |
if (is.data.frame(d) || is.matrix(d)) { |
| 80 |
d[d == "-"] <- NA_real_ |
| 81 |
d[] <- lapply(d, function(x) { |
| 82 |
tryCatch(as.numeric(x), warning = function(w) x) |
| 83 |
}) |
| 84 |
} |
| 85 |
d |
| 86 |
} |