Skip to content

Commit

Permalink
tidy up tidy fun
Browse files Browse the repository at this point in the history
  • Loading branch information
bailliem committed Jul 26, 2024
1 parent ee56e78 commit d3e1d78
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# beeca 0.1.4

- Add `tidy_beeca()` function

# beeca 0.1.3

- Preparation for CRAN submission
Expand Down
3 changes: 3 additions & 0 deletions R/beeca-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
#' @importFrom lifecycle deprecated
## usethis namespace: end
NULL

## declare global variables
utils::globalVariables(c("estimate", "std.error", "statistic"))
21 changes: 14 additions & 7 deletions R/tidy_beeca.R
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(
Expand All @@ -69,19 +74,21 @@ 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
if (conf.int) {

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)
}


12 changes: 8 additions & 4 deletions man/tidy_beeca.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions tests/testthat/test-tidy_beeca.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})



0 comments on commit d3e1d78

Please sign in to comment.