Learn R Programming

LRTesteR Overview

LRTesteR provides likelihood ratio tests and confidence intervals for many common distributions.

Example 1: Test lambda of a poisson distribution

To test lambda, simply call poisson_lambda_one_sample.

library(LRTesteR)

set.seed(1)
x <- rpois(n = 100, lambda = 1)
poisson_lambda_one_sample(x = x, lambda = 1, alternative = "two.sided")
#> Log Likelihood Statistic: 0.01
#> p value: 0.92
#> Confidence Level: 95%
#> Confidence Interval: (0.826, 1.22)

Example 2: Confidence Interval

To get a confidence interval, set the conf.level to the desired confidence. Below gets a two sided 90% confidence interval for scale from a Cauchy random variable.

set.seed(1)
x <- rcauchy(n = 100, location = 3, scale = 5)
cauchy_scale_one_sample(x = x, scale = 5, alternative = "two.sided", conf.level = .90)
#> Log Likelihood Statistic: 1.21
#> p value: 0.271
#> Confidence Level: 90%
#> Confidence Interval: (4.64, 7.284)

Setting alternative to “less” gets a lower one sided interval.

cauchy_scale_one_sample(x = x, scale = 5, alternative = "less", conf.level = .90)
#> Log Likelihood Statistic: 1.1
#> p value: 0.865
#> Confidence Level: 90%
#> Confidence Interval: (0, 6.93)

Setting it to “greater” gets an upper one sided interval.

cauchy_scale_one_sample(x = x, scale = 5, alternative = "greater", conf.level = .90)
#> Log Likelihood Statistic: 1.1
#> p value: 0.135
#> Confidence Level: 90%
#> Confidence Interval: (4.878, Inf)

Example 3: One-way Analysis

One-way ANOVA is generalized to all distributions. Here gamma random variables are created with different shapes. The one way test has a small p value and provides confidence intervals with 95% confidence for the whole set.

set.seed(1)
x <- c(rgamma(n = 50, shape = 1, rate = 2), rgamma(n = 50, shape = 2, rate = 2), rgamma(n = 50, shape = 3, rate = 2))
fctr <- c(rep(1, 50), rep(2, 50), rep(3, 50))
fctr <- factor(fctr, levels = c("1", "2", "3"))
gamma_shape_one_way(x = x, fctr = fctr, conf.level = .95)
#> Log Likelihood Statistic: 68.59
#> p value: 0
#> Confidence Level Of Set: 95%
#> Individual Confidence Level: 98.3%
#> Confidence Interval For Group 1: (0.65, 1.515)
#> Confidence Interval For Group 2: (1.376, 3.376)
#> Confidence Interval For Group 3: (1.691, 4.192)

Example 4: Empirical Likelihood

The empirical likelihood tests do not require any distributional assumptions and work with less data.

set.seed(1)
x <- rnorm(n = 25, mean = 1, sd = 1)
empirical_mu_one_sample(x = x, mu = 1, alternative = "two.sided")
#> Log Likelihood Statistic: 0.73
#> p value: 0.392
#> Confidence Level: 95%
#> Confidence Interval: (0.752, 1.501)

The $\chi^2$ approximation

As implemented, all functions depend on the asymptotic $\chi^2$ approximation. To get a sense of accuracy of this approximation for large samples, the likelihood tests are compared to the exact tests.

X is normally distributed with mu equal to 3 and standard deviation equal to 2. The two intervals for $\mu$ are similar.

set.seed(1)
x <- rnorm(n = 500, mean = 3, sd = 2)
exactTest <- t.test(x = x, mu = 2.5, alternative = "two.sided", conf.level = .95)
likelihoodTest <- gaussian_mu_one_sample(x = x, mu = 2.5, alternative = "two.sided", conf.level = .95)
as.numeric(exactTest$conf.int)
#> [1] 2.867461 3.223115
likelihoodTest$conf.int
#> [1] 2.867729 3.222847

The confidence intervals for variance are similar as well.

