R has a function called t.test
that does one-sample and two-sample
tests and confidence intervals about mean parameters of samples from
normally distributed populations.
In order to use t.test
you need actual data, not just
sample mean and standard deviation. Hence I have made up some normal-looking
data with the same sample size, sample mean, and sample standard deviation
as in Example 8.5.1 in DeGroot and Schervish.
Then the following statement does the test
The only things you have to do are
mu = 5.2
)
alternative = "greater"
). The choices are
alternative = "two.sided"
, which is the default
and so does not need to be specified.
alternative = "less"
alternative = "greater"
This function is geared toward P-values. It does not mention the critical
value, and you don't need to know it. It does report the test statistic
t = 1.8329
and the P-value p-value = 0.04408
.
If you want to convert the P-value into a decision, you can do that in
the usual way. Reject the null hypothesis as level 0.05 if the P-value
is less than 0.05. Here p-value = 0.04408
is indeed less than
0.05 so reject the null.
Suppose we want a confidence interval instead of a test. The same
R function t.test
does both. The alternative
optional argument controls which kind of confidence interval is made.
By far the most common type of confidence interval is two-sided,
which is specified by alternative = "two-sided"
,
which is the default and so is what happens when you specify no
alternative
argument.
Very rarely, one actually wants a one-sided confidence interval, which is produced by one of the other alternatives. But the main way one gets one-sided confidence intervals is as an unwanted byproduct of doing a one-tailed test.
So just get rid of the alternative
argument to
get an ordinary confidence interval.
Another optional argument is conf.level
specifies the
confidence level. The default is 0.95, so this argument can be omitted
when you want a 95% confidence interval.
The result: the 90% confidence interval for the true mean μ computed from these data is (5.207815, 5.592185) or when rounded to reasonable precision (5.21, 5.59).
For a test or confidence interval about the difference of the means
of two normal distributions from which we have two independent samples,
say x
and y
we use the form
t.test(x, y, alternative = "less")
or something similar.
As in the one-sample case, in order to use t.test
you need actual data, not just sample means and standard deviations.
Hence I have made up some normal-looking
data with the same sample sizes, sample means, and sample standard deviations
as in Example 8.6.2 in DeGroot and Schervish.
As the printout says, we get test statistic t = 3.3213
and P-value p-value = 0.005796
(two-tailed test with
null hypothesis μ1 = μ2).
This is not the old-fashioned two sample t-test based on
the assumption of equal variances described at the beginning of
Section 8.6 in DeGroot and Schervish and in the
following section. Rather it is the
newer test without the equal variance assumption described
starting at the bottom of p. 501 under
the heading Unequal Variances. Note that the
test statistic and P-value agree with those given in Example 8.6.3
on p. 503. Also in agreement is the reported non-integer degrees
of freedom df = 12.492
, although when we use t.test
to do the calculations we don't really need to know this.
If one wants to do the old-fashioned test, the optional argument
var.equal = TRUE
to the t.test
function
makes it do that.
Why would one want to do this? Here are the rationales for the two procedures.
Unless you know somehow that the variances are equal (a direct pipeline to God perhaps), you should use Welch's approximation.
Now we get test statistic t = 3.4427
and P-value p-value = 0.003345
(two-tailed test with
null hypothesis μ1 = μ2). This matches
what DeGroot and Schervish get in their Example 8.6.2.
Not much to say here. The preceding two examples produced 95% confidence intervals for the parameter μ1 &minus μ2.
The example in the Welch's approximation section produces the confidence interval (0.1040533, 0.4959467).
The example in the equal variance section produces the confidence interval (0.1152669, 0.4847331). This latter relies on a generally unverifiable assumption of equal population variances.