The 64257rst step is to de64257ne which relationships between observations are to be given a nonzero weight that is to choose the neighbour criterion to be used the second is to assign weights to the identi64257ed neighbour links The 281 census trac ID: 11169
Download Pdf The PPT/PDF document "Creating Neighbours Roger Bivand Septemb..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
CreatingNeighbours*RogerBivandJune29,20201IntroductionCreatingspatialweightsisanecessarystepinusingarealdata,perhapsjusttocheckthatthereisnoremainingspatialpatterninginresiduals.Therststepistodenewhichrelationshipsbetweenobservationsaretobegivenanon-zeroweight,thatistochoosetheneighbourcriteriontobeused;thesecondistoassignweightstotheidentiedneighbourlinks.The281censustractdatasetforeightcentralNewYorkStatecountiesfeaturedprominentlyinWallerandGotway(2004)willbeusedinmanyoftheexamples,1sup-plementedwithtractboundariesderivedfromTIGER1992anddistributedbySEDAC/CIESIN.Thisleisnotidenticalwiththeboundariesusedintheoriginalsource,butisverycloseandmaybere-distributed,unliketheversionusedinthebook.Startingfromthecensustractqueencontiguities,wherealltouchingpolygonsareneighbours,usedinWallerandGotway(2004)andprovidedasaDBFleontheirwebsite,aGALformatlehasbeencreatedandreadintoR.if(require(rgdal,quietly=TRUE)){+NY8-readOGR(system.file("shapes/NY8_utm18.shp",package="spData"))+}else{+require(maptools,quietly=TRUE)+NY8-readShapeSpatial(system.file("shapes/NY8_utm18.shp",package="spData"))+}OGRdatasourcewithdriver:ESRIShapefileSource:"/home/rsb/lib/r_libs/spData/shapes/NY8_utm18.shp",layer:"NY8_utm18"with281featuresIthas17fieldsကlibrary(spdep)ကNY_nb-read.gal(system.file("weights/NY_nb.gal",package="spData"),+region.id=row.names(NY8))Forthesakeofsimplicityinshowinghowtocreateneighbourobjects,weworkonasubsetofthemapconsistingofthecensustractswithinSyracuse,althoughthesameprinciplesapplytothefulldataset.WeretrievethepartoftheneighbourlistinSyracuseusingthesubsetmethod. *Thisvignetteformedpp.239251ofthersteditionofBivand,R.S.,Pebesma,E.andGómez-RubioV.(2008)AppliedSpatialDataAnalysiswithR,Springer-Verlag,NewYork.Itwasretiredfromthesec-ondedition(2013)toaccommodatematerialonothertopics,andismadeavailableinthisformwiththeunderstandingofthepublishers.1TheboundarieshavebeenprojectedfromgeographicalcoordinatestoUTMzone18.1 Syracuse-NY8[NY8$AREANAME=="Syracusecity",]ကSy0_nb-subset(NY_nb,NY8$AREANAME=="Syracusecity")ကsummary(Sy0_nb)Neighbourlistobject:Numberofregions:63Numberofnonzerolinks:346Percentagenonzeroweights:8.717561Averagenumberoflinks:5.492063Linknumberdistribution:123456789115914179611leastconnectedregion:164with1link1mostconnectedregion:136with9links2CreatingContiguityNeighboursWecancreateacopyofthesameneighboursobjectforpolygoncontiguitiesusingthepoly2nbfunctioninspdep.IttakesanobjectextendingtheSpatialPolygonsclassasitsrstargument,andusingheuristicsidentiespolygonssharingboundarypointsasneighbours.Italsohasasnapargument,toallowthesharedboundarypointstobeashortdistancefromoneanother.ကclass(Syracuse)[1]"SpatialPolygonsDataFrame"attr(,"package")[1]"sp"ကSy1_nb-poly2nb(Syracuse)ကisTRUE(all.equal(Sy0_nb,Sy1_nb,check.attributes=FALSE))[1]TRUEAswecansee,creatingthecontiguityneighboursfromtheSyracuseobjectrepro-ducestheneighboursfromWallerandGotway(2004).CarefulexaminationofFig.??shows,however,thatthegraphofneighboursisnotplanar,sincesomeneighbourlinkscrosseachother.Bydefault,thecontiguityconditionismetwhenatleastonepointontheboundaryofonepolygoniswithinthesnapdistanceofatleastonepointofitsneighbour.Thisrelationshipisgivenbytheargumentqueen=TRUEbyanalogywithmovementsonachessboard.Sowhenthreeormorepolygonsmeetatasinglepoint,theyallmeetthecontiguitycondition,givingrisetocrossedlinks.Ifqueen=FALSE,atleasttwoboundarypointsmustbewithinthesnapdistanceofeachother,withtheconventionalnameofa`rook'relationship.Figure1showsthecrossedlinedifferencesthatarisewhenpolygonstouchonlyatasinglepoint,comparedtothestricterrookcriterion.ကSy2_nb-poly2nb(Syracuse,queen=FALSE)ကisTRUE(all.equal(Sy0_nb,Sy2_nb,check.attributes=FALSE))[1]FALSEIfwehaveaccesstoaGISsuchasGRASSorArcGIS,wecanexporttheSpa-tialPolygonsDataFrameobjectandusethetopologyengineintheGIStondconti-guitiesinthegraphofpolygonedgesasharededgewillyieldthesameoutputastherookrelationship.2 Figure1:(a)Queen-stylecensustractcontiguities,Syracuse;(b)Rook-stylecontiguitydifferencesshownasthickerlinesThisproceduredoes,however,dependonthetopologyofthesetofpolygonsbe-ingclean,whichholdsforthissubset,butnotforthefulleight-countydataset.Notinfrequently,therearesmallartefacts,suchassliverswhereboundarylinesintersectordivergebydistancesthatcannotbeseenonplots,butwhichrequireinterventiontokeepthegeometriesanddatacorrectlyassociated.Whenthesegeometricalartefactsarepresent,thetopologyisnotclean,becauseunambiguoussharedpolygonbound-ariescannotbefoundinallcases;artefactstypicallyarisewhendatacollectedforonepurposearecombinedwithotherdataorusedforanotherpurpose.Topologiesareusu-allycleanedinaGISby`snapping'verticescloserthanathresholddistancetogether,removingartefactsforexample,snappingacrossariverchannelwherethecorrectboundaryisthemedianlinebuttheinputpolygonsstopatthechannelbanksoneachside.Thepoly2nbfunctiondoeshaveasnapargument,whichmayalsobeusedwheninputdatapossessgeometricalartefacts.library(spgrass6)writeVECT6(Syracuse,"SY0")contig-vect2neigh("SY0")ကSy3_nb-sn2listw(contig)$neighboursကisTRUE(all.equal(Sy3_nb,Sy2_nb,check.attributes=FALSE))SimilarapproachesmayalsobeusedtoreadArcGIScoveragedatabytallyingtheleftneighbourandrightneighbourarcindiceswiththepolygonsinthedataset,usingeitherRArcInfoorrgdal.InourSyracusecase,therearenoexclavesor`islands'belongingtothedataset,butnotsharingboundarypointswithinthesnapdistance.Ifthenumberofpolygonsismoderate,themissingneighbourlinksmaybeaddedinteractivelyusingtheeditmethodfornbobjects,anddisplayingthepolygonbackground.Thesamemethodmaybeusedforremovinglinkswhich,althoughcontiguityexists,maybeconsideredvoid,suchasacrossamountainrange.3 Figure2:(a)Delauneytriangulationneighbours;(b)Sphereofinuenceneighbours(ifavailable);(c)Gabrielgraphneighbours;(d)Relativegraphneighbours3CreatingGraph-BasedNeighboursContinuingwithirregularlylocatedarealentities,itispossibletochooseapointtorepresentthepolygon-supportentities.Thisisoftenthepolygoncentroid,whichisnottheaverageofthecoordinatesineachdimension,buttakespropercaretoweightthecomponenttrianglesofthepolygonbyarea.Itisalsopossibletouseotherpoints,orifdataareavailable,construct,forexamplepopulation-weightedcentroids.Oncerepre-sentativepointsareavailable,thecriteriaforneighbourhoodcanbeextendedfromjustcontiguitytoincludegraphmeasures,distancethresholds,andk-nearestneighbours.ThemostdirectgraphrepresentationofneighboursistomakeaDelaunaytriangu-lationofthepoints,shownintherstpanelinFig.2.Theneighbourrelationshipsaredenedbythetriangulation,whichextendsoutwardstotheconvexhullofthepointsandwhichisplanar.Notethatgraph-basedrepresentationsconstructtheinterpointre-lationshipsbasedonEuclideandistance,withnooptiontouseGreatCircledistancesforgeographicalcoordinates.Becauseitjoinsdistantpointsaroundtheconvexhull,itmaybeworthwhiletothinthetriangulationasaSphereofInuence(SOI)graph,removinglinksthatarerelativelylong.PointsareSOIneighboursifcirclescentredonthepoints,ofradiusequaltothepoints'nearestneighbourdistances,intersectintwoplaces(AvisandHorton,1985).2coords-coordinates(Syracuse)ကIDs-row.names(as(Syracuse,"data.frame"))ကSy4_nb-tri2nb(coords,row.names=IDs)ကif(require(rgeos,quietly=TRUE)&&require(RANN,quietly=TRUE)){+Sy5_nb-graph2nb(soi.graph(Sy4_nb,coords),row.names=IDs)+}elseSy5_nb-NULLကSy6_nb-graph2nb(gabrielneigh(coords),row.names=IDs)ကSy7_nb-graph2nb(relativeneigh(coords),row.names=IDs)DelaunaytriangulationneighboursandSOIneighboursaresymmetricbydesignifiisaneighbourofj,thenjisaneighbourofi.TheGabrielgraphisalsoasub-graphoftheDelaunaytriangulation,retainingadifferentsetofneighbours(MatulaandSokal,1980).Itdoesnot,however,guaranteesymmetry;thesameappliestoRelative 2Functionsforgraph-basedneighbourswerekindlycontributedbyNicholasLewin-Koh.4 graphneighbours(Toussaint,1980).Thegraph2nbfunctiontakesasymargumenttoinsertlinkstorestoresymmetry,butthegraphsthennolongerexactlyfulltheirneigh-bourcriteria.Allthegraph-basedneighbourschemesalwaysensurethatallthepointswillhaveatleastoneneighbour.Subgraphsofthefulltriangulationmayalsohavemorethanonegraphaftertrimming.Thefunctionsis.symmetric.nbcanbeusedtocheckforsymmetry,withargumentforce=TRUEifthesymmetryattributeistobeover-ridden,andn.comp.nbreportsthenumberofgraphcomponentsandthecomponentstowhichpointsbelong(afterenforcingsymmetry,becausethealgorithmassumesthatthegraphisnotdirected).Whentherearemorethanonegraphcomponent,thematrixrepresentationofthespatialweightscanbecomeblock-diagonalifobservationsareappropriatelysorted.nb_l-list(Triangulation=Sy4_nb,Gabriel=Sy6_nb,Relative=Sy7_nb)ကif(!is.null(Sy5_nb))nb_l-c(nb_l,list(SOI=Sy5_nb))ကsapply(nb_l,function(x)is.symmetric.nb(x,verbose=FALSE,force=TRUE))TriangulationGabrielRelativeSOITRUEFALSEFALSETRUEကsapply(nb_l,function(x)n.comp.nb(x)$nc)TriangulationGabrielRelativeSOI11114Distance-BasedNeighboursAnalternativemethodistochoosetheknearestpointsasneighboursthisadaptsacrossthestudyarea,takingaccountofdifferencesinthedensitiesofarealentities.Naturally,intheoverwhelmingmajorityofcases,itleadstoasymmetricneighbours,butwillensurethatallareashavekneighbours.Theknearneighreturnsaninterme-diateformconvertedtoannbobjectbyknn2nb;knearneighcanalsotakealonglatargumenttohandlegeographicalcoordinates.ကSy8_nb-knn2nb(knearneigh(coords,k=1),row.names=IDs)ကSy9_nb-knn2nb(knearneigh(coords,k=2),row.names=IDs)ကSy10_nb-knn2nb(knearneigh(coords,k=4),row.names=IDs)ကnb_l-list(k1=Sy8_nb,k2=Sy9_nb,k4=Sy10_nb)ကsapply(nb_l,function(x)is.symmetric.nb(x,verbose=FALSE,force=TRUE))k1k2k4FALSEFALSEFALSEကsapply(nb_l,function(x)n.comp.nb(x)$nc)k1k2k41511Figure3showstheneighbourrelationshipsfork=1;2;4,withmanycomponentsfork=1.Ifneedbe,k-nearestneighbourobjectscanbemadesymmetricalusingthemake.sym.nbfunction.Thek=1objectisalsousefulinndingtheminimumdistanceatwhichallareashaveadistance-basedneighbour.Usingthenbdistsfunction,wecancalculatealistofvectorsofdistancescorrespondingtotheneighbourobject,hereforrstnearestneighbours.Thegreatestvaluewillbetheminimumdistanceneededtomakesurethatalltheareasarelinkedtoatleastoneneighbour.Thednearneighfunctionisusedtondneighbourswithaninterpointdistance,withargumentsd1andd2settingthelowerandupperdistancebounds;itcanalsotakealonglatargumenttohandlegeographicalcoordinates.ကdsts-unlist(nbdists(Sy8_nb,coords))ကsummary(dsts)5 Figure3:(a)k=1neighbours;(b)k=2neighbours;(c)k=4neighboursMin.1stQu.MedianMean3rdQu.Max.395.7587.3700.1760.4906.11544.6max_1nn-max(dsts)ကmax_1nn[1]1544.615ကSy11_nb-dnearneigh(coords,d1=0,d2=0.75*max_1nn,row.names=IDs)ကSy12_nb-dnearneigh(coords,d1=0,d2=1*max_1nn,row.names=IDs)ကSy13_nb-dnearneigh(coords,d1=0,d2=1.5*max_1nn,row.names=IDs)ကnb_l-list(d1=Sy11_nb,d2=Sy12_nb,d3=Sy13_nb)ကsapply(nb_l,function(x)is.symmetric.nb(x,verbose=FALSE,force=TRUE))d1d2d3TRUETRUETRUEကsapply(nb_l,function(x)n.comp.nb(x)$nc)d1d2d3411Figure4showshowthenumbersofdistance-basedneighboursincreasewithmod-erateincreasesindistance.Movingfrom0:75timestheminimumall-includeddis-tance,totheall-includeddistance,and1:5timestheminimumall-includeddistance,thenumbersoflinksgrowrapidly.Thisisamajorproblemwhensomeoftherstnearestneighbourdistancesinastudyareaaremuchlargerthanothers,sincetoavoidno-neighbourarealentities,thedistancecriterionwillneedtobesetsuchthatmanyar-eashavemanyneighbours.Figure5showsthecountsofsizesofsetsofneighboursforthethreedifferentdistancelimits.InSyracuse,thecensustractsareofsimilarareas,butwerewetotrytousethedistance-basedneighbourcriterionontheeight-countystudyarea,thesmallestdistancesecuringatleastoneneighbourforeveryarealentityisover38km.ကdsts0-unlist(nbdists(NY_nb,coordinates(NY8)))ကsummary(dsts0)Min.1stQu.MedianMean3rdQu.Max.82.71505.03378.75865.88954.338438.1Ifthearealentitiesareapproximatelyregularlyspaced,usingdistance-basedneigh-boursisnotnecessarilyaproblem.Providedthatcareistakentohandlethesideeffects6 Figure4:(a)Neighbourswithin1,158m;(b)neighbourswithin1,545m;(c)neigh-bourswithin2,317m Figure5:Distance-basedneighbours:frequenciesofnumbersofneighboursbycensustractofweightingareasoutoftheanalysis,usinglistsofneighbourswithno-neighbourareasisnotnecessarilyaproblemeither,butcertainlyoughttoraisequestions.Dif-ferentdisciplineshandlethedenitionofneighboursintheirownwaysbyconvention;inparticular,itseemsthatecologistsfrequentlyusedistancebands.Ifmanydistancebandsareused,theyapproachthevariogram,althoughtheunderlyingunderstandingofspatialautocorrelationseemstobebycontagionratherthancontinuous.5Higher-OrderNeighboursDistancebandscanbegeneratedbyusingasequenceofd1andd2argumentvaluesforthednearneighfunctionifneededtoconstructaspatialautocorrelogramasunderstood7 inecology.Inotherconventions,correlogramsareconstructedbytakinganinputlistofneighboursastherst-ordersets,andsteppingoutacrossthegraphtosecond-,third-,andhigher-orderneighboursbasedonthenumberoflinkstraversed,butnotpermittingcycles,whichcouldriskmakingianeighbourofiitself(O'SullivanandUnwin,2003,p.203).Thenblagfunctiontakesanexistingneighbourlistandreturnsalistoflists,fromrsttomaxlagorderneighbours.Sy0_nb_lags-nblag(Sy0_nb,maxlag=9)yTable1showshowthewaveofconnectednessinthegraphspreadstothethirdorder,re-cedingtotheeighthorder,anddyingawayattheninthordertherearenotractsninestepsfromeachotherinthisgraph.Boththedistancebandsandthegraphsteporderapproachestospreadingneighbourhoodscanbeusedtoexaminetheshapeofrelationshipintensitiesinspace,likethevariogram,andcanbeusedinattemptingtolookattheeffectsofscale.6GridNeighboursWhenthedataareknowntobearrangedinaregular,rectangulargrid,thecell2nbfunctioncanbeusedtoconstructneighbourlists,includingthoseonatorus.Theseareusefulforsimulations,because,sinceallarealentitieshaveequalnumbersofneigh-bours,andtherearenoedges,thestructureofthegraphisasneutralascanbeachieved.Neighbourscaneitherbeoftyperookorqueen.ကcell2nb(7,7,type="rook",torus=TRUE)Neighbourlistobject:Numberofregions:49Table1:Higher-ordercontiguities:frequenciesofnumbersofneighboursbyorderofneighbourlist rstsecondthirdfourthfthsixthseventheighthninth 0 0000062149631 1000037602 1000004503 5000125204 9200189105 14200327006 17000153007 9610155008 6631341009 111537800010 0115513900011 047712500012 0314168510013 076169100014 04853000015 06331000016 01330000017 00020000018 00100000019 00110000020 00110000021 00300000022 00100000023 00000000024 001000000 8 Numberofnonzerolinks:196Percentagenonzeroweights:8.163265Averagenumberoflinks:4cell2nb(7,7,type="rook",torus=FALSE)Neighbourlistobject:Numberofregions:49Numberofnonzerolinks:168Percentagenonzeroweights:6.997085Averagenumberoflinks:3.428571Whenaregular,rectangulargridisnotcomplete,thenwecanuseknowledgeofthecellsizestoredinthegridtopologytocreateanappropriatelistofneighbours,usingatightlyboundeddistancecriterion.Neighbourlistsofthiskindarecommonlyfoundinecologicalassays,suchasstudiesofspeciesrichnessatanationalorcontinentalscale.Itisalsointhesesettings,withmoderatelylargen,heren=3,103,thattheuseofasparse,listbasedrepresentationshowsitsstrength.Handlinga281281matrixfortheeight-countycensustractsisfeasible,easyfora6363matrixforSyracusecensustracts,butdemandingfora3,1033,103matrix.data(meuse.grid)coordinates(meuse.grid)-c("x","y")ကgridded(meuse.grid)-TRUEကdst-max(slot(slot(meuse.grid,"grid"),"cellsize"))ကmg_nb-dnearneigh(coordinates(meuse.grid),0,dst)ကmg_nbNeighbourlistobject:Numberofregions:3103Numberofnonzerolinks:12022Percentagenonzeroweights:0.1248571Averagenumberoflinks:3.874315ကtable(card(mg_nb))123411331212848ReferencesAvis,D.andHorton,J.(1985).Remarksonthesphereofinuencegraph.InGoodman,J.E.,editor,DiscreteGeometryandConvexity.NewYorkAcademyofSciences,NewYork,pp323327.Matula,D.W.andSokal,R.R.(1980).PropertiesofGabrielgraphsrelevanttoge-ographicvariationresearchandtheclusteringofpointsintheplane.GeographicAnalysis,12:205222.O'Sullivan,D.andUnwin,D.J.(2003).GeographicalInformationAnalysis.Wiley,Hoboken,NJ.Toussaint,G.T.(1980).Therelativeneighborhoodgraphofaniteplanarset.PatternRecognition,12:261268.Waller,L.A.andGotway,C.A.(2004).AppliedSpatialStatisticsforPublicHealthData.JohnWiley&Sons,Hoboken,NJ.9