[R] ploting an ellipse keeps giving errors

Charles Annis, P.E. Charles.Annis at StatisticalEngineering.com
Wed Oct 27 11:53:35 CEST 2004


Found it!

Here is the original ellipse function:

----------
From: 	Bishop, Lane[SMTP:lane.bishop at honeywell.com]
Sent: 	Monday, November 13, 2000 4:05 PM
To: 	'Jane_2_Xu at sbphrd.com'; s-news at wubios.wustl.edu
Subject: 	RE: [S] 95% ellipse region

Try this function:

ellipse <- function(loc, cov, confidence = 0.95)
{
  A <- cov
  detA <- A[1, 1] * A[2, 2] - A[1, 2]^2
  dist <- sqrt(qchisq(confidence, 2))
  ylimit <- sqrt(A[2, 2]) * dist
  y <- seq( - ylimit, ylimit, 0.01 * ylimit)
  sqrt.discr <- sqrt(detA/A[2, 2]^2 * (A[2, 2] * dist^2 - y^2))
  sqrt.discr[c(1, length(sqrt.discr))] <- 0
  b <- loc[1] + A[1, 2]/A[2, 2] * y
  x1 <- b - sqrt.discr
  x2 <- b + sqrt.discr
  y <- loc[2] + y
  return(rbind(cbind(x1, y), cbind(rev(x2), rev(y))))
}

Usage example:

x <- 60 + 15 * rnorm( 200 )
y <- 50 + .8 * x + 10 * rnorm( 200 )
plot( x, y )

lines( ellipse( c(mean(x),mean(y)), var(cbind(x,y)), .95 ),
   col=2 )

The ellipse is based on the estimated covaraince matrix, though, so it may
not cover exactly 95% of the actual data.


Charles Annis, P.E.
 
Charles.Annis at StatisticalEngineering.com
phone: 561-352-9699
eFax:  503-217-5849
http://www.StatisticalEngineering.com

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Sun
Sent: Wednesday, October 27, 2004 5:09 AM
To: Patrick Burns
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] ploting an ellipse keeps giving errors

Thanks a lot. I added that line.

library(ellipse)
plot(-2:2, type='n')
shape1 = c (1, 0, 0,1)
dim(shape1) = c(2,2)
center1 = c(0,0)
radius1 = 1
ellipse (center1, shape1, radius1)

But now it said:

Error in ellipse(center, shape, radius) : dim<- : dims [product 4] do not
match the length of object [200]

What is wrong?

I don't understand the shape and radius's meaning so I have no idea of what
object's lenght.

Many thanks!

Sun
----- Original Message ----- 
From: "Patrick Burns" <pburns at pburns.seanet.com>
To: "Sun" <sun at cae.wisc.edu>
Sent: Wednesday, October 27, 2004 3:51 AM
Subject: Re: [R] ploting an ellipse keeps giving errors


> The error message is telling you that it expects a plot to already be
> there.  You can do something like:
>
> plot(-2:2, type='n')
>
> Patrick Burns
>
> Burns Statistics
> patrick at burns-stat.com
> +44 (0)20 8525 0696
> http://www.burns-stat.com
> (home of S Poetry and "A Guide for the Unwilling S User")
>
> Sun wrote:
>
> >library (ellipse)
> >
> >shape1 = c (1, 0, 0,1)
> >dim(shape1) = c(2,2)
> >ellipse (center = c(0,0), shape = shape1, radius = 1)
> >
> >=============================
> >Error in plot.xy(xy.coords(x, y), type = type, col = col, lty = lty, ...)
:
> >        plot.new has not been called yet
> >
> >
> >It is really frustrating. Also what do the shape matrix, radius
correspond to an ellipse function
> >
> >(x-x0)^2/a + (y-y0)^2/b = 1
> >
> >? Please advise!
> >
> >Many thanks,
> >
> >Sun
> > [[alternative HTML version deleted]]
> >
> >______________________________________________
> >R-help at stat.math.ethz.ch mailing list
> >https://stat.ethz.ch/mailman/listinfo/r-help
> >PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
> >
> >
> >
> >
>
>
>

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html




More information about the R-help mailing list