This function extracts and performs a test of the distribution of (leading) digits in a vector against a reference distribution. By default, the distribution of leading digits is checked against Benford's law.
distr.test(x, check = 'first', reference = 'benford')
a numeric vector.
location of the digits to analyze. Can be first
, firsttwo
, or last
.
which character string given the reference distribution for the digits, or a vector of probabilities for each digit. Can be benford
for Benford's law, uniform
for the uniform distribution. An error is given if any entry of reference
is negative. Probabilities that do not sum to one are normalized.
An object of class dt.distr
containing:
the observed counts.
the expected counts under the null hypothesis.
the number of observations in x
.
the value the chi-squared test statistic.
the degrees of freedom of the approximate chi-squared distribution of the test statistic.
the p-value for the test.
checked digits.
vector of digits.
reference distribution
a character string giving the name(s) of the data.
Benford's law is defined as \(p(d) = log10(1/d)\). The uniform distribution is defined as \(p(d) = 1/d\).
Benford, F. (1938). The law of anomalous numbers. In Proceedings of the American Philosophical Society, 551-572.
set.seed(1)
x <- rnorm(100)
# Digit analysis against Benford's law
distr.test(x, check = 'first', reference = 'benford')
#>
#> Digit distribution test
#>
#> data: x
#> n = 100, X-squared = 14.557, df = 8, p-value = 0.06836
#> alternative hypothesis: leading digit(s) are not distributed according to the benford distribution.
# Digit analysis against custom distribution
distr.test(x, check = 'last', reference = rep(1/9, 9))
#>
#> Digit distribution test
#>
#> data: x
#> n = 100, X-squared = 4.22, df = 8, p-value = 0.8367
#> alternative hypothesis: last digit(s) are not distributed according to the reference distribution.