[R] Adding axis to an ellipse: "ellipse" package

Rolf Turner rolf.turner at xtra.co.nz
Sun Oct 2 04:42:51 CEST 2011


See comments in-line:

On 01/10/11 23:26, Antoine wrote:
> Dear Rolf,
>
> I tryed to follow your advices but the results I am getting seems still
> strange to me. See below an example of a matrix:
>
> datamat<- matrix(c(2.2, 0.4, 0.4, 2.8), 2, 2)
> plot(ellipse(datamat),type='l')
> eigenval<- eigen(datamat)$values
> eigenvect<- eigen(datamat)$vectors
> eigenscl<- eigenvect * sqrt(eigenval) * (qchisq(0.95,2))# One solution to
> get rescale

     This is wrong because you are multiplying the i-th row of ``eigenvect''
     the square root of the i-th eigenvalue.  The *columns* of ``eigenvect''
     are the eigenvectors.  So you need to multiply the j-th column by
     the square root of the j-th eigenvalue.
> v1<- (eigenvect[,1])*(sqrt(eigenval[1]))*(qchisq(0.95,2))#or directly
> rescale the vectors needed
> v2<- (eigenvect[,2])*(sqrt(eigenval[2]))*(qchisq(0.95,2))

     The foregoing is correct except that you need to take the square
     root of the chi-squared quantile.
> #Or
> v1<- eigenscl[1,]
> v2<- eigenscl[2,]
> segments(-v1[1],-v1[2],v1[1],v1[2])
> segments(-v2[1],-v2[2],v2[1],v2[2])
>
> The vectors don't seem to be scaled properly and I don't see what I am doing
> wrong. Any ideas?

Here is correct code:

require(ellipse)
S <- matrix(c(2.2, 0.4, 0.4, 2.8), 2, 2)
# Note the ``asp=1'' which makes orthogonal lines
# look orthogonal:
plot(ellipse(S),type='l',asp=1)
E <- eigen(S)
Val <- E$values
Vec <- E$vectors
v1 <- sqrt(Val[1]*qchisq(0.95,2))*Vec[,1]
v2 <- sqrt(Val[2]*qchisq(0.95,2))*Vec[,2]
segments(-v1[1],-v1[2],v1[1],v1[2])
segments(-v2[1],-v2[2],v2[1],v2[2])

     cheers,

         Rolf Turner



More information about the R-help mailing list