Learn R Programming

MonotonicityTest (version 1.3)

monotonicity_test: Perform Monotonicity Test

Description

Performs a monotonicity test between the vectors \(X\) and \(Y\) as described in Hall and Heckman (2000). This function uses a bootstrap approach to test for monotonicity in a nonparametric regression setting.

Usage

monotonicity_test(
  X,
  Y,
  bandwidth = bw.nrd(X) * (length(X)^-0.1),
  boot_num = 200,
  m = floor(0.05 * length(X)),
  ncores = 1,
  negative = FALSE,
  check_m = FALSE,
  seed = NULL
)

Value

A monotonicity_result object. Has associated `print`, `summary`, and `plot` S3 functions.

Arguments

X

Numeric vector of predictor variable values. Must not contain missing or infinite values.

Y

Numeric vector of response variable values. Must not contain missing or infinite values.

bandwidth

Numeric value for the kernel bandwidth used in the Nadaraya-Watson estimator. Default is calculated as bw.nrd(X) * (length(X) ^ -0.1).

boot_num

Integer specifying the number of bootstrap samples. Default is 200.

m

Integer parameter used in the calculation of the test statistic. Corresponds to the minimum window size to calculate the test statistic over or a "smoothing" parameter. Lower values increase the sensitivity of the test to local deviations from monotonicity. Default is floor(0.05 * length(X)).

ncores

Integer specifying the number of cores to use for parallel processing. Default is 1.

negative

Logical value indicating whether to test for a monotonic decreasing (negative) relationship. Default is FALSE.

check_m

Boolean value indicating whether to run the test for many different values of m. This produces extra plots when calling plot and has a marginal impact on performance. Default is FALSE.

seed

Optional integer for setting the random seed. If NULL (default), the global random state is used.

Details

The test evaluates the following hypotheses:

\(H_0\): The regression function is monotonic

  • Non-decreasing if negative = FALSE

  • Non-increasing if negative = TRUE

\(H_A\): The regression function is not monotonic

References

Hall, P., & Heckman, N. E. (2000). Testing for monotonicity of a regression mean by calibrating for linear functions. The Annals of Statistics, 28(1), 20–39.

Examples

Run this code
# Example 1: Usage on monotonic increasing function
# Generate sample data
seed <- 42
set.seed(seed)

X <- runif(500)
Y <- 4 * X + rnorm(500, sd = 1)
result <- monotonicity_test(X, Y, boot_num = 25, seed = seed)

print(result)

# Example 2: Usage on non-monotonic function
seed <- 42
set.seed(seed)

X <- runif(500)
Y <- (X - 0.5) ^ 2 + rnorm(500, sd = 0.5)
result <- monotonicity_test(X, Y, boot_num = 25, seed = seed)

print(result)

Run the code above in your browser using DataLab