Skip to contents

This function performs Fisher's exact test comparing each level of a factor to a control level, using counts of success and failure.

Usage

compare_to_control_fisher(
  data,
  factor_col,
  success_col,
  failure_col,
  control_level = NULL,
  alternative = "two.sided",
  conf.level = 0.95,
  p.adjust.method = "holm"
)

Arguments

data

A data frame containing the data

factor_col

String name of the column containing the factor (e.g., "dose")

success_col

String name of the column containing success counts (e.g., "alive")

failure_col

String name of the column containing failure counts (e.g., "dead")

control_level

The level to use as control (default: first level in the data)

alternative

Direction of the alternative hypothesis ("two.sided", "less", or "greater")

conf.level

Confidence level for the returned confidence interval

p.adjust.method

p-value adjustment method, passing to the stats function.

Value

A data frame with factor levels and corresponding p-values

Examples

# Generate example data
set.seed(123)
doses <- c(0, 0.1, 1, 10, 50, 100)
reps <- 4
data <- data.frame(
  dose = rep(doses, each = reps),
  replicate = rep(1:reps, times = length(doses))
)
data$alive <- sapply(data$dose, function(x) rbinom(1, 10, prob = exp(-0.02 * x)))
data$dead <- 10 - data$alive

# Run Fisher's exact test
compare_to_control_fisher(data, "dose", "alive", "dead", control_level = 0)
#>    dose p_value odds_ratio ci_lower ci_upper p_adjusted
#> 1   0.1  1.0000          0   0.0000      Inf     1.0000
#> 2   1.0  0.4937        Inf   0.1886      Inf     0.9873
#> 3  10.0  0.0010        Inf   2.6809      Inf     0.0031
#> 4  50.0  0.0000        Inf  12.5317      Inf     0.0000
#> 5 100.0  0.0000        Inf  47.0319      Inf     0.0000