Description
Plots the pdf of the normal distribution with mean equal to mean
and standard deviation equal to sd
and shades in the area under the curve as determined by q
(either a scalar of a vector of length 2)
Usage
qnorm(p, mean = 0, sd = 1, lower.tail = TRUE, ...)
q
vector or scalar quantitiesmean
the mean of your univariate normal distributionsd
the mean of your -univariate normal distributionlower.tail
logical; if TRUE (default), probabilities are \(P(X ≤ x)\) otherwise, \(P(X > x)\).poly.col
colour used for shaded region (default = “skyblue”)add2plot
logical; if FALSE (default) this function will create a new plot, otherwise it will add to an existing plotadd.prob
logical; if TRUE (default), the probabilities associated with the shaded region will appear in textadd.q
logical; if TRUE (default), the quantile associated with the shaded probability will appear on the x-axisadd.legend
logical; if TRUE, a legend with the mean and standard deviation will be plotted in a legend...
arguments to be passed to the plot
functionReferences
Adapted from code from here. Used in my lab here
To see the function, click the “CODE” button on the right-hand-side our source it here.
snorm <- function(q, mean=0, sd=1, rsd=4, lower.tail=TRUE, poly.col="skyblue",
add2plot = FALSE, add.prob=TRUE, add.q=TRUE, add.legend=FALSE, ...){
if (length(q)==2) {
q <- sort(q)
xx=seq(mean-rsd*sd,mean+rsd*sd,length=200)
yy=dnorm(xx,mean,sd)
if (!add2plot) {
plot(xx,yy,type="l", xlab="x", ylab= expression(paste(f*"("* x *" ; " * mu *", "*sigma*")")), ...)
} else {
lines(xx, yy, type="l")
}
x=seq(q[1],q[2],length=100)
y=dnorm(x,mean,sd)
polygon(c(q[1],x,q[2]),c(0,y,0),col=poly.col)
if (add.prob){
textheight <- max(dnorm(x, mean, sd))/4
prob <- round(pnorm(q[2], mean, sd) - pnorm(q[1], mean, sd), 3)
text(mean(q),textheight, prob)
}
if (add.q) {
axis(1, at=q[1], labels = q[1], col="skyblue", col.axis="skyblue", line=1)
axis(1, at=q[1], labels = "", col="skyblue", col.axis="skyblue")
axis(1, at=q[1], labels = "", col="skyblue", col.axis="skyblue", line=0.5)
axis(1, at=q[2], labels = q[2], col="skyblue", col.axis="skyblue", line=1)
axis(1, at=q[2], labels = "", col="skyblue", col.axis="skyblue")
axis(1, at=q[2], labels = "", col="skyblue", col.axis="skyblue", line=0.5)
}
} else {
if (lower.tail) {
# lower tail probabilities:
xx=seq(mean-rsd*sd,mean+rsd*sd,length=200)
yy=dnorm(xx,mean,sd)
if (!add2plot) {
plot(xx,yy,type="l", xlab="x", ylab= expression(paste(f*"("* x *" ; " * mu *", "*sigma*")")) , ...)
} else {
lines(xx, yy, type="l")
}
x=seq(xx[1],q,length=100)
y=dnorm(x,mean, sd)
polygon(c(xx[1],x,q),c(0,y,0),col=poly.col)
if (add.prob){
textheight <- dnorm(q, mean, sd)/4
prob <- round(pnorm(q, mean, sd), 3)
text(q-0.5*sd,textheight, prob)
}
if (add.q) {
axis(1, at=q, labels = "", col="skyblue", col.axis="skyblue")
axis(1, at=q, labels = q, col="skyblue", col.axis="skyblue", line=1)
axis(1, at=q, labels = "", col="skyblue", col.axis="skyblue", line=0.5)
}
# arrows(0.5,0.1,-0.2,0,length=.15)
# text(0.5,0.12,"-0.2533")
} else if (!lower.tail) {
xx=seq(mean-rsd*sd,mean+rsd*sd,length=200)
yy=dnorm(xx,mean,sd)
if (!add2plot) {
plot(xx,yy,type="l", xlab="x", ylab= expression(paste(f*"("* x *" ; " * mu *", "*sigma*")")), ...)
} else {
lines(xx, yy, type="l")
}
x=seq(q,xx[length(xx)],length=100)
y=dnorm(x,mean,sd)
polygon(c(q,x,xx[length(xx)]),c(0,y,0),col=poly.col)
if (add.prob){
textheight <- dnorm(q, mean, sd)/4
prob <- round(pnorm(q, mean, sd, lower.tail = FALSE), 3)
text(q+0.5*sd,textheight, prob)
}
if (add.q) {
axis(1, at=q, labels = "", col="skyblue", col.axis="skyblue")
axis(1, at=q, labels = q, col="skyblue", col.axis="skyblue", line=1)
axis(1, at=q, labels = "", col="skyblue", col.axis="skyblue", line=0.5)
}
} else {
stop("please specify an appropriate value for lower.tail: TRUE, FALSE, or inner")
}
}
if (add.legend) {
ltext <- bquote(mu *" = "* .(mean)*", "*sigma *" = "* .(sd))
legend("topright", lwd= 2,
legend = ltext)
}
}
myz <- -0.2533
snorm(myz)
x <- 7.1 ; mymean = 7 ; mysd = 0.1
snorm(x, mean = mymean , sd = mysd, add.legend = TRUE )
snorm(myz, lower.tail=FALSE)
snorm(x, mean = mymean , sd = mysd, add.legend = TRUE , lower.tail = FALSE)
a <- -1.1
b <- 2.0
q <- c(a,b)
snorm(q, lower.tail=FALSE)
a <- 6.9
b <- 7.1
q <- c(a,b)
snorm(q, mymean, mysd, lower.tail=FALSE)