如何计算r中椭圆交集的体积
How to calculate the volume of the intersection of ellipses in r
我想知道如何计算两个椭圆之间的交点,例如如下图所示,versicolor 和 virginca 交汇处的体积:
这是基于此 tutorial:
使用以下 mwe 绘制的
data(iris)
log.ir <- log(iris[, 1:4])
ir.species <- iris[, 5]
ir.pca <- prcomp(log.ir, center = TRUE, scale. = TRUE)
library(ggbiplot)
g <- ggbiplot(ir.pca, obs.scale = 1, var.scale = 1,
groups = ir.species, ellipse = TRUE,
circle = TRUE)
g <- g + scale_color_discrete(name = '')
g <- g + theme(legend.direction = 'horizontal',
legend.position = 'top')
print(g)
我得到椭圆的协方差和中心如下:
setosa.cov <- cov(ir.pca$x[ir.species=="setosa",])
versicolor.cov <- cov(ir.pca$x[ir.species=="versicolor",])
virginica.cov <- cov(ir.pca$x[ir.species=="virginica",])
setosa.centre <- colMeans(ir.pca$x[ir.species=="setosa",])
versicolor.centre <- colMeans(ir.pca$x[ir.species=="versicolor",])
virginica.centre <- colMeans(ir.pca$x[ir.species=="virginica",])
但后来我束手无策:-|
编辑:
按照下面@carl-witthoft的指示,这里有一个使用siar::overlap的例子:
library(siar)
setosa <- ir.pca$x[ir.species=="setosa",]
versicolor <- ir.pca$x[ir.species=="versicolor",]
virginica <- ir.pca$x[ir.species=="virginica",]
overlap.fun <- function(data.1, data.2){
dimensions <- ncol(data.1)
for(i in 1:(dimensions-1)){
overlap.out <- overlap(data.1[,i], data.1[,i+1], data.2[,i], data.2[,i+1], steps = 5)
out$overlap[i] <- overlap.out$overlap
out$area1[i] <- overlap.out$area1
out$area2[i] <- overlap.out$area2
}
return(out)
}
overlap.fun(versicolor, virginica)
returns:
$overlap
[1] 0.01587977 0.48477088 0.08375927
$area1
[1]1.020596 1.04614461 0.08758691
$area2
[1] 1.028594 1.1535106 0.1208483
奇怪的是,当我进行百分比计算时,这些值并不真正对应于 ggbiplot PCA 中的椭圆体:
tmp <- overlap(versicolor[,1], versicolor[,2], virginica[,1], virginica[,2], steps = 5)
virginica.percentage <- round(x=(tmp$overlap/tmp$area2*100), digits = 2)
versicolor.percentage <- round(x=(tmp$overlap/tmp$area1*100), digits = 2)
> virginica.percentage [1] 1.54
> versicolor.percentage[1] 1.56
这比上图 1 中显示的要少得多。
但最好在 .
上打开另一个线程
可能的工具:
spatstat::overlap.owin , geo::geointersect, siar::overlap .
你可能会问——你 应该问 ——“他怎么这么快就得到这些答案的?
为您获取包裹 sos
并输入 ???overlap
我想知道如何计算两个椭圆之间的交点,例如如下图所示,versicolor 和 virginca 交汇处的体积:
data(iris)
log.ir <- log(iris[, 1:4])
ir.species <- iris[, 5]
ir.pca <- prcomp(log.ir, center = TRUE, scale. = TRUE)
library(ggbiplot)
g <- ggbiplot(ir.pca, obs.scale = 1, var.scale = 1,
groups = ir.species, ellipse = TRUE,
circle = TRUE)
g <- g + scale_color_discrete(name = '')
g <- g + theme(legend.direction = 'horizontal',
legend.position = 'top')
print(g)
我得到椭圆的协方差和中心如下:
setosa.cov <- cov(ir.pca$x[ir.species=="setosa",])
versicolor.cov <- cov(ir.pca$x[ir.species=="versicolor",])
virginica.cov <- cov(ir.pca$x[ir.species=="virginica",])
setosa.centre <- colMeans(ir.pca$x[ir.species=="setosa",])
versicolor.centre <- colMeans(ir.pca$x[ir.species=="versicolor",])
virginica.centre <- colMeans(ir.pca$x[ir.species=="virginica",])
但后来我束手无策:-|
编辑: 按照下面@carl-witthoft的指示,这里有一个使用siar::overlap的例子:
library(siar)
setosa <- ir.pca$x[ir.species=="setosa",]
versicolor <- ir.pca$x[ir.species=="versicolor",]
virginica <- ir.pca$x[ir.species=="virginica",]
overlap.fun <- function(data.1, data.2){
dimensions <- ncol(data.1)
for(i in 1:(dimensions-1)){
overlap.out <- overlap(data.1[,i], data.1[,i+1], data.2[,i], data.2[,i+1], steps = 5)
out$overlap[i] <- overlap.out$overlap
out$area1[i] <- overlap.out$area1
out$area2[i] <- overlap.out$area2
}
return(out)
}
overlap.fun(versicolor, virginica)
returns:
$overlap
[1] 0.01587977 0.48477088 0.08375927
$area1
[1]1.020596 1.04614461 0.08758691
$area2
[1] 1.028594 1.1535106 0.1208483
奇怪的是,当我进行百分比计算时,这些值并不真正对应于 ggbiplot PCA 中的椭圆体:
tmp <- overlap(versicolor[,1], versicolor[,2], virginica[,1], virginica[,2], steps = 5)
virginica.percentage <- round(x=(tmp$overlap/tmp$area2*100), digits = 2)
versicolor.percentage <- round(x=(tmp$overlap/tmp$area1*100), digits = 2)
> virginica.percentage [1] 1.54
> versicolor.percentage[1] 1.56
这比上图 1 中显示的要少得多。
但最好在
可能的工具:
spatstat::overlap.owin , geo::geointersect, siar::overlap .
你可能会问——你 应该问 ——“他怎么这么快就得到这些答案的?
为您获取包裹 sos
并输入 ???overlap