将95%CI的椭圆拟合到点并计算特定线段内的面积

问题描述 投票:0回答:1

我有一组点

  • x 在 [-1, 1] 中,
  • y 介于 [-1, 1] 之间,且
  • abs(x)+abs(y)=1之间的最大和。

我想拟合一个省略号 (95% CI),然后计算位于

A[(0,0);(1,0);(0.5,0.5)]
B[(0,0);(0,1);(0.5,0.5)]
段内的省略号的面积。我怎样才能实现这个?

可重现的示例

library(dplyr)
# Set seed for reproducibility
set.seed(30)
#Generate  points
n_points <- 1000
x <- runif(n=n_points, -1, 1)
y <- runif(n=n_points, -1, 1)
df<-cbind(x,y)%>%as.data.frame
df%>%
  mutate(sum=abs(x)+abs(x),
         ratio_x=x/abs(y),
         ratio_y=y/abs(x))%>%
  #Create skewed data
  filter(sum<=1,
         ratio_x>0,
         ratio_y<1)->df
# Visualize
plot(x~y,data=df)
#
# Fit an ellipse
df %>%
  select(x,y) %>%
  as.matrix -> matDat
#
matCovLS <- cov(matDat)
vecMeans <- colMeans(matDat)
vecMeans <- colMeans(matDat)
### get 95% CI ellipse
d2.95 <- qchisq(0.95, df = 2)
cluster::ellipsoidPoints(matCovLS, d2.95, loc = vecMeans) -> matEllipseHull95
plot(matDat, asp = 1, xlim = c(-1, 1))
lines(matEllipseHull95, col="blue")
r geometry intersection ellipse
1个回答
0
投票

你在找吗

library(sf)
elli <- st_cast(
  st_multipoint(matEllipseHull95), "POLYGON")
tri <- st_cast(st_multilinestring(
    list(matrix(c(0,0,1,0,0,1,0,0) ,,2 , byrow = TRUE))), "POLYGON")
plot(tri)
plot(elli, add = TRUE)
plot(st_intersection(st_union(elli), st_union(tri)), add = TRUE, col = 'red')

这给出了

st_area(st_intersection(st_union(elli), st_union(tri)))
[1] 0.2001515
© www.soinside.com 2019 - 2024. All rights reserved.