mvrnorm()
from the MASS package, part R, although the mvtnorm package also offers functions for simulating both multidimensional normal and t-distribution.kde2d()
function, also from the MASS package, generates a two-dimensional sound density distribution estimate . # # https://stat.ethz.ch/pipermail/r-help/2003-September/038314.html # library(MASS) # mu <- c(0,0) # Sigma <- matrix(c(1, .5, .5, 1), 2) #
# > Sigma # [,1] [,2] # [1,] 1.0 0.1 # [2,] 0.1 1.0
# N(mu, Sigma) bivn <- mvrnorm(5000, mu = mu, Sigma = Sigma ) # MASS head(bivn) # bivn.kde <- kde2d(bivn[,1], bivn[,2], n = 50) # MASS
# , image(bivn.kde) # contour(bivn.kde, add = TRUE) #
ellipse()
function from the ellipse package, generates the classic two-dimensional normal distribution graph found in many textbooks. # library(ellipse) rho <- cor(bivn) y_on_x <- lm(bivn[,2] ~ bivn[,1]) # Y ~ X x_on_y <- lm(bivn[,1] ~ bivn[,2]) # X ~ Y plot_legend <- c("99% ", "95% ","90% ", "Y X ", "X Y ") plot(bivn, xlab = "X", ylab = "Y", col = "dark blue", main = " ") lines(ellipse(rho), col="red") # ellipse() ellipse lines(ellipse(rho, level = .99), col="green") lines(ellipse(rho, level = .90), col="blue") abline(y_on_x) abline(x_on_y, col="brown") legend(3,1,legend=plot_legend,cex = .5, bty = "n")
# # persp(bivn.kde, phi = 45, theta = 30, shade = .1, border = NA) # # RGL library(rgl) col2 <- heat.colors(length(bivn.kde$z))[rank(bivn.kde$z)] persp3d(x=bivn.kde, col = col2)
# threejs Javascript- library(threejs) # kde x <- bivn.kde$x; y <- bivn.kde$y; z <- bivn.kde$z # x,y,z xx <- rep(x,times=length(y)) yy <- rep(y,each=length(x)) zz <- z; dim(zz) <- NULL # ra <- ceiling(16 * zz/max(zz)) col <- rainbow(16, 2/3) # 3D- scatterplot3js(x=xx,y=yy,z=zz,size=0.4,color = col[ra],bg="black")
rtmvt()
function from the tmvtnorm package to generate a two-dimensional t distribution. The rgl plot depicts the surface of the sound density distribution estimate in detail. # t- library (tmvtnorm) Sigma <- matrix(c(1, .1, .1, 1), 2) # X1 <- rtmvt(n=1000, mean=rep(0, 2), sigma = Sigma, df=2) # tmvtnorm t.kde <- kde2d(X1[,1], X1[,2], n = 50) # MASS col2 <- heat.colors(length(bivn.kde$z))[rank(bivn.kde$z)] persp3d(x=t.kde, col = col2)
genPositiveDefmat()
function from the clusterGeneration package was used to generate the covariance matrix. This is because the mvrnorm()
function will mvrnorm()
error, as it should theoretically happen if the covariance matrix is not positively defined, and selecting a combination of elements of a multidimensional matrix to make it positively defined will require decent luck and computation time.corrplot()
function from the corrplot package to derive a beautiful graph of pairwise correlations determined by color and shape. corrplot()
scales well with an increase in the number of variables and will produce a decent result for 40-50 variables. (Note that ggcorrplot is now used for ggplot2 graphs.) You can use other options to build pairwise scatterplots, and R offers many alternatives. # library(corrplot) library(clusterGeneration) mu <- rep(0,10) pdMat <- genPositiveDefMat(10,lambdaLow=10) Sigma <- pdMat$Sigma dim(Sigma) mvn <- mvrnorm(5000, mu = mu, Sigma = Sigma ) corrplot(cor(mvn), method="ellipse", tl.pos="n", title=" ")
rlnorm()
from the packages of compositions , which generates random variables from a multidimensional lognormal distribution. They are as easy to use as mvrorm()
, but she will have to look for others like her. I think a more fruitful approach if you really need to work with probability distributions is to get acquainted with the copula package.Source: https://habr.com/ru/post/279647/
All Articles