kaigu1990 / stabiot

Common Statistical Analysis for Clinical Trials in Biotech
GNU General Public License v3.0
2 stars 0 forks source link

Response Rate and Odds Ratio #4

Closed kaigu1990 closed 5 months ago

kaigu1990 commented 6 months ago

Create several functions below:


Regarding response rate and CI, its's a question of confidence Intervals for proportions. I prefer to utilize the functions that have been built up in existing packages.

DescTools::BinomCI(x = 54, n = 100, method = "clopper-pearson")

But how about handling the stratified Wilson? A few packages wrap it up. Such as tern::s_proportion(). However, I feel like it should remain as is and not be added because it is not often utilized.

The response difference can be computed by DescTools::BinomDiffCI() as well, so just wrap this function inside.

DescTools::BinomDiffCI(60, 100, 45, 100, method=c("wald"))

Regarding odds ratio and CI, it depends on if we need to add stratified variables. If not, it's a common odds ratio calculation that can be done with the DescTools::OddsRatio function.

DescTools::OddsRatio(matrix(c(20, 22, 30, 28), nrow = 2, byrow = TRUE), 
                     method = "wald", conf.level = 0.95)

Otherwise we can use logistic regression to calculate the odds ratio as well. And then if we use confint() to obtain the CI , that is Wald's CI actually.

set.seed(12)
dta <- data.frame(
  rsp = sample(c(TRUE, FALSE), 100, TRUE),
  grp = factor(rep(c("A", "B"), each = 50), levels = c("B", "A")),
  strata = factor(sample(c("C", "D"), 100, TRUE))
)

fit <- glm(rsp ~ grp, data = dta, family = binomial(link = "logit"))
exp(cbind(Odds_Ratio = coef(fit), confint(fit)))[-1, , drop = FALSE]

If we want to compute the odds ratio from the Cochran-Mantel-Haenszel test with stratified variables, I know there will be two approaches to handle it. One is mantelhaen.test() function, another is conditional logistic regression like survival::clogit() function. Given the results from those two way is not same, I'm going to compare with SAS and refer the documents to decide which one will be wrapped. As I know, tern package use the survival::clogit() to deal with the stratified CMH analysis.

set.seed(12)
dta <- data.frame(
  rsp = sample(c(TRUE, FALSE), 100, TRUE),
  grp = factor(rep(c("A", "B"), each = 50), levels = c("B", "A")),
  strata1 = factor(sample(c("C", "D"), 100, TRUE)),
  strata2 = factor(sample(c("E", "F"), 100, TRUE)),
  strata3 = factor(sample(c("G", "H"), 100, TRUE))
)

Using mantelhaen.test() function:

df <- dta %>% 
  count(grp, rsp, strata1, strata2, strata3)
tab <- xtabs(n ~ grp + rsp + strata1 + strata2 + strata3, data = df)
tb <- as.table(array(c(tab), dim = c(2, 2, 2 * 2 *2)))
mantelhaen.test(tb, correct = FALSE)

Using conditional logistic regression:

library(survival)
fit <- clogit(formula = rsp ~ grp + strata(strata1, strata2, strata3), data = dta)
exp(cbind(Odds_Ratio = coef(fit), confint(fit)))