Paquetes que se utilizarán

library(spatialreg)
library(spdep)
library(maptools)
library(RColorBrewer)
library(leaflet)
library(dplyr)
library(ggplot2)
library(tmap)
library(tmaptools)
library(lmtest)
library(normtest)
library(splm)
library(data.table)
library(sphet)
library(spgwr)
library(rgeos)

Base de datos

data(georgia)
ls()
## [1] "gSRDF"
geo<-
data<-gSRDF@data
attach(data)

Reading shape file

georgia<-readShapePoly('Counties_Georgia.shp')
class(georgia)
## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"
plot(georgia)

Running OLS

MODEL<-PctBach ~ PctRural+PctEld+PctFB+PctPov+PctBlack
MOD.OLS<-lm(MODEL, data=gSRDF@data)
summary(MOD.OLS)
## 
## Call:
## lm(formula = MODEL, data = gSRDF@data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9.4471 -2.0275 -0.3775  1.6223 18.7245 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 17.24373    1.75329   9.835  < 2e-16 ***
## PctRural    -0.07032    0.01358  -5.179 6.93e-07 ***
## PctEld       0.01145    0.12953   0.088 0.929693    
## PctFB        1.85247    0.30683   6.037 1.14e-08 ***
## PctPov      -0.25524    0.07248  -3.522 0.000566 ***
## PctBlack     0.04911    0.02648   1.854 0.065602 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.714 on 153 degrees of freedom
## Multiple R-squared:  0.5884, Adjusted R-squared:  0.575 
## F-statistic: 43.75 on 5 and 153 DF,  p-value: < 2.2e-16

Moran’s I for residuals

coords = cbind(Longitud,Latitude)
dist.mat<-as.matrix(dist(coords, method="euclidean"))
dist.mat.inv<-1/dist.mat
diag(dist.mat.inv)<-0
dist.W1<-mat2listw(dist.mat.inv, row.names = data$SR_ID, style="W")
lm.morantest(MOD.OLS, listw = dist.W1)
## 
##  Global Moran I for regression residuals
## 
## data:  
## model: lm(formula = MODEL, data = gSRDF@data)
## weights: dist.W1
## 
## Moran I statistic standard deviate = 4.4215, p-value = 4.901e-06
## alternative hypothesis: greater
## sample estimates:
## Observed Moran I      Expectation         Variance 
##     1.907755e-02    -9.075049e-03     4.054098e-05
RES<-residuals(MOD.OLS)
colours <- c("dark blue", "blue", "red", "dark red") 
MAP.RES<-SpatialPointsDataFrame(data=data.frame(RES), coords=cbind(Longitud,Latitude)) 
spplot(MAP.RES, cuts=quantile(RES), col.regions=colours, cex=1) 

Running GWR

