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


The problem with the second method is that there is the risk of getting values bigger than $1$ (in fact up to $2$):

[1] 2

This can of course be remedied by

[1] 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.

  • 8
    $\begingroup$ Look at the code: getAnywhere(t.test.default). You'll find pval <- 2 * pt(-abs(tstat), df) there. $\endgroup$
    – 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
  p.value <- 2 * pt(-abs(t.value), df = nrow(data)-2)

Expanded answer at the request of mdewey.

  • 1
    $\begingroup$ Can you expand on how you think this adds to the comment and answer that are already there? $\endgroup$
    – mdewey
    Dec 12 '16 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.