Skip to content

Instantly share code, notes, and snippets.

@liammclennan
Last active April 8, 2016 02:32
Show Gist options
  • Save liammclennan/51749b8217cc21962931696a00715162 to your computer and use it in GitHub Desktop.
Save liammclennan/51749b8217cc21962931696a00715162 to your computer and use it in GitHub Desktop.
Draw a normal probability distribution
mean=132; sd=13
lb=105; ub=159
x <- seq(-4,4,length=100)*sd + mean
hx <- dnorm(x,mean,sd)
plot(x, hx, type="n", xlab="Duration (days)", ylab="",
main="Duration Probability Distribution", axes=FALSE)
i <- x >= lb & x <= ub
lines(x, hx)
polygon(c(lb,x[i],ub), c(0,hx[i],0), col="red", density=5)
area <- pnorm(ub, mean, sd) - pnorm(lb, mean, sd)
result <- paste("P(",lb,"< Duration <",ub,") =",
signif(area, digits=3), " mean = ",mean)
mtext(result,3)
axis(1, at=seq(40, 180, 20), pos=0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment