This document will explore evaluating and plotting continuous distributions. We can start by revisiting how to plotted for continuous distributions, generating a sequence of points, and then evaluating the pdf at those locations.
x = seq(-4, 4, length=101)
Lets start with the uniform distribution.
plot(x, dunif(x,min=-2.2, max=2.2), type='l')
Note that it is now ok to use a “type=‘l’” since the pdf can be evaluated everywhere.
This looks fine, since we used 101 evals along the x-axis. But what if we used fewer.
x = seq(-4, 4, length=21)
plot(x, dunif(x,min=-2.2, max=2.2), type='l')
This now looks more trapezoidal, which is not quite right.
Luckily R has a better command to automatically plot a smooth curve, as
follows:
curve(dunif(x, min=-2.2, max=2.2), from=-4, to=4)
We can also plot if cumulative density function (cdf)
curve(punif(x, min=-2.2, max=2.2), from=-4, to=4)
An exponential distribution has a pdf of \(f(x) = \lambda e^{-\lambda x}\) and a cdf of \(F(a) = 1 - e^{-\lambda a}\). The value \(\lambda\) is know as the rate parameter. An exponential random variable has notation \(X \sim Exp(\lambda)\). We can plot the pdf and cdf as follows
curve(dexp(x, rate = 0.5), from=0, to=10)
curve(pexp(x, rate = 0.5), from=0, to=10)
Lets plot several with different parameters at once
curve(dexp(x, rate = .5), from=0, to=10, col='blue')
curve(dexp(x, rate = 1), from=0, to=10, col='red', add=TRUE)
curve(dexp(x, rate = 1.5), from=0, to=10, col='purple', add=TRUE)
#add legend
legend(7, .5, legend=c("rate=.5", "rate=1", "rate=1.5"),
col=c("blue", "red", "purple"), lty=1, cex=1.2)
A normal distribution has a pdf of \(f(x) = \frac{1}{\sqrt{2 \pi} \sigma} \exp(-\frac{(x-\mu)^2}{2 \sigma^2})\); its cdf does not have a closed form. The value \(\sigma\) is a width or bandwidth or standard deviation, and the \(\mu\) is a mean parameter. An normal random variable has notation \(X \sim N(\mu, \sigma^2)\). We can plot the pdf and cdf as follows
curve(dnorm(x,0,1.2), from=-5, to=5)
And even though its cdf is not defined, we can still evaluate and plot it:
curve(pnorm(x,0,1.2), from=-5, to=5)