Learn R Programming

ci (version 0.0.1)

ci_mean_t: Mean CI from Data

Description

ci_mean_t() calculates the mean's confidence interval (CI) using the classic formula with Student's t coefficient for data in data frame format. This enhanced version of DescTools::MeanCI() responds to dplyr::group_by(), enabling subgroup calculations. Result is a data frame.

Usage

ci_mean_t(.data, x, conf.level = 0.95, ...)

Value

A data frame with columns:

  • (if present) grouping variable names;

  • mean (<dbl>) -- mean estimate;

  • lwr.ci, upr.ci (<dbl>) -- lower and upper CI bounds.

Arguments

.data

Data frame.

x

Column name (unquoted).

conf.level

Confidence level. Default: 0.95.

...

Additional parameters for DescTools::MeanCI(). See that function's documentation.

Examples

Run this code
# Example with built-in dataset
data(npk, package = "datasets")
head(npk)

# Basic CI calculation for crop yield
ci_mean_t(npk, yield)
# Interpretation: We're 95% confident the true mean yield
# falls between lwr.ci and upr.ci

# Using pipe operator (tidyverse style)
npk |> ci_mean_t(yield)

# Compare yields with nitrogen (N) treatment vs. without
npk |>
  dplyr::group_by(N) |>
  ci_mean_t(yield)
# Look at the CIs: Do they overlap? Non-overlapping CIs suggest
# a potential difference between groups

# More complex grouping: Three factors at once
npk |>
  dplyr::group_by(N, P, K) |>
  ci_mean_t(yield)

# Example with iris dataset: Petal length by species
data(iris, package = "datasets")
iris |>
  dplyr::group_by(Species) |>
  ci_mean_t(Petal.Length)
# Notice how the three species have clearly different intervals

# Example with mtcars: MPG by number of cylinders
data(mtcars, package = "datasets")
mtcars |>
  dplyr::group_by(cyl) |>
  ci_mean_t(mpg)

# 90% confidence interval (less confident, narrower interval)
npk |> ci_mean_t(yield, conf.level = 0.90)

# 99% confidence interval (more confident, wider interval)
npk |> ci_mean_t(yield, conf.level = 0.99)

Run the code above in your browser using DataLab