x <- runif(100)
cr <- conf.interval(x, cl = 0.97)
print(cr)
d <- density(x)
plot(d, type="n", main = "PDF with mean and 0.97 confidence interval")
polygon(d, col="cyan3")
abline(v=mean(x, na.rm = TRUE), lty = 2)
segments( x0=cr[["lci"]], y0=mean(d$y), x1=cr[["uci"]],
y1=mean(d$y), lwd = 2.5,
col = "black")
legend("topright", legend = c("mean", "CI"),
lty = c(2,1), lwd = c(1,2.5))
Run the code above in your browser using DataLab