# 用STATA做空间计量

How can I calculate Moran's I in Stata?

Note: The commands shown in this page are user-written Stata commands that must be downloaded. To install the package of spatial analysis tools, type findit spatgsa in the command window.

Moran's I is a measure of spatial autocorrelation--how related the values of a variable are based on the locations where they were measured. Using a set of user-written Stata commands, we can calculate Moran's I in Stata. We will be using the spatwmat command to generate a matrix of weights based on the locations in our data and the spatgsa command to calculate Moran's I or other spatial autocorrelation measures.

Let's look at an example. Our dataset, ozone, contains ozone measurements from thirty-two locations in the Los Angeles area aggregated over one month. The dataset includes the station number (station), the latitude and longitude of the station (lat and lon), and the average of the highest eight hour daily averages (av8top). This data, and other spatial datasets, can be downloaded from the University of Illinois's Spatial Analysis Lab. We can look at a summary of our location variables to see the range of locations under consideration.

use http://www.wendangku.net/doc/a2928123102de2bd97058872.html/stat/stata/faq/ozone.dta, clear

summarize lat lon

Variable | Obs Mean Std. Dev. Min Max

-------------+--------------------------------------------------------

lat | 32 34.0146 .2228168 33.6275 34.69012

lon | 32 -117.7078 .5683853 -118.5347 -116.2339

Based on the minimum and maximum values of these variables, we can calculate the greatest Euclidean distance we might measure between two points in our dataset.

display sqrt((34.69012 - 33.6275)^2 + (-116.2339 - -118.5347)^2)

2.5343326

Knowing this maximum distance between two points in our data, we can generate a matrix based on the distances between points. In the spatwmat command, we name the weights matrix to be generated, indicate which of our variables are the x- and y-coordinate variables, and provide a range of distance values that are of interest in the band option. All of the distances are of interest in this example, so we create a band with an upper bound greater than our largest possible distance. If we did not care about distances greater than 2, we could indicate this in the band option.

spatwmat, name(ozoneweights) xcoord(lon) ycoord(lat) band(0 3)

The following matrix has been created:

1. Inverse distance weights matrix ozoneweights

Dimension: 32x32

Distance band: 0 < d <= 3

Friction parameter: 1

Minimum distance: 0.1

1st quartile distance: 0.4

Median distance: 0.6

3rd quartile distance: 1.0

Maximum distance: 2.4

Largest minimum distance: 0.50

Smallest maximum distance: 1.23

As described in the output, the command above generated a matrix with 32 rows and 32 columns because our data includes 32 locations. Each off-diagonal entry [i, j] in the matrix is equal to 1/(distance between point i and point j). Thus, the matrix entries for pairs of points that are close together are higher than for pairs of points that are far apart. If you wish to look at the matrix, you can display it with the matrix list command. With our matrix of weights, we can now calculate Moran's I.

spatgsa av8top, weights(ozoneweights) moran

Measures of global spatial autocorrelation

Weights matrix

--------------------------------------------------------------

Name: ozoneweights

Type: Distance-based (inverse distance)

Distance band: 0.0 < d <= 3.0

Row-standardized: No

--------------------------------------------------------------

Moran's I

--------------------------------------------------------------

Variables | I E(I) sd(I) z p-value*

--------------------+-----------------------------------------

av8top | 0.248 -0.032 0.036 7.679 0.000

--------------------------------------------------------------

*1-tail test

Based on these results, we can reject the null hypothesis that there is zero spatial autocorrelation present in the variable av8top at alpha = .05.

Variations

Binary Matrix: If there exists some threshold distance d such that pairs with distances less than d

are neighbors and pairs with distances greater than d are not, you can create a binary neighbors matrix with the spatwmat command (indicating bin and setting band to have an upper bound of d) and use this weights matrix for calculating Moran's I. We could do this for d = .75:

spatwmat, name(ozoneweights) xcoord(lon) ycoord(lat) band(0 .75) bin

The following matrix has been created:

1. Distance-based binary weights matrix ozoneweights

Dimension: 32x32

Distance band: 0 < d <= .75

Friction parameter: 1

Minimum distance: 0.1

1st quartile distance: 0.4

Median distance: 0.6

3rd quartile distance: 1.0

Maximum distance: 2.4

Largest minimum distance: 0.50

Smallest maximum distance: 1.23

spatgsa av8top, weights(ozoneweights) moran

Measures of global spatial autocorrelation

Weights matrix

--------------------------------------------------------------

Name: ozoneweights

Type: Distance-based (binary)

Distance band: 0.0 < d <= 0.75

Row-standardized: No

--------------------------------------------------------------

Moran's I

--------------------------------------------------------------

Variables | I E(I) sd(I) z p-value*

--------------------+-----------------------------------------

av8top | 0.188 -0.032 0.033 6.762 0.000

--------------------------------------------------------------

*1-tail test

In this example, the binary formulation of distance yields a similar result. We can reject the null hypothesis that there is zero spatial autocorrelation present in the variable av8top at alpha = .05.

