Learn R Programming

sensitivityIxJ (version 0.1.5)

exact.general.sen.IxJ: Exact Sensitivity Analysis for General Test Statistics in I by J Tables

Description

This function computes exact p-values for sensitivity analysis in I by J contingency tables under the generic bias model. It enumerates all tables in the reference set and calculates the maximum p-value over the sensitivity parameter space (u allocations). The function is designed for general permutation-invariant test statistics.

Usage

exact.general.sen.IxJ(
  u_space = NULL,
  obs.table,
  table_space,
  gamma,
  delta,
  row = "treatment",
  verbose = FALSE
)

Value

A list containing:

rct.prob

Probability under Randomized Controlled Trial (RCT) with u_allocation set to zero.

max.prob

Maximum probability found across all allocations in u_space.

maximizer

The u_allocation vector that yields max.prob.

gamma

Extracted gamma value from the generic bias model.

delta

remind the practitioners of their delta

Arguments

u_space

A matrix where each row represents a unique allocation of u_i=1 across outcomes. If NULL, the function generates a default set of corner allocations based on the number of outcomes. Defaults to NULL.

obs.table

A matrix or table object representing the observed contingency table.

table_space

A list of matrices or table objects representing the space of contingency tables to consider (typically all tables with test statistic >= observed).

gamma

A scalar

delta

A binary vector with no more than two unique values, corresponding to treatment levels. The length must match the number of treatments (rows of obs.table if row = "treatment", or columns if row = "outcome").

row

A string indicating whether rows represent "outcome" or "treatment". Must be either "outcome" or "treatment". Default is "treatment".

verbose

A logical flag indicating whether to print progress messages showing the current maximizer and probability at each step. Default is FALSE.

Details

The function performs exact sensitivity analysis by:

  1. Enumerating all possible u allocations (or using provided u_space)

  2. Computing the p-value for each allocation by summing probabilities over table_space

  3. Finding the allocation that maximizes the p-value

For computational efficiency, the function only supports certain table dimensions. If u_space is not provided, default corner allocations are generated for J <= 5.

Examples

Run this code
# \donttest{
## Example 1: 3 by 3 table with custom test statistic
# Create an observed table (example data)
obs.table <- matrix(c(5, 3, 2, 6, 11, 7, 3, 0, 3), ncol = 3, byrow = TRUE)

# Define a test statistic emphasizing certain cells
transform.fun <- function(tb){
  test.stat <- 4 * tb[3, 3] + 3 * tb[2, 3]
  return(test.stat)
}
obs.stat <- transform.fun(obs.table)

# Find the reference set (tables with test statistic >= observed)
table.set <- possible.table(
  threshold = obs.stat,
  table = obs.table,
  direction = "greater than",
  transform.fun = transform.fun
)

# Perform sensitivity analysis
sen.result <- exact.general.sen.IxJ(
  obs.table = obs.table,
  table_space = table.set,
  gamma = 0.5,
  delta = c(0, 1, 1)
)
sen.result
# }

Run the code above in your browser using DataLab