Obtaining standard deviation ellipse geometry in R

128 views Asked by At

I am working with a data frame consisting of points. Take the data below as fake example of the points I observe for individual i:

D <- tibble(trip_long = c(-87.7, -87.6, -87.6, -87.7, -87.7, -87.8, -87.5, -87.6, -87.6, -87.6,
                          -87.7, -87.6, -87.6, -87.6, -87.6, -87.6, -87.6, -87.7, -87.7, -87.6),
            trip_lat = c(41.7, 41.8, 41.7, 41.7, 41.7, 41.7, 41.6, 41.7, 41.8, 41.8, 41.7, 41.7,
                         41.7, 41.7, 41.7, 41.7, 41.8, 41.7, 41.7, 41.7))

I want to calculate the standard deviation ellipse of the points for this individual, for which I am using the aspace library.

library(aspace)
sde <- calc_sde(id = 1, points = D))

sde contains the my standard deviation ellipse:

plot_sde(sde)

I want to extract that ellipse as a geometry column for all of my observations, but I have not managed to figure out how to do it yet.

I am not totally sure if this is a possibility.

1

There are 1 answers

0
Friede On

It is not clear, what you like to achieve. Pleae specify. A wild guess:

suppressPackageStartupMessages({
  library(aspace) 
  library(dplyr)
})

D <- tibble(trip_long = c(-87.7, -87.6, -87.6, -87.7, -87.7, -87.8, -87.5, -87.6, -87.6, -87.6, -87.7, -87.6, -87.6, -87.6, -87.6, -87.6, -87.6, -87.7, -87.7, -87.6), trip_lat = c(41.7, 41.8, 41.7, 41.7, 41.7, 41.7, 41.6, 41.7, 41.8, 41.8, 41.7, 41.7, 41.7, 41.7, 41.7, 41.7, 41.8, 41.7, 41.7, 41.7) )

sde <- calc_sde(id = 1, points = D)
# plot_sde(sde)

I would like to mention that there are only five different data points, see

unique(D) 
#> # A tibble: 5 × 2
#>   trip_long trip_lat
#>       <dbl>    <dbl>
#> 1     -87.7     41.7
#> 2     -87.6     41.8
#> 3     -87.6     41.7
#> 4     -87.8     41.7
#> 5     -87.5     41.6

A routine, how to use the result of calc_sde to extract attributes of the ellipse? Simply look at

sde$ATTRIBUTES
#>   id CALCCENTRE weighted CENTRE.x CENTRE.y    Sigma.x    Sigma.y  Major  Minor
#> 1  1       TRUE    FALSE  -87.635   41.715 0.07109755 0.09747151 SigmaY SigmaX
#>      Theta Eccentricity   Area.sde TanTheta  SinTheta   CosTheta
#> 1 89.28395    0.6840674 0.02177119  80.0125 0.9999219 0.01249707
#>   SinThetaCosTheta Sin2Theta    Cos2Theta ThetaCorr
#> 1        0.0124961 0.9998438 0.0001561768  89.28395

You can use the information to plot a ellipse. Try

ftb <- seq(from = 0L, to = 2L * pi, by = .01)
plot(x =  sde$ATTRIBUTES[, "CENTRE.x"] + sde$ATTRIBUTES[, "Sigma.x"] * cos(ftb) , 
     y =  sde$ATTRIBUTES[, "CENTRE.y"] + sde$ATTRIBUTES[, "Sigma.y"] * sin(ftb), 
     xlab = "", ylab = "")

Created on 2023-10-18 with reprex v2.0.2