knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%", message = F )
R
package of the course Métodos Estatísticos at Federal University of Bahia.
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.
In this package, there are three approaches for computing a confidence interval for a population proportion.
n
(scalar value) trialslibrary(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)
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)
library(tidyverse) library(statBasics) x <- rbinom(50, size = 1, prob = 0.75) ci_1pop_bern(x, conf_level = 0.99)
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.
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)
library(tidyverse) library(statBasics) media_pop <- 10 sd_pop <- 2 x <- rnorm(100, mean = media_pop) ci_1pop_norm(x, conf_level = 0.91)
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)
library(tidyverse) library(statBasics) media_pop <- 800 taxa_pop <- 1 / media_pop x <- rexp(100, rate = taxa_pop) ci_1pop_exp(x)
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.
library(tidyverse) library(statBasics) media_pop <- 50 x <- rpois(100, lambda = media_pop) ci_1pop_general(x)
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.
In the examples below, mean_null
is the mean in the null hypothesis H0
:
alternative == "two.sided"
: H0: mu == mean_null
and H1: mu != mean_null
. Default value.alternative == "less"
: H0: mu >= mean_null
and H1: mu < mean_null
alternative == "greater"
: H0: mu =< mean_null
and H1: mu > mean_null
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")
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")
In the examples below, sigma_null
is the standard deviation in the null hypothesis H0
:
alternative == "two.sided"
: H0: sigma == sigma_null
and H1: sigma != sigma_null
. Default value.alternative == "less"
: H0: sigma >= sigma_null
and H1: sigma < sigma_null
alternative == "greater"
: H0: sigma =< sigma_null
and H1: sigma > sigma_null
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")
In the examples below, proportion_null
is the proportion in the null hypothesis H0
:
alternative == "two.sided"
: H0: proportion == proportion_null
and H1: proportion != proportion_null
. Default value.alternative == "less"
: H0: proportion >= proportion_null
and H1: proportion < proportion_null
alternative == "greater"
: H0: proportion =< proportion_null
and H1: proportion > proportion_null
The following example illustrates how to perform a hypothesis test when the number of successes (in a number of trials) is a scalar.
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)
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.
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)
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.
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)
In this package, there are two approaches for computing a confidence interval for the difference in proportions.
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.
x <- 3 n_x <- 100 y <- 50 n_y <- 333 ci_2pop_bern(x, y, n_x, n_y)
In this case, we have a vector of 0 and 1 (x
and y
) for both populations.
x <- rbinom(100, 1, 0.75) y <- rbinom(500, 1, 0.75) ci_2pop_bern(x, y)
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!
Next, we illustrate how to compute a confidence interval for the difference in means of two populations when population standard deviations are unknown.
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)
The example below illustrates how to obtain a confidence interval for the difference in means of two populations when population standard deviations are known.
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)
In thsi case, a confidence interval is obtained by considering the ratio of standard deviations (or variances) of the two populations.
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")
There two approaches to compare the proportions in two populations:
x
and y
) and the numbers of trials (n_x
and n_y
) for both populations;In this case, we have a vector of 1 (success) and 0 (failure) for both populations.
x <- rbinom(100, 1, 0.75) y <- rbinom(500, 1, 0.75) ht_2pop_prop(x, y)
In this case, we have the number of success and the number of trials for both populations.
x <- 3 n_x <- 100 y <- 50 n_y <- 333 ht_2pop_prop(x, y, n_x, n_y)
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.
t-test
)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)
t-test
)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)
z-test
)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")
x <- rnorm(100, sd = 2) y <- rnorm(1000, sd = 10) ht_2pop_var(x, y)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.