I'm hoping to be able to take a 3x3 covariance matrix and turn this into an error ellipsoid but so far I haven't been able to achieve this.
I'm very new to R (in fact turned to it to attempt to solve this problem) - and after finding a working example for a 2x2 matrix I've not managed to modify this for 3x3 successfully - can anyone point me in the right direction as to which changes are required please?
The working code for the 2x2 matrix (found here in another question) is below:
ctr <- c(0, 0)
A <- matrix(c(2.2, 0.4, 0.4, 2.8), nrow=2)
RR <- chol(A)
angles <- seq(0, 2*pi, length.out=200)
ell <- 1 * cbind(cos(angles), sin(angles)) %*% RR
ellCtr <- sweep(ell, 2, ctr, "+")
eigVal <- eigen(A)$values
eigVec <- eigen(A)$vectors
eigScl <- eigVec %*% diag(sqrt(eigVal))
xMat <- rbind(ctr[1] + eigScl[1, ], ctr[1] - eigScl[1, ])
yMat <- rbind(ctr[2] + eigScl[2, ], ctr[2] - eigScl[2, ])
ellBase <- cbind(sqrt(eigVal[1])*cos(angles), sqrt(eigVal[2])*sin(angles))
ellRot <- eigVec %*% t(ellBase)
plot((ellRot+ctr)[1, ], (ellRot+ctr)[2, ], asp=1, type="l", lwd=2)
matlines(xMat, yMat, lty=1, lwd=2, col="green")
points(ctr[1], ctr[2], pch=4, col="red", lwd=3)
Many thanks in advance for any help.
carandrgland use functioncar:::ellipsoid(car doesn't export this function, so you need the:::). It returns anrglmesh object which can be displayed withshade3d(object, options)andwire3d(object, options). A use case is incar:::scatter3d.default- look for the lines followingif (ellipsoid) {and runexample(scatter3d)after loadingcar. – caracal Mar 25 '14 at 08:42