# Average Nearest Neighbor (Spatial Statistics)

## Summary

Calculates a nearest neighbor index based on the average distance from each feature to its nearest neighboring feature. Results are accessible from the Results window.

## Illustration ## Usage

• The Average Nearest Neighbor tool returns five values: Observed Mean Distance, Expected Mean Distance, Nearest Neighbor Index, z-score, and p-value. These values are accessible from the Results window and are also passed as derived output values for potential use in models or scripts. Optionally, this tool will create an HTML file with a graphical summary of results. Double-clicking on the HTML entry in the Results window will open the HTML file in the default Internet browser. Right-clicking on the Messages entry in the Results window and selecting View will display the results in a Message dialog box.  Note:

• If this tool is part of a custom model tool, the HTML link will only appear in the Results window if it is set as a model parameter prior to running the tool.
• For best display of HTML graphics, ensure your monitor is set for 96 DPI.

• The z-score and p-value results are measures of statistical significance which tell you whether or not to reject the null hypothesis. For the Average Nearest Neighbor statistic, the null hypothsis states that features are randomly distributed.

• The Nearest Neighbor Index is expressed as the ratio of the Observed Mean Distance to the Expected Mean Distance. The expected distance is the average distance between neighbors in a hypothetical random distribution. If the index is less than 1, the pattern exhibits clustering; if the index is greater than 1, the trend is toward dispersion or competition.

• The average nearest neighbor method is very sensitive to the Area value (small changes in the Area parameter value can result in considerable changes in the results). Consequently, the Average Nearest Neighbor tool is most effective for comparing different features in a fixed study area. Use the Calculate Area tool on the study area polygon as one way to get an Area value for the Area parameter. The picture below is a classic example of how identical feature distributions can be dispersed or clustered depending on the study area specified. • If an Area parameter value is not specified, then the area of the minimum enclosing rectangle around the input features is used. Unlike the extent, a minimum enclosing rectangle will not necessarily align with the x- and y-axes.

• There are special cases of input features that would result in invalid (zero-area) minimum enclosing rectangles. In these cases, a small value derived from the input feature XY tolerance will be used to create the minimum enclosing rectangle. For example, if all features are coincident (i.e., all have the exact same X and Y coordinates), the area for a very small square polygon around the single location will be used in calculations. Another example would be if all features align perfectly (e.g., 3 points in a straight line); in this case the area of a rectangle polygon with a very small width around the features will be used in computations. It is always best to supply an Area value when using the Average Nearest Neighbor tool.

• Although this tool will work with polygon or line data, it is most appropriate for event, incident, or other fixed-point feature data. For line and polygon features, the true geometric centroid for each feature is used in computations. For multipoint, polyline, or polygons with multiple parts, the centroid is computed using the weighted mean center of all feature parts. The weighting for point features is 1, for line features is length, and for polygon features is area.

• Calculations based on either Euclidean or Manhattan distance require projected data to accurately measure distances.

• Map layers can be used to define the Input Feature Class. When using a layer with a selection, only the selected features are included in the analysis.

• Caution:

When using shapefiles, keep in mind that they cannot store null values. Tools or other procedures that create shapefiles from non-shapefile inputs may store or interpret null values as zero. This can lead to unexpected results. See also Geoprocessing considerations for shapefile output.

## Syntax

AverageNearestNeighbor_stats (Input_Feature_Class, Distance_Method, Generate_Report, {Area})
 Parameter Explanation Data Type Input_Feature_Class The feature class, typically a point feature class, for which the average nearest neighbor distance will be calculated. Feature Layer Distance_Method Specifies how distances are calculated from each feature to neighboring features. EUCLIDEAN_DISTANCE —The straight-line distance between two points (as the crow flies) MANHATTAN_DISTANCE —The distance between two points measured along axes at right angles (city block); calculated by summing the (absolute) difference between the x- and y-coordinates String Generate_Report NO_REPORT —No graphical summary will be created (default). GENERATE_REPORT —A graphical summary will be created as an HTML file. Boolean Area(Optional) A numeric value representing the study area size. The default value is the area of the minimum enclosing rectangle that would encompass all features (or all selected features). Units should match those for the Output Coordinate System. Double

## Code Sample

AverageNearestNeighbor Example (Python Window)

The following Python Window script demonstrates how to use the AverageNearestNeighbor tool.

```import arcpy
arcpy.env.workspace = r"C:\data"
arcpy.AverageNearestNeighbor_stats("burglaries.shp", "EUCLIDEAN_DISTANCE", "NO_REPORT", "#")```
AverageNearestNeighbor Example (Stand-alone Python script)

The following stand-alone python script demonstrates how to use the AverageNearestNeighbor tool.

```# Analyze crime data to determine if spatial patterns are statistically significant

# Import system modules
import arcpy

# Local variables...
workspace = "C:/data"
crime_data = "burglaries.shp"

try:
# Set the current workspace (to avoid having to specify the full path to the feature classes each time)
arcpy.env.workspace = workspace

# Obtain Nearest Neighbor Ratio and z-score
# Process: Average Nearest Neighbor...
nn_output = arcpy.AverageNearestNeighbor_stats(crime_data, "EUCLIDEAN_DISTANCE", "NO_REPORT", "#")

# Create list of Average Nearest Neighbor output values by splitting the result object
nn_values = nn_output.split(";")
print "The nearest neighbor index is: " + nn_values
print "The z-score of the nearest neighbor index is: " + nn_values
print "The p-value of the nearest neighbor index is: " + nn_values
print "The expected mean distance is: " + nn_values
print "The observed mean distance is: " + nn_values
print "The path of the HTML report: " + nn_values

except:
# If an error occurred when running the tool, print out the error message.
print arcpy.GetMessages()```

## Environments

Output Coordinate System

Feature geometry is projected to the Output Coordinate System prior to analysis. All mathematical computations are based on the Output Coordinate System spatial reference.

ArcView: Yes
ArcEditor: Yes
ArcInfo: Yes

3/7/2012