Getting the parameters of a data ellipse produced by the car package in R -


i using dataellipse function car package in r elliptic confidence region data. example:

datapoints_x = c(1,3,5,7,8,6,5,4,9) datapoints_y = c(3,6,8,9,5,8,7,4,8) ellipse = dataellipse(cbind(datapoints_x, datapoints_y), levels=0.95) 

the output 2 vectors x , y corresponding points define ellipse:

head(ellipse) #             x        y # [1,] 12.79906 10.27685 # [2,] 12.74248 10.84304 # [3,] 12.57358 11.34255 # [4,] 12.29492 11.76781 # [5,] 11.91073 12.11238 # [6,] 11.42684 12.37102 

but rather interested in length of ellipsis axes , center. there way without carrying out pca myself?

from ?dataellipse read these functions plotting functions, not functions designed give fitted ellipse. reading source code of dataellipse, becomes clear function used fit ellipse cov.wt stats package. function should able give center , covariance matrix used specify ellipse location , shape:

set.seed(144) x <- rnorm(1000) y <- 3*x + rnorm(1000) (ell.info <- cov.wt(cbind(x, y))) # $cov #          x         y # x 1.022985  3.142274 # y 3.142274 10.705215 #  # $center #           x           y  # -0.09479274 -0.23889445  #  # $n.obs # [1] 1000 

the center of ellipse readily available ell.info$center. directions of axes accessible eigenvectors of covariance matrix (columns of eigen.info$vectors below).

(eigen.info <- eigen(ell.info$cov)) # $values # [1] 11.63560593  0.09259443 #  # $vectors #           [,1]       [,2] # [1,] 0.2839051 -0.9588524 # [2,] 0.9588524  0.2839051 

finally need know length of axes (i'll give length center ellipse, aka radius on axis):

(lengths <- sqrt(eigen.info$values * 2 * qf(.95, 2, length(x)-1))) # [1] 8.3620448 0.7459512 

now can 4 endpoints of axes of ellipse:

ell.info$center + lengths[1] * eigen.info$vectors[,1] #        x        y  # 2.279234 7.779072  ell.info$center - lengths[1] * eigen.info$vectors[,1] #         x         y  # -2.468820 -8.256861  ell.info$center + lengths[2] * eigen.info$vectors[,2] #           x           y  # -0.81004983 -0.02711513  ell.info$center - lengths[2] * eigen.info$vectors[,2] #          x          y  #  0.6204643 -0.4506738  

we can confirm these accurate using dataellipse:

library(car) dataellipse(x, y, levels=0.95) 

enter image description here


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -