# Manually calculating p-value for t-test: How to avoid values bigger than $1$?

These two methods for calculating the p-value should be equivalent:

t.test(rats.drug,mu=1.2)$p.value 2*pt((mean(rats.drug)-1.2)*sqrt(n)/sd(rats.drug),df=n-1)  The problem with the second method is that there is the risk of getting values bigger than$1$(in fact up to$2$): 2*pt((1.5-1.2)*sqrt(100)/.5,df=100-1)  2  This can of course be remedied by 2*pt((1.5-1.2)*sqrt(100)/.5,df=100-1,lower=F)  3.245916e-08  My question Obviously the algorithm of the t-test function is intelligent enough to distinguish these two cases (whether the sample mean is bigger or smaller than the given mean). Is there an easy method to manually replicate the calculation of the p-value as it is done by the t-test function? My solution at the moment is an if-statement which checks whether the resulting value is bigger than$1\$ and in this case does the same calculation again with the lower=F option but perhaps there is a better way.

• Look at the code: getAnywhere(t.test.default). You'll find pval <- 2 * pt(-abs(tstat), df) there. – Roland Jan 12 '15 at 12:25

You can make use of abs in the numerator (so it's always >0) and keep the lower.tail=FALSE.
Glen_b is absolutely right about the abs, however, I have found that in certain data sets the values would require -abs to have the desired effect. I'm not able to explain why, but I'll leave these line of code here, incase anyone who is having a similar problem finds this thread.
  t.value <- betacoeff/standard error of the beta coefficients