Statistical Analysis
This section covers statistical functions for analyzing spatial patterns and distributions in geographic datasets. These functions help determine point clustering, calculate representative centers, and visualize distribution characteristics.
For geometric operations like buffers and intersections, see Geometric Operations. For clustering algorithms, see Clustering.
Overview
Turf.js provides several statistical analysis functions that examine the spatial characteristics of point datasets:
Statistical Analysis Module Architecture
Center Calculations
The centerMedian() function calculates the geometric median - the point that minimizes the sum of distances to all other points in the dataset.
| Function | Input | Output | Purpose |
|---|---|---|---|
centerMedian | FeatureCollection<any> | Feature<Point> | Finds point requiring least total travel |
Center Median Implementation
Center Median Algorithm Flow
The function uses the Weiszfeld algorithm to iteratively find the spatial median:
- Extracts centroids from input features using
@turf/centroid - Calculates distances using
@turf/distance - Supports weighted calculations via feature properties
- Iterates until tolerance threshold is met (default: 0.001)
Spatial Distribution Analysis
The nearestNeighborAnalysis() function determines whether points are clustered, dispersed, or randomly distributed by analyzing distances between nearest neighbors.
Function Interface
nearestNeighborAnalysis(
dataset: FeatureCollection<any>,
options?: {
studyArea?: Feature<Polygon>;
units?: Units & AreaUnits;
properties?: GeoJsonProperties;
}
): NearestNeighborStudyAreaAnalysis Process
Nearest Neighbor Analysis Process
The analysis follows these steps:
- Uses
@turf/centroidto extract centroids from input features - Finds nearest neighbors using
@turf/nearest-point - Calculates observed mean distance using
@turf/distance - Determines population density from study area using
@turf/area - Computes expected distance and z-score for statistical inference
Directional Distribution Analysis
The standardDeviationalEllipse() function creates an elliptical polygon showing the directional distribution of points, containing approximately one standard deviation (70%) of the dataset.
Function Signature and Dependencies
Standard Deviational Ellipse Dependencies
Ellipse Properties
The returned ellipse contains statistical metadata in its properties:
meanCenterCoordinates: Centroid of the distributionsemiMajorAxis: Length of major axis (standard deviation)semiMinorAxis: Length of minor axis (standard deviation)angle: Rotation angle in degreesnumberOfFeatures: Count of input pointspercentageWithinEllipse: Actual percentage contained
Implementation Flow
Standard Deviational Ellipse Algorithm
Statistical Analysis Data Flow
The statistical analysis modules integrate with core Turf.js infrastructure and share common dependency patterns:
Statistical Analysis Module Dependencies
All statistical analysis functions follow consistent patterns:
- Accept
FeatureCollectioninputs via standardized interfaces - Use
@turf/helpersfor GeoJSON creation and unit conversions - Leverage
@turf/metafor coordinate iteration and traversal - Return standardized GeoJSON outputs with statistical metadata
- Support optional weighting via feature properties