“The dispersion of points in one dimension is easy to calculate and to visualize, but the spread of points in two (or more) dimensions is less simple. Instead of familiar error bars, the spread of a set of points on a Cartesian plane can be described using either the standard distance deviation (SDD) or the standard deviational ellipse (SDE).
For the purpose of geographic visualization, the SDD is typically portrayed as a circle with radius SDD centered on the mean center of a set of point observations. The orthogonal dispersion of a set of points can also be described using the standard deviation of the x- and y-coordinates of a set of point observations.
The aspace R library provides functionalities for calculating SDD and SDE.
#install.packages("aspace")
library(aspace)
## Loading required package: splancs
## Loading required package: sp
##
## Spatial Point Pattern Analysis Code in S-Plus
##
## Version 2 - Spatial and Space-Time analysis
## Loading required package: Hmisc
## Loading required package: lattice
## Loading required package: survival
## Loading required package: Formula
## Loading required package: ggplot2
##
## Attaching package: 'Hmisc'
## The following object is masked from 'package:splancs':
##
## zoom
## The following objects are masked from 'package:base':
##
## format.pval, round.POSIXt, trunc.POSIXt, units
## Loading required package: shapefiles
## Loading required package: foreign
##
## Attaching package: 'shapefiles'
## The following objects are masked from 'package:foreign':
##
## read.dbf, write.dbf
## plot sample data
plot(activities, xlab="", ylab="", asp=1, axes=FALSE, main="Activities Data (aspace)", type="n")
## SDD example
calc_sdd(id=1, filename="SDD_Output.txt", centre.xy=NULL, calccentre=TRUE,
weighted=FALSE, weights=NULL, points=activities, verbose=FALSE)
## $id
## [1] 1
##
## $calccentre
## [1] TRUE
##
## $weighted
## [1] FALSE
##
## $CENTRE.x
## [1] 610941.3
##
## $CENTRE.y
## [1] 4830837
##
## $SDD.radius
## [1] 8117.952
##
## $SDD.area
## [1] 207034548
### SDD plot
plot_sdd(plotnew=FALSE, plotcentre=FALSE, centre.col="red", centre.pch="1", sdd.col="red",sdd.lwd=1,titletxt="", plotpoints=TRUE,points.col="black")
# Label the centroid, explicitly using the hidden r.SDD object that was used in plot_sde
text(r.SDD$CENTRE.x, r.SDD$CENTRE.y, "+", col="red")
## plot_sdd by default takes as input the result produced from the calc_sdd, read from the current workspace.
## SDD to shapefile example
shp <- convert.to.shapefile(sddloc,sddatt,"id",5)
write.shapefile(shp, "SDD_Shape", arcgis=T)
The result of SDE is similar to a contour line that traces the edge of one standard deviation, as on a topographic map or an isochore map. The standard deviational ellipse algorithm implemented is described at http://goo.gl/RvcII3. The SDEs can be calculated by calc_sde and are visualized by plot_sde.
Here is an example of Pamela Toman’s code for SDEs (Toman, 2012):
#
# Create the plot but don't show the markers
plot(activities, xlab="", ylab="", asp=1, axes=FALSE, main="Activities Data (aspace)", type="n")
# Calculate and plot the first standard deviational ellipse on the existing plot
calc_sde(id=1,points=activities);
## $id
## [1] 1
##
## $CALCCENTRE
## [1] TRUE
##
## $weighted
## [1] FALSE
##
## $CENTRE.x
## [1] 610941.3
##
## $CENTRE.y
## [1] 4830837
##
## $Sigma.x
## [1] 3602.822
##
## $Sigma.y
## [1] 10900.55
##
## $Major
## [1] "SigmaY"
##
## $Minor
## [1] "SigmaX"
##
## $Theta
## [1] 52.4399
##
## $Eccentricity
## [1] 0.9437999
##
## $Area.sde
## [1] 123378936
##
## $TanTheta
## [1] 1.300399
##
## $SinTheta
## [1] 0.7927144
##
## $CosTheta
## [1] 0.6095933
##
## $SinThetaCosTheta
## [1] 0.4832333
##
## $Sin2Theta
## [1] 0.6283961
##
## $Cos2Theta
## [1] 0.3716039
##
## $ThetaCorr
## [1] 52.4399
plot_sde(plotnew=FALSE, plotcentre=FALSE, centre.col="red", centre.pch="1", sde.col="red",sde.lwd=1,titletxt="", plotpoints=TRUE,points.col="black")
# Label the centroid, explicitly using the hidden r.SDE object that was used in plot_sde
text(r.SDE$CENTRE.x, r.SDE$CENTRE.y, "+", col="red")
Pamela’s advice for plotting SDEs follows: The above code will plot the data without axes, layering the SDE ellipse on top of a plot that does not display data marker.
The aspace 3.0 calc_sde code (accessible by typing the function name without parentheses at the R prompt) includes the lines:
if (tantheta < 0) {
theta <- 90 - atan_d(abs(tantheta))
} else {
theta <- atan_d(tantheta)
}
This code seems to aim to ensure that theta is a positive number — but the first line doesn’t ensure that. Instead it causes negative rotations to end up at 90 degrees to where they should be. Instead that first if-clause could be:
if (tantheta < 0) {
theta <- 180 - atan_d(abs(tantheta))
} else {
theta <- atan_d(tantheta)
}
This code is one of multiple options that fixes the off-by-90-degrees issue.
The aspace 3.0 calc_sde code only will only trace an ellipse of one standard deviation in each direction. To change this, add a multiplicative factor to sigmax and sigmay immediately before (or immediately after) the following lines:
if (sigmax > sigmay) {
Major <- "SigmaX"
Minor <- "SigmaY"
}
else {
Major <- "SigmaY"
Minor <- "SigmaX"
}
For instance, to calculate (and therefore plot) two standard deviations around the centroid, add in the lines:
sigmax=sigmax*2
sigmay=sigmay*2
These lines double the length of the single-standard-deviation major and minor axes.
Do not forget to replicate this exercise using your own data as well as to test Pamela’s advice for a better visualization of SDEs.
Bui, R., Buliung, R.N. and Remmel, T.K., 2012. A collection of functions for estimating centrographic statistics and computational geometries for spatial point patterns
Oyana, T.J. & Margai, F.M., 2016. Spatial Analysis – Statistics, Visualization, and Computational Methods. CRC Press. 294 pp.
Toman, P., 2012. Calculating and visualizing standard deviations in two dimensions. http://www.pamelatoman.net/blog/tag/standard-deviational-ellipses/