8
$\begingroup$

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)
[1] 2

This can of course be remedied by

2*pt((1.5-1.2)*sqrt(100)/.5,df=100-1,lower=F)
[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.

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

2 Answers 2

9
$\begingroup$

You can make use of abs in the numerator (so it's always >0) and keep the lower.tail=FALSE.

$\endgroup$
0
$\begingroup$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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