Hello everyone I really need some help here. I made a confidence ellipse using the function ellipse from the package ellipse: ellipse(SD, centre=colMeans(pcsref),t=sqrt((p * (n-1)/(n-p))*qf(0.99, p,n-p)) Now, I want to write a function whom return TRUE or FALSE if a given observation is in the confidence ellipse. But I have no clue how to do it Can anyone help me? Best regards Jessica [[alternative HTML version deleted]]
There are point in polygon functions in some of the spatial packages that would tell you if a point is within a polynomial approximation of the ellipse. But in your case I would take a different approach. Generally confidence ellipses are based on manhalobis distances, so you can just compute the manhalobis distance of the point relative to the mean vector (taking the appropriate covariance matrix into account), then if that distance is above a certain value it is outside the ellipse, if less it is inside. -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Jessica Minkue Sent: Saturday, May 28, 2011 11:45 AM To: r-help at r-project.org Subject: [R] Observation in a confidence ellipse Hello everyone I really need some help here. I made a confidence ellipse using the function ellipse from the package ellipse: ellipse(SD, centre=colMeans(pcsref),t=sqrt((p * (n-1)/(n-p))*qf(0.99, p,n-p)) Now, I want to write a function whom return TRUE or FALSE if a given observation is in the confidence ellipse. But I have no clue how to do it Can anyone help me? Best regards Jessica [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
On 29/05/11 05:45, Jessica Minkue wrote:> Hello everyone > I really need some help here. I made a confidence ellipse using the function ellipse from the package ellipse: > > ellipse(SD, centre=colMeans(pcsref),t=sqrt((p * (n-1)/(n-p))*qf(0.99, p,n-p)) > > > Now, I want to write a function whom return TRUE or FALSE if a given observation is in the confidence ellipse. But I have no clue how to do it > Can anyone help me?First of all, you are probably way off base talking about ***confidence*** ellipses here. If you are testing whether observations are inside the ellipses, then you are most likely interested in ***prediction*** ellipses. It is vital that you understand the difference. But to answer your question: It would be easy enough to do it from scratch. Let your ellipse be defined by (x - mu)' M(x-mu) = c where mu is the ``centre'', x is a 2-vector (x_1,x_2)', M is a positive definite matrix, c is a positive constant, and " ' " denotes ``transpose''. Then a point x = (x_1,x_2)' is inside the ellipse if and only if (x - mu)' M(x-mu) <= c. Coding this up is an easy exercise. If you can't do it, you probably shouldn't be messing with this stuff in the first place. However if you want to use a sledge-hammer to crack a peanut, install the "spatstat" package and then do: require(spatstat) W <- owin(poly=<your ellipse>) inside.owin(x1,x2,W) # Where x1 and x2 are the x and y coordinates of the points you wish to test. cheers, Rolf Turner