Created
October 25, 2013 12:12
-
-
Save menugget/7153698 to your computer and use it in GitHub Desktop.
a test bit of code
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
mu <- 5 | |
sigma <- 2 | |
vals <- seq(-2,12,,100) | |
ds <- dnorm(vals, mean=mu, sd=sigma) | |
plot(vals, ds, t="l") | |
qs <- qnorm(c(0.05, 0.95), mean=mu, sd=sigma) | |
abline(v=qs, col=2, lty=2) | |
#install.packages("mvtnorm") | |
require(mvtnorm) | |
n <- 2 | |
mmu <- rep(mu, n) | |
msigma <- rep(sigma, n) | |
mcov <- diag(msigma^2) | |
mvals <- expand.grid(seq(-2,12,,100), seq(-2,12,,100)) | |
mvds <- dmvnorm(x=mvals, mean=mmu, sigma=mcov) | |
persp(matrix(mvds,100,100), axes=FALSE) | |
mvqs <- qmvnorm(0.95, mean=mmu, sigma=mcov, tail = "both") #? | |
#ex. plot | |
png("tmp.png", width=8, height=4, units="in", res=400) | |
par(mfcol=c(1,2)) | |
#univariate | |
plot(vals, ds, t="l") | |
qs <- qnorm(c(0.05, 0.95), mean=mu, sd=sigma) | |
abline(v=qs, col=2, lty=2) | |
#multivariate | |
pmat <- persp(seq(-2,12,,100), seq(-2,12,,100), matrix(mvds,100,100), axes=FALSE, shade=TRUE, lty=0) | |
cont <- contourLines(seq(-2,12,,100), seq(-2,12,,100), matrix(mvds,100,100), levels=0.05^2) | |
lines(trans3d(cont[[1]]$x, cont[[1]]$y, cont[[1]]$level, pmat), col=2, lty=2) | |
dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment