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?
 A: R has a suite of probability functions for density or mass in the form d* (e.g., dbeta, dchisq), and distribution in the form p* (e.g., pf, pgamma). You might wish to start there.
A: 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 $1-p$ 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

A: Yes, it is possible to calculate the chi-square value for a given p-value (p) and degrees of freedom (df). Below is how to go about it:
For the sake of verification, I first calculate p for a given chi-square value = 1.1 and df=1:
Solution:
pchisq(1.1, df=1, lower.tail=FALSE)# the answer is p=0.2942661
Now, to go backward by using p and df to calculate chi-square value, I used the p=0.2942661 I obtained from above and df=1 above:
Solution:
qchisq(0.2942661, 1, lower.tail=FALSE) # the answer is 1.1 as in the first solution.
So using your example of Chi Squared = 15 with df = 2, the solutions are below:
Solution: calculate p-value
pchisq(15, df=2, lower.tail=FALSE)# answer: p= 0.0005530844
use the p= 0.0005530844 and df=2 to get back the chi-square value
qchisq(0.0005530844, 2, lower.tail=FALSE)# answer: chi-square = 15
Hope this helps!!!
A: Try,
pchisq(chi,df)

in your example,
pchisq(15,2)

[1] 0.9994469