coords<-cbind(gSRDF@data$Longitud,gSRDF@data$Latitude)
bw<-gwr.sel(MODEL, coords = coords, gweight = gwr.Gauss, adapt = TRUE)
## Adaptive q: 0.381966 CV score: 2246.445 
## Adaptive q: 0.618034 CV score: 2298.206 
## Adaptive q: 0.236068 CV score: 2245.9 
## Adaptive q: 0.3057039 CV score: 2233.276 
## Adaptive q: 0.3082464 CV score: 2233.181 
## Adaptive q: 0.3163436 CV score: 2235.26 
## Adaptive q: 0.307651 CV score: 2233.182 
## Adaptive q: 0.3079746 CV score: 2233.168 
## Adaptive q: 0.3079339 CV score: 2233.17 
## Adaptive q: 0.3080266 CV score: 2233.166 
## Adaptive q: 0.3081106 CV score: 2233.162 
## Adaptive q: 0.3081625 CV score: 2233.16 
## Adaptive q: 0.3082032 CV score: 2233.168 
## Adaptive q: 0.3081625 CV score: 2233.16
MOD.GWR<-gwr(MODEL, coords = coords, adapt = bw, hatmatrix = TRUE)
MOD.GWR
## Call:
## gwr(formula = MODEL, coords = coords, adapt = bw, hatmatrix = TRUE)
## Kernel function: gwr.Gauss 
## Adaptive quantile: 0.3081625 (about 48 of 159 data points)
## Summary of GWR coefficient estimates at data points:
##                    Min.    1st Qu.     Median    3rd Qu.       Max.
## X.Intercept. 14.1902994 15.4994133 17.1733971 18.1741029 18.8831188
## PctRural     -0.0814128 -0.0732267 -0.0646942 -0.0562377 -0.0519377
## PctEld       -0.1969998 -0.0966747 -0.0642309 -0.0285829  0.0171665
## PctFB         0.9407392  1.3594518  2.0628107  2.7363501  3.0864117
## PctPov       -0.3070984 -0.2572649 -0.2025800 -0.1247436 -0.0412649
## PctBlack     -0.0191289  0.0074139  0.0341490  0.0610207  0.0878578
##               Global
## X.Intercept. 17.2437
## PctRural     -0.0703
## PctEld        0.0114
## PctFB         1.8525
## PctPov       -0.2552
## PctBlack      0.0491
## Number of data points: 159 
## Effective number of parameters (residual: 2traceS - traceS'S): 15.98299 
## Effective degrees of freedom (residual: 2traceS - traceS'S): 143.017 
## Sigma (residual: 2traceS - traceS'S): 3.408584 
## Effective number of parameters (model: traceS): 12.16292 
## Effective degrees of freedom (model: traceS): 146.8371 
## Sigma (model: traceS): 3.363954 
## Sigma (ML): 3.232729 
## AICc (GWR p. 61, eq 2.33; p. 96, eq. 4.21): 853.2405 
## AIC (GWR p. 96, eq. 4.22): 836.5033 
## Residual sum of squares: 1661.636 
## Quasi-global R2: 0.6759729

MAPA: PctBLACK

georgia_lines<- fortify(georgia, region="Label")
## SpP is invalid
results<-as.data.frame(MOD.GWR$SDF)
data$coefPctBlack<-results$PctBlack
gwr.point1<-ggplot(data, aes(x=Longitud,y=Latitude))+geom_point(aes(colour=data$coefPctBlack              ))+scale_colour_gradient2(low = "red", mid = "white", high = "blue", midpoint = 0, space = "rgb", na.value = "grey50", guide = "colourbar", guide_legend(title="Coefs"))
gwr.point1+geom_path(data=georgia_lines,aes(long, lat, group=id), colour="grey")+coord_equal()

MAPA: PctPov

results<-as.data.frame(MOD.GWR$SDF)
data$coefPctPov<-results$PctPov
gwr.point1<-ggplot(data, aes(x=Longitud,y=Latitude))+geom_point(aes(colour=data$coefPctPov              ))+scale_colour_gradient2(low = "red", mid = "white", high = "blue", midpoint = 0, space = "rgb", na.value = "grey50", guide = "colourbar", guide_legend(title="Coefs"))
gwr.point1+geom_path(data=georgia_lines,aes(long, lat, group=id), colour="grey")+coord_equal()

MAPA: PctFB

results<-as.data.frame(MOD.GWR$SDF)
data$coefPctFB<-results$PctFB
gwr.point1<-ggplot(data, aes(x=Longitud,y=Latitude))+geom_point(aes(colour=data$coefPctFB              ))+scale_colour_gradient2(low = "red", mid = "white", high = "blue", midpoint = 0, space = "rgb", na.value = "grey50", guide = "colourbar", guide_legend(title="Coefs"))
gwr.point1+geom_path(data=georgia_lines,aes(long, lat, group=id), colour="grey")+coord_equal()

MAPA: PctRural

results<-as.data.frame(MOD.GWR$SDF)
data$coefPctRural<-results$PctRural
gwr.point1<-ggplot(data, aes(x=Longitud,y=Latitude))+geom_point(aes(colour=data$coefPctRural              ))+scale_colour_gradient2(low = "red", mid = "white", high = "blue", midpoint = 0, space = "rgb", na.value = "grey50", guide = "colourbar", guide_legend(title="Coefs"))
gwr.point1+geom_path(data=georgia_lines,aes(long, lat, group=id), colour="grey")+coord_equal()