Sampling from a chi-squared distribution
Chi-squared distribution is often used by chi-squared tests to inspect the difference between observed value and expected value, or to examine the independence of two variables. In addition, one can infer confidence intervals using chi-squared distribution. In the following recipe, we will discuss how to use R to generate chi-squared distribution further.
Getting ready
In this recipe, you need to prepare your environment with R installed.
How to do it…
Please perform the following steps to generate samples from chi-squared distribution:
First, we can use
rchisq
to generate three samples with a degree of freedom equal to10
:> set.seed(123) > rchisq(3,df=10) [1] 6.779170 14.757915 3.259122
We can then use
dchisq
to obtain the density at x=3 with a degree of freedom equal to10
:> dchisq(3,df=10) [1] 0.02353326
Also, we can use
pchisq
andqchisq
to obtain the distribution function and quantile function of the distribution:> pchisq(3,df=10...