Manually Calculating P value from t-value in t-test
I have a sample dataset with 31 values. I ran a two-tailed t-test using R to test if the true mean is equal to 10:
t.test(x=data, mu=10, conf.level=0.95)
t = 11.244, df = 30, p-value = 2.786e-12 alternative hypothesis: true mean is not equal to 10 95 percent confidence interval: 19.18980 23.26907 sample estimates: mean of x 21.22944
Now I'm trying to do the same thing manually:
t.value = (mean(data) - 10) / (sd(data) / sqrt(length(data))) p.value = dt(t.value, df=length(lengths-1))
The t-value calculated using this method is the same as output by the t-test R function. The p-value, however, comes out to be 3.025803e-12.
Any ideas what I'm doing wrong?
Here is the full R code, including my dataset:
# Raw dataset -- 32 observations data = c(21.75, 18.0875, 18.75, 23.5, 14.125, 16.75, 11.125, 11.125, 14.875, 15.5, 20.875, 17.125, 19.075, 25.125, 27.75, 29.825, 17.825, 28.375, 22.625, 28.75, 27, 12.825, 26, 32.825, 25.375, 24.825, 25.825, 15.625, 26.825, 24.625, 26.625, 19.625) # Student t-Test t.test(x=data, mu=10, conf.level=0.95) # Manually calculate p-value t.value = (mean(data) - 10) / (sd(data) / sqrt(length(data))) p.value = dt(t.value, df=length(data) - 1)
ptand make it two-tailed.
> 2*pt(11.244, 30, lower=FALSE)  2.785806e-12
I think there is a detail missing: When to use lower=F? - Please see my question here: http://stats.stackexchange.com/questions/133091/manually-calculating-p-value-for-t-test-how-to-avoid-values-bigger-1
The value needs to be positive, so if using this with a variable that could be negative, wrap in `abs`.
For a two tailed test, you're looking for the probability that the value is less than -11.244 or more than +11.244. lower=F tells R to compute the probability of the value being more than the first parameter. Otherwise, it gives you the probability of the value being less than the first parameter. As such, you could also do 2*pt(-11.244, 30). Personally, I usually do 2*pt(-abs(q), df=n-1) as R defaults to lower=T.