R: ellipse confidence visualization package


In the last post from the R-hub “Visualization of a two-dimensional Gaussian on a plane” , an algorithm for constructing a confidence ellipse from a covariance matrix was described. The algorithm was accompanied by an example and an R-script.

Perhaps the author of the post on Visualization of the Gaussian mephistopheies and readers of the R-hub will find the following information useful. The R repository has an ellipse package . This package contains various procedures for constructing ellipses of confidence areas.

Consider an example.

Sample from a two-dimensional normal distribution

To generate a sample from the two-dimensional normal distribution, we use the mvtnorm package .

We construct a data sample of 1000 elements with a vector of average values ​​of mu and a covariance matrix sigma :

mu <- c(1,2) 
sigma <- matrix(c(4,2,2,3), ncol=2) 
data <- rmvnorm(n=1000, mean=mu, sigma=sigma) # генерируем выборку с заданными параметрами

Ellipse 95% Confidence Area

For the sigma covariance matrix, we find the coordinates of 100 points of the ellipse of the 95% confidence region:

confidence.ellipse <- ellipse(sigma,centre=mu,level=0.95,npoints=100)


Plot the data points and the confidence.ellipse ellipse on the plane

plot(data,pch=19,col=rgb(0, 0.5, 1, 0.2),  xlab="x", ylab="y",
         xlim=range(data[,1]), ylim=range(data[,2]))
plot(confidence.ellipse,type="l", xlab="",ylab="",


Also popular now: