A function for calculating and formatting means and confidence interval.
Details
Given a numeric vector, mean_ci
will return a vector with the mean,
LCL, and UCL. Using frmtci
will be helpful for reporting the results
in print.
Examples
# using the standard normal for the CI
mean_ci(mtcars$mpg)
#> [1] "20.09 (18.00, 22.18)"
# print it nicely
qwraps2::frmtci(mean_ci(mtcars$mpg))
#> [1] "20.09 (18.00, 22.18)"
qwraps2::frmtci(mean_ci(mtcars$mpg), show_level = TRUE)
#> [1] "20.09 (95% CI: 18.00, 22.18)"
qwraps2::frmtci(mean_ci(mtcars$mpg, alpha = 0.01), show_level = TRUE)
#> [1] "20.09 (99% CI: 17.35, 22.83)"
# Compare to the ci that comes form t.test
t.test(mtcars$mpg)
#>
#> One Sample t-test
#>
#> data: mtcars$mpg
#> t = 18.857, df = 31, p-value < 2.2e-16
#> alternative hypothesis: true mean is not equal to 0
#> 95 percent confidence interval:
#> 17.91768 22.26357
#> sample estimates:
#> mean of x
#> 20.09062
#>
t.test(mtcars$mpg)$conf.int
#> [1] 17.91768 22.26357
#> attr(,"conf.level")
#> [1] 0.95
mean_ci(mtcars$mpg, qdist = stats::qt, qdist.args = list(df = 31))
#> [1] "20.09 (17.92, 22.26)"