# threejs Javascript plot library(threejs) # Unpack data from kde grid format x ; y ; z # Construct x,y,z coordinates xx rep(x,times=length(y)) yy rep(y,each=length(x)) zz ; dim(zz) NULL # Set up color range ra ceiling(16 * zz/max(zz)) col rainbow(16, 2/3) # 3D interactive scatter plot scatterplot3js(x=xx,y=yy,z=zz,size=0.4,color = col[ra],bg="black")
The code that follows uses the rtmvt() function from the tmvtnorm package to generate bivariate t distribution. The rgl plot renders the surface kernel density estimate of the surface in impressive detail.
# Draw from multi-t distribution without truncation library (tmvtnorm) Sigma matrix(c(1, .1, .1, 1), 2) # Covariance matrix X1 (n=1000, mean=rep(0, 2), sigma = Sigma, df=2) # from tmvtnorm package t.kde kde2d(X1[,1], X1[,2], n = 50) # from MASS package col2 heat.colors(length(bivn.kde$z))[rank(bivn.kde$z)] persp3d(x=t.kde, col = col2)
The real value of the multivariate distribution functions from the data science perspective is to simulate data sets with many more than two variables. The functions we have been considering are up to the task, but there are some technical considerations and, of course, we don’t have the same options for visualization. The following code snippet generates 10 variables from a multivariate normal distribution with a specified covariance matrix. Note that I’ve used the genPositiveDefmat() function from the clusterGeneration package to generate the covariance matrix. This is because mvrnorm() will throw an error, as theory says it should, if the covariance matrix is not positive definite, and guessing a combination of matrix elements to make a high dimensional matrix positive definite would require quite a bit of luck along with some serious computation time.
After generating the matrix, I use the corrplot() function from the corrplot package to produce an attractive pairwise correlation plot that is coded both by shape and color. corrplot() scales pretty well with the number of variables and will give a decent chart with 40 to 50 variables. (Note that now ggcorrplot will do …read more
Source:: r-bloggers.com