[R] ploting an ellipse keeps giving errors
Martin Maechler
maechler at stat.math.ethz.ch
Thu Oct 28 11:18:34 CEST 2004
>>>>> "Sun" == Sun <sun at cae.wisc.edu>
>>>>> on Wed, 27 Oct 2004 04:25:00 -0500 writes:
Sun> Thank you. I found there are two ellipses
Sun> 1.
Sun> R2.0
Sun> library (car)
Sun> 2.
Sun> R1.9 and R2.0
Sun> library (ellipse)
Sun> And they are different! I can't run 1.
Sun> But the 2. is kind of specialized for t-distribution confidence and so on.
Sun> I need to find a general ellipse for an ellipse equation like
Sun> (x-x0)^2/a + (y-y0)^2/b =1
Sun> . Since I used chi-square percentile not t. I am trying to obtain the large
Sun> sample 95% simultaneous confidence ellipse for two population means (say,
Sun> weight and height). The input are the two sample means and their
Sun> covariances.
Sun> Maybe I have to make my own ellipse function.
maybe not.
There is more (than you mentioned above) available:
1) The recommended (i.e. you don't have to install it) package
"cluster" has
a. ellipsoidhull() {which is not what you need directly}
which returns an (S3) object of class 'ellipsoid'
and there's methods for such objects:
predict(<ellipsoid>) computes points you can draw.
Note that
library(cluster)
help(ellipsoidhull)
tells you how an "ellipsoid" object must look like.
(they *are* defined in terms of cov()-matrix , center and "radius^2")
and also has examples.
b. ellipsoidPoints() is the function you can really directly use:
A version of the following example will be in the next
version of cluster:
library(cluster)
library(MASS)
## Robust vs. L.S. covariance matrix
set.seed(143)
x <- rt(200, df=3)
y <- 3*x + rt(200, df=2)
plot(x,y, main="non-normal data (N=200)")
X <- cbind(x,y)
C.ls <- cov(X) ; m.ls <- colMeans(X)
Cxy <- cov.rob(cbind(x,y))
lines(ellipsoidPoints(C.ls, d2 = 2, loc=m.ls), col="green")
lines(ellipsoidPoints(Cxy$cov, d2 = 2, loc=Cxy$center), col="red")
2) The 'sfsmisc' package has a complementary useful
ellipsePoints() function for ellipses ``given by geometry''
the help of which starts with
>> Compute Radially Equispaced Points on Ellipse
>>
>> Description:
>>
>> Compute points on (the boundary of) an ellipse which is given by
>> elementary geometric parameters.
>>
>> Usage:
>>
>> ellipsePoints(a, b, alpha = 0, loc = c(0, 0), n = 201)
>>
>> Arguments:
>>
>> a,b: length of half axes in (x,y) direction.
>>
>> alpha: angle (in degrees) giving the orientation of the ellipse,
>> i.e., the original (x,y)-axis ellipse is rotated by 'angle'.
>>
>> loc: center (LOCation) of the ellipse.
>>
>> n: number of points to generate.
install.packages('sfsmisc')
library(sfsmisc)
example(ellipsePoints)
has a "nice" example of ellipse drawing,
even a movie of a rotating ellipse...
Martin Maechler, ETH Zurich
More information about the R-help
mailing list