convert_x_to_p: Convert X Distribution to the p-Value Distribution

View source: R/distn_process.R

convert_x_to_pR Documentation

Convert X Distribution to the p-Value Distribution

Description

This function converts a discrete X statistic and its distribution (characterized by its support vector and probability mass vector) to the p-value and its distribution (characterized by the p-value support, with increasingly sorted unique possible elements).

Usage

convert_x_to_p(x = NULL, x_support, x_prob, side = "two")

Arguments

x

An observed X value. Default is NULL (for the purpose of getting the p_support only).

x_support

Vector of support of X, i.e., all possible unique X values sorted increasingly.

x_prob

Vector of probability masses, matching with elements of x_support.

side

Side of p-value. One of "two" (default), "left", or "right", specifying the tail for the p-value computation.

Details

Let X be a random discrete variable. The p-values are defined as:

For left-sided:

p(x) = P(X \le x)

For right-sided:

p(x) = P(X \ge x)

For two-sided:

p(x) = \sum_{y: P(X \le y) \le P(X \le x)} P(X = y)

By definition, p_support does not contain 0 and always contains 1. However, the p_support may contain repeated 0's and 1's due to numerical limitations. The package has tried to account for this issue.

Value

A list with elements:

p

The observed p-value corresponding to the input x. It is NULL if x is NULL.

p_support

Unique sorted vector of p-value support.

p_by_x

Vector of possible p-values matching the X support. This is not used in the main function, but can be useful for debugging.

Examples

x_support <- 0:5
x_prob <- dbinom(x_support, size = 5, prob = 0.1)
convert_x_to_p(3, x_support, x_prob, side = "left")
convert_x_to_p(3, x_support, x_prob, side = "right")
convert_x_to_p(3, x_support, x_prob, side = "two")
convert_x_to_p(NULL, x_support, x_prob, side = "two") # x can be NULL: output p is NULL

# x must be in x_support if not NULL. Error message otherwise.
# convert_x_to_p(6, x_support, x_prob, side = "two") #This gives Error as expected.

# symmetric distribution leads to duplicated values in p_by_x and a shorter p_support.
x_prob <- dbinom(x_support, size = 5, prob = 0.5)
convert_x_to_p(3, x_support, x_prob, side = "two")

# For large distributions, the p_support is truncated for numerical stability.
# It is still valid for practical hypothesis testing purposes.
n = 100000
x_support = 0:n
x_prob = dbinom(x_support, size = n, prob = 0.7)
result = convert_x_to_p(3, x_support, x_prob, side = "left")
result$p
length(result$p_support) # truncated

DPComb documentation built on Aug. 22, 2026, 5:08 p.m.