How to determine if a series of points is in a three-dimensional ellipsoid using R?

I generate 3 sets of binomial data where the previous one is a multidimensional normal distribution with some average value and variance using the following code in R. What do I need to determine if the true values โ€‹โ€‹of p (p1, p2, p3) fall into the three-dimensional ellipsoid space created by the estimates p (rate1, rate2, rate3). I made a similar problem for two-dimensional, and I used the point.in.polygon function as a function. Is there something similar for both trivariant and 4-variables? I edited my code, how I was able to build an ellipsoid using 3 sets of generated data.

library(MASS)

  set.seed(1234)

  m<-300

  x<-NULL

  k<-20

  Sigma2 <- matrix(c(.58,.49,.37,.49,.58,.38,.37,.38,.34),3,3)

  eta<-mvrnorm(k, mu=c(-0.85,-2.09,-1.90), Sigma2)

  p1<-exp(eta[,1])/(1+exp(eta[,1]))

  p2<-exp(eta[,2])/(1+exp(eta[,2]))

  p3<-exp(eta[,3])/(1+exp(eta[,3]))

  n<-60

  x1<-replicate(m,rbinom(k,n,p1))

  x2<-replicate(m,rbinom(k,n,p2))

  x3<-replicate(m,rbinom(k,n,p3))

  x<-cbind(x1,x2,x3)

  rate<-x/60

  x<-cbind(x1,x2,x3)

 sigma<-var(rate)

 origin<-c(mean(rate[,1]), mean(rate[,2]), mean(rate[,3]))

 #origin<-c(0.349,0.184,0.148)

  coords<-matrix(c(p1,p2,p3),nrow=3)

  A<-sigma

  cFromO<-coords-origin

  Ell<-apply(cFromO,2,function(c){t(c) %*% A %*% c})<=1

  Ell

  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE     TRUE TRUE TRUE TRUE TRUE TRUE TRUE
+4
source share
2 answers

-, elipse, , , , :

. wikipaedia

(x-h) ยฒ/(rx) ยฒ + (y-k) ยฒ/(ry) ยฒ = 1

(x, y) (h, k) rx, ry .

wikipaedia:

(x-v) ^ T A (x-v) = < 1

x - , . v - , . A - - , .

R:

    origin <- c(h=1,k=2,l=3)
    coords <- matrix(c(1,2,3,4,5,6),nrow=3)
    A <- matrix(c(0,1,1,1,0,1,1,1,0),nrow=3)
    cFromO <- coords-origin

:

    apply(cFromO,2,function(c){t(c) %*% A %*% c})<=1

, , roxygen:

    #' @title Is point in an ellispoid?
    #' @description Returns if each point in a matrix of points is in the ellispoid defined by its origin and its matrix of semi-principal axes
    #' @param points A matrix of points with a coordinate per column
    #' @param origin The coordinates of the center of the ellipsoid
    #' @param A The square matrix defining the ellipsoid: one 
    #'       semi-principal axe per line (vectors)
    #' @return A vector of boolean indicating for each given point if it is in the ellispoid

    IsInEllipsoid <- function(points,origin,A){
        cFromO <- points-origin
        isIn <- (diag(cFromO %*% A %*% t(cFromO))<=1)
        return(isIn)
    }

:

    # definition of points
    p1 <- c(1,2,3)
    p2 <- c(4,5,6)
    points <- rbind(p1,p2)

    # definition of the ellipsoid
    origin <- c(h=1,k=2,l=3)
    axe1 <- c(0,0,2)
    axe2 <- c(0,1,0)
    axe3 <- c(0.5,0,0)  
    A <- rbind(axe1,axe2,axe3)

    expect_equal(IsInEllipsoid(points,origin,A),c(p1=TRUE,p2=FALSE))

Edit , , / A chi , . , R:

    # just to make sure everybody get the same draws
    set.seed(1234)

    #-----------------
    # definition of a 95% enveloppe for a 3D multivariate normal
    #-----------------
    # the origin defining your multivariate normal
    origin <- c(0.5,0.5,0.5)

    # the variance covariance matrix defining your multivariate normal
    sigma <- matrix(c(.58,.49,.37,.49,.58,.38,.37,.38,.34),3,3)

    # the precision matrix needed in the calculus 
    sigmaInv <-solve(sigma)

    # the function defining the CV% enveloppe
    ArePointsInCVEllipsoid <- function(points,origin,sigmaInv,CV){
        nDegreeFreedom <- length(dim(sigmaInv)[1])
        cFromO <- points-origin
        areIn <- apply(cFromO,2,function(p){t(p) %*% sigmaInv %*% p <= qchisq(CV,nDegreeFreedom)})
        return(areIn)
    }
    #-----------------------
    # example of use
    #-----------------------
    k<-10000 # number of points to be drawn 

    # random draw in a cube
    toTest <- matrix(runif(k*3,min=-3,max=3),nrow=3)

    # plot the points fitting in the 0.75% confidence region
    library(scatterplot3d)
    toPlot <-t(toTest[,which(ArePointsInCVEllipsoid(toTest,origin,sigmaInv,0.95))])
    scatterplot3d(toPlot)
+3

, :

R - , - R script

, , , M, p , ginv (M)% *% p.

+3

All Articles