From 6141257880c4d36bbf13234a11ea0c8ca8f21f8d Mon Sep 17 00:00:00 2001 From: Kei Saito Date: Fri, 5 Jun 2026 17:10:17 -0700 Subject: [PATCH 1/3] feat(one-sample-t-test): add data_summary type to tidy_rowwise Adds tidy(type="data_summary", conf_level=0.95) support to exp_one_sample_t_test, returning Rows, Mean, Conf Low/High, Std Error of Mean, Std Deviation, Minimum, and Maximum. Co-Authored-By: Claude Sonnet 4.6 --- R/test_wrapper.R | 20 +++++++++- tests/testthat/test_one_sample_t_test.R | 52 +++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/R/test_wrapper.R b/R/test_wrapper.R index bef4c3ec..3db3c57b 100644 --- a/R/test_wrapper.R +++ b/R/test_wrapper.R @@ -3597,7 +3597,7 @@ exp_one_sample_t_test <- function(df, var, mu = 0, alternative = "two.sided", } #' @export -tidy.one_sample_t_test_exploratory <- function(x, type = "model") { +tidy.one_sample_t_test_exploratory <- function(x, type = "model", conf_level = 0.95) { if ("error" %in% class(x)) return(tibble::tibble(Note = x$message)) if (type == "model") { diff <- x$observed_mean - x$mu @@ -3624,6 +3624,24 @@ tidy.one_sample_t_test_exploratory <- function(x, type = "model") { `Test Direction` = direction, `Result` = result_label ) + } else if (type == "data_summary") { + conf_threshold <- 1 - (1 - conf_level) / 2 + vec <- x$data[[x$var_col]] + vec <- vec[!is.na(vec)] + n <- length(vec) + mean_val <- mean(vec) + sd_val <- sd(vec) + se_val <- sd_val / sqrt(n) + tibble::tibble( + `Rows` = n, + `Mean` = mean_val, + `Conf Low` = mean_val - se_val * qt(p = conf_threshold, df = n - 1), + `Conf High` = mean_val + se_val * qt(p = conf_threshold, df = n - 1), + `Std Error of Mean` = se_val, + `Std Deviation` = sd_val, + `Minimum` = min(vec), + `Maximum` = max(vec) + ) } else if (type == "prob_dist") { generate_ttest_density_data(t = unname(x$htest$statistic), p.value = x$htest$p.value, df = unname(x$htest$parameter), sig_level = x$sig.level, diff --git a/tests/testthat/test_one_sample_t_test.R b/tests/testthat/test_one_sample_t_test.R index 1c6d008b..2f8a2271 100644 --- a/tests/testthat/test_one_sample_t_test.R +++ b/tests/testthat/test_one_sample_t_test.R @@ -412,6 +412,58 @@ test_that("tidy prob_dist_mean no-ops (0-row tibble) when stderr is degenerate", expect_equal(nrow(pd), 0) }) +# --- tidy(type = "data_summary") --- + +test_that("tidy data_summary type returns summary statistics with confidence intervals", { + set.seed(42) + vec <- rnorm(50, mean = 5, sd = 2) + df <- data.frame(x = vec) + model <- exp_one_sample_t_test(df, x, mu = 4.5)$model[[1]] + result <- tidy(model, type = "data_summary", conf_level = 0.95) + + expected_cols <- c("Rows", "Mean", "Conf Low", "Conf High", + "Std Error of Mean", "Std Deviation", "Minimum", "Maximum") + expect_true(all(expected_cols %in% names(result))) + expect_equal(nrow(result), 1) + + n <- length(vec) + conf_threshold <- 1 - (1 - 0.95) / 2 + expected_se <- sd(vec) / sqrt(n) + expect_equal(result$Rows, n) + expect_equal(result$Mean, mean(vec)) + expect_equal(result$`Std Deviation`, sd(vec)) + expect_equal(result$`Std Error of Mean`, expected_se) + expect_equal(result$`Conf Low`, mean(vec) - expected_se * qt(p = conf_threshold, df = n - 1)) + expect_equal(result$`Conf High`, mean(vec) + expected_se * qt(p = conf_threshold, df = n - 1)) + expect_equal(result$Minimum, min(vec)) + expect_equal(result$Maximum, max(vec)) +}) + +test_that("tidy data_summary conf_level parameter changes confidence intervals", { + set.seed(42) + vec <- rnorm(50, mean = 5, sd = 2) + df <- data.frame(x = vec) + model <- exp_one_sample_t_test(df, x, mu = 4.5)$model[[1]] + + result_95 <- tidy(model, type = "data_summary", conf_level = 0.95) + result_90 <- tidy(model, type = "data_summary", conf_level = 0.90) + + expect_true(result_95$`Conf Low` < result_90$`Conf Low`) + expect_true(result_95$`Conf High` > result_90$`Conf High`) +}) + +test_that("tidy_rowwise with type=data_summary works for one sample t-test", { + set.seed(42) + vec <- rnorm(50, mean = 5, sd = 2) + df <- data.frame(x = vec) + result <- exp_one_sample_t_test(df, x, mu = 4.5) %>% + tidy_rowwise(model, type = "data_summary", conf_level = 0.95) + + expect_true("Rows" %in% names(result)) + expect_true("Mean" %in% names(result)) + expect_equal(result$Rows, 50) +}) + # --- tidy(type = "data") --- test_that("tidy data type returns the raw data with the target column", { From 1fffd8cc09a4fa9b76ae3e8ed6b9a7083d8bc5ce Mon Sep 17 00:00:00 2001 From: Kei Saito Date: Fri, 5 Jun 2026 17:20:53 -0700 Subject: [PATCH 2/3] Bump the version --- DESCRIPTION | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 787975a9..85ccfd6f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: exploratory Type: Package Title: R package for Exploratory -Version: 15.1.15 -Date: 2026-06-03 +Version: 15.1.16 +Date: 2026-06-05 Authors@R: c(person("Hideaki", "Hayashi", email = "hideaki@exploratory.io", role = c("aut", "cre")), person("Hide", "Kojima", email = "hide@exploratory.io", role = c("aut")), person("Kan", "Nishida", email = "kan@exploratory.io", role = c("aut")), person("Kei", "Saito", email = "kei@exploratory.io", role = c("aut")), person("Yosuke", "Yasuda", email = "double.y.919.quick@gmail.com", role = c("aut"))) URL: https://github.com/exploratory-io/exploratory_func Description: Functions for Exploratory From cf9d9f17bc71ddf300606f285d6446bd88d79160 Mon Sep 17 00:00:00 2001 From: Kei Saito Date: Fri, 5 Jun 2026 18:46:49 -0700 Subject: [PATCH 3/3] feat(one-sample-t-test): add Diff Conf Low/High columns to tidy model output Add confidence interval for the difference (mean - mu) to the type="model" output as "Diff Conf Low" and "Diff Conf High", complementing the existing "Difference" column. Callers no longer need to subtract mu themselves. Co-Authored-By: Claude Sonnet 4.6 --- R/test_wrapper.R | 2 ++ tests/testthat/test_one_sample_t_test.R | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/R/test_wrapper.R b/R/test_wrapper.R index 3db3c57b..f070507f 100644 --- a/R/test_wrapper.R +++ b/R/test_wrapper.R @@ -3619,6 +3619,8 @@ tidy.one_sample_t_test_exploratory <- function(x, type = "model", conf_level = 0 `P Value` = x$htest$p.value, `Conf Low` = x$htest$conf.int[1], `Conf High` = x$htest$conf.int[2], + `Diff Conf Low` = x$htest$conf.int[1] - x$mu, + `Diff Conf High` = x$htest$conf.int[2] - x$mu, `Cohen's d` = x$cohens_d, `Power` = x$power, `Test Direction` = direction, diff --git a/tests/testthat/test_one_sample_t_test.R b/tests/testthat/test_one_sample_t_test.R index 2f8a2271..3fcd3350 100644 --- a/tests/testthat/test_one_sample_t_test.R +++ b/tests/testthat/test_one_sample_t_test.R @@ -211,8 +211,8 @@ test_that("tidy model type returns the expected column set", { expected_cols <- c( "Number of Rows", "Mean", "Std Deviation", "Std Error", "Hypothesized Mean", "Difference", "t Value", "DF", "P Value", - "Conf Low", "Conf High", "Cohen's d", "Power", - "Test Direction", "Result" + "Conf Low", "Conf High", "Diff Conf Low", "Diff Conf High", + "Cohen's d", "Power", "Test Direction", "Result" ) for (col in expected_cols) { expect_true(col %in% names(tidied), info = paste("Missing column:", col)) @@ -237,6 +237,8 @@ test_that("tidy model values are numerically correct", { expect_equal(tidied$`P Value`, expected$p.value) expect_equal(tidied$`Conf Low`, expected$conf.int[1]) expect_equal(tidied$`Conf High`, expected$conf.int[2]) + expect_equal(tidied$`Diff Conf Low`, expected$conf.int[1] - 4.5) + expect_equal(tidied$`Diff Conf High`, expected$conf.int[2] - 4.5) }) test_that("tidy model Test Direction maps correctly from alternative", {