R language Computing Moran ' I
Install.packages ("Maptools")#A package that draws a mapInstall.packages ("SPDEP")#Spatial statistics, Moran ' IInstall.packages ("Tripack") Install.packages ("Rann") Library ("Maptools") Library ("SPDEP") Library ("Tripack") Library ("Rann")#Read spatial dataRdata <-Readshapepoly ("d:/data/cairo.shp")#reading SHP polygon DataNames (Rdata)#Show data field nameHead ([email protected])#Show property sheet#Paint by field twoSpplot (Rdata[c ("tfr96_03","tfr86_03")], main="Spatial distribute of tfr96_03",#Picture nameXlab ="X Coords",#Horizontal NameYlab ="Y Coords",#Ordinate nameCut = 30#Segmented )#Draw by FieldSpplot (rdata["tfr96_03"], main="Spatial distribute of tfr96_03",#Picture nameXlab ="X Coords",#Horizontal NameYlab ="Y Coords",#Ordinate nameCut = 30#Segmented )#neighbor Generation by Edge adjacency (mode one)QUEEN_NB <-poly2nb (rdata, Queen = TRUE)#There are 8 ofROOK_NB <-poly2nb (rdata, Queen = FALSE)#There are 4 of#Get center point coordinate numberCoords <-coordinates (RDATA)#map data to data frame-Generate IDIDs <-row.names (As.data.frame (rdata))#Set Drawing ParametersOopar <-par (Mfrow = C (), Mar= C (3,3,1,1) +0.1)#Draw BorderPlot (rdata, border ="Grey", main ="Queen-style")#Add to modify the previous diagramPlot (QUEEN_NB, coords, col ="DodgerBlue", add= TRUE, pch = 0.5, CeX =)#Generate adjacency Relationship (Mode II) Knearneigh () definition k-nearK4_NB<-knn2nb (Knearneigh (coords, k = 4), Row.names =IDs)
#To identify the symmetry of the adjacency relationship is. SYMMETRIC.NB (k4_nb, verbose = FALSE, force =TRUE)#complement adjacency RelationshipN.COMP.NB (K4_NB) $nc k4_w <-NB2LISTW (K4_NB)#calculate Moran ' IMoran.test (rdata$tfr96_03, LISTW =k4_w)#calculate Moran ' I (Monte Carlo method)MORAN.MC (rdata$tfr96_03, LISTW = k4_w, Nsim = 999)
R language Computing Moran ' I