| Title: | Confidence Intervals and Hypothesis Tests with a Consistent Layout |
|---|---|
| Description: | Computes confidence intervals and hypothesis tests for means and proportions. Every function prints its result in the same consistent three-section layout -- sample information, intermediary calculations, and the final answer -- so students always know where to look. Intended for classroom teaching, where each step of a calculation is shown, while every reported quantity is also returned in a standard R object for further use. |
| Authors: | Doru Cojoc [aut, cre] |
| Maintainer: | Doru Cojoc <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.0 |
| Built: | 2026-11-24 23:16:58 UTC |
| Source: | https://github.com/cran/statelier |
Computes a confidence interval for a single population mean or for the difference between two population means, and prints the result in the package's consistent three-section layout: sample information, intermediary calculations, and the final answer – the same layout used by every function in the package.
ci_mean_stats( data1, data2 = NULL, conf_level = 0.95, diff_direction = c("2-1", "1-2"), var_equal = FALSE, data = NULL, first = NULL ) ci_mean( data1, data2 = NULL, conf_level = 0.95, diff_direction = c("2-1", "1-2"), var_equal = FALSE, data = NULL, first = NULL )ci_mean_stats( data1, data2 = NULL, conf_level = 0.95, diff_direction = c("2-1", "1-2"), var_equal = FALSE, data = NULL, first = NULL ) ci_mean( data1, data2 = NULL, conf_level = 0.95, diff_direction = c("2-1", "1-2"), var_equal = FALSE, data = NULL, first = NULL )
data1 |
A numeric vector with the sample data, or a formula of the
form |
data2 |
An optional second numeric vector. If |
conf_level |
Confidence level, a single number strictly between 0 and
|
diff_direction |
Direction of the estimated difference in the
two-sample case: |
var_equal |
Should the two population variances be treated as equal?
If |
data |
A data frame, used only together with a formula in |
first |
What goes first in the difference (the difference is
mean(first) - mean(other)). With a formula, |
ci_mean() is a short alias for ci_mean_stats(); both names are exported.
There are two ways to give the samples. With vectors,
ci_mean_stats(x) is a one-sample interval and ci_mean_stats(x, y) a
two-sample interval. With a data frame, ci_mean_stats(y ~ group, data = df) splits the column y by the two values of the column
group – the tilde reads as "by", so this is "the mean of y, by
group". Exactly one of these patterns may be used; mixing them (a
formula and a second vector, or data without a formula) is an error.
The grouping variable must have exactly two groups; rows with a missing
outcome or group are dropped and counted.
The direction of the difference – which mean is subtracted from
which – follows one rule: the difference is mean(first) - mean(other),
where first is chosen by the first argument. When first is not
supplied, the defaults are: with two vectors, the second vector minus
the first (treatment - control); with a formula and numeric groups, the
larger code minus the smaller (1 - 0, 2 - 1); with a factor, the factor
level order; with character groups, alphabetical order. The printed
output always states the direction with the actual group labels (e.g.
"Difference in Means: male - female"), so it never has to be guessed.
(diff_direction is the older way to set the direction for vector
calls and is still honoured; do not supply both it and first.)
The one-sample interval is the usual
mean ± t1-α/2, n-1 · s/√n
interval. The two-sample interval uses either Welch's approximation
(var_equal = FALSE) or the pooled-variance formula
(var_equal = TRUE).
An object of class "statelier_ci" (a list) returned invisibly when
called interactively through its print method. The list carries every
quantity shown in the printed output (sample statistics, standard error,
degrees of freedom, interval limits, ...) so it can be used in further
calculations. Assigning the result to a variable suppresses printing.
ht_mean_stats() for the corresponding hypothesis test,
ci_prop_stats() for proportions.
set.seed(1) x <- rnorm(30, mean = 100, sd = 15) y <- rnorm(40, mean = 105, sd = 15) # One-sample interval ci_mean_stats(x) # Two-sample interval, Welch ci_mean_stats(x, y) # Two-sample interval, equal variances ci_mean_stats(x, y, var_equal = TRUE) # Capture the numbers without printing res <- ci_mean_stats(x, y) res$ci_lower res$ci_upper # The same two-sample interval from a data frame: # "weight, by sex" (default direction: female - male, alphabetical) df <- data.frame( weight = c(70, 82, 65, 90, 77, 68, 74, 88, 61, 79), sex = c("male", "male", "female", "male", "female", "female", "male", "male", "female", "female") ) ci_mean_stats(weight ~ sex, data = df) ci_mean_stats(weight ~ sex, data = df, first = "male")set.seed(1) x <- rnorm(30, mean = 100, sd = 15) y <- rnorm(40, mean = 105, sd = 15) # One-sample interval ci_mean_stats(x) # Two-sample interval, Welch ci_mean_stats(x, y) # Two-sample interval, equal variances ci_mean_stats(x, y, var_equal = TRUE) # Capture the numbers without printing res <- ci_mean_stats(x, y) res$ci_lower res$ci_upper # The same two-sample interval from a data frame: # "weight, by sex" (default direction: female - male, alphabetical) df <- data.frame( weight = c(70, 82, 65, 90, 77, 68, 74, 88, 61, 79), sex = c("male", "male", "female", "male", "female", "female", "male", "male", "female", "female") ) ci_mean_stats(weight ~ sex, data = df) ci_mean_stats(weight ~ sex, data = df, first = "male")
Computes a Wald confidence interval for a single population proportion or for the difference between two population proportions, and prints the result in the package's consistent three-section layout: sample information, intermediary calculations, and the final answer – the same layout used by every function in the package.
ci_prop_stats( data1, data2 = NULL, conf_level = 0.95, diff_direction = c("2-1", "1-2"), data = NULL, first = NULL ) ci_prop( data1, data2 = NULL, conf_level = 0.95, diff_direction = c("2-1", "1-2"), data = NULL, first = NULL )ci_prop_stats( data1, data2 = NULL, conf_level = 0.95, diff_direction = c("2-1", "1-2"), data = NULL, first = NULL ) ci_prop( data1, data2 = NULL, conf_level = 0.95, diff_direction = c("2-1", "1-2"), data = NULL, first = NULL )
data1 |
A numeric vector of 0s and 1s with the sample data (logical
vectors are coerced), or a formula of the form |
data2 |
An optional second numeric vector of 0s and 1s. If |
conf_level |
Confidence level, a single number strictly between 0 and
|
diff_direction |
Direction of the estimated difference in the
two-sample case: |
data |
A data frame, used only together with a formula in |
first |
What goes first in the difference (the difference is
prop(first) - prop(other)). With a formula, |
ci_prop() is a short alias for ci_prop_stats(); both names are exported.
There are two ways to give the samples. With vectors,
ci_prop_stats(x) is a one-sample interval and ci_prop_stats(x, y) a
two-sample interval. With a data frame, ci_prop_stats(y ~ group, data = df) splits the 0/1 column y by the two values of the column
group – the tilde reads as "by", so this is "the proportion of y,
by group". Exactly one of these patterns may be used; mixing them (a
formula and a second vector, or data without a formula) is an error.
The grouping variable must have exactly two groups; rows with a
missing outcome or group are dropped and counted.
The direction of the difference – which proportion is subtracted
from which – follows one rule: the difference is prop(first) -
prop(other), where first is chosen by the first argument. When
first is not supplied, the defaults are: with two vectors, the
second vector minus the first (treatment - control); with a formula
and numeric groups, the larger code minus the smaller (1 - 0, 2 - 1);
with a factor, the factor level order; with character groups,
alphabetical order. The printed output always states the direction
with the actual group labels, so it never has to be guessed.
(diff_direction is the older way to set the direction for vector
calls and is still honoured; do not supply both it and first.)
Both intervals use the large-sample normal (Wald) approximation.
The one-sample interval is truncated to
. A warning is issued when the normal approximation looks
unreliable (fewer than 5 expected successes or failures).
An object of class "statelier_ci" (a list) with every quantity shown
in the printed output. Assigning the result to a variable suppresses
printing.
ht_prop_stats() for the corresponding hypothesis test,
ci_mean_stats() for means.
set.seed(2) x <- rbinom(200, 1, 0.35) y <- rbinom(250, 1, 0.45) # One-sample interval ci_prop_stats(x) # Two-sample interval for prop2 - prop1 ci_prop_stats(x, y) # The same two-sample interval from a data frame: # "passed, by section" (default direction is alphabetical) df <- data.frame( passed = c(1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1), section = rep(c("morning", "evening"), each = 20) ) ci_prop_stats(passed ~ section, data = df) ci_prop_stats(passed ~ section, data = df, first = "morning")set.seed(2) x <- rbinom(200, 1, 0.35) y <- rbinom(250, 1, 0.45) # One-sample interval ci_prop_stats(x) # Two-sample interval for prop2 - prop1 ci_prop_stats(x, y) # The same two-sample interval from a data frame: # "passed, by section" (default direction is alphabetical) df <- data.frame( passed = c(1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1), section = rep(c("morning", "evening"), each = 20) ) ci_prop_stats(passed ~ section, data = df) ci_prop_stats(passed ~ section, data = df, first = "morning")
Tests a hypothesis about a single population mean or about the difference
between two population means, and prints the result in the package's
consistent three-section layout: sample information, intermediary
calculations, and the final answer – the same layout used by every
function in the package. (The reject / fail-to-reject conclusion is shown
only when alpha is supplied.)
ht_mean_stats( data1, data2 = NULL, null_value = 0, alpha = NULL, alternative = c("two.sided", "less", "greater"), diff_direction = c("2-1", "1-2"), var_equal = FALSE, data = NULL, first = NULL ) ht_mean( data1, data2 = NULL, null_value = 0, alpha = NULL, alternative = c("two.sided", "less", "greater"), diff_direction = c("2-1", "1-2"), var_equal = FALSE, data = NULL, first = NULL )ht_mean_stats( data1, data2 = NULL, null_value = 0, alpha = NULL, alternative = c("two.sided", "less", "greater"), diff_direction = c("2-1", "1-2"), var_equal = FALSE, data = NULL, first = NULL ) ht_mean( data1, data2 = NULL, null_value = 0, alpha = NULL, alternative = c("two.sided", "less", "greater"), diff_direction = c("2-1", "1-2"), var_equal = FALSE, data = NULL, first = NULL )
data1 |
A numeric vector with the sample data, or a formula of the
form |
data2 |
An optional second numeric vector. If |
null_value |
The hypothesized value under the null hypothesis: the
population mean for a one-sample test, or the difference in population
means (in the direction given by |
alpha |
Significance level, a single number strictly between 0 and 1.
If |
alternative |
The alternative hypothesis: |
diff_direction |
Direction of the estimated difference in the
two-sample case: |
var_equal |
Should the two population variances be treated as equal?
If |
data |
A data frame, used only together with a formula in |
first |
What goes first in the difference (the difference is
mean(first) - mean(other)). With a formula, |
ht_mean() is a short alias for ht_mean_stats(); both names are exported.
There are two ways to give the samples. With vectors,
ht_mean_stats(x) is a one-sample test and ht_mean_stats(x, y) a
two-sample test. With a data frame, ht_mean_stats(y ~ group, data = df) splits the column y by the two values of the column group –
the tilde reads as "by", so this is "test y, by group". Exactly one of
these patterns may be used; mixing them (a formula and a second vector,
or data without a formula) is an error. The grouping variable must
have exactly two groups; rows with a missing outcome or group are
dropped and counted.
The direction of the difference – which mean is subtracted from
which – follows one rule: the difference is mean(first) - mean(other),
where first is chosen by the first argument. When first is not
supplied, the defaults are: with two vectors, the second vector minus
the first (treatment - control); with a formula and numeric groups, the
larger code minus the smaller (1 - 0, 2 - 1); with a factor, the factor
level order; with character groups, alphabetical order. The printed
output always states the direction with the actual group labels (e.g.
"Difference in Means: male - female"), so it never has to be guessed.
(diff_direction is the older way to set the direction for vector
calls and is still honoured; do not supply both it and first.)
An object of class "statelier_ht" (a list) with every quantity shown
in the printed output (sample statistics, test statistic, p-value,
rejection decision, ...). Assigning the result to a variable suppresses
printing.
ci_mean_stats() for the corresponding confidence interval,
ht_prop_stats() for proportions.
set.seed(1) x <- rnorm(30, mean = 100, sd = 15) y <- rnorm(40, mean = 105, sd = 15) # One-sample test of H0: mu = 100 ht_mean_stats(x, null_value = 100) # Two-sample test of H0: mean2 - mean1 = 0, Welch ht_mean_stats(x, y) # One-sided test with a non-zero null difference ht_mean_stats(x, y, null_value = 5, alternative = "less") # Supply alpha to print the significance level and the conclusion ht_mean_stats(x, null_value = 100, alpha = 0.05) # The same two-sample test from a data frame: # "test weight, by sex" (default direction: female - male, alphabetical) df <- data.frame( weight = c(70, 82, 65, 90, 77, 68, 74, 88, 61, 79), sex = c("male", "male", "female", "male", "female", "female", "male", "male", "female", "female") ) ht_mean_stats(weight ~ sex, data = df) ht_mean_stats(weight ~ sex, data = df, first = "male")set.seed(1) x <- rnorm(30, mean = 100, sd = 15) y <- rnorm(40, mean = 105, sd = 15) # One-sample test of H0: mu = 100 ht_mean_stats(x, null_value = 100) # Two-sample test of H0: mean2 - mean1 = 0, Welch ht_mean_stats(x, y) # One-sided test with a non-zero null difference ht_mean_stats(x, y, null_value = 5, alternative = "less") # Supply alpha to print the significance level and the conclusion ht_mean_stats(x, null_value = 100, alpha = 0.05) # The same two-sample test from a data frame: # "test weight, by sex" (default direction: female - male, alphabetical) df <- data.frame( weight = c(70, 82, 65, 90, 77, 68, 74, 88, 61, 79), sex = c("male", "male", "female", "male", "female", "female", "male", "male", "female", "female") ) ht_mean_stats(weight ~ sex, data = df) ht_mean_stats(weight ~ sex, data = df, first = "male")
Tests a hypothesis about a single population proportion or about the
difference between two population proportions, and prints the result in
the package's consistent three-section layout: sample information,
intermediary calculations, and the final answer – the same layout used
by every function in the package. (The reject / fail-to-reject
conclusion is shown only when alpha is supplied.)
ht_prop_stats( data1, data2 = NULL, p0 = 0.5, alpha = NULL, alternative = c("two.sided", "less", "greater"), diff_direction = c("2-1", "1-2"), data = NULL, first = NULL ) ht_prop( data1, data2 = NULL, p0 = 0.5, alpha = NULL, alternative = c("two.sided", "less", "greater"), diff_direction = c("2-1", "1-2"), data = NULL, first = NULL )ht_prop_stats( data1, data2 = NULL, p0 = 0.5, alpha = NULL, alternative = c("two.sided", "less", "greater"), diff_direction = c("2-1", "1-2"), data = NULL, first = NULL ) ht_prop( data1, data2 = NULL, p0 = 0.5, alpha = NULL, alternative = c("two.sided", "less", "greater"), diff_direction = c("2-1", "1-2"), data = NULL, first = NULL )
data1 |
A numeric vector of 0s and 1s with the sample data (logical
vectors are coerced), or a formula of the form |
data2 |
An optional second numeric vector of 0s and 1s. If |
p0 |
The hypothesized population proportion under the null hypothesis for a one-sample test, a single number strictly between 0 and 1. Defaults to 0.5. Ignored (with a warning if non-default) for two-sample tests, where the null hypothesis is always that the difference in proportions is 0. |
alpha |
Significance level, a single number strictly between 0 and 1.
If |
alternative |
The alternative hypothesis: |
diff_direction |
Direction of the estimated difference in the
two-sample case: |
data |
A data frame, used only together with a formula in |
first |
What goes first in the difference (the difference is
prop(first) - prop(other)). With a formula, |
ht_prop() is a short alias for ht_prop_stats(); both names are exported.
There are two ways to give the samples. With vectors,
ht_prop_stats(x) is a one-sample test and ht_prop_stats(x, y) a
two-sample test. With a data frame, ht_prop_stats(y ~ group, data = df) splits the 0/1 column y by the two values of the column
group – the tilde reads as "by", so this is "test y, by group".
Exactly one of these patterns may be used; mixing them (a formula and
a second vector, or data without a formula) is an error. The
grouping variable must have exactly two groups; rows with a missing
outcome or group are dropped and counted.
The direction of the difference – which proportion is subtracted
from which – follows one rule: the difference is prop(first) -
prop(other), where first is chosen by the first argument. When
first is not supplied, the defaults are: with two vectors, the
second vector minus the first (treatment - control); with a formula
and numeric groups, the larger code minus the smaller (1 - 0, 2 - 1);
with a factor, the factor level order; with character groups,
alphabetical order. The printed output always states the direction
with the actual group labels, so it never has to be guessed.
(diff_direction is the older way to set the direction for vector
calls and is still honoured; do not supply both it and first.)
Both tests use the large-sample normal approximation. The one-sample test standardizes with the standard error under the null hypothesis. The two-sample test standardizes with the pooled-proportion standard error. A warning is issued when the normal approximation looks unreliable (fewer than 5 expected successes or failures).
An object of class "statelier_ht" (a list) with every quantity shown
in the printed output. Assigning the result to a variable suppresses
printing.
ci_prop_stats() for the corresponding confidence interval,
ht_mean_stats() for means.
set.seed(2) x <- rbinom(200, 1, 0.35) y <- rbinom(250, 1, 0.45) # One-sample test of H0: p = 0.3 ht_prop_stats(x, p0 = 0.3) # Two-sample test of H0: prop2 - prop1 = 0 ht_prop_stats(x, y) # Supply alpha to print the significance level and the conclusion ht_prop_stats(x, p0 = 0.3, alpha = 0.05) # The same two-sample test from a data frame: # "test passed, by section" (default direction is alphabetical) df <- data.frame( passed = c(1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1), section = rep(c("morning", "evening"), each = 20) ) ht_prop_stats(passed ~ section, data = df) ht_prop_stats(passed ~ section, data = df, first = "morning")set.seed(2) x <- rbinom(200, 1, 0.35) y <- rbinom(250, 1, 0.45) # One-sample test of H0: p = 0.3 ht_prop_stats(x, p0 = 0.3) # Two-sample test of H0: prop2 - prop1 = 0 ht_prop_stats(x, y) # Supply alpha to print the significance level and the conclusion ht_prop_stats(x, p0 = 0.3, alpha = 0.05) # The same two-sample test from a data frame: # "test passed, by section" (default direction is alphabetical) df <- data.frame( passed = c(1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1), section = rep(c("morning", "evening"), each = 20) ) ht_prop_stats(passed ~ section, data = df) ht_prop_stats(passed ~ section, data = df, first = "morning")
Prints a "statelier_ci" object (as returned by ci_mean_stats() or
ci_prop_stats()) in the package's three-section layout: sample
information, intermediary calculations, and the final answer.
## S3 method for class 'statelier_ci' print(x, ...)## S3 method for class 'statelier_ci' print(x, ...)
x |
A |
... |
Ignored. |
x, invisibly.
Prints a "statelier_ht" object (as returned by ht_mean_stats() or
ht_prop_stats()) in the package's three-section layout: sample
information, intermediary calculations, and the final answer. The
significance level and the reject / fail-to-reject conclusion are shown
only when alpha was supplied.
## S3 method for class 'statelier_ht' print(x, ...)## S3 method for class 'statelier_ht' print(x, ...)
x |
A |
... |
Ignored. |
x, invisibly.