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:

  1. First, we can use rchisq to generate three samples with a degree of freedom equal to 10:
    > set.seed(123)
    > rchisq(3,df=10)
    [1]  6.779170 14.757915  3.259122
    
  2. We can then use dchisq ...

Get R for Data Science Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.