File: methods_epi2x2.R

package info (click to toggle)
r-cran-parameters 0.24.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,852 kB
  • sloc: sh: 16; makefile: 2
file content (41 lines) | stat: -rw-r--r-- 1,906 bytes parent folder | download
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
#' @export
model_parameters.epi.2by2 <- function(model, verbose = TRUE, ...) {
  # get parameter estimates
  params <- insight::get_parameters(model)
  colnames(params)[2] <- "Coefficient"

  # get coefficients including CI
  coef_names <- grepl("^([^NNT]*)(\\.strata\\.wald)", names(model$massoc.detail), perl = TRUE)
  cf <- model$massoc.detail[coef_names]
  names(cf) <- gsub(".strata.wald", "", names(cf), fixed = TRUE)

  # extract CI
  cis <- do.call(rbind, cf)
  cis$Parameter <- rownames(cis)
  cis$est <- NULL
  colnames(cis) <- c("CI_low", "CI_high", "Parameter")

  # merge
  params <- merge(params, cis, sort = FALSE)

  # find fraction estimates, multiply by 100 to get percentages
  fractions <- params$Parameter %in% c("AFRisk", "PAFRisk")
  params[fractions, c("Coefficient", "CI_low", "CI_high")] <- 100 * params[fractions, c("Coefficient", "CI_low", "CI_high")]

  # pretty names
  pretty_names <- params$Parameter
  pretty_names[pretty_names == "PR"] <- "Prevalence Ratio"
  pretty_names[pretty_names == "RR"] <- "Risk Ratio"
  pretty_names[pretty_names == "OR"] <- "Odds Ratio"
  pretty_names[pretty_names == "ARisk"] <- "Attributable Risk"
  pretty_names[pretty_names == "PARisk"] <- "Attributable Risk in Population"
  pretty_names[pretty_names == "AFRisk"] <- "Attributable Fraction in Exposed (%)"
  pretty_names[pretty_names == "PAFRisk"] <- "Attributable Fraction in Population (%)"

  stats <- model$massoc.detail$chi2.strata.uncor
  attr(params, "footer_text") <- paste0("Test that Odds Ratio = 1: Chi2(", stats[["df"]], ") = ", insight::format_value(stats[["test.statistic"]]), ", ", insight::format_p(stats[["p.value.2s"]]))
  attr(params, "pretty_names") <- stats::setNames(pretty_names, params$Parameter)
  attr(params, "no_caption") <- TRUE
  class(params) <- c("parameters_model", "see_parameters_model", class(params))
  params
}