Using an existing matrix: If you have calculated a weights matrix according to some other metric than those available in spatwmat and wish to use it in calculating Moran's I, spatwmat allows you to read in a Stata dataset of the required dimensions and format it as a distance matrix that can be used by spatgsa. If altweights.dta is a dataset with 32 columns and 32 rows, it could be converted to a weighted matrix aweights to be used in spatgsa analyzing av8top:

spatwmat using "C:\altweights.dta", name(aweights)

How do I generate a variogram for spatial data in Stata?

When analyzing geospatial data, describing the spatial pattern of a measured variable is of great importance. User written Stata commands allow you to explore such patterns. This page will use the variog and variog2 command. To install this, type findit variog in your command window.

The variog command allows you to calculate and graph a variogram for regularly spaced one-dimensional data. The variog2 command allows you to calculate and graph a variogram for two-dimensional data without constraints on spacing. In both cases, the variogram illustrates how differences in a measured variable Z vary as the distances between the points at which Z is measured increase.

Let's look at an example. Our dataset contains ozone measurements from thirty-two locations in the Los Angeles area aggregated over one month. The dataset includes the station number (station), the latitude and longitude of the station (lat and lon), and the average of the highest eight hour daily averages (av8top). This data, and other spatial datasets, can be downloaded from the GeoDa Center for Geospatial Analysis and Computation.

use http://www.wendangku.net/doc/a2928123102de2bd97058872.html/stat/stata/faq/ozone, clear

clist in 1/5

station av8top lat lon

1. 60 7.225806 34.13583 -117.9236

2. 69 5.899194 34.17611 -118.3153

3. 72

4.052885 33.82361 -118.1875

4. 74 7.181452 34.19944 -118.5347

5. 75

6.076613 34.06694 -11

7.7514

For the sake of an example, let's imagine that instead of specific latitude and longitude locations, the stations are evenly spaced along a single latitude. If we assume the observations are in the order in which the stations appear, we can use the variog command. In the command, we indicate the measured outcome and we will opt for the calculated values to be listed. By default, a plot of the semi-variogram will be generated.

variog av8top, list

+----------------------------------+

| Lag Semi-variance # of pairs |

|----------------------------------|

| 1 2.328506 31 |

| 2 2.615086 30 |

| 3 2.629862 29 |

| 4 2.983584 28 |

| 5 3.415026 27 |

|----------------------------------|

| 6 2.923007 26 |

| 7 4.104437 25 |

| 8 3.378503 24 |

| 9 3.531528 23 |

| 10 4.49281 22 |

|----------------------------------|

| 11 5.22965 21 |

| 12 6.657857 20 |

| 13 6.5462 19 |

| 14 6.126221 18 |

| 15 6.556983 17 |

|----------------------------------|

| 16 6.451519 16 |

+----------------------------------+

Next, let's generate a variogram using the latitude and longitude of the stations. For this, we will use the variog2 command. While the lag distance in variog was assumed to be the distance between each evenly spaced observation, variog2 requires the user to specify the lag distance. Let's look at a summary of our coordinates to get a sense of the distances existing in our data.

summarize lat lon

Variable | Obs Mean Std. Dev. Min Max -------------+--------------------------------------------------------

lat | 32 34.0146 .2228168 33.6275 34.69012

lon | 32 -117.7078 .5683853 -118.5347 -116.2339 Based on this, we can calculate the maximum possible distance we might see in our data.

dis sqrt((33.6275 - 34.69012)^2 + (-118.5347 - -116.2339)^2)

2.5343326

As a starting point, we can choose a lag distance of .1 and we can examine distances up to 12 lags apart. We want to choose a lag distance that yields enough pairs in each lag to generate a variance that we trust. We might aim to have at least 15 pairs in each lag.

variog2 av8top lat lon, width(.1) lags(12) list

+----------------------------------+

| Lag Semi-variance # of pairs |

|----------------------------------|

| 1 4.729442 6 |

| 2 1.8984963 31 |

| 3 1.3789778 41 |

| 4 2.7462469 50 |

| 5 4.3899238 49 |

|----------------------------------|

| 6 4.1974818 43 |

| 7 5.2652506 48 |

| 8 7.3351494 41 |

| 9 6.8823236 36 |

| 10 8.0089961 29 |

|----------------------------------|

| 11 6.6957223 29 |

| 12 7.1360346 23 |

+----------------------------------+

We can see that our first lag contains only 6 pairs. We might increase the size of our lags and look at fewer of them.

variog2 av8top lat lon, width(.15) lags(10) list

+----------------------------------+

| Lag Semi-variance # of pairs |

|----------------------------------|

| 1 1.8485044 21 |

| 2 1.8412199 57 |

| 3 3.1204523 74 |

| 4 4.4411303 68 |

| 5 5.8693088 70 |

|----------------------------------|

| 6 7.0979125 55 |

| 7 7.8960334 44 |

| 8 6.5713557 37 |

| 9 4.0710902 23 |

| 10 3.3176015 16 |

+----------------------------------+

In the output, we can see lag distances up to 10*.15 = 1.5, the number of pairs that are this far apart in the dataset, and the semi-variance. As we can see from the plot, the semi-variance increases until the lag distance exceeds .15*7 = 1.05.