用STATA做空间计量.docx
《用STATA做空间计量.docx》由会员分享,可在线阅读,更多相关《用STATA做空间计量.docx(10页珍藏版)》请在冰点文库上搜索。
![用STATA做空间计量.docx](https://file1.bingdoc.com/fileroot1/2023-6/1/2e1ad8ca-fc19-4131-8ece-a5816250bec1/2e1ad8ca-fc19-4131-8ece-a5816250bec11.gif)
用STATA做空间计量
用STATA做空间计量
HowcanIcalculateMoran'sIinStata?
Note:
Thecommandsshowninthispageareuser-writtenStatacommandsthatmustbedownloaded.Toinstallthepackageofspatialanalysistools,typefinditspatgsainthecommandwindow.
Moran'sIisameasureofspatialautocorrelation--howrelatedthevaluesofavariablearebasedonthelocationswheretheyweremeasured.Usingasetofuser-writtenStatacommands,wecancalculateMoran'sIinStata.WewillbeusingthespatwmatcommandtogenerateamatrixofweightsbasedonthelocationsinourdataandthespatgsacommandtocalculateMoran'sIorotherspatialautocorrelationmeasures.
Let'slookatanexample.Ourdataset,ozone,containsozonemeasurementsfromthirty-twolocationsintheLosAngelesareaaggregatedoveronemonth.Thedatasetincludesthestationnumber(station),thelatitudeandlongitudeofthestation(latandlon),andtheaverageofthehighesteighthourdailyaverages(av8top).Thisdata,andotherspatialdatasets,canbedownloadedfromtheUniversityofIllinois'sSpatialAnalysisLab.Wecanlookatasummaryofourlocationvariablestoseetherangeoflocationsunderconsideration.
usehttp:
//www.ats.ucla.edu/stat/stata/faq/ozone.dta,clear
summarizelatlon
Variable|ObsMeanStd.Dev.MinMax
-------------+--------------------------------------------------------
lat|3234.0146.222816833.627534.69012
lon|32-117.7078.5683853-118.5347-116.2339
Basedontheminimumandmaximumvaluesofthesevariables,wecancalculatethegreatestEuclideandistancewemightmeasurebetweentwopointsinourdataset.
displaysqrt((34.69012-33.6275)^2+(-116.2339--118.5347)^2)
2.5343326
Knowingthismaximumdistancebetweentwopointsinourdata,wecangenerateamatrixbasedonthedistancesbetweenpoints.Inthespatwmatcommand,wenametheweightsmatrixtobegenerated,indicatewhichofourvariablesarethex-andy-coordinatevariables,andprovidearangeofdistancevaluesthatareofinterestinthebandoption.Allofthedistancesareofinterestinthisexample,sowecreateabandwithanupperboundgreaterthanourlargestpossibledistance.Ifwedidnotcareaboutdistancesgreaterthan2,wecouldindicatethisinthebandoption.
spatwmat,name(ozoneweights)xcoord(lon)ycoord(lat)band(03)
Thefollowingmatrixhasbeencreated:
1.Inversedistanceweightsmatrixozoneweights
Dimension:
32x32
Distanceband:
0Frictionparameter:
1
Minimumdistance:
0.1
1stquartiledistance:
0.4
Mediandistance:
0.6
3rdquartiledistance:
1.0
Maximumdistance:
2.4
Largestminimumdistance:
0.50
Smallestmaximumdistance:
1.23
Asdescribedintheoutput,thecommandabovegeneratedamatrixwith32rowsand32columnsbecauseourdataincludes32locations.Eachoff-diagonalentry[i,j]inthematrixisequalto1/(distancebetweenpointiandpointj).Thus,thematrixentriesforpairsofpointsthatareclosetogetherarehigherthanforpairsofpointsthatarefarapart.Ifyouwishtolookatthematrix,youcandisplayitwiththematrixlistcommand.Withourmatrixofweights,wecannowcalculateMoran'sI.
spatgsaav8top,weights(ozoneweights)moran
Measuresofglobalspatialautocorrelation
Weightsmatrix
--------------------------------------------------------------
Name:
ozoneweights
Type:
Distance-based(inversedistance)
Distanceband:
0.0Row-standardized:
No
--------------------------------------------------------------
Moran'sI
--------------------------------------------------------------
Variables|IE(I)sd(I)zp-value*
--------------------+-----------------------------------------
av8top|0.248-0.0320.0367.6790.000
--------------------------------------------------------------
*1-tailtest
Basedontheseresults,wecanrejectthenullhypothesisthatthereiszerospatialautocorrelationpresentinthevariableav8topatalpha=.05.
Variations
BinaryMatrix:
Ifthereexistssomethresholddistancedsuchthatpairswithdistanceslessthandareneighborsandpairswithdistancesgreaterthandarenot,youcancreateabinaryneighborsmatrixwiththespatwmatcommand(indicatingbinandsettingbandtohaveanupperboundofd)andusethisweightsmatrixforcalculatingMoran'sI.Wecoulddothisford=.75:
spatwmat,name(ozoneweights)xcoord(lon)ycoord(lat)band(0.75)bin
Thefollowingmatrixhasbeencreated:
1.Distance-basedbinaryweightsmatrixozoneweights
Dimension:
32x32
Distanceband:
0Frictionparameter:
1
Minimumdistance:
0.1
1stquartiledistance:
0.4
Mediandistance:
0.6
3rdquartiledistance:
1.0
Maximumdistance:
2.4
Largestminimumdistance:
0.50
Smallestmaximumdistance:
1.23
spatgsaav8top,weights(ozoneweights)moran
Measuresofglobalspatialautocorrelation
Weightsmatrix
--------------------------------------------------------------
Name:
ozoneweights
Type:
Distance-based(binary)
Distanceband:
0.0Row-standardized:
No
--------------------------------------------------------------
Moran'sI
--------------------------------------------------------------
Variables|IE(I)sd(I)zp-value*
--------------------+-----------------------------------------
av8top|0.188-0.0320.0336.7620.000
--------------------------------------------------------------
*1-tailtest
Inthisexample,thebinaryformulationofdistanceyieldsasimilarresult.Wecanrejectthenullhypothesisthatthereiszerospatialautocorrelationpresentinthevariableav8topatalpha=.05.
Usinganexistingmatrix:
IfyouhavecalculatedaweightsmatrixaccordingtosomeothermetricthanthoseavailableinspatwmatandwishtouseitincalculatingMoran'sI,spatwmatallowsyoutoreadinaStatadatasetoftherequireddimensionsandformatitasadistancematrixthatcanbeusedbyspatgsa.Ifaltweights.dtaisadatasetwith32columnsand32rows,itcouldbeconvertedtoaweightedmatrixaweightstobeusedinspatgsaanalyzingav8top:
spatwmatusing"C:
\altweights.dta",name(aweights)
HowdoIgenerateavariogramforspatialdatainStata?
Whenanalyzinggeospatialdata,describingthespatialpatternofameasuredvariableisofgreatimportance. UserwrittenStatacommandsallowyoutoexploresuchpatterns.Thispagewillusethevariogandvariog2command. Toinstallthis,typefinditvarioginyourcommandwindow.
Thevariogcommandallowsyoutocalculateandgraphavariogramforregularlyspacedone-dimensionaldata. Thevariog2commandallowsyoutocalculateandgraphavariogramfortwo-dimensionaldatawithoutconstraintsonspacing. Inbothcases,thevariogramillustrateshowdifferencesinameasuredvariableZvaryasthedistancesbetweenthepointsatwhichZismeasuredincrease.
Let'slookatanexample. Ourdatasetcontainsozonemeasurementsfromthirty-twolocationsintheLosAngelesareaaggregatedoveronemonth. Thedatasetincludesthestationnumber(station),thelatitudeandlongitudeofthestation(latandlon),andtheaverageofthehighesteighthourdailyaverages(av8top).Thisdata,andotherspatialdatasets,canbedownloadedfromtheGeoDaCenterforGeospatialAnalysisandComputation.
usehttp:
//www.ats.ucla.edu/stat/stata/faq/ozone,clear
clistin1/5
stationav8toplatlon
1.607.22580634.13583-117.9236
2.695.89919434.17611-118.3153
3.724.05288533.82361-118.1875
4.747.18145234.19944-118.5347
5.756.07661334.06694-117.7514
Forthesakeofanexample,let'simaginethatinsteadofspecificlatitudeandlongitudelocations,thestationsareevenlyspacedalongasinglelatitude. Ifweassumetheobservationsareintheorderinwhichthestationsappear,wecanusethevariogcommand. Inthecommand,weindicatethemeasuredoutcomeandwewilloptforthecalculatedvaluestobelisted. Bydefault,aplotofthesemi-variogramwillbegenerated.
variogav8top,list
+----------------------------------+
|LagSemi-variance#ofpairs|
|----------------------------------|
|12.32850631|
|22.61508630|
|32.62986229|
|42.98358428|
|53.41502627|
|----------------------------------|
|62.92300726|
|74.10443725|
|83.37850324|
|93.53152823|
|104.4928122|
|----------------------------------|
|115.2296521|
|126.65785720|
|136.546219|
|146.12622118|
|156.55698317|
|----------------------------------|
|166.45151916|
+----------------------------------+
Next,let'sgenerateavariogramusingthelatitudeandlongitudeofthestations. Forthis,wewillusethevariog2command. Whilethelagdistanceinvariogwasassumedtobethedistancebetweeneachevenlyspacedobservation,variog2requirestheusertospecifythelagdistance.Let'slookatasummaryofourcoordinatestogetasenseofthedistancesexistinginourdata.
summarizelatlon
Variable|ObsMeanStd.Dev.MinMax
-------------+--------------------------------------------------------
lat|3234.0146.222816833.627534.69012
lon|32-117.7078.5683853-118.5347-116.2339
Basedonthis,wecancalculatethemaximumpossibledistancewemightseeinourdata.
dissqrt((33.6275-34.69012)^2+(-118.5347--116.2339)^2)
2.5343326
Asastartingpoint,wecanchoosealagdistanceof.1andwecanexaminedistancesupto12lagsapart.Wewanttochoosealagdistancethatyieldsenoughpairsineachlagtogenerateavariancethatwetrust.Wemightaimtohaveatleast15pairsineachlag.
variog2av8toplatlon,width(.1)lags(12)list
+----------------------------------+
|LagSemi-variance#ofpairs|
|----------------------------------|
|14.7294426|
|21.8984