-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
114 lines (87 loc) · 4.31 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# LRTesteR Overview
<!-- badges: start -->
[](https://github.com/gmcmacran/LRTesteR/actions/workflows/R-CMD-check.yaml)
[](https://cran.r-project.org/package=LRTesteR)
<!-- badges: end -->
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.
```{r example1Part1}
library(LRTesteR)
set.seed(1)
x <- rpois(n = 100, lambda = 1)
poisson_lambda_one_sample(x = x, lambda = 1, alternative = "two.sided")
```
# 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.
```{r example2Part1}
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)
```
Setting alternative to "less" gets a lower one sided interval.
```{r example2Part2}
cauchy_scale_one_sample(x = x, scale = 5, alternative = "less", conf.level = .90)
```
Setting it to "greater" gets an upper one sided interval.
```{r example2Part3}
cauchy_scale_one_sample(x = x, scale = 5, alternative = "greater", conf.level = .90)
```
# 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.
```{r example3Part1}
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)
```
# Example 4: Empirical Likelihood
The empirical likelihood tests do not require any distributional assumptions and work with less data.
```{r example4Part1}
set.seed(1)
x <- rnorm(n = 25, mean = 1, sd = 1)
empirical_mu_one_sample(x = x, mu = 1, alternative = "two.sided")
```
# 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.
```{r MDPart1}
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)
likelihoodTest$conf.int
```
The confidence intervals for variance are similar as well.
```{r MDPart2}
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)
likelihoodTest$conf.int
```
Changing to p for a binomial random variable, the confidence intervals are similar yet again.
```{r MDPart3}
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)
likelihoodTest$conf.int
```
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](https://github.com/gmcmacran/TypeOneTypeTwoSim).