R-beta: t.test

MM Peterson (magnus@balhaldie.u-net.com)
Thu, 19 Feb 1998 12:08:46 +0000


Message-Id: <3.0.1.32.19980219120846.006918c8@mail.u-net.com>
Date: Thu, 19 Feb 1998 12:08:46 +0000
To: r-help@stat.math.ethz.ch
From: MM Peterson <magnus@balhaldie.u-net.com>
Subject: R-beta: t.test

As a very new user of R, and a user of S with very
limited experience, I thought I would start humbly
with a simple t.test.

The response to the command sequence

sample <- scan()
4 5 6 7 8 9

t.test(sample,mu=20)

rather surprised me, because of the idiosyncratic
choice of a confidence interval for the mean of the
population from which the sample was drawn.  I asked
my colleague Bob Henery about this, and his reply
is appended.

Is there a good reason for choosing this rather
unconventional behaviour for R, or is it just
a blunder as Bob suggests?  At any rate Bob's
fix is easy enough for those of us who prefer
our confidence intervals to include the sample
mean, until or if the developers modify the
main code to change this.

Magnus Peterson

  ***********************************


Magnus,

It appears (from inspecting the relevant bit of code in the function t.test)

                df <- length(x) - 1
                stderr <- sqrt(vx/nx)
                tstat <- (mx - mu)/stderr


                pval <- 2 * pt(-abs(tstat), df)
                alpha <- 1 - conf.level
                cint <- c(tstat * stderr - qt((1 - alpha/2), 
                        df) * stderr, tstat * stderr + qt((1 - 
                        alpha/2), df) * stderr)


that the confidence interval reported is for the expected value of (X-mu)
rather than E(X).

The help file says

	conf.int : a confidence interval for the mean appropriate to
	          the specified alternative hypothesis.
	

but does not specify the alternative hypothesis (or even the null hypothesis).
Presumably they are thinking of a null hypothesis of the form

E(X-mu) = 0.

At the very least, the help file should make it clear what is being reported.
I would describe the present behaviour as quite unacceptable (a blunder rather
than a bug).


Bob


PS

Of course it is easy to change the function to your own local function by
inserting the following line just before exiting the function:

cint <- cint + mu


#                      new line
cint <- cint + mu
#                      old lines ...
        attr(cint, "conf.level") <- conf.level
        rval <- list(statistic = tstat, parameter = df, p.value = pval, 
                conf.int = cint, estimate = estimate, null.value = mu, 
                alternative = alternative, method = method, data.name = dname)
        attr(rval, "class") <- "htest"
        return(rval)
}

so you can have your own personal behaviour (or you can change the behaviour
of t.test for all your students).


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._