The below examples assume a N(0,1). A link to the R manual is here.
dnorm-returns the height of the normal curve at a specified value along the x-axis
pnorm-the cumulative density function (CDF) that returns the area to the left of a specified value
qnorm-returns quantiles or "critical values"
rnorm-generates random numbers from the normal distribution
These are examples for the normal distribution, but as you could imagine R has commands for numerous other distributions such as the chi-square, beta, uniform, and poisson.
If you are curious, here are the commands I used to plot these figures:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
normal <- rnorm(10000000) | |
d_normal <- density(normal) | |
# dnorm | |
plot(d_normal, main="dnorm(1.5)", xlab="", xaxs="i", yaxs="i", xlim=c(-4.5,4.5), ylim=c(0,0.43)) | |
abline(h=0, col="black") | |
arrows(x0=1.5,x1=1.5,y0=0,y1=dnorm(1.5), angle=0, length=0.1, lty=3) | |
points(1.5, dnorm(1.5), pch=19, col="gray") | |
text(1.5, dnorm(1.5), labels=paste("height=",round(dnorm(1.5),4)), pos=4) | |
# pnorm | |
plot(d_normal, main="pnorm(1.5)", xlab="", xaxs="i", yaxs="i", xlim=c(-4.5,4.5), ylim=c(0,0.43)) | |
abline(h=0, col="black") | |
x3 <- 1 | |
x4 <- max(which(d_normal$x < 1.5)) | |
with(d_normal, polygon(x=c(x[c(x3,x3:x4,x4)]), y= c(0,y[x3:x4],0), col="gray")) | |
text(-1, dnorm(1.2), labels=paste("area=",round(pnorm(1.5),4)), pos=4) | |
# qnorm | |
plot(d_normal, main="qnorm(0.9332)", xlab="", xaxs="i", yaxs="i", xlim=c(-4.5,4.5), ylim=c(0,0.43)) | |
abline(h=0, col="black") | |
arrows(x0=1.5,x1=1.5,y0=0,y1=dnorm(1.5), angle=0, length=0.1, col="gray", lwd=2) | |
text(1.5, dnorm(1.5)/2, labels=paste("cutoff value=",round(qnorm(0.9332),4)), pos=4) | |
# rnorm | |
plot(d_normal, main="rnorm(500)", xlab="", xaxs="i", yaxs="i", xlim=c(-4.5,4.5), ylim=c(0,0.43)) | |
x <- rnorm(500) | |
o <- runif(500) | |
y <- dnorm(x)*o | |
points(x,y, pch=19, col="gray") | |
abline(h=0, col="black") |
No comments:
Post a Comment