Skip to content
Merged

Dev #25

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ S3method(plot,dunnett_test_result)
S3method(print,RSCABS)
S3method(print,StepDownRSCABS)
S3method(print,drcComp)
S3method(print,dunn_test_result)
S3method(print,dunnett_test_result)
S3method(print,stepDownTrendBinom)
S3method(print,tskresult)
Expand All @@ -19,11 +20,13 @@ export(ECx_rating)
export(ED.ZG)
export(ED.plus)
export(RSCABK)
export(SpearmanKarber_modified)
export(Tarone.test)
export(Tarone.trend.test)
export(addECxCI)
export(aggregate_from_individual_simple)
export(aggregate_from_individual_tidy)
export(analyze_SK)
export(backCalcSE)
export(broom_dunnett)
export(broom_williams)
Expand All @@ -33,12 +36,14 @@ export(calcTaronesTest)
export(calculate_noec_rstatix)
export(cochranArmitageTrendTest)
export(compare_to_control_fisher)
export(compute_mdd_williams)
export(contEndpoint)
export(convert2Score)
export(convert_fish_data)
export(create_contingency_table)
export(dose.p.glmmPQL)
export(drcCompare)
export(dunn_test)
export(dunnett_test)
export(expand_to_individual_simple)
export(expand_to_individual_tidy)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# drcHelper (development version)

* Initial CRAN submission preparation.
57 changes: 57 additions & 0 deletions R/MDD.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#' Calculate MDD% for a Williams Test Result
#'
#' @param williams_obj The tibble result from broom_williams.
#' @param data The original dataframe used for the test.
#' @param formula The formula used for the test, e.g., Response ~ Dose.
#' @return A tibble with Dose, MDD, and MDD_pct.
#' @export
compute_mdd_williams <- function(williams_obj, data, formula) {

if (!inherits(williams_obj, "tbl_df") || !all(c("comparison", "t'-crit") %in% names(williams_obj))) {
stop("williams_obj must be the result from a broom_williams call.")
}
if (nrow(williams_obj) == 0) return(tibble::tibble(Dose=numeric(), MDD_pct=numeric()))

# 1. Extract variable names
resp_name <- all.vars(formula)[1]
dose_name <- all.vars(formula)[2] # Correctly gets "Dose"

# --- START OF FIX ---
# Create a local copy of the data to avoid modifying the original
local_data <- data

# Ensure the dose column is a factor with control (0) as the first level.
# This makes the function robust and independent of the input data's column type.
dose_values <- local_data[[dose_name]]
factor_levels <- sort(unique(dose_values))
local_data$dose_factor_col <- factor(dose_values, levels = factor_levels)

# Define the new, reliable factor column name and update formula for aov()
factor_col_name <- "dose_factor_col"
aov_formula <- as.formula(paste(resp_name, "~", factor_col_name))
# --- END OF FIX ---

# 2. Extract info from the Williams test object
Tcrit <- williams_obj[["t'-crit"]]
doses <- as.numeric(gsub(" - 0.*", "", williams_obj$comparison))

# 3. Get ANOVA stats (MSE) using the corrected formula and local data
aov_fit <- stats::aov(aov_formula, data = local_data)
mse <- summary(aov_fit)[[1]]["Residuals", "Mean Sq"]

# 4. Get control group stats using the reliable factor column
control_level <- levels(local_data[[factor_col_name]])[1]
ctrl_data <- local_data[local_data[[factor_col_name]] == control_level, ]
mu_c <- mean(ctrl_data[[resp_name]], na.rm = TRUE)
n_c <- nrow(ctrl_data)

# 5. Get treatment sample sizes
n_t <- sapply(doses, function(d) sum(local_data[[dose_name]] == d))

# 6. Calculate MDD and MDD%
SE_diff <- sqrt(mse * (1 / n_c + 1 / n_t))
MDD <- Tcrit * SE_diff
MDD_pct <- 100 * MDD / abs(mu_c)

tibble::tibble(Dose = doses, MDD_pct = MDD_pct)
}
22 changes: 11 additions & 11 deletions R/RSCABS.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

#' Run RSCABS test (DEPRECATED)
#'
#' @description
#' **DEPRECATED**: Please use [step_down_RSCABS()] instead for new code.
#'
#' @description
#' **DEPRECATED**: Please use [drcHelper::step_down_RSCABS()] instead for new code.
#'
#' Runs the Rao-Scott adjusted Cochran-Armitage trend test by slices (RSCABS)
#' analysis.The function is adapted from the archived version of RSCABS developed by
#' Joe Swintek et al with CC0 license. It is not updated anymore and included
#' for validation purpose. The modern replacement is [step_down_RSCABS()].
#'
#' @details
#' for validation purpose. The modern replacement is [drcHelper::step_down_RSCABS()].
#'
#' @details
#' This function is deprecated. For new analyses, please use the modern
#' implementation in [step_down_RSCABS()] which provides:
#' implementation in [drcHelper::step_down_RSCABS()] which provides:
#' - Better error handling and input validation
#' - More flexible data input formats
#' - More flexible data input formats
#' - Improved statistical methodology
#' - Better documentation and examples
#'
Expand Down Expand Up @@ -51,11 +51,11 @@
#' }
runRSCABS <- function(Data,Treatment,Replicate='',Effects='',test.type='RS'){
# Issue deprecation warning
.Deprecated("step_down_RSCABS",
msg = paste("runRSCABS() is deprecated.",
.Deprecated("step_down_RSCABS",
msg = paste("runRSCABS() is deprecated.",
"Please use step_down_RSCABS() for new code.",
"See ?step_down_RSCABS for the modern API."))

#This function will produce a table of step-down Cochran-Armitage trend tests with possible Rao-Scott adjustment by slices
#It will Run the test on every effect in the Effect list
#' @export
Expand Down
Loading