plot Ellipse bounding a percentage of points

后端 未结 1 352
自闭症患者
自闭症患者 2021-01-21 06:05

I am using ellipsoidhull() function to derive an ellipse that bounds all the points in x,y coordinates. I then use point.in.polygon() function to predict if a new set of X,Y co-

1条回答
  •  被撕碎了的回忆
    2021-01-21 06:45

    Presumably you were using cluster::ellipsoidhull. In a different package the car::dataEllipse function calculates a center, shape and radius value and passes to ellipse. For the "presumed Normal" situation, which it seems you might be assuming, the relevant code is:

    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)
    

    Then 'ellipse' calculates its individual points which get passed to lines. The code to do that final calculation is

    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")
    

    So the combination of these two functions works with your data:

    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")
    

    You could obviously save or pass the results of the ellcalc call to any object you wanted

    enter image description here

    0 讨论(0)
提交回复
热议问题