# 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?

• To determine a p-value you need to specify whether it is one-sided or two-sided. For two-sided it is P(|X|>15) and for one-sided lower tail it is P(X<-15) and P(X.>15) for one-sided upper tail. X is a chi-square random variable with 2 degrees of freedom. – Michael R. Chernick Dec 10 '16 at 19:41
• It is two-sided. What would be the code in R in order to carry this test? – mat Dec 10 '16 at 19:44
• I don;t know R very well. I would think there is a a chi-square function that you could call. Aside from R you can get the value from a table of the chi-square distribution which are available in many elementary stat books. The only problem with the table is that 15 may not appear in which case you can get close by intrtpolation. – Michael R. Chernick Dec 10 '16 at 19:56
• Let me correct myself. The chi-square doesn't go negative. For a one-sided test you want P(X>15). In my book P(X>13.815) is 0.001. So the p-value is < 0.001. – Michael R. Chernick Dec 10 '16 at 20:04
• (1) It is rare for Chi-squared tests to be two-sided: what is your application? (2) (cc @michael) One would not interpolate into the tails; one would estimate the integral. BTW, this is an elementary integral, because the Chi-squared distribution with 2 DF is an Exponential distribution: you should obtain $e^{-15/2}$ for the upper tail probability. See en.wikipedia.org/wiki/Chi-squared_distribution. – whuber Dec 10 '16 at 20:37

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.

• Is it possible to go backwards? Deriving Chi-Square value using P-value and degrees of freedom using R code? – Eric Mar 9 '18 at 0:54
• @Eric Just type the following in R: help(qchisq) – Gordon Smyth Dec 31 '18 at 1:30

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.

• Is it possible to go backwards? Deriving Chi-Square value using P-value and degrees of freedom using R code? – Eric Mar 9 '18 at 0:54

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!!!

Try,

pchisq(chi,df)


pchisq(15,2)

• This is incorrect. It doesn't even pass the smell test. Consider that the expected value of a chi-squared distribution is its df, and that the variance is 2*df (wikipedia), so 15 is ~9 SDs from the mean. That should tell you something is wrong here. In essence, you are using this to perform a 1-tailed test that a fit is 'too good' (cf here). Under the typical usage of the chi-squared, you want pchisq(15, 2, lower.tail=FALSE), or 1-pchisq(15, 2), which is 0.0005530844. – gung - Reinstate Monica Jun 30 '17 at 1:23