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 + eigScl[1, ], ctr - eigScl[1, ]) yMat <- rbind(ctr + eigScl[2, ], ctr - eigScl[2, ]) ellBase <- cbind(sqrt(eigVal)*cos(angles), sqrt(eigVal)*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, ctr, pch=4, col="red", lwd=3)
Many thanks in advance for any help.