r - How to determine whether a points lies in an ellipse -


i had posted similar question earlier. trying determine whether point lies within ellipse. generate bivariate normal data , create ellipse. heres code use

 library(mass)  set.seed(1234)  x1<-null  x2<-null  k<-1  sigma2 <- matrix(c(.72,.57,.57,.46),2,2)  sigma2  rho <- sigma2[1,2]/sqrt(sigma2[1,1]*sigma2[2,2])   eta<-replicate(300,mvrnorm(k, mu=c(-2.503,-1.632), sigma2))    p1<-exp(eta)/(1+exp(eta))  n<-60  x1<-replicate(300,rbinom(k,n,p1[,1]))  x2<-replicate(300,rbinom(k,n,p1[,2]))   rate1<-x1/60  rate2<-x2/60   library(car)  dataellipse(rate1,rate2,levels=c(0.05, 0.95))  

i need find out whether pair (p1[,1],p1[,2]) lies within area of ellipse above.

dataellipse returns ellipses polygons, use point.in.polygon function sp library check whether points inside ellipse:

ell = dataellipse(rate1, rate2, levels=c(0.05, 0.95))  point.in.polygon(rate1, rate2, ell$`0.95`[,1], ell$`0.95`[,2]) 

when run following code...

library(mass) set.seed(1234) x1<-null x2<-null k<-1 sigma2 <- matrix(c(.72,.57,.57,.46),2,2) sigma2 rho <- sigma2[1,2]/sqrt(sigma2[1,1]*sigma2[2,2]) eta<-replicate(300,mvrnorm(k, mu=c(-2.503,-1.632), sigma2)) p1<-exp(eta)/(1+exp(eta)) n<-60 x1<-replicate(300,rbinom(k,n,p1[,1])) x2<-replicate(300,rbinom(k,n,p1[,2])) rate1<-x1/60 rate2<-x2/60 library(car) ell = dataellipse(rate1, rate2, levels=c(0.05, 0.95)) library(sp) point.in.polygon(rate1, rate2, ell$`0.95`[,1], ell$`0.95`[,2]) 

... following output

  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1  [56] 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [111] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [166] 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 [221] 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [276] 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -