想必您正在使用cluster::ellipsoidhull
。在不同的包中car::dataEllipse
函数计算中心、形状和半径值并传递给ellipse
。对于您可能假设的“假定正常”情况,相关代码是:
library(car)
dataEllipse
function(x,y, ....
...
else {
shape <- var(cbind(x, y))
center <- c(mean(x), mean(y))
}
for (level in levels) {
radius <- sqrt(dfn * qf(level, dfn, dfd)
然后“椭圆”计算其各个点,并将其传递给线。进行最终计算的代码是
ellipse <-
function (center, shape, radius, ....)
....
angles <- (0:segments) * 2 * pi/segments
unit.circle <- cbind(cos(angles), sin(angles))
ellipse <- t(center + radius * t(unit.circle %*% chol(shape)))
colnames(ellipse) <- c("x", "y")
因此,这两个函数的组合适用于您的数据:
getEparams <-function(x,y, level) { dfn <- 2
dfd <- length(x) - 1
shape <- var(cbind(x, y))
center <- c(mean(x), mean(y))
radius <- sqrt(dfn * qf(level, dfn, dfd))
return(list(center=center, shape=shape, radius=radius) ) }
ellcalc <- function (center, shape, radius, segments=51){segments=segments
angles <- (0:segments) * 2 * pi/segments
unit.circle <- cbind(cos(angles), sin(angles))
ellipse <- t(center + radius * t(unit.circle %*% chol(shape)))
colnames(ellipse) <- c("x", "y")
return(ellipse)}
evals <- getEparams(Query$X, Query$Y, 0.80)
plot(ellcalc(evals[["center"]], evals[["shape"]], evals[["radius"]]))
title(main='Output of plot(ellcalc(evals[["center"]], evals[["shape"]],
evals[["radius"]]))\nStackOverflow Demonstration')
points(Query$X, Query$Y, cex=0.3, col="red")
显然,您可以保存或传递结果ellcalc
调用您想要的任何对象
![enter image description here](https://i.stack.imgur.com/KTfHJ.png)