R Under development (unstable) (2024-10-14 r87233 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(qwraps2) > > # simple ci > stopifnot( + all.equal( + unclass(mean_ci(mtcars$mpg)) + , + c(mean(mtcars$mpg), + mean(mtcars$mpg) + qnorm(0.025)*sd(mtcars$mpg)/sqrt(nrow(mtcars)), + mean(mtcars$mpg) + qnorm(0.975)*sd(mtcars$mpg)/sqrt(nrow(mtcars))) + , check.attributes = FALSE + ) + ) > > # Printing nicely > out <- capture.output(print(mean_ci(mtcars$mpg))) > stopifnot(grepl("20\\.09 \\(18\\.00, 22\\.18\\)", out)) > > out <- capture.output(print(mean_ci(mtcars$mpg), show_level = TRUE)) > stopifnot(grepl("20.09 \\(95% CI: 18\\.00, 22.18\\)", out)) > > out <- capture.output(print(mean_ci(mtcars$mpg, alpha = 0.01), show_level = TRUE)) > stopifnot(grepl("20.09 \\(99% CI: 17\\.35, 22\\.83\\)", out)) > > # Compare to the ci that comes form t.test > stopifnot( + all.equal( + t.test(mtcars$mpg)$conf.int, + mean_ci(mtcars$mpg, qdist = stats::qt, qdist.args = list(df = 31))[2:3], + check.attributes = FALSE + )) > > # simple ci with missing data > x <- mtcars$mpg > x[c(12, 14)] <- NA > > stopifnot( + all.equal(unclass(mean_ci(x, na_rm = TRUE)), + c(mean(x, na.rm = TRUE), + mean(x, na.rm = TRUE) + qnorm(0.025)*sd(x, na.rm = TRUE)/sqrt(sum(!is.na(x))), + mean(x, na.rm = TRUE) + qnorm(0.975)*sd(x, na.rm = TRUE)/sqrt(sum(!is.na(x)))) + , check.attributes = FALSE) + ) > > # warnings > stopifnot(inherits(tryCatch(mean_ci(mtcars$mpg, na.rm = TRUE), warning = function(w) w), "warning")) > > # errors > stopifnot(inherits(tryCatch(mean_ci(mtcars$mpg, transform = "exp"), error = function(e) e), "error")) > > proc.time() user system elapsed 0.25 0.15 0.35