From d3e1d78418b55c86929dc3503a60357f12d9d0fe Mon Sep 17 00:00:00 2001 From: bailliem Date: Fri, 26 Jul 2024 20:12:41 +0200 Subject: [PATCH] tidy up tidy fun --- NEWS.md | 2 ++ R/beeca-package.R | 3 +++ R/tidy_beeca.R | 21 ++++++++++++++------- man/tidy_beeca.Rd | 12 ++++++++---- tests/testthat/test-tidy_beeca.R | 8 +------- 5 files changed, 28 insertions(+), 18 deletions(-) diff --git a/NEWS.md b/NEWS.md index 9c90526..fcfb1b2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # beeca 0.1.4 +- Add `tidy_beeca()` function + # beeca 0.1.3 - Preparation for CRAN submission diff --git a/R/beeca-package.R b/R/beeca-package.R index 94b926a..10a8b0c 100644 --- a/R/beeca-package.R +++ b/R/beeca-package.R @@ -5,3 +5,6 @@ #' @importFrom lifecycle deprecated ## usethis namespace: end NULL + +## declare global variables +utils::globalVariables(c("estimate", "std.error", "statistic")) diff --git a/R/tidy_beeca.R b/R/tidy_beeca.R index 7818901..fac676e 100644 --- a/R/tidy_beeca.R +++ b/R/tidy_beeca.R @@ -45,7 +45,7 @@ #' broom::tidy(fit1, conf.int = TRUE) #' tidy_beeca(fit1, conf.int = TRUE) #' -tidy_beeca <- function(x, conf.int = FALSE, conf.level = 0.95, ...) { +tidy_beeca <- function(x, conf.int = FALSE, conf.level = 0.95) { results <- NULL @@ -56,11 +56,16 @@ tidy_beeca <- function(x, conf.int = FALSE, conf.level = 0.95, ...) { stop("x must be a 'beeca' object") } - results <- broom::tidy(x) - - ## Extract results + # extract results marginal_results <- x$marginal_results + # Validate that marginal_results has the necessary columns + required_cols <- c("TRTVAR", "STAT", "TRTVAL", "STATVAL") + if (!all(required_cols %in% names(marginal_results))) { + stop("marginal_results must contain the required columns: ", paste(required_cols, collapse = ", ")) + } + + # tidy inputs --------------------------------------------------------------- # define a tidy tibble result <- tibble::tibble( @@ -69,7 +74,7 @@ tidy_beeca <- function(x, conf.int = FALSE, conf.level = 0.95, ...) { estimate = marginal_results[marginal_results$STAT == "diff", "STATVAL"][[1]], std.error = marginal_results[marginal_results$STAT == "diff_se", "STATVAL"][[1]], statistic = estimate / std.error, # z-score - p.value = 2 * (1 - pnorm(abs(statistic))) # 2-sided p-value + p.value = 2 * (1 - stats::pnorm(abs(statistic))) # 2-sided p-value ) ## add confidence interval if specified @@ -77,11 +82,13 @@ tidy_beeca <- function(x, conf.int = FALSE, conf.level = 0.95, ...) { result <- result |> dplyr::mutate( - conf.low = estimate - qnorm(1 - conf.level / 2) * std.error, - conf.high = estimate + qnorm(1 - conf.level / 2) * std.error + conf.low = estimate - stats::qnorm(1 - conf.level / 2) * std.error, + conf.high = estimate + stats::qnorm(1 - conf.level / 2) * std.error ) } # return tidy tibble return(result) } + + diff --git a/man/tidy_beeca.Rd b/man/tidy_beeca.Rd index f30d335..a28f6f3 100644 --- a/man/tidy_beeca.Rd +++ b/man/tidy_beeca.Rd @@ -4,7 +4,7 @@ \alias{tidy_beeca} \title{Tidy a beeca/glm object} \usage{ -tidy_beeca(x, conf.int = FALSE, conf.level = 0.95, ...) +tidy_beeca(x, conf.int = FALSE, conf.level = 0.95) } \arguments{ \item{x}{a 'glm' object created with \code{get_marginal_effect()}} @@ -15,7 +15,7 @@ tidy_beeca(x, conf.int = FALSE, conf.level = 0.95, ...) } \value{ a tibble -the default values returned are the following +The default values returned are the following \tabular{ll}{ term \tab treatment variable \cr contrast \tab the contrast used to estimate the marginal effect \cr @@ -23,14 +23,18 @@ estimate \tab the average treatment effect \cr std.error \tab the standard error based on the Ge or Ye method \cr statistic \tab the z-score of the estimate divided by the standard error \cr p.value \tab 2-sided p-value \cr +conf.low \tab lower bound of the confidence interval (if specified) \cr +conf.high \tab upper bound of the confidence interval (if specified) \cr } } \description{ +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}}\cr +} +\details{ The broom package exports a tidier for \code{"glm"} objects. This function adds on top of that and returns more information that is stored from g-computation. -} -\details{ + The function also utilizes additional information stored when the glm object is created with \code{get_marginal_effect()}. It's recommended to always use this function with \code{get_marginal_effect()}. diff --git a/tests/testthat/test-tidy_beeca.R b/tests/testthat/test-tidy_beeca.R index 8c47781..1cbd2eb 100644 --- a/tests/testthat/test-tidy_beeca.R +++ b/tests/testthat/test-tidy_beeca.R @@ -43,22 +43,16 @@ test_that("tidy_beeca returns correct values", { expect_equal(result$term, "trtp") expect_equal(result$estimate, fit$marginal_results$STATVAL[fit$marginal_results$STAT == "diff"]) expect_equal(result$std.error, fit$marginal_results$STATVAL[fit$marginal_results$STAT == "diff_se"]) - expect_equal(result$statistic, NA) - expect_equal(result$p.value, NA) -}) + }) test_that("tidy_beeca returns correct values with confidence intervals", { result <- tidy_beeca(fit, conf.int = TRUE) expect_equal(result$term, "trtp") expect_equal(result$estimate, fit$marginal_results$STATVAL[fit$marginal_results$STAT == "diff"]) expect_equal(result$std.error, fit$marginal_results$STATVAL[fit$marginal_results$STAT == "diff_se"]) - expect_equal(result$statistic, NA) - expect_equal(result$p.value, NA) conf.low <- result$estimate - qnorm(1 - 0.95 / 2) * result$std.error conf.high <- result$estimate + qnorm(1 - 0.95 / 2) * result$std.error expect_equal(result$conf.low, conf.low, tolerance = 1e-8) expect_equal(result$conf.high, conf.high, tolerance = 1e-8) }) - -