set.seed(1)
x <- rnorm(n = 500, mean = 3, sd = 2)
sigma2 <- 1.5^2 # Variance, not standard deviation.
exactTest <- EnvStats::varTest(x = x, sigma.squared = sigma2, alternative = "two.sided", conf.level = .95)
likelihoodTest <- gaussian_variance_one_sample(x = x, sigma.squared = sigma2, alternative = "two.sided", conf.level = .95)
as.numeric(exactTest$conf.int)
#> [1] 3.631734 4.655834
likelihoodTest$conf.int
#> [1] 3.620303 4.639384

Changing to p for a binomial random variable, the confidence intervals are similar yet again.

exactTest <- stats::binom.test(x = 250, n = 500, p = .50, alternative = "two.sided", conf.level = .95)
likelihoodTest <- binomial_p_one_sample(x = 250, n = 500, p = .50, alternative = "two.sided", conf.level = .95)
as.numeric(exactTest$conf.int)
#> [1] 0.4552856 0.5447144
likelihoodTest$conf.int
#> [1] 0.4562579 0.5437421

When sample size is small, similarity will decrease. When exact methods are available, they are the better option. The utility of the likelihood based approach is its generality. Many tests in this package don’t have other well known options.

Estimated asymptotic type I and type II error rates can be found here.

Copy Link

Version

Install

install.packages('LRTesteR')

Monthly Downloads

592

Version

1.2.1

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Greg McMahan

Last Published

September 8th, 2024

Functions in LRTesteR (1.2.1)

gamma_scale_one_sample

Test the scale parameter of a gamma distribution.
exponential_rate_one_way

Test the equality of rate parameters of exponential distributions.
gaussian_mu_one_sample

Test the mean of a gaussian distribution.
gamma_shape_one_sample

Test the shape parameter of a gamma distribution.
poisson_lambda_one_sample

Test the lambda parameter of a poisson distribution.
gamma_scale_one_way

Test the equality of scale parameters of gamma distributions.
inverse_gaussian_mu_one_way

Test the equality of means of inverse gaussian distributions.
inverse_gaussian_mu_one_sample

Test the mean of an inverse gaussian distribution.
poisson_lambda_one_way

Test the equality of lambda parameters of poisson distributions.
gaussian_variance_one_way

Test the equality of variance parameters of gaussian distributions.
gaussian_variance_one_sample

Test the variance of a gaussian distribution.
print.lrtest

Print results of tests.
inverse_gaussian_dispersion_one_way

Test the equality of dispersion parameters of inverse gaussian distributions.
inverse_gaussian_dispersion_one_sample

Test the dispersion parameter of an inverse gaussian distribution.
gaussian_mu_one_way

Test the equality of means of gaussian distributions.
negative_binomial_p_one_way

Test the equality of p parameters of negative binomial distributions.
negative_binomial_p_one_sample

Test the p parameter of a negative binomial distribution.
gamma_shape_one_way

Test the equality of shape parameters of gamma distributions.
inverse_gaussian_shape_one_sample

Test the shape parameter of an inverse gaussian distribution.
inverse_gaussian_shape_one_way

Test the equality of shape parameters of inverse gaussian distributions.
beta_shape1_one_sample

Test the shape1 parameter of a beta distribution.
beta_shape2_one_way

Test the equality of shape 2 parameters of beta distributions.
cauchy_location_one_sample

Test the location parameter of a cauchy distribution.
beta_shape1_one_way

Test the equality of shape 1 parameters of beta distributions.
cauchy_scale_one_way

Test the equality of scale parameters of cauchy distributions.
binomial_p_one_way

Test the equality of p parameters of binomial distributions.
binomial_p_one_sample

Test the p parameter of a binomial distribution.
cauchy_location_one_way

Test the equality of location parameters of cauchy distributions.
cauchy_scale_one_sample

Test the scale parameter of a cauchy distribution.
beta_shape2_one_sample

Test the shape2 parameter of a beta distribution.
gamma_rate_one_sample

Test the rate parameter of a gamma distribution.
gamma_rate_one_way

Test the equality of rate parameters of gamma distributions.
exponential_rate_one_sample

Test the rate parameter of a exponential distribution.
empirical_mu_one_sample

Test the mean parameter of an unknown distribution.
empirical_mu_one_way

Test the equality of means of an unknown distribution.
empirical_quantile_one_sample

Test a quantile of an unknown distribution.
empirical_quantile_one_way

Test the equality of a quantile from an unknown distribution.