Consider the following fictitious data:
set.seed(2022)
x1 = rnorm(30, 350, 50)
x2 = rnorm(30, 300, 70)
Now do a two sample Welch t test of $H_0: \mu_1=\mu_2$
against $H_a: \mu_1 > \mu_2, using t.test
in R:
t.test(x1,x2, alt="gr")
Welch Two Sample t-test
data: x1 and x2
t = 2.6864, df = 55.074, p-value = 0.004764
alternative hypothesis:
true difference in means is greater than 0
95 percent confidence interval:
13.8086 Inf
sample estimates:
mean of x mean of y
344.2034 307.5991
The P-value of the test is computed by looking in the upper tail of Student's t distribution with 55.074 degrees of freedom. [DF is adjusted downward from $n_1+n_2-2=58$ to compensate for the difference in sample variances.]
1 - pt(2.6864, 55.074)
[1] 0.004764504
If you do a 2-sided t test, then the P-value is calculated
by looking in the lower tail below $-2.6864$ and above $2.6864.$ [By using $
-notation we show only the P-value.]
t.test(x1, x2)$p.val
[1] 0.009528523
Computed as follows:
pt(-2.6864, 55.074) + 1 - pt(2.6864, 55.074) # left + right
[1] 0.009529008
Alternatively, by the symmetry of the t distribution
2*pt(-2.6864, 55.074) $ Double left tail probability
[1] 0.009529008
Quantities in the output to the test are rounded slightly to save space, so there is a tiny discrepancy with the P-values shown just above.
However, if you get confused (easy to do), and ask for the
wrong side, using parameter alt="less"
in t.test
, then
you get a nonsense P-value near $1.$
t.test(x1, x2, alt="less")$p.val
[1] 0.9952357