R: Chi-Square Distribution

I. Calculating P(X > x)
If Q~Χ2(df), use the pchisq(q, df, lower.tail = FALSE) function to calculate P(Q > q).

Example 1:

If Q~Χ2(df = 7), use the following R code to calculate P(Q > 13).
> pchisq(13, df = 7, lower.tail = FALSE)
[1] 0.07210839

Example 2:

If Q~Χ2(df =12), use the following R code to calculate P(Q > 9).
> pchisq(9, df = 12, lower.tail = FALSE)
[1] 0.7029304

II. Given percentile, find corresponding q-value

If Q~Χ2(df =12), use the qchisq(percentile, df) function to find the q-value that corresponds with a given percentile.

Example:
IQ~Χ2(df =12), what q-value corresponds with the 75th percentile?
> qchisq(.75, df = 12)
[1] 14.8454

III. Simulating Chi-Square Random Variables
In statistics, one may finds the need to simulate random scenarios that have a Chi-Square distribution. To do this, we need to use the rchisq(n,
df) function, where n represents the number of random observations you wish to observe.

Example:

Simulate 16 random variables drawn from the chi-square distribution with 7 degrees of freedom.
> rchisq(16, 7)
[1]  3.347265  5.020331  5.454771  2.754932  3.777500 12.515111 15.640380  5.803006
[9]  9.271064  6.013901 10.975353  3.197264  3.151790  2.162119  6.893636  7.417652