Given a set of Monte Carlo samples, estimates a p-value from the proportion of values that fall above or below a comparison point. If string is TRUE, takes a numeric p-value and converts it into a formatted character string, either 'p = ...' or 'p < ...'.

pvalues(
  x,
  comparison = 0,
  alternative = "two-sided",
  digits = 3,
  string = FALSE,
  pad = FALSE
)

Arguments

x

Either a) a vector of numeric values (Monte Carlo samples) or b) a single p-value.

comparison

The comparison value; the p-value is computed from the proportion of Monte Carlo samples above or below this cut-off.

alternative

A character string indicating the type of alternative hypothesis to test, either a) 'two-sided', b) 'less', or c) 'greater'. If 'two-side', uses the alternative hypothesis that produces the small p-value, and then multiplies by two to adjust for the two-sided comparison.

digits

Number of digits to round to when formatting the p-value.

string

Logical; if TRUE, returns output as a nicely formatted character string. Automatically set to TRUE if length of x equals 1.

pad

Logical; if TRUE, pads number of values to right of decimal to always equal digits.

Value

Either a numeric p-value or a character string, a nicely formatted version of the p-value.

Examples

# Example based on two-sample t-test
set.seed(40)
x <- data.frame(
  y = c(rnorm(50), rnorm(50, mean = .3)),
  group = rep(1:2, each = 50)
)

# Two-sample t-test
tt <- t.test(y ~ group, data = x)
print(pvalues(tt$p.value))
#> [1] "p = 0.059"
print(pvalues(tt$p.value, digits = 2))
#> [1] "p = 0.06"

# For very small p-values, automatically
# converts to 'p < cut-off' format
print(pvalues(1e-6))
#> [1] "p < 0.001"

# Computing p-values from
# Monte Carlo samples

# Simulate data from standard normal;
# on average 50% of sample falls
# below zero
set.seed(50)
x <- rnorm(1000)

# Default is two-sided
pvalues(x)
#> [1] 0.984
# Can specify less than or greater than
pvalues(x, alternative = "less")
#> [1] 0.508
pvalues(x, alternative = "greater")
#> [1] 0.492
# Against different comparison point
pvalues(x, alternative = "less", comparison = .68)
#> [1] 0.247

# Simulate data from normal distribution
# with mean of 0.68, on average
# approximately 75% of sample falls
# below zero
set.seed(60)
x <- rnorm(1000, mean = .68)
pvalues(x)
#> [1] 0.448
pvalues(x, alternative = "less")
#> [1] 0.776
pvalues(x, alternative = "greater")
#> [1] 0.224