Robert Baer
2025-Nov-04 17:22 UTC
[R] Did behavior of "paired" argument in t.test() change?
Hi all,
Did the behavior of base t.test() subtly change with respect to the
paired argument?
I think this code used to work with long form data, but now it seems not
to? work:
? ? # Example long format data
? ? data_long <- data.frame(
? ? ? value = c(10, 12, 15, 8, 9, 11),
? ? ? time = factor(rep(c("before", "after"), each = 3))
? ? )
? ? t.test(value ~ time, data = data_long, paired = TRUE)
# Errorin t.test.formula(value ~ time, data = data_long, paired = TRUE)
: # cannot use 'paired' in formula method
A quick Google seems to suggest that the above code should still work.
What am I missing?
-------------------------------
Less sure about this one ...
Also, I thought it had always been possible to use x = "difference
scores" in t.test(..., paired = TRUE) as long as y = NULL. My memory
about this is less secure than the behavior above.
Control = c(10, 12, 15) Treat = c(8, 9, 11) data_wide <- data.frame(
Control, Treat, Diff = Treat - Control) t.test(data_wide$Diff, NULL,
"two.sided", 0, paired = FALSE) # works but y = NULL, mu = 0 mostly
implies difference scores
t.test(data_wide$Diff, NULL, "two.sided", 0, paired = TRUE) # fails
but
if y = NULL the value of paired should not matter should it?
For y, help says "y an optional (non-empty) numeric vector of data
values". I take this to imply that x = compared to a population mean
hypothesis whenever y = NULL. Why should the value of paired influence
the outcome?
Just bad memory? on my part?
R.version _ platform x86_64-w64-mingw32 arch x86_64 os mingw32 crt ucrt
system x86_64, mingw32 status major 4 minor 5.1 year 2025 month 06 day
13 svn rev 88306 language R version.string R version 4.5.1 (2025-06-13
ucrt) nickname Great Square Root
--
---
Robert W. Baer, Ph.D.
Professor of Physiology
Kirksville College of Osteopathic Medicine
A.T. Still Univerisity of Health Sciences
800 W. Jefferson St.
Kirksville, MO 63501
[[alternative HTML version deleted]]
Petr Pikal
2025-Nov-04 17:39 UTC
[R] Did behavior of "paired" argument in t.test() change?
Hallo According to recent documentation this works for paired t.test. t.test(Pair(value,time)~1, data=data_long) It is the last example in help page. And your second question seems to be also different from what help page says: If paired is TRUE then both x and y must be specified and they must be the same length. Missing values are silently removed (in pairs if paired is TRUE ). Cheers. Petr ?t 4. 11. 2025 v 18:23 odes?latel Robert Baer via R-help < r-help at r-project.org> napsal:> Hi all, > > Did the behavior of base t.test() subtly change with respect to the > paired argument? > > I think this code used to work with long form data, but now it seems not > to work: > > # Example long format data > data_long <- data.frame( > value = c(10, 12, 15, 8, 9, 11), > time = factor(rep(c("before", "after"), each = 3)) > ) > > t.test(value ~ time, data = data_long, paired = TRUE) > > # Errorin t.test.formula(value ~ time, data = data_long, paired = TRUE) > : # cannot use 'paired' in formula method > > A quick Google seems to suggest that the above code should still work. > What am I missing? > > ------------------------------- > > Less sure about this one ... > > Also, I thought it had always been possible to use x = "difference > scores" in t.test(..., paired = TRUE) as long as y = NULL. My memory > about this is less secure than the behavior above. > > Control = c(10, 12, 15) Treat = c(8, 9, 11) data_wide <- data.frame( > Control, Treat, Diff = Treat - Control) t.test(data_wide$Diff, NULL, > "two.sided", 0, paired = FALSE) # works but y = NULL, mu = 0 mostly > implies difference scores > > t.test(data_wide$Diff, NULL, "two.sided", 0, paired = TRUE) # fails but > if y = NULL the value of paired should not matter should it? > > For y, help says "y an optional (non-empty) numeric vector of data > values". I take this to imply that x = compared to a population mean > hypothesis whenever y = NULL. Why should the value of paired influence > the outcome? > > Just bad memory on my part? > > R.version _ platform x86_64-w64-mingw32 arch x86_64 os mingw32 crt ucrt > system x86_64, mingw32 status major 4 minor 5.1 year 2025 month 06 day > 13 svn rev 88306 language R version.string R version 4.5.1 (2025-06-13 > ucrt) nickname Great Square Root > > > -- > --- > Robert W. Baer, Ph.D. > Professor of Physiology > Kirksville College of Osteopathic Medicine > A.T. Still Univerisity of Health Sciences > 800 W. Jefferson St. > Kirksville, MO 63501 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Ivan Krylov
2025-Nov-04 17:40 UTC
[R] Did behavior of "paired" argument in t.test() change?
? Tue, 4 Nov 2025 11:22:57 -0600 Robert Baer via R-help <r-help at r-project.org> ?????:> Errorin t.test.formula(value ~ time, data = data_long, paired > TRUE) : > cannot use 'paired' in formula methodThis was changed in R-4.4.0: https://bugs.r-project.org/show_bug.cgi?id=14359 The news item (see news(grepl('paired', Text))) recommends the use of Pair(x1, x2) ~ 1 instead of the 'paired' argument. A further fix is currently in R-devel (but not R-patched): without it, Pair(x1, x2) does not handle the 'subset' argument. -- Best regards, Ivan