Fix paired t-test support for long-format data with grouped workflows
nice_t_test() with paired = TRUE failed when used in dplyr::group_by() workflows with long-format data, throwing invalid type (list) for variable 'Pair(...)'. The function attempted to use R's formula method for paired tests, which is incompatible with R >= 4.4.0.
Changes
Core fix (R/nice_t_test.R)
- Extract numeric vectors directly for paired tests instead of creating formulas
- Pass vectors to
t.test(x, y, paired = TRUE, ...)andeffectsize::cohens_d(x, y, paired = TRUE, ...) - Formula method now only used for independent samples tests
Tests (tests/testthat/test-nice_t_test.R)
- Added test coverage for paired t-tests in grouped long-format workflows
Documentation (man/nice_t_test.Rd)
- Clarified paired t-test usage and data format requirements
- Added paired t-test example
Version
- Bump 0.2.0 → 0.2.1 with NEWS.md entry
Example
Previously failing code now works:
df_long %>%
pivot_longer(cols = c(quality, status),
names_to = "item",
values_to = "quality_status") %>%
group_by(country, stimuli) %>%
summarise(
eff = nice_t_test(
data = pick("quality_status", "item"),
response = "quality_status",
group = "item",
paired = TRUE
)
) %>%
unnest_wider(eff)
Results match the one-sample t-test workaround (computing differences manually) exactly.
Original prompt
This section details on the original issue you should resolve
<issue_title>Ask for help with nice_t_test() for paired samples t test</issue_title> <issue_description>Dear Dr. Thériault,
I hope this message finds you well.
Could you please help me with an issue I encountered when using nice_t_test() to run paired samples t tests for multiple groups? I’m currently using R 4.5.1 and rempsyc 0.2.0 on Windows OS.
I’m trying to perform paired t-tests for a set of stimuli, and I’ve attached my data for your reference.
However, my code does not seem to work as intended. When I change it to a one-sample t-test, it runs successfully.
Paited t test did not work:
ttest <- df_long %>% pivot_longer(cols = c(quality, status), names_to = "item", values_to = "quality_status") %>% # prepare variable for paired samples t test dplyr::group_by(country, stimuli) %>% dplyr::summarise(eff = nice_t_test(data = pick("quality_status", "item"), response = "quality_status", group = "item", paired = TRUE)) %>% unnest_wider(eff) pander(ttest)![]()
One sample t test works:
ttest <- df_long %>% dplyr::mutate(quality_status = quality - status) %>% # prepare variable for one sample t test dplyr::group_by(country, stimuli) %>% dplyr::summarise(eff = nice_t_test(data = pick("quality_status"), response = "quality_status", nu = 0)) %>% unnest_wider(eff) pander(ttest)Previously, I had a similar issue using t.test() for paired t-tests. While setting paired = TRUE worked in some cases, it sometimes produced incorrect results if the rows in my data were not properly ordered. I later realized that paired t-tests in R require a wide-format dataset. I’m wondering if this might also explain why nice_t_test() is not working with my long-format data.
Thank you very much for your time and help!
Best regards, Liman Wang </issue_description>
Comments on the Issue (you are @copilot in this section)
- Fixes rempsyc/rempsyc#81
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.