📜 ⬆️ ⬇️

R: ellipse package for rendering trust areas

Hello.

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

Perhaps the author of the post about the “Visualization of Gaussians” mephistopheies and the 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 two-dimensional normal distribution

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

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

require(mvtnorm) mu <- c(1,2) sigma <- matrix(c(4,2,2,3), ncol=2) set.seed(100) data <- rmvnorm(n=1000, mean=mu, sigma=sigma) #      


Ellipse 95% confidence area

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

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


Visualization

We display data and ellipse confidence.ellipse points 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])) par(new=TRUE) plot(confidence.ellipse,type="l", xlab="",ylab="", xlim=range(data[,1]),ylim=range(data[,2])) 


Result:

Source: https://habr.com/ru/post/199282/


All Articles