How do I calculate a p-value if I have the t-statistic and d.f. (in Perl)?
-
05-07-2019 - |
Question
I have written a Perl script that performs many one-sample t-tests. I get thousands of t-statistics with their degrees of freedom (df). I need to upgrade the script to also return their p-values (there are too many to look them up manually in a table). Is there some kind of formula I can use for this with the t-statistic and d.f as input? I hope someone can help me with this, many thanks in advance!
A.A.
Solution
Using Statistics::Distributions seems pretty straightforward:
print Statistics::Distributions::tprob($dof,$tstat);
OTHER TIPS
A search of MetaCPAN reveals the following:
If you are doing a two-tailed test, then your p-value = 2*P(T > t), where t is your calculated test statistic. So essentially, you need a way to model the T-dist in order to integrate(T-dist from t to INFINITY). Here is a demo: http://www.stat.tamu.edu/~west/applets/tdemo.html
I'm not familiar with Perl and its libraries, but hopefully this gets you started. You can write a rudimentary integrator and check some values to make sure that it is accurate enough.