Learn R Programming

fastrerandomize (version 0.3)

randomization_test: Fast randomization test

Description

Fast randomization test

Usage

randomization_test(
  obsW = NULL,
  obsY = NULL,
  alpha = 0.05,
  candidate_randomizations = NULL,
  candidate_randomizations_array = NULL,
  n0_array = NULL,
  n1_array = NULL,
  findFI = FALSE,
  c_initial = 2,
  conda_env = "fastrerandomize_env",
  conda_env_required = TRUE
)

Value

Returns an S3 object with slots:

  • p_value A numeric value or vector representing the p-value of the test (or the expected p-value under the prior structure specified in the function inputs).

  • FI A numeric vector representing the fiducial interval if findFI=TRUE.

  • tau_obs A numeric value or vector representing the estimated treatment effect(s).

  • fastrr_env The fastrerandomize environment.

Arguments

obsW

A numeric vector where 0's correspond to control units and 1's to treated units.

obsY

An optional numeric vector of observed outcomes. If not provided, the function assumes a NULL value.

alpha

The significance level for the test. Default is 0.05.

candidate_randomizations

A numeric matrix of candidate randomizations.

candidate_randomizations_array

An optional 'JAX' array of candidate randomizations. If not provided, the function coerces candidate_randomizations into a 'JAX' array.

n0_array

An optional array specifying the number of control units.

n1_array

An optional array specifying the number of treated units.

findFI

A logical value indicating whether to find the fiducial interval. Default is FALSE.

c_initial

A numeric value representing the initial criterion for the fiducial interval search. Default is 2.

conda_env

A character string specifying the name of the conda environment to use via reticulate. Default is "fastrerandomize_env".

conda_env_required

A logical indicating whether the specified conda environment must be strictly used. If TRUE, an error is thrown if the environment is not found. Default is TRUE.

References

  • Zhang, Y. and Zhao, Q., 2023. What is a randomization test?. Journal of the American Statistical Association, 118(544), pp.2928-2942.

See Also

generate_randomizations for randomization generation function.

Examples

Run this code
if (FALSE) {
# A small synthetic demonstration with 6 units, 3 treated and 3 controls:

# Generate pre-treatment covariates
X <- matrix(rnorm(24*2), ncol = 2)

# Generate candidate randomizations
RandomizationSet_MC <- generate_randomizations(
  n_units = nrow(X),
  n_treated = round(nrow(X)/2),
  X = X,
  randomization_accept_prob = 0.1,
  randomization_type = "monte_carlo",
  max_draws = 100000,
  batch_size = 1000
)

# Generate outcome
W <- RandomizationSet_MC$randomizations[1,]
obsY <- rnorm(nrow(X), mean = 2 * W)

# Perform randomization test
results_base <- randomization_test(
  obsW = W,
  obsY = obsY,
  candidate_randomizations = RandomizationSet_MC$randomizations
)
print(results_base)

# Perform randomization test with fiducial interval
result_fi <- randomization_test(
  obsW = W,
  obsY = obsY,
  candidate_randomizations = RandomizationSet_MC$randomizations,
  findFI = TRUE
)
print(result_fi)
}

Run the code above in your browser using DataLab