Last active
September 30, 2021 09:30
-
-
Save SirSaleh/419c81d1d2d13d434f0a827f38876e68 to your computer and use it in GitHub Desktop.
plot bivariate normal distribution in R
This file contains hidden or 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
# need mvtnorm package | |
library("mvtnorm") | |
range = seq(-5,5,0.1) | |
mean = c(0,0) | |
Sigma = matrix(c(1, .5, .5, 1), 2) | |
out = matrix (rep(0,101*101),101) | |
for (i in 1:length(range)){ | |
for (j in 1:length(range)){ | |
out[i,j] = dmvnorm(c(range[i],range[j]),mean=mean,sigma=Sigma) | |
} | |
} | |
persp(out,theta = 30,phi = 30,col="lightblue") |
This file contains hidden or 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
library(MASS) | |
bivn <- mvrnorm(100000, mu = c(0, 0), Sigma = matrix(c(1, .5, .5, 1), 2)) | |
# now we do a kernel density estimate | |
bivn.kde <- kde2d(bivn[,1], bivn[,2], n = 50) | |
persp(bivn.kde, phi = 45, theta = 30) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment