R has functions available for most of the famous statistical distributions. Prefix the name as follows:
The rest of the name indicates the distribution. Table 8-1 lists some common statistical distribution functions.
Table 8-1. Common R Statistical Distribution Functions
Distribution | Density/pmf | cdf | Quantiles | Random Numbers |
---|---|---|---|---|
Normal |
|
|
|
|
Chi square |
|
|
|
|
Binomial |
|
|
|
|
As an example, let’s simulate 1,000 chi-square variates with 2 degrees of freedom and find their mean.
> mean(rchisq(1000,df=2)) [1] 1.938179
The r
in rchisq
specifies that we wish to generate random numbers—in this case, from the chi-square distribution. As seen in this example, the first argument in the r
-series functions is the number of random variates to generate.
These functions also have arguments specific to the given distribution families. In our example, we use the df
argument for the chi-square family, indicating the number of degrees of freedom.
Consult R’s online help for details on the arguments for the statistical distribution functions. For instance, to find our more about the chi-square function for quantiles, type ?qchisq
at the command prompt.
Let’s also compute the 95th percentile of the chi-square distribution with two degrees of freedom:
> qchisq(0.95,2) [1] 5.991465
Here, we used q
to indicate quantile—in this case, the 0.95 quantile, or the 95th percentile.
The first argument in the d
, p
, and q
series is actually a vector so that we can evaluate the density/pmf, cdf, or quantile function at multiple points. Let’s find both the 50th and 95th percentiles of the chi-square distribution with 2 degrees of freedom.
qchisq(c(0.5,0.95),df=2) [1] 1.386294 5.991465