install.packages("scatterplot3d")
library(scatterplot3d)
library("mvtnorm")
x1 <- x2 <- seq(-10, 10, length = 51)
dens <- matrix(dmvnorm(expand.grid(x1, x2),
sigma = rbind(c(3, 2), c(2, 3))),
ncol = length(x1))
s3d <- scatterplot3d(x1, x2,
seq(min(dens), max(dens), length = length(x1)),
type = "n", grid = FALSE, angle = 70,
zlab = expression(f(x[1], x[2])),
xlab = expression(x[1]), ylab = expression(x[2]),
main = "Bivariate normal distribution")
text(s3d$xyz.convert(-1, 10, 0.07),
labels = expression(f(x) == frac(1, sqrt((2 * pi)^n *phantom(".") * det(Sigma[X]))) *
phantom(".") * exp * {bgroup("(", - scriptstyle(frac(1, 2) * phantom(".")) *
(x - mu)^T * Sigma[X]^-1 * (x - mu), ")")}))
text(s3d$xyz.convert(1.5, 10, 0.05),
labels = expression("with" * phantom("m") *mu == bgroup("(", atop(0, 0), ")") *
phantom(".") * "," *phantom(0) *
{Sigma[X] == bgroup("(", atop(3 * phantom(0) * 2,2 * phantom(0) * 3), ")")}))
for(i in length(x1):1)
s3d$points3d(rep(x1, length(x2)), x2, dens, type = "l")
for(i in length(x2):1)
s3d$points3d(x1, rep(x2, length(x1)), dens[,i], type = "l")
How to plot dynamic and rotatable 3D of the density function for the bivariate normal distribution in R? Thanks
How to plot the second plot in
http://personal.kenyon.edu/hartlaub/MellonProject/Bivariate2.html
This post was edited by Shivakumar Kota at September 22, 2020 3:17 PM IST