2017-02-07 7 views
2

cluster에있는 ellipsoidhull 함수에 대한 질문이 있으면 도움을받을 수 있는지 궁금합니다. 나는 2 차원 점들의 연속을 포함하는 최소 타원을 발견하기 위해 그것을 사용하고있다. 예 :R에있는 타원체에 대한 설명 R

library(cluster) 
d <- matrix(c(1,2,3,1,3,2),ncol=2) 
e <- ellipsoidhull(d) 

함수 타원 및 공분산 행렬의 중심을 포함하는 구조를 반환하는 타원 OK를 계산한다.

summary(e) 
## 'ellipsoid' in 2 dimensions: 
## center = (2 2); squared ave.radius d^2 = 2 
## and shape matrix = 
##  [,1] [,2] 
## [1,] 0.66667 0.33333 
## [2,] 0.33333 0.66667 
##  hence, area = 3.6276 

질문

가) 어떻게 주어진 포인트가 타원에 속하는지 확인하기 위해이 데이터를 사용할 수 있습니까?

b)이 데이터를 사용하여 주어진 점에서 타원까지의 거리를 어떻게 계산할 수 있습니까?

답변

1

우리는 다음을 시도 할 수 있습니다

library(cluster) 
d <- matrix(c(1,2,3,1,3,2),ncol=2) 
e <- ellipsoidhull(d) 
eg <- eigen(e$cov) 
axes <- sqrt(eg$values) 
angle <- atan(eg$vectors[1,1]/eg$vectors[2,1]) # angle of major axis with x axis 

# check if the point (xp, yp) belongs to the ellipse with parameters a,b,... with tolerance eps 
belongs.to <- function (xp, yp, a, b, x0, y0, alpha, eps=1e-3) { 
    return(abs((cos(alpha)*(xp-x0)+sin(alpha)*(yp-y0))^2/a^2+(sin(alpha)*(xp-x0)-cos(alpha)*(yp-y0))^2/b^2 - 1) <= eps) 
} 

# check if the point (xp, yp) is inside the ellipse with parameters a,b,... 
is.inside <- function (xp, yp, a, b, x0, y0, alpha) { 
    return((cos(alpha)*(xp-x0)+sin(alpha)*(yp-y0))^2/a^2+(sin(alpha)*(xp-x0)-cos(alpha)*(yp-y0))^2/b^2 <= 1) 
} 

# plot ellipse 
plot(e$loc, xlim=c(0,4), ylim=c(0,4), main = "ellipsoidhull", xlab='x', ylab='y') 
lines(predict(e), col="blue") 
points(rbind(e$loc), col = "red", cex = 3, pch = 13) 

x0 <- e$loc[1] # centroid locations 
y0 <- e$loc[2] 
a <- sqrt(e$d2) * axes[1] # major axis length 
b <- sqrt(e$d2) * axes[2] # minor axis length 

alpha <- angle 
xp <- 3 
yp <- 2.9 
is.inside(xp, yp, a, b, x0, y0, alpha) 
# [1] TRUE 
points(xp, yp, pch=19, col='green') 
xp <- 3 
yp <- 3.1 
is.inside(xp, yp, a, b, x0, y0, alpha) 
# [1] FALSE 
points(xp, yp, pch=19, col='blue') 
xp <- 3 
yp <- 3 
belongs.to(xp, yp, a, b, x0, y0, alpha) 
# [1] TRUE 
points(xp, yp, pch=19, col='pink') 


# distance of a point from the center of the ellipse 
sqrt((xp-x0)^2+(yp-y0)^2) 

enter image description here

+1

우수함! 감사. 당신은 제 질문에 도움을 줄뿐만 아니라 타원의 공분산 표현과 제가 이해하는 표현 사이의 관계를 분명히했습니다. 감사. 나는 내가 거리 문제를 스스로 해결할 수 있다고 생각한다. – user2345448