0

So I have log-transformed measurement data arranged in a simple table:

      x         y
1.158362492 1.322219295
1.1430148   1.267171728
1.11058971  1.252853031
1.120573931 1.260071388
1.149219113 1.278753601
1.123851641 1.276461804
1.096910013 1.222716471

I know there are functions for plotting a confidence ellipse for these data, but how to I calculate the area of the generated shape?

Thanks

tjebo
  • 21,977
  • 7
  • 58
  • 94
Nick Crouch
  • 301
  • 3
  • 14

3 Answers3

5

First calculate the ellipse, then determine the lengths of the major and minor axes, and then calculate the area.

Here's a brainless approximation.

First, your data.

dat <- structure(list(x = c(1.158362492, 1.1430148, 1.11058971, 1.120573931, 
          1.149219113, 1.123851641, 1.096910013), y = c(1.322219295, 1.267171728, 
          1.252853031, 1.260071388, 1.278753601, 1.276461804, 1.222716471
          )), .Names = c("x", "y"), class = "data.frame", row.names = c(NA, 
          -7L))

Then load the package car; dataEllipse can be used to calculate an ellipse using a bivariate normal approximation to the data.

require(car)
dataEllipse(dat$x, dat$y, levels=0.5)

A call to ellipse can give points along the ellipse that dataEllipse plots.

me <- apply(dat, 2, mean)
v <- var(dat)
rad <- sqrt(2*qf(0.5, 2, nrow(dat)-1))
z <- ellipse(me, v, rad, segments=1001)

We can then calculate the distance from each point on the ellipse to the center.

dist2center <- sqrt(rowSums((t(t(z)-me))^2))

The minimum and maximum of these distances are the half-lengths of the minor and major axes. So we can get the area as follows.

pi*min(dist2center)*max(dist2center)
Karl
  • 2,009
  • 15
  • 14
  • "Calculate the ellipse" needs a bit of elaboration. – Benjamin Sep 25 '11 at 03:19
  • @Benjamin - Perhaps I was being overly facetious, but it seemed that the questioner knew how to calculate the ellipse, at which the area wouldn't be difficult, unless there was difficulty making sense of things, in which case he should have asked about that part. – Karl Sep 25 '11 at 15:27
  • very nice. The link to "calculate the area" is unfortunately broken. BTW Thanks for your extremely helpful blog on making R packages. – tjebo Feb 27 '20 at 11:15
0

The area can be directly calculated from the covariance matrix by calculating the eigenvalues first.

You need to scale the variances / eigenvalues by the factor of confidence that you want to get.

This thread is very helpful

cov_dat <- cov(dat) # covariance matrix

eig_dat <- eigen(cov(dat))$values #eigenvalues of covariance matrix

vec <- sqrt(5.991* eig_dat) # half the length of major and minor axis for the 95% confidence ellipse

pi * vec[1] * vec[2]  
#> [1] 0.005796157

Created on 2020-02-27 by the reprex package (v0.3.0)

dat from user Karl's answer

tjebo
  • 21,977
  • 7
  • 58
  • 94
0

You can use package mclust, there is a hidden function called mvn_plot, the input parameters are mean and std. You may try to read its code and modify it to get the length of each axis.

Alpha
  • 807
  • 1
  • 10
  • 14
Ming
  • 1