0

How do I calculate the probability (%) for chi square test using $X^2$ value and DoF as inputs?

Im trying to create a C++ program to calculate chi square tests with very high DoF, so I cannot use the table to check the probability. I have already written functions to calculate DoF and $X^2$ values.

I'm not a mathematician so an example with the values inserted would be greatly helpful, for example with DoF of $14$ and $X^2$ of $17$.

Empty
  • 13,012

1 Answers1

0

For very large $n$ the chi-squared distribution is nearly normal with mean equal to df and variance twice the df.

R statistical software available free from r-project.org has density function 'dchisq', cumulative distribution function 'pchisq', quantile function (inverse CDF) 'qchisq', and also 'rchisq' which samples from the distribution.

From what you say I believe you want the area under the density curve of chi-squared with 14 DF to the right of 17 (the P-value for a right-tailed test with test statistic 17). In R that would be

 > 1 - pchisq(17, 14)
 [1] 0.2561779

Here are other examples, some of which are in your table, and the last of which shows the good agreement with normal for large DF:

 > qchisq(.95, 10)
 [1] 18.30704   # 95% area below 18.3
 > dchisq(2, 15)
 [1] 9.829755e-05  # PDF almost 0 at 2
 > pchisq(190, 200);  pnorm(190, 200, 20)  # 3rd arg is SD
 [1] 0.3173568  # exact
 [1] 0.3085375  # normal appprox

Unless you have considerable experience programming probability functions I do not recommend writing your own C++ code. Surely, there is compiled code you can import that is written by experts, using best procedures for various DFs.

Addendum: If $Q \sim Chisq(df = \nu),$ with large $\nu,$ then $q$ with $P\{Q > q\} = \alpha$ has $$q \approx \nu\left(1 - \frac{2}{9\nu} + z_{\alpha} \sqrt{\frac{2}{9\nu}}\right)^3,$$ where $z_{\alpha}$ cuts probability $\alpha$ from the upper tail of a standard normal curve. For example:

 > qchisq(.95, 150)
 [1] 179.5806  # cuts 5% from right tail of CHISQ(150)
 > 150*(1 - 2/(9*150) + qnorm(.95)*sqrt(2/(9*150)))^3
 [1] 179.5788  # good approximation of above
BruceET
  • 51,500