# How to find critical values and p-values from the t-distribution

## Description

If we have a test statistic and need to find the corresponding p-value from the t-distribution, how do we do that? If we need to find a p-value from the t distribution, given that we know the significance level and degrees of freedom, how do we do that?

## Solution, in Julia

View this solution alone.

If we choose a value $0 \le \alpha \le 1$ as our Type 1 error rate, then we can find the critical value from the normal distribution using the quantile() function in Julia’s Distributions package.

If you don’t have that package installed, first run using Pkg and then Pkg.add( "Distributions" ) from within Julia.

The code below shows how to do this for left-tailed, right-tailed, and two-tailed hypothesis tests.

1
2
3
4
5
using Distributions
alpha = 0.05                  # Replace with your alpha value
n = 68                        # Replace with your sample size
tdist = TDist( n - 1 )
quantile( tdist, alpha )      # Critical value for a left-tailed test

1
-1.6679161141074252

1
quantile( tdist, 1 - alpha )  # Critical value for a right-tailed test

1
1.6679161141074252

1
quantile( tdist, alpha / 2 )  # Critical value for a two-tailed test

1
-1.996008354025297


We can also compute $p$-values from the normal distribution to compare to a test statistic. As an example, we’ll use a test statistic of 2.67, but you can substitute your test statistic’s value instead.

We can find the $p$-value for this test statistic using the cdf() function in Julia’s Distributions package. Again, we show code for left-tailed, right-tailed, and two-tailed tests.

1
2
test_statistic = 2.67                     # Replace with your test statistic
cdf( tdist, test_statistic )              # p-value for a left-tailed test

1
0.9952454518351646

1
1 - cdf( tdist, test_statistic )          # p-value for a right-tailed test

1
0.004754548164835448

1
2 * ( 1 - cdf( tdist, test_statistic ) )  # p-value for a two-tailed test

1
0.009509096329670896


See a problem? Tell us or edit the source.

## Solution, in R

View this solution alone.

If we choose a value $0 \le \alpha \le 1$ as our Type 1 error rate, and we know the sample size of our data, then we can find the critical value from the $t$-distribution using R’s qt() function. The code below shows how to do this for left-tailed, right-tailed, and two-tailed hypothesis tests.

1
2
3
4
5
alpha <- 0.05                                  # Replace with your alpha value
n <- 68                                        # Replace with your sample size
qt(p = alpha, df = n-1, lower.tail = TRUE)     # Critical value for a left-tailed test
qt(p = alpha, df = n-1, lower.tail = FALSE)    # Critical value for a right-tailed test
qt(p = alpha/2, df = n-1, lower.tail = FALSE)  # Critical value for a two-tailed test

1
2
3
4
5
6
7
8
9
[1] -1.667916

[1] 1.667916

[1] 1.996008


We can also compute $p$-values from the $t$-distribution to compare to a test statistic. As an example, we’ll use a test statistic of 2.67, but you can substitute your test statistic’s value instead.

We can find the $p$-value for this test statistic using R’s pt() function. We will use the same example sample size as above. Again, we show code for left-tailed, right-tailed, and two-tailed tests.

1
2
3
4
5
test_statistic <- 2.67                              # Replace with your test statistic
n <- 68                                             # Replace with your sample size
pt(test_statistic, df = n-1, lower.tail = TRUE)     # p-value for a left-tailed test
pt(test_statistic, df = n-1, lower.tail = FALSE)    # p-value for a right-tailed test
2*pt(test_statistic, df = n-1, lower.tail = FALSE)  # p-value for a two-tailed test

1
2
3
4
5
6
7
8
9
[1] 0.9952455

[1] 0.004754548

[1] 0.009509096