I did a linear regression for a two tailed t-test with 178 degrees of freedom. The summary
function gives me two p-values for my two t-values.
It is
2 * pt(-abs(t_value), df)
For example:
2 * pt(-5.06, 178)
#[1] 1.038543e-06
2 * pt(-10.09, 178)
#[1] 3.223683e-19
Alternatively, use
2 * pt(abs(t_value), df, lower.tail = FALSE)
We can compute the p value Pr(>|t|) in the following different ways:
tval <- 5.06
df <- 178
# compute area under the t-pdf
integrate(function(x) dt(x, df), -Inf, -tval)$value + integrate(function(x) dt(x, df), tval, Inf)$value # sum of two areas
# [1] 1.038543e-06
1-integrate(function(x) dt(x, df), -tval, tval)$value
# [1] 1.038543e-06
# 2-sided t-test: Pr_T(|t|>|tval|) = 2*(1 - F_T(|tval|)) = 2*F_T(-|tval|), where T~t(df=178)
2*(1 - pt(tval, df))
# [1] 1.038543e-06
2*pt(tval, df, lower.tail = FALSE)
# [1] 1.038543e-06
1 - (pt(tval, df) - pt(-tval, df))
# [1] 1.038543e-06
2*pt(-tval, df)
# [1] 1.038543e-06
The following illustrates the same geometrically with a different (less extreme) value of the t-statistic, as we can see, there are two (symmetric) blue regions that together represent the corresponding probability, under the 2-sided t-test.
df <- 178
x <- seq(-6, 6,0.01)
y <- dt(x, df)
tval <- 1.25
plot(x, y, type='l', main='t-distribution and p-value (5% significance level, 2-sided t-test)')
abline(h=0)
abline(v = c(tval, -tval), col='red')
index1 <- which(x >= -tval)[1]
index2 <- which(x >= tval)[1]
polygon(x = c(x[1:index1], x[index1], x[1]),
y = c(y[1:index1], 0, 0),
col = "blue")
polygon(x = c(x[index2], x[index2], x[index2:length(x)]),
y = c(0, y[index2], y[index2:length(y)]),
col = "blue")