calculate area of 2-diminsional ellipsoid in R

2019-09-20 07:02发布

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

标签: r area ellipse
2条回答
唯我独甜
2楼-- · 2019-09-20 07:37

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.

查看更多
Luminary・发光体
3楼-- · 2019-09-20 07:39

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)
查看更多
登录 后发表回答