SamecodeasinLecture2toopenlibrariessetworkingdirectoryandreadintheHoustonshape28leLoadlibrarieslibrarymaptoolsloadssplibrarytooLoadingrequiredpackagespCheckingrgeosavailabilityTRUElibraryRColorBrewerc ID: 878730
Download Pdf The PPT/PDF document "HoustonCrimeSlipperySlopesLanceAWaller71..." 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.
1 HoustonCrimeSlipperySlopesLanceA.Waller7
HoustonCrimeSlipperySlopesLanceA.Waller7/16/2020HoustonCrimeGeographicallyWeightedRegressionExampleThislereadsindatafromashapeleofHoustoncensustracts(usingthesamecodeinthemarkdownleweusedforLecture2).Nextwewillusespgwrtotgeographicallyweightedregression. SamecodeasinLecture2toopenlibraries,setworkingdirectory,andreadintheHoustonshapele. ##Loadlibrarieslibrary(maptools)#loadssplibrarytoo##Loadingrequiredpackage:sp##Checkingrgeosavailability:TRUE library(RColorBrewer)#createsnicecolorschemeslibrary(classInt)#findsclassintervalsforcontinuousvariableslibrary(spgwr)#Addsthegeographicallyweightedregressionfunctions##Loadingrequiredpackage:spData##Toaccesslargerdatasetsinthispackage,installthespDataLarge##packagewith:`install.packages('spDataLarge',##repos='https://nowosad.github.io/drat/',type='source')`##NOTE:ThispackagedoesnotconstituteapprovalofGWR##asamethodofspatialanalysis;seeexample(gwr) library(rgdal)#Providestoolforreadinginshapefiles##rgdal:v
2 ersion:1.5-12,(SVNrevision1018)##Geospat
ersion:1.5-12,(SVNrevision1018)##GeospatialDataAbstractionLibraryextensionstoRsuccessfullyloaded##LoadedGDALruntime:GDAL2.4.2,released2019/06/28##PathtoGDALsharedfiles:/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/gdal##GDALbinarybuiltwithGEOS:FALSE##LoadedPROJruntime:Rel.5.2.0,September15th,2018,[PJ_VERSION:520]##PathtoPROJsharedfiles:/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/proj##Linkingtospversion:1.4-2##OverwrittenPROJ_LIBwas/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/proj #Setmyworkingdirectory(Lance'shereforexample)setwd("~/OneDrive-EmoryUniversity/meetings/SISMID.2020/SISMID2020.Waller.Rcode")1 ##Readinshapefile-HoustonCensusTractshouston=readOGR(dsn=getwd(),layer="HoustonENAR2012final")##OGRdatasourcewithdriver:ESRIShapefile##Source:"/Users/lwaller/OneDrive-EmoryUniversity/meetings/SISMID.2020/SISMID2020.Waller.Rcode",layer:"HoustonENAR2012final"##with439features##Ithas133fields Nowtoplotthemapoutl
3 ines(samecodeasbefore) ##Mapthetractsusi
ines(samecodeasbefore) ##Mapthetractsusingplot...oldschool#Herearetheoutlinesplot(houston)##Makingchoroplethmaps#Definethevariable(attribute)toshadetractsbypop2000houston@data$POP2000#Definethenumberofclassesnclr5#quintiles#UseRColorBrewertochoosethecolorsplotclrbrewer.pal(nclr,"BuPu")classclassIntervals(pop2000,nclr,style="quantile")colcodefindColours(class,plotclr)#Fillinthetractswiththecolorsplot(houston,col=colcode,add=T)#Addatitletitle(main="Population2000",sub="Quantile(Equal-Frequency)ClassIntervals")#Addalegend(Coordinatesareinlongitude,latitude).legend(-95.7,29.65,legend=names(attr(colcode,"table")),fill=attr(colcode,"palette"),cex=0.6,bty="n")2 Nowsetupthecovariatesandmapthedata(samecodeasLecture2) #Thedatatablehasalotofcensusdataandvarioustransformations#oftheviolentcrime,alcoholsales,anddrugarrestdata.Thenext#sectionpullsthevalueswewant.#Outcome:Numberofviolentcrimesbytractviolence=houston@data$violence_2#Dividebythe2000populationtogettherateviolence.rate=violence/hous
4 ton@data$tot_pop#Covariate1(logstandardi
ton@data$tot_pop#Covariate1(logstandardizedtotalalcoholsales)Z.log.total=houston@data$Zl_total#Covariate2(logstandardizedillegaldrugarrests)Z.log.drug=houston@data$Zl_drug#setupmaps2rowsof2plotseach.#par(mfrow=c(2,2))#PlotOutcomefirstplot(houston)#Definethenumberofclassesnclr5#quintiles#UseRColorBrewertochoosethecolors3 Population 2000 Quantile (Equal-Frequency) Class Intervals [1,2781.6) [2781.6,3837.4) [3837.4,5205.6) [5205.6,6995.4) [6995.4,15411] plotclrbrewer.pal(nclr,"BuPu")classclassIntervals(violence.rate,nclr,style="quantile")colcodefindColours(class,plotclr)#Fillinthetractswiththecolorsplot(houston,col=colcode,add=T)#Addatitletitle(main="ViolenceRate",sub="Quantile(Equal-Frequency)ClassIntervals")#Addalegend(Coordinatesareinlongitude,latitude).legend(-95.7,29.65,legend=names(attr(colcode,"table")),fill=attr(colcode,"palette"),cex=0.6,bty="n") #Nextplotstdizedlogtotalalcoholsalesplot(houston)#Definethenumberofclassesnclr5#quintiles#UseRColorBrewertochoosethecolorsplotclrb
5 rewer.pal(nclr,"BuPu")classclassInterval
rewer.pal(nclr,"BuPu")classclassIntervals(Z.log.total,nclr,style="quantile")colcodefindColours(class,plotclr)#Fillinthetractswiththecolorsplot(houston,col=colcode,add=T)#Addatitletitle(main="Stdlogtotalsales",sub="Quantile(Equal-Frequency)ClassIntervals")#Addalegend(Coordinatesareinlongitude,latitude).legend(-95.7,29.65,legend=names(attr(colcode,"table")),fill=attr(colcode,"palette"),cex=0.6,bty="n")4 Violence Rate Quantile (Equal-Frequency) Class Intervals [0,0.002504642) [0.002504642,0.006596003) [0.006596003,0.01063653) [0.01063653,0.01666243) [0.01666243,13.33333] #Nextplotstdizedlogillegalarrestsplot(houston)#Definethenumberofclassesnclr5#quintiles#UseRColorBrewertochoosethecolorsplotclrbrewer.pal(nclr,"BuPu")classclassIntervals(Z.log.drug,nclr,style="quantile")colcodefindColours(class,plotclr)#Fillinthetractswiththecolorsplot(houston,col=colcode,add=T)#Addatitletitle(main="Stdlogdrugarrests",sub="Quantile(Equal-Frequency)ClassIntervals")#Addalegend(Coordinatesareinlongitude,
6 latitude).legend(-95.7,29.65,legend=name
latitude).legend(-95.7,29.65,legend=names(attr(colcode,"table")),fill=attr(colcode,"palette"),cex=0.6,bty="n")5 Std log total sales Quantile (Equal-Frequency) Class Intervals [-2.39442,-0.6491342) [-0.6491342,-0.1319006) [-0.1319006,0.2098122) [0.2098122,0.6422904) [0.6422904,6.162848] #ThismatchesFigure1inWalleretal.2007 Nowwearereadytotgeographicallyweightedregression(GWR).GWRusesweightingtotlocalregressionvaluestoallowtheslopesassociatedwitheachcovariatetochange(smoothly)overspace.(SeeLecturenotesfordetails).Toestimatetheassociation(s)foralocations,GWRuseskernelweights(similartoourintensityestimationforpointprocessdata)toweightobservations(outcomesandcovariates)nearsmoretogiveanestimateofassociatedwithlocations.Ifyoumoveover,theweightschangeandtheestimatedassociation().Thekernelsmakesurethatifyoudon'tmovefar,theestimatedoesn'tchangemuchsowegetasmoothsurfacefor.Wedothisintwosteps.First,weestimatethebandwidthforthekernelusingcrossvalidation(CV).Next,
7 weusetheestimatedbandwidthtotthePoi
weusetheestimatedbandwidthtotthePoissonGWRmodel. Togetthebandwidth,ggwr.selwilltestthecrossvalidationscoretondaminimum. ###NowtofitPoissonGWR!#Thefunction'ggwr'inthe'spgwr'packageusessyntaxsimilarto'glm'#(likewewoulduseinastandardPoissonregression).#'longlat'tellsthefunctionthatourcoordinatesareinlongitudeand#latitudecoordinates.#'ggwr.sel'selectsthebandwidthforGWRbasedonthedata,themodel,#andcross-validation6 Std log drug arrests Quantile (Equal-Frequency) Class Intervals [-1.83021,-0.8269682) [-0.8269682,-0.2024862) [-0.2024862,0.3081254) [0.3081254,0.765134) [0.765134,4.630079] houston.bw=ggwr.sel(violence~Z.log.total+Z.log.drug+offset(log(pop2000)),data=houston,#coords,adapt=FALSE,gweight=gwr.Gauss,family=poisson,verbose=TRUE,longlat=TRUE,RMSE=FALSE,tol=.Machine$double.eps^0.25)##Bandwidth:31.46106CVscore:1210981##Bandwidth:50.85425CVscore:1225360##Bandwidth:19.47542CVscore:1183007##Bandwidth:12.06788CVscore:1146373##Bandwidth:7.48977CVscore:1114997##Bandwid
8 th:4.660343CVscore:1080648##Bandwidth:2.
th:4.660343CVscore:1080648##Bandwidth:2.91166CVscore:19314054##Bandwidth:5.741088CVscore:1096956##Bandwidth:3.992405CVscore:1068727##Bandwidth:3.579598CVscore:1063343##Bandwidth:3.324468CVscore:1118224##Bandwidth:3.737276CVscore:1064610##Bandwidth:3.453874CVscore:1067964##Bandwidth:3.633023CVscore:1063485##Bandwidth:3.58037CVscore:1063341##Bandwidth:3.592384CVscore:1063330##Bandwidth:3.607907CVscore:1063359##Bandwidth:3.590323CVscore:1063330##Bandwidth:3.59054CVscore:1063330##Bandwidth:3.590497CVscore:1063330##Bandwidth:3.590457CVscore:1063330##Bandwidth:3.590497CVscore:1063330 houston.ggwr=ggwr(violence~Z.log.total+Z.log.drug+offset(log(pop2000)),data=houston,family=poisson,bandwidth=houston.bw,longlat=TRUE) Nowtomaptheresults.Thehouston.ggwrobjectcontainsanelementcalledSDFwhichcontainsthespatialdataframe(SDF)oftheoutcomes.Wewillwanttomapthe(spatiallyvarying)interceptandtheparametersassociatedwitheachofourtwocovariates.Thenotationistrickysincethespatialdataframeiswithinthehouston
9 .ggwrobject. intercepts=houston.ggwr$SDF
.ggwrobject. intercepts=houston.ggwr$SDF@data$X.Interceptalcohol.effects=houston.ggwr$SDF@data$Z.log.totaldrug.effects=houston.ggwr$SDF@data$Z.log.drug7 #Plottheoutlinesfirstplot(houston)#Definethenumberofclassesnclr5#quintiles#UseRColorBrewertochoosethecolorsplotclrbrewer.pal(nclr,"BuPu")classclassIntervals(intercepts,nclr,style="quantile")colcodefindColours(class,plotclr)#Fillinthetractswiththecolorsplot(houston,col=colcode,add=T)#Addatitletitle(main="GGWRIntercepts",sub="Quantile(Equal-Frequency)ClassIntervals")#Addalegend(Coordinatesareinlongitude,latitude).legend(-95.7,29.65,legend=names(attr(colcode,"table")),fill=attr(colcode,"palette"),cex=0.6,bty="n") #Plottheoutlinesfirstplot(houston)#Definethenumberofclassesnclr5#quintiles#UseRColorBrewertochoosethecolorsplotclrbrewer.pal(nclr,"BuPu")classclassIntervals(alcohol.effects,nclr,style="quantile")colcodefindColours(class,plotclr)#Fillinthetractswiththecolorsplot(houston,col=colcode,add=T)#Addatitle8 GGWR Intercepts Quantile (
10 Equal-Frequency) Class Intervals [-6.202
Equal-Frequency) Class Intervals [-6.202037,-4.916804) [-4.916804,-4.806073) [-4.806073,-4.723529) [-4.723529,-4.646341) [-4.646341,-4.465681] title(main="GGWRAlcohol",sub="Quantile(Equal-Frequency)ClassIntervals")#Addalegend(Coordinatesareinlongitude,latitude).legend(-95.7,29.65,legend=names(attr(colcode,"table")),fill=attr(colcode,"palette"),cex=0.6,bty="n") #Plottheoutlinesfirstplot(houston)#Definethenumberofclassesnclr5#quintiles#UseRColorBrewertochoosethecolorsplotclrbrewer.pal(nclr,"BuPu")classclassIntervals(drug.effects,nclr,style="quantile")colcodefindColours(class,plotclr)#Fillinthetractswiththecolorsplot(houston,col=colcode,add=T)#Addatitletitle(main="GGWRDrug",sub="Quantile(Equal-Frequency)ClassIntervals")#Addalegend(Coordinatesareinlongitude,latitude).legend(-95.7,29.65,legend=names(attr(colcode,"table")),fill=attr(colcode,"palette"),cex=0.6,bty="n")9 GGWR Alcohol Quantile (Equal-Frequency) Class Intervals [-0.2720501,0.07134612) [0.07134612,0.1257156) [0.1257156,0.172
11 981) [0.172981,0.2411054) [0.2411054,1.7
981) [0.172981,0.2411054) [0.2411054,1.726148] Theseareverysmoothmaps.Let'stryitwithasmallerbandwidth. #Trywithasmaller,fixedbandwidth(1/8thesizeoftheother)#ThisisclosertowhatispresentedinFigure5ofWalleretal.2007houston.ggwr=ggwr(violence~Z.log.total+Z.log.drug+offset(log(pop2000)),data=houston,family=poisson,bandwidth=houston.bw/8,longlat=TRUE)#Nowtomaptheoutcomes#Thehouston.ggwrobjectcontainsanelementcalledSDFwhichcontains#thespatialdataframe(SDF)oftheoutcomes.#Wewillwanttomapthe(spatiallyvarying)interceptandtheparameters#associatedwitheachofourtwocovariates.#Thenotationistrickysincethespatialdataframeiswithinthe#houston.ggwrobject.intercepts=houston.ggwr$SDF@data$X.Interceptalcohol.effects=houston.ggwr$SDF@data$Z.log.totaldrug.effects=houston.ggwr$SDF@data$Z.log.drug#Plottheoutlinesfirstplot(houston)10 GGWR Drug Quantile (Equal-Frequency) Class Intervals [0.4843723,0.6625861) [0.6625861,0.7162815) [0.7162815,0.7853365) [0.7853365,0.9090064) [0.9090064,1.998665] #Definethenumber
12 ofclassesnclr5#quintiles#UseRColorBrewer
ofclassesnclr5#quintiles#UseRColorBrewertochoosethecolorsplotclrbrewer.pal(nclr,"BuPu")classclassIntervals(intercepts,nclr,style="quantile")colcodefindColours(class,plotclr)#Fillinthetractswiththecolorsplot(houston,col=colcode,add=T)#Addatitletitle(main="GGWRIntercepts",sub="Quantile(Equal-Frequency)ClassIntervals")#Addalegend(Coordinatesareinlongitude,latitude).legend(-95.7,29.65,legend=names(attr(colcode,"table")),fill=attr(colcode,"palette"),cex=0.6,bty="n") #Plottheoutlinesfirstplot(houston)#Definethenumberofclassesnclr5#quintiles#UseRColorBrewertochoosethecolorsplotclrbrewer.pal(nclr,"BuPu")classclassIntervals(alcohol.effects,nclr,style="quantile")colcodefindColours(class,plotclr)#Fillinthetractswiththecolorsplot(houston,col=colcode,add=T)#Addatitletitle(main="GGWRAlcohol",sub="Quantile(Equal-Frequency)ClassIntervals")11 GGWR Intercepts Quantile (Equal-Frequency) Class Intervals [-38.95876,-5.632501) [-5.632501,-5.010546) [-5.010546,-4.720881) [-4.720881,-4.359973) [-4.359973
13 ,10.32148] #Addalegend(Coordinatesareinl
,10.32148] #Addalegend(Coordinatesareinlongitude,latitude).legend(-95.7,29.65,legend=names(attr(colcode,"table")),fill=attr(colcode,"palette"),cex=0.6,bty="n") #Plottheoutlinesfirstplot(houston)#Definethenumberofclassesnclr5#quintiles#UseRColorBrewertochoosethecolorsplotclrbrewer.pal(nclr,"BuPu")classclassIntervals(drug.effects,nclr,style="quantile")colcodefindColours(class,plotclr)#Fillinthetractswiththecolorsplot(houston,col=colcode,add=T)#Addatitletitle(main="GGWRDrug",sub="Quantile(Equal-Frequency)ClassIntervals")#Addalegend(Coordinatesareinlongitude,latitude).legend(-95.7,29.65,legend=names(attr(colcode,"table")),fill=attr(colcode,"palette"),cex=0.6,bty="n")12 GGWR Alcohol Quantile (Equal-Frequency) Class Intervals [-40.11836,-0.4044299) [-0.4044299,-0.005595059) [-0.005595059,0.3458348) [0.3458348,0.7832294) [0.7832294,16.29554] 13 GGWR Drug Quantile (Equal-Frequency) Class Intervals [-31.32848,0.160059) [0.160059,0.5901473) [0.5901473,0.9178376) [0.9178376,1.526265) [1.5262