R: calculate p-value given Chi Squared and Degrees of Freedom

How can I calculate the p-value given Chi Squared and the Degrees of Freedom?
For example, what would be the exact p-value of a Chi Squared = 15 with df = 2?

Answer

In applied statistics, chisquared test statistics arise as sums of squared residuals, or from sums of squared effects or from log-likelihood differences. In all of these applications, the aim is to test whether some vector parameter is zero vs the alternative that it is non-zero and the chisquare statistic is related to the squared size of the observed effect. The required p-value is the right tail probability for the chisquare value, which in R for your example is:

> pchisq(15, df=2, lower.tail=FALSE)
[1] 0.0005530844

For other df or statistic values, you obviously just substitute them into the above code.

All cumulative probability functions in R compute left tail probabilities by default. However they also have a lower.tail argument, and you can always set this FALSE to get the right tail probability. It is good practice to do this rather than to compute 1p as you might see in some elementary textbooks.

The function qchisq does the reverse calculation, finding the value (“q” is for quantile) of the chisquare statistic corresponding to any given tail probability.
For example, the chisquare statistic corresponding to a p-value of 0.05 is given by

> qchisq(0.05, df=2, lower.tail=FALSE)
[1] 5.991465

Attribution
Source : Link , Question Author : mat , Answer Author : Gordon Smyth

Leave a Comment