计算R中的二维置信椭圆面积 [英] calculate area of 2-dimensional confidence ellipse in R
问题描述
所以我将对数转换后的测量数据排列在一个简单的表中:
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?
谢谢
推荐答案
First calculate the ellipse, then determine the lengths of the major and minor axes, and then calculate the area.
这是一个无脑的近似.
Here's a brainless approximation.
首先,您的数据.
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))
然后加载包car
; dataEllipse
可用于使用对数据的二元正态近似来计算椭圆.
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)
调用ellipse
可以沿dataEllipse
绘制的椭圆给出点.
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)
这篇关于计算R中的二维置信椭圆面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!