Skip to content

Commit 393d8b5

Browse files
authored
Merge pull request #1520 from exploratory-io/fix/issue-36193
Support data_summary type in One-Sample t Test
2 parents 161af66 + cf9d9f1 commit 393d8b5

3 files changed

Lines changed: 79 additions & 5 deletions

File tree

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: exploratory
22
Type: Package
33
Title: R package for Exploratory
4-
Version: 15.1.15
5-
Date: 2026-06-03
4+
Version: 15.1.16
5+
Date: 2026-06-05
66
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")))
77
URL: https://github.com/exploratory-io/exploratory_func
88
Description: Functions for Exploratory

R/test_wrapper.R

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3597,7 +3597,7 @@ exp_one_sample_t_test <- function(df, var, mu = 0, alternative = "two.sided",
35973597
}
35983598

35993599
#' @export
3600-
tidy.one_sample_t_test_exploratory <- function(x, type = "model") {
3600+
tidy.one_sample_t_test_exploratory <- function(x, type = "model", conf_level = 0.95) {
36013601
if ("error" %in% class(x)) return(tibble::tibble(Note = x$message))
36023602
if (type == "model") {
36033603
diff <- x$observed_mean - x$mu
@@ -3619,11 +3619,31 @@ tidy.one_sample_t_test_exploratory <- function(x, type = "model") {
36193619
`P Value` = x$htest$p.value,
36203620
`Conf Low` = x$htest$conf.int[1],
36213621
`Conf High` = x$htest$conf.int[2],
3622+
`Diff Conf Low` = x$htest$conf.int[1] - x$mu,
3623+
`Diff Conf High` = x$htest$conf.int[2] - x$mu,
36223624
`Cohen's d` = x$cohens_d,
36233625
`Power` = x$power,
36243626
`Test Direction` = direction,
36253627
`Result` = result_label
36263628
)
3629+
} else if (type == "data_summary") {
3630+
conf_threshold <- 1 - (1 - conf_level) / 2
3631+
vec <- x$data[[x$var_col]]
3632+
vec <- vec[!is.na(vec)]
3633+
n <- length(vec)
3634+
mean_val <- mean(vec)
3635+
sd_val <- sd(vec)
3636+
se_val <- sd_val / sqrt(n)
3637+
tibble::tibble(
3638+
`Rows` = n,
3639+
`Mean` = mean_val,
3640+
`Conf Low` = mean_val - se_val * qt(p = conf_threshold, df = n - 1),
3641+
`Conf High` = mean_val + se_val * qt(p = conf_threshold, df = n - 1),
3642+
`Std Error of Mean` = se_val,
3643+
`Std Deviation` = sd_val,
3644+
`Minimum` = min(vec),
3645+
`Maximum` = max(vec)
3646+
)
36273647
} else if (type == "prob_dist") {
36283648
generate_ttest_density_data(t = unname(x$htest$statistic), p.value = x$htest$p.value,
36293649
df = unname(x$htest$parameter), sig_level = x$sig.level,

tests/testthat/test_one_sample_t_test.R

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ test_that("tidy model type returns the expected column set", {
211211
expected_cols <- c(
212212
"Number of Rows", "Mean", "Std Deviation", "Std Error",
213213
"Hypothesized Mean", "Difference", "t Value", "DF", "P Value",
214-
"Conf Low", "Conf High", "Cohen's d", "Power",
215-
"Test Direction", "Result"
214+
"Conf Low", "Conf High", "Diff Conf Low", "Diff Conf High",
215+
"Cohen's d", "Power", "Test Direction", "Result"
216216
)
217217
for (col in expected_cols) {
218218
expect_true(col %in% names(tidied), info = paste("Missing column:", col))
@@ -237,6 +237,8 @@ test_that("tidy model values are numerically correct", {
237237
expect_equal(tidied$`P Value`, expected$p.value)
238238
expect_equal(tidied$`Conf Low`, expected$conf.int[1])
239239
expect_equal(tidied$`Conf High`, expected$conf.int[2])
240+
expect_equal(tidied$`Diff Conf Low`, expected$conf.int[1] - 4.5)
241+
expect_equal(tidied$`Diff Conf High`, expected$conf.int[2] - 4.5)
240242
})
241243

242244
test_that("tidy model Test Direction maps correctly from alternative", {
@@ -412,6 +414,58 @@ test_that("tidy prob_dist_mean no-ops (0-row tibble) when stderr is degenerate",
412414
expect_equal(nrow(pd), 0)
413415
})
414416

417+
# --- tidy(type = "data_summary") ---
418+
419+
test_that("tidy data_summary type returns summary statistics with confidence intervals", {
420+
set.seed(42)
421+
vec <- rnorm(50, mean = 5, sd = 2)
422+
df <- data.frame(x = vec)
423+
model <- exp_one_sample_t_test(df, x, mu = 4.5)$model[[1]]
424+
result <- tidy(model, type = "data_summary", conf_level = 0.95)
425+
426+
expected_cols <- c("Rows", "Mean", "Conf Low", "Conf High",
427+
"Std Error of Mean", "Std Deviation", "Minimum", "Maximum")
428+
expect_true(all(expected_cols %in% names(result)))
429+
expect_equal(nrow(result), 1)
430+
431+
n <- length(vec)
432+
conf_threshold <- 1 - (1 - 0.95) / 2
433+
expected_se <- sd(vec) / sqrt(n)
434+
expect_equal(result$Rows, n)
435+
expect_equal(result$Mean, mean(vec))
436+
expect_equal(result$`Std Deviation`, sd(vec))
437+
expect_equal(result$`Std Error of Mean`, expected_se)
438+
expect_equal(result$`Conf Low`, mean(vec) - expected_se * qt(p = conf_threshold, df = n - 1))
439+
expect_equal(result$`Conf High`, mean(vec) + expected_se * qt(p = conf_threshold, df = n - 1))
440+
expect_equal(result$Minimum, min(vec))
441+
expect_equal(result$Maximum, max(vec))
442+
})
443+
444+
test_that("tidy data_summary conf_level parameter changes confidence intervals", {
445+
set.seed(42)
446+
vec <- rnorm(50, mean = 5, sd = 2)
447+
df <- data.frame(x = vec)
448+
model <- exp_one_sample_t_test(df, x, mu = 4.5)$model[[1]]
449+
450+
result_95 <- tidy(model, type = "data_summary", conf_level = 0.95)
451+
result_90 <- tidy(model, type = "data_summary", conf_level = 0.90)
452+
453+
expect_true(result_95$`Conf Low` < result_90$`Conf Low`)
454+
expect_true(result_95$`Conf High` > result_90$`Conf High`)
455+
})
456+
457+
test_that("tidy_rowwise with type=data_summary works for one sample t-test", {
458+
set.seed(42)
459+
vec <- rnorm(50, mean = 5, sd = 2)
460+
df <- data.frame(x = vec)
461+
result <- exp_one_sample_t_test(df, x, mu = 4.5) %>%
462+
tidy_rowwise(model, type = "data_summary", conf_level = 0.95)
463+
464+
expect_true("Rows" %in% names(result))
465+
expect_true("Mean" %in% names(result))
466+
expect_equal(result$Rows, 50)
467+
})
468+
415469
# --- tidy(type = "data") ---
416470

417471
test_that("tidy data type returns the raw data with the target column", {

0 commit comments

Comments
 (0)