Skip to content

Instantly share code, notes, and snippets.

@SirSaleh
Last active September 30, 2021 09:30
Show Gist options
  • Save SirSaleh/419c81d1d2d13d434f0a827f38876e68 to your computer and use it in GitHub Desktop.
Save SirSaleh/419c81d1d2d13d434f0a827f38876e68 to your computer and use it in GitHub Desktop.
plot bivariate normal distribution in R
# 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")
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