Hello,
Something like this?
t.test2 <- function(...) {
ht <- t.test(...)
class(ht) <- c("htest_tjl", class(ht))
ht
}
print.htest_tjl <- function(x, ...) {
NextMethod(x, ...)
se <- as.vector(abs(diff(x$estimate)/x$statistic))
cat("Standard error of the difference:", se, "\n\n")
invisible(x)
}
t.test2(1:10, y = c(7:20))
t.test2(extra ~ group, data = sleep) # last example from ?t.test
(The suffix tjl commes from the OP's initials.)
Hope this helps,
Rui Barradas
Às 21:51 de 21/02/2019, Fox, John escreveu:
Dear Thomas,
it is, unfortunately, not that simple. t.test() returns an object of class
"htest" and not all such objects have standard errors. I'm not entirely sure
what the point is since it's easy to compute the standard error of the difference from
the information in the object (adapting an example from ?t.test):
(res <- t.test(1:10, y = c(7:20)))
Welch Two Sample t-test
data: 1:10 and c(7:20)
t = -5.4349, df = 21.982, p-value = 1.855e-05
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-11.052802 -4.947198
sample estimates:
mean of x mean of y
5.5 13.5
as.vector(abs(diff(res$estimate)/res$statistic)) # SE
[1] 1.47196
class(res)
[1] "htest"
and if you really want to print the SE as a matter of course, you could always write your
own wrapper for t.test() that returns an object of class, say, "t.test" for
which you can provide a print() method. Much of the advantage of working in a statistical
computing environment like R (or Stata, for that matter) is that you can make things work
the way you like.
Best,
John
-------------------------------------------------
John Fox, Professor Emeritus
McMaster University
Hamilton, Ontario, Canada
Web: http::/socserv.mcmaster.ca/jfox
On Feb 21, 2019, at 3:57 PM, Thomas J. Leeper <thosjlee...@gmail.com> wrote:
A recent thread on Twitter [1] by a Stata user highlighted that t.test()
does not return or print the standard error of the mean difference, despite
it being calculated by the function.
I know this isn’t the kind of change that’s likely to be made but could we
at least return the SE even if the print() method isn’t updated? Or,
better, update the print() method to display this as well?
Best,
Thomas
[1]
https://twitter.com/amandayagan/status/1098314654470819840?s=21
--
Thomas J. Leeper
http://www.thomasleeper.com
[[alternative HTML version deleted]]
______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel