calculate area of 2-dimensional confidence ellipse in R
So I have log-transformed measurement data arranged in a simple table开发者_Python百科:
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
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)
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.
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
精彩评论