Skip to contents

[Stable]

Various tests were implemented to test the difference between two proportions.

Usage

prop_chisq(tbl)

prop_schouten(tbl)

prop_fisher(tbl)

prop_cmh(ary)

s_test_proportion_diff(
  df,
  .var,
  .ref_group,
  .in_ref_col,
  variables = list(strata = NULL),
  method = c("chisq", "schouten", "fisher", "cmh")
)

a_test_proportion_diff(
  df,
  .var,
  .ref_group,
  .in_ref_col,
  variables = list(strata = NULL),
  method = c("chisq", "schouten", "fisher", "cmh")
)

test_proportion_diff(
  lyt,
  vars,
  ...,
  show_labels = "hidden",
  table_names = vars,
  .stats = NULL,
  .formats = NULL,
  .labels = NULL,
  .indent_mods = NULL
)

Arguments

tbl

(matrix)
with two groups in rows and the binary response (TRUE/FALSE) in columns.

ary

(array, 3 dimensions)
with two groups in rows, the binary response (TRUE/FALSE) in columns, the strata in the third dimension.

df

(data frame)
data set containing all analysis variables.

.var

(string)
single variable name that is passed by rtables when requested by a statistics function.

.ref_group

(data frame or vector)
the data corresponding to the reference group.

.in_ref_col

(logical)
TRUE when working with the reference level, FALSE otherwise.

variables

(named list of string)
list of additional analysis variables.

method

(string)
one of (chisq, cmh, fisher, schouten; specifies the test used to calculate the p-value.

lyt

(layout)
input layout where analyses will be added to.

vars

(character)
variable names for the primary analysis variable to be iterated over.

...

other arguments are passed to s_test_proportion_diff().

show_labels

label visibility: one of "default", "visible" and "hidden".

table_names

(character)
this can be customized in case that the same vars are analyzed multiple times, to avoid warnings from rtables.

.stats

(character)
statistics to select for the table.

.formats

(named character or list)
formats for the statistics.

.labels

(named character)
labels for the statistics (without indent).

.indent_mods

(named integer)
indent modifiers for the labels.

Value

Named list with a single item pval with an attribute label

describing the method used. The p-value tests the null hypothesis that proportions in two groups are the same.

Functions

  • prop_chisq(): performs Chi-Squared test. Internally calls stats::prop.test().

  • prop_schouten(): performs the Chi-Squared test with Schouten correction.

  • prop_fisher(): performs the Fisher's exact test. Internally calls stats::fisher.test().

  • prop_cmh(): performs stratified Cochran-Mantel-Haenszel test. Internally calls stats::mantelhaen.test(). Note that strata with less than two observations are automatically discarded.

  • s_test_proportion_diff(): Statistics function which test the difference between two proportions.

  • a_test_proportion_diff(): Formatted Analysis function which can be further customized by calling rtables::make_afun() on it. It is used as afun in rtables::analyze().

  • test_proportion_diff(): Layout creating function which can be used for creating tables, which can take statistics function arguments and additional format arguments.

See also

For information on the Schouten correction (Schouten, 1980), visit https://onlinelibrary.wiley.com/doi/abs/10.1002/bimj.4710220305.

Examples

# Non-stratified proportion difference test

## Data
A <- 20
B <- 20
set.seed(1)
rsp <- c(
  sample(c(TRUE, FALSE), size = A, prob = c(3 / 4, 1 / 4), replace = TRUE),
  sample(c(TRUE, FALSE), size = A, prob = c(1 / 2, 1 / 2), replace = TRUE)
)
grp <- c(rep("A", A), rep("B", B))
tbl <- table(grp, rsp)

## Chi-Squared test
# Internal function - prop_chisq
if (FALSE) {
prop_chisq(tbl)
}

## Chi-Squared test + Schouten correction.
# Internal function - prop_schouten
if (FALSE) {
prop_schouten(tbl)
}

## Fisher's exact test
# Internal function - prop_fisher
if (FALSE) {
prop_fisher(tbl)
}

# Stratified proportion difference test

## Data
rsp <- sample(c(TRUE, FALSE), 100, TRUE)
grp <- factor(rep(c("A", "B"), each = 50))
strata <- factor(rep(c("V", "W", "X", "Y", "Z"), each = 20))
tbl <- table(grp, rsp, strata)

## Cochran-Mantel-Haenszel test
# Internal function - prop_cmh
if (FALSE) {
prop_cmh(tbl)
}


# Statistics function
dta <- data.frame(
  rsp = sample(c(TRUE, FALSE), 100, TRUE),
  grp = factor(rep(c("A", "B"), each = 50)),
  strat = factor(rep(c("V", "W", "X", "Y", "Z"), each = 20))
)

# Internal function - s_test_proportion_diff
if (FALSE) {
s_test_proportion_diff(
  df = subset(dta, grp == "A"),
  .var = "rsp",
  .ref_group = subset(dta, grp == "B"),
  .in_ref_col = FALSE,
  variables = list(strata = "strat"),
  method = "cmh"
)
}

# Internal function - a_test_proportion_diff
if (FALSE) {
a_test_proportion_diff(
  df = subset(dta, grp == "A"),
  .var = "rsp",
  .ref_group = subset(dta, grp == "B"),
  .in_ref_col = FALSE,
  variables = list(strata = "strat"),
  method = "cmh"
)
}

# With `rtables` pipelines.
l <- basic_table() %>%
  split_cols_by(var = "grp", ref_group = "B") %>%
  test_proportion_diff(
    vars = "rsp",
    method = "cmh", variables = list(strata = "strat")
  )

build_table(l, df = dta)
#>                                            B     A   
#> —————————————————————————————————————————————————————
#>   p-value (Cochran-Mantel-Haenszel Test)       0.3736