-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
358 lines (262 loc) · 10.5 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
---
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%",
message = F
)
```
# statBasics
`R` package of the course **Métodos Estatísticos** at Federal University of Bahia.
## Confidence interval for a single population parameter
Confidence interval are computed using the methods presented by Montgomery and Runger (2010). All implemented methods are slighted modification of methods already implemented in `stats`. The user can compute bilateral and unilateral confidence intervals.
### Confidence interval for a population proportion
In this package, there are three approaches for computing a confidence interval for a population proportion.
#### Number of successes in `n` (scalar value) trials
```{r}
library(tidyverse)
library(statBasics)
size <- 1000
sample <- rbinom(size, 1, prob = 0.5)
n_success <- sum(sample)
ci_1pop_bern(n_success, size, conf_level = 0.99)
```
#### Number of successes in a vector
```{r}
library(tidyverse)
library(statBasics)
n <- c(30, 20, 10)
x <- n |> map_int(~ sum(rbinom(1, size = .x, prob = 0.75)))
ci_1pop_bern(x, n, conf_level = 0.99)
```
#### Vector of successes
```{r}
library(tidyverse)
library(statBasics)
x <- rbinom(50, size = 1, prob = 0.75)
ci_1pop_bern(x, conf_level = 0.99)
```
### Confidence interval for a populationa mean (normal distribution)
We illustrate how to compute a confidence interval for the mean of a normal distribution in two cases: 1) the standard deviation is known; 2) the standard deviation is unknown.
#### Known standard deviation
```{r}
library(tidyverse)
library(statBasics)
media_pop <- 10
sd_pop <- 2
x <- rnorm(100, mean = media_pop, sd = sd_pop)
ci_1pop_norm(x, sd_pop = sd_pop, conf_level = 0.91)
```
#### Unknown standard deviation
```{r}
library(tidyverse)
library(statBasics)
media_pop <- 10
sd_pop <- 2
x <- rnorm(100, mean = media_pop)
ci_1pop_norm(x, conf_level = 0.91)
```
### Confidence interval for a population standard deviation (normal distribution)
```{r}
library(tidyverse)
library(statBasics)
media_pop <- 10
sd_pop <- 2
x <- rnorm(100, mean = media_pop, sd = sd_pop)
ci_1pop_norm(x, parameter = 'variance', conf_level = 0.91)
```
### Confidence interval for a population mean (exponential distribution)
```{r}
library(tidyverse)
library(statBasics)
media_pop <- 800
taxa_pop <- 1 / media_pop
x <- rexp(100, rate = taxa_pop)
ci_1pop_exp(x)
```
### Confidence interval for a population mean (general case)
In the general case, a confidence interval using the *t-Student* distribution is still suitable, even if the distribution is not normal, as illustrated in the example bellow.
```{r}
library(tidyverse)
library(statBasics)
media_pop <- 50
x <- rpois(100, lambda = media_pop)
ci_1pop_general(x)
```
## Hypothesis testing for a single population parameter
Next, we will illustrate how to use this package to test a statistical hypothesis about a single population parameter. All methods are already implemented in `R`. This package provides slight modifications for teaching purposes.
### Hypothesis testing for a population mean
In the examples below, `mean_null` is the mean in the null hypothesis `H0`:
1. `alternative == "two.sided"`: `H0: mu == mean_null` and `H1: mu != mean_null`. Default value.
2. `alternative == "less"`: `H0: mu >= mean_null` and `H1: mu < mean_null`
3. `alternative == "greater"`: `H0: mu =< mean_null` and `H1: mu > mean_null`
#### Normal distribution with known standard deviation
```{r}
library(tidyverse)
library(statBasics)
mean_null <- 5
sd_pop <- 2
x <- rnorm(100, mean = 10, sd = sd_pop)
ht_1pop_mean(x, mu = mean_null, conf_level = 0.95, sd_pop = sd_pop, alternative = "two.sided")
```
#### Normal distribution with unknown standard deviation
```{r}
library(tidyverse)
library(statBasics)
mean_null <- 5
sd_pop <- 2
x <- rnorm(100, mean = 10, sd = sd_pop)
ht_1pop_mean(x, mu = mean_null, conf_level = 0.95, alternative = "two.sided")
```
### Hypothesis testing for a population standard deviation
In the examples below, `sigma_null` is the standard deviation in the null hypothesis `H0`:
1. `alternative == "two.sided"`: `H0: sigma == sigma_null` and `H1: sigma != sigma_null`. Default value.
2. `alternative == "less"`: `H0: sigma >= sigma_null` and `H1: sigma < sigma_null`
3. `alternative == "greater"`: `H0: sigma =< sigma_null` and `H1: sigma > sigma_null`
```{r}
library(tidyverse)
library(statBasics)
sigma_null <- 4
sd_pop <- 2
x <- rnorm(100, mean = 10, sd = sd_pop)
ht_1pop_var(x, sigma = sigma_null, conf_level = 0.95, alternative = "two.sided")
```
### Hypothesis testing for a population proportion
In the examples below, `proportion_null` is the proportion in the null hypothesis `H0`:
1. `alternative == "two.sided"`: `H0: proportion == proportion_null` and `H1: proportion != proportion_null`. Default value.
2. `alternative == "less"`: `H0: proportion >= proportion_null` and `H1: proportion < proportion_null`
3. `alternative == "greater"`: `H0: proportion =< proportion_null` and `H1: proportion > proportion_null`
#### Number of successes
The following example illustrates how to perform a hypothesis test when the number of successes (in a number of trials) is a scalar.
```{r}
library(tidyverse)
library(statBasics)
proportion_null <- 0.1
p0 <- 0.75
x <- rbinom(1, size = 1000, prob = p0)
ht_1pop_prop(x, 1000, proportion = p0, alternative = "two.sided", conf_level = 0.95)
```
#### Number of successes in a vector
The example below shows how to perform a hypothesis test when the number of successes (in a number of trials) is a vector. The vector of number of trials must also be provided.
```{r}
library(tidyverse)
library(statBasics)
proportion_null <- 0.9
p0 <- 0.75
n <- c(10, 20, 30)
x <- n |> map_int(~ rbinom(1, .x, prob = p0))
ht_1pop_prop(x, n, proportion = p0, alternative = "less", conf_level = 0.99)
```
#### Vector of successes (0 or 1)
The following example shows how to perform a hypothesis test when the number of successes (in a number of trials) is a vector of zeroes and ones.
```{r}
library(tidyverse)
library(statBasics)
proportion_null <- 0.1
p0 <- 0.75
x <- rbinom(1000, 1, prob = p0)
ht_1pop_prop(x, proportion = p0, alternative = "greater", conf_level = 0.95)
```
## Confidence interval for two populations
### Bernoulli distribution
In this package, there are two approaches for computing a confidence interval for the difference in proportions.
#### Confidence Interval -- Number of successes
In this case, we have the number of trials (`n_x` and `n_y`) and the number of success (`x` and `y`) for both popuations.
```{r}
x <- 3
n_x <- 100
y <- 50
n_y <- 333
ci_2pop_bern(x, y, n_x, n_y)
```
#### Confidence Interval -- Vectors of 0 and 1
In this case, we have a vector of 0 and 1 (`x` and `y`) for both populations.
```{r}
x <- rbinom(100, 1, 0.75)
y <- rbinom(500, 1, 0.75)
ci_2pop_bern(x, y)
```
### Normal distribution
In this case, we can build the interval for the difference in means of two populations with known or unknown standard deviations, and we can build the ratio of the variances of two populations. DÚVIDA!
#### Confidence Interval -- Comparing means when standard deviations are unknown
Next, we illustrate how to compute a confidence interval for the difference in means of two populations when population standard deviations are unknown.
```{r}
x <- rnorm(1000, mean = 0, sd = 2)
y <- rnorm(1000, mean = 0, sd = 1)
# unknown variance and confidence interval for difference of means
ci_2pop_norm(x, y)
```
#### Confidence Interval -- Comparing means when standard deviations are known
The example below illustrates how to obtain a confidence interval for the difference in means of two populations when population standard deviations are known.
```{r}
x <- rnorm(1000, mean = 0, sd = 2)
y <- rnorm(1000, mean = 0, sd = 3)
# known variance and confidence interval for difference of means
ci_2pop_norm(x, y, sd_pop_1 = 2, sd_pop_2 = 3)
```
#### Confidence Interval -- Comparing standard deviations
In thsi case, a confidence interval is obtained by considering the ratio of standard deviations (or variances) of the two populations.
```{r}
x <- rnorm(1000, mean = 0, sd = 2)
y <- rnorm(1000, mean = 0, sd = 3)
# confidence interval for the variance ratio of 2 populations
ci_2pop_norm(x, y, parameter = "variance")
```
## Hypothesis testing for two populations
### Comparing proportions in two populations
There two approaches to compare the proportions in two populations:
* We have the numbers of sucecss (`x` and `y`) and the numbers of trials (`n_x` and `n_y`) for both populations;
* We have vector of 1 (success) and 0 (failure) for both populations.
#### Vector of 1 and 0
In this case, we have a vector of 1 (success) and 0 (failure) for both populations.
```{r}
x <- rbinom(100, 1, 0.75)
y <- rbinom(500, 1, 0.75)
ht_2pop_prop(x, y)
```
#### Number of successes
In this case, we have the number of success and the number of trials for both populations.
```{r}
x <- 3
n_x <- 100
y <- 50
n_y <- 333
ht_2pop_prop(x, y, n_x, n_y)
```
## Hypothesis testing for comparing the means of two independent populations
There are three cases to be considere when comparing two means:
i. `t-test` unknown but equal variances;
i. `t-test` unknown and unequal variances;
i. `z-test` known variances.
### Comparing two means -- unknown, equal variances (`t-test`)
```{r}
x <- rnorm(1000, mean = 10, sd = 2)
y <- rnorm(500, mean = 5, sd = 2)
# H0: mu_1 - mu_2 == -1 versus H1: mu_1 - mu_2 != -1
ht_2pop_mean(x, y, delta = -1, var_equal = TRUE)
```
### Comparing two means -- unknown, unequal variances (`t-test`)
```{r}
x <- rnorm(1000, mean = 10, sd = 2)
y <- rnorm(500, mean = 5, sd = 1)
# H0: mu_1 - mu_2 == -1 versus H1: mu_1 - mu_2 != -1
ht_2pop_mean(x, y, delta = -1)
```
### Comparing two means -- known variances (`z-test`)
```{r}
x <- rnorm(1000, mean = 10, sd = 3)
x <- rnorm(500, mean = 5, sd = 1)
# H0: mu_1 - mu_2 >= 0 versus H1: mu_1 - mu_2 < 0
ht_2pop_mean(x, y, delta = 0, sd_pop_1 = 3, sd_pop_2 = 1, alternative = "less")
```
## Hypothesis testing for comparing variances of two independet populations
```{r}
x <- rnorm(100, sd = 2)
y <- rnorm(1000, sd = 10)
ht_2pop_var(x, y)
```