1 Clusters de feridoss por Affinity Propagation

library(leaflet.extras)
library(apcluster)
library(magrittr)
library(dplyr)   
library(leaflet)
library(rgdal)
library(rgeos)
library(geojsonio)
library(mapview)
library(contoureR)
library(geosphere)
library(cluster)
library(sp)
library(rgdal)
library(RColorBrewer)
library(sp)
library(foreign)

1.1 Carga de Dados

dados = read.dbf("/Users/fagne/OneDrive/r-files/CIET/acidentes2020/_Base/acidentes_2014a2020_WGS84.dbf")
dados = dados[dados$ANO > 2014, ]
table(dados$TIPO_ACID)
## 
##   ABALROAMENTO  ATROPELAMENTO      CAPOTAGEM         CHOQUE        COLISAO 
##          36700           4514            256           6024          24419 
##       EVENTUAL       INCENDIO NAO CADASTRADO          QUEDA     TOMBAMENTO 
##           1038             23              4           1944            180
table(dados$FERIDOS)
## 
##    -1     0     1     2     3     4     5     6     7     8     9    19    21 
##     1 50751 20508  3112   505   134    49    14    15     4     3     1     1 
##    25    35 
##     3     1
dados = dados[(dados$FERIDOS > 0) |  (dados$FATAIS >0), ]
sort(unique(dados$ANO))
## [1] 2015 2016 2017 2018 2019 2020
anos = length(unique(dados$ANO))
anos
## [1] 6
class(dados)
## [1] "data.frame"
x2 <- cbind(dados$LONGITUDE, dados$LATITUDE)
x2 <- x2[complete.cases(x2), ]
dim(x2)
## [1] 24716     2
head(x2)
##           [,1]      [,2]
## [1,] -51.01213 -30.19741
## [2,] -51.03732 -30.21369
## [3,] -51.05466 -30.23513
## [4,] -51.05470 -30.23517
## [5,] -51.05470 -30.23517
## [6,] -51.05470 -30.23517

1.2 Preparação

x1 <- x2
x2 <- x2[sample(nrow(x2), 5000), ]
x2 = as.data.frame(x2)
names(x2) = c("LONGITUDE", "LATITUDE" )
head(x2)
save(x2, file = "data/x2-feridos-999.rda")
dim(x1)
## [1] 24716     2
dim(x2)
## [1] 5000    2

1.3 Treino

apres <- apcluster(negDistMat(r=2), x2, q=0.999)
plot(apres, x2)
A caption

A caption

summary(apres)
##   Length    Class     Mode 
##     1800 APResult       S4
save(apres, file = "data/apres2-feridos-999.rda")

1.4 Obtenção de Centróides

centroides = unique(apres@exemplars)
poly = data.frame()
centr_indice = 0
for (i in centroides){
  centr_indice = centr_indice + 1
  centr_lat=x2[i,1]
  centr_lon=x2[i,2]
  poly = rbind(poly, c(centr_lat, centr_lon, centr_indice))
}
names(poly) = c("Lat", "Lon", "Cluster")
head(poly)
dim(poly)
## [1] 1800    3
exemplars = poly
save(exemplars, file = "data/exemplars-feridos-999.rda")

1.5 Classificação Global

predict.apcluster <- function(s, exemplars, newdata){
  simMat <- s(rbind(exemplars, newdata), sel=(1:nrow(newdata)) + nrow(exemplars))[1:nrow(exemplars), ]
  unname(apply(simMat, 2, which.max))
}
resultado <- list()

dados$cluster = 0
for(i in seq(from=1, to=length(dados$ID)-1000, by=1000)){
  inicio = i
  final = i+999
  resultado = predict.apcluster(negDistMat(r=2), x2[apres@exemplars, ],  dados[inicio:final, 2:3])
  dados$cluster[inicio:final] = resultado
}
controle = length(dados$cluster)  - final

resultado = predict.apcluster(negDistMat(r=2), x2[apres@exemplars, ],  dados[(final + 1):length(dados$cluster), 2:3])
dados$cluster[(final + 1):length(dados$cluster)] = resultado
head(dados)
tail(dados)
save(dados, file = "data/acidentes-feridos-999.rda")

1.6 Acidentes por Cluster

pal <- colorFactor(
  palette = 'Dark2',
  domain = dados$cluster
)

leaflet(dados) %>%
  addTiles(group="Mapa") %>% 
  addCircles(group="Acidentes", ~LONGITUDE, ~LATITUDE, weight = 0.1, radius=7, color=~pal(cluster),
             stroke = TRUE, fillOpacity = 0.8, popup=~paste("Cluster Nº: ", cluster,  
            "<br>Ano: ", ANO, "<br>Tipo: ", TIPO_ACID, "<br>Local: ", LOG1,  "<br>UPS: ", UPS,   sep = " ")) %>% 
  addLegend(group="Legenda", "topright", colors= "", labels=paste("Classificados em meio a ", summary(apres)[1], "Clusters"), title="Acidentes em Porto Alegre") %>% 
  addLayersControl(overlayGroups = c("Mapa", "Acidentes", "Legenda"),
                   options = layersControlOptions(collapsed = FALSE)) %>% 
  addProviderTiles(providers$CartoDB.DarkMatter)

A caption

1.7 Acidentes por Cluster pelo ponto de corte

dados = dados[dados$cluster >0 ,]
pal <- colorFactor(
  palette = 'Dark2',
  domain = dados$cluster
)

leaflet(dados) %>%
  addTiles(group="Mapa") %>% 
  addCircles(group="Acidentes", ~LONGITUDE, ~LATITUDE, weight = 0.1, radius=7, color=~pal(cluster),
             stroke = TRUE, fillOpacity = 0.8, popup=~paste("Cluster Nº: ", cluster,  
            "<br>Ano: ", ANO, "<br>Tipo: ", TIPO_ACID, "<br>Local: ", LOG1,  "<br>UPS: ", UPS,   sep = " ")) %>% 
  addLegend(group="Legenda", "topright", colors= "", labels=paste("Classificados em meio a ", summary(apres)[1], "Clusters"), title="Acidentes em Porto Alegre") %>% 
  addLayersControl(overlayGroups = c("Mapa", "Acidentes", "Legenda"),
                   options = layersControlOptions(collapsed = FALSE)) %>% 
  addProviderTiles(providers$CartoDB.DarkMatter)

A caption

1.8 Enriquecimento Informacional

dados = dados[dados$cluster >0 ,]
rm(apres)
clusters_encontrados = sort(unique(dados$cluster))
#clusters_encontrados
parq = dados
poly = data.frame()
for (i in clusters_encontrados){
  temp = parq[parq$"cluster" == i,  ]
  ch1 = convexHullAM_Indexes(temp[,2],temp[,3], includeColinear=FALSE,zeroBased = FALSE)
  #print(i)
  #print(ch1)
  poligono = temp[ch1, 2:3 ]
  area <- geosphere::areaPolygon(x = poligono)
  acidentes = nrow(temp)
  pol = temp
  coordinates(pol) = ~LONGITUDE+LATITUDE
  centr_lat=gCentroid(pol, byid=FALSE)$x
  centr_lon=gCentroid(pol, byid=FALSE)$y
  if(nrow(temp) >= anos) {
    for (ii in ch1) {
    polying = temp[ii,]
    polying$area = area
    polying$acidentes = acidentes
    polying$centroide_lat = centr_lat
    polying$UPS = sum(temp$UPS)
    polying$centroide_lon = centr_lon
    poly = rbind(poly, polying)
    }  
  }
}
head(poly)
tail(poly)
mean(poly$area)
## [1] 26846.39
median(poly$area)
## [1] 14280.19
minimoquantil = quantile(poly$area, probs = 0.01)
maximoquantil = quantile(poly$area, probs = 0.90)
quantile(poly$area, probs = c(0.01, 0.25, 0.5,0.75,0.99))
##         1%        25%        50%        75%        99% 
##    172.313   5577.720  14280.187  27018.101 248371.655
poly = poly[(poly$area < maximoquantil) & (poly$area > minimoquantil), ]
dim(poly)
## [1] 7732   48
class(poly)
## [1] "data.frame"
pol = poly

1.8.1 Área

areas = poly[!duplicated(poly$cluster),]
summary(areas$area)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   175.2  4826.0 12313.2 14856.0 22000.9 51006.9

1.8.2 UPS

summary(areas$UPS)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   30.00   50.00   75.00   87.86  111.00  355.00

1.9 Combinando Informações

#save(pol, file = "data/pol.Rds")
#load("data/acidentes.rda")
#load("data/apres2.rda")
#load("data/exemplars.rda")
#load("data/x2.rda")
dados = poly[,c(1:9, 11:13,38,41,44:48)]
head(dados)
names(dados) = c("ID","lat", "lon", "log1", "Log2", "Pred", "Local", "Tipo", "Via", "Data", "Dia", "Hora", 
                 "Fx_horaria","UPS", "box_id", "Area", "Acidentes", "CentLon", "CentLat")
dados$id = (dados$box_id * 11)
dados$group = dados$id
head(dados)
dadostemp = dados[, c(15:21)]
coordinates(dados)=c("lat","lon")
df = dados
df
## class       : SpatialPointsDataFrame 
## features    : 7732 
## extent      : -51.25981, -51.05466, -30.23944, -29.9684  (xmin, xmax, ymin, ymax)
## crs         : NA 
## variables   : 19
## names       :     ID,                          log1,                       Log2,  Pred,      Local,         Tipo,                       Via,       Data,         Dia,  Hora, Fx_horaria, UPS, box_id,             Area, Acidentes, ... 
## min values  : 601007, AC B CONJ RES ALTO PETROPOLIS, AC SEIS TREVO ASSIS BRASIL,     0, Cruzamento, ABALROAMENTO,     0 AV ANTONIO CARVALHO, 01/01/2015,     DOMINGO, 00:00,          0,  30,      1,  175.14956940175,         6, ... 
## max values  : 683126,     VDT IMPERATRIZ LEOPOLDINA,                VDT OBIRICI, 15555, Logradouro,   TOMBAMENTO, VDT IMPERATRIZ LEOPOLDINA, 31/12/2019, TERCA-FEIRA, 23:59,         23, 355,   1799, 51006.9507346377,        71, ...
data <- data.frame(box_id=unique(df$box_id),row.names=unique(df$id))
head(data)
dadostemp2 = dados[!duplicated(dados$id),]
#head(dadostemp2, 15)
data = as.data.frame(cbind(data, dadostemp2@data))

1.10 Criação de Polígonos

points2polygons <- function(df,data) {
  get.grpPoly <- function(group,ID,df) {
    Polygon(coordinates(df[df$id==ID & df$group==group,]))
  }
  get.spPoly  <- function(ID,df) {
    Polygons(lapply(unique(df[df$id==ID,]$group),get.grpPoly,ID,df),ID)
  }
  spPolygons  <- SpatialPolygons(lapply(unique(df$id),get.spPoly,df))
  SpatialPolygonsDataFrame(spPolygons,match.ID=T,data=data)
}

#Criamos o SpatialPolygonsDataFrame
data$Log2 = NULL
spDF <- points2polygons(df,data)
spDF
## class       : SpatialPolygonsDataFrame 
## features    : 1230 
## extent      : -51.25981, -51.05466, -30.23944, -29.9684  (xmin, xmax, ymin, ymax)
## crs         : NA 
## variables   : 19
## names       : box_id,     ID,                     log1,  Pred,      Local,         Tipo,                                                 Via,       Data,         Dia,  Hora, Fx_horaria, UPS, box_id.1,             Area, Acidentes, ... 
## min values  :      1, 601114,  AC B VILA NOVA BRASILIA,     0, Cruzamento, ABALROAMENTO,                                    0 AV BELEM VELHO, 01/03/2015,     DOMINGO, 00:00,          0,  30,        1,  175.14956940175,         6, ... 
## max values  :   1799, 683092, VDT ABDIAS DO NASCIMENTO, 12000, Logradouro,   TOMBAMENTO, VDT ABDIAS DO NASCIMENTO & AV EDVALDO PEREIRA PAIVA, 31/10/2020, TERCA-FEIRA, 23:50,         23, 355,     1799, 51006.9507346377,        71, ...
class(spDF)
## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"
spDF@data$group = 1
spDF@data$box_id = NULL
dim(spDF@data)
## [1] 1230   18
dadostemp = unique(dadostemp)
spDF@data = merge(spDF@data, dadostemp, by = "box_id")
dim(spDF@data)
## [1] 1230   24
spDF$log1 = spDF$Pred = spDF$CentLon.x = spDF$CentLat.x = spDF$CentLon.y = spDF$CentLat.y   = spDF$id.y = spDF$group.y  = spDF$Tipo = spDF$Via = spDF$Tipo = spDF$Dia =  spDF$Data= spDF$group.x =  spDF$Local=   spDF$Hora= spDF$Area.x=   spDF$Fx_horaria = NULL
plot(spDF,col=spDF$box_id+1)
A caption

A caption

library(rgdal)
rgdal::writeOGR(obj = spDF,
                dsn = "data/feridos.json",
                layer = "myParq",
                driver = "GeoJSON",
                overwrite_layer = TRUE)

Acidentes por Cluster

#carregamos os dados SpatialPolygonsDataFrame
parqs <- geojsonio::geojson_read("data/feridos.json", what = "sp")
#Verificamos o objeto
parqs
## class       : SpatialPolygonsDataFrame 
## features    : 1230 
## extent      : -51.25981, -51.05466, -30.23944, -29.9684  (xmin, xmax, ymin, ymax)
## crs         : +proj=longlat +datum=WGS84 +no_defs 
## variables   : 7
## names       : box_id,     ID, UPS, Acidentes.x,  id.x,           Area.y, Acidentes.y 
## min values  :      1, 601114,  30,           6,    11,  175.14956940175,           6 
## max values  :   1799, 683092, 355,          71, 19789, 51006.9507346377,          71
dim(parqs)
## [1] 1230    7
library(raster)
projection(parqs)
## [1] "+proj=longlat +datum=WGS84 +no_defs"
library(mapview)
mapviewPalette(name = "Viridis")
library(RColorBrewer)
mapview(parqs, zcol = "Acidentes.x", col.regions=brewer.pal(9, "YlOrRd"))

Acidentes por m2

parqs@data$densidade = (parqs@data$Acidentes.x/parqs@data$Area.y)*1000000
mapview(parqs, zcol = "densidade", col.regions=brewer.pal(9, "YlOrRd"))

Acidentes por poligono

hist(parqs@data$Acidentes.x, col = "magenta")
A caption

A caption

Acidentes por KM2

hist(parqs@data$densidade, col = "orange")
A caption

A caption

Locais mais densos

quantile(parqs$densidade, probs = 0.99)
##      99% 
## 41117.32
temp = parqs[parqs$densidade > quantile(parqs$densidade, probs = 0.001), ] # Atualziar
mapview(temp, zcol = "densidade")

1.11 Calculando a matriz de vizinhanças

projection(parqs)
## [1] "+proj=longlat +datum=WGS84 +no_defs"
parqstemp = parqs
require(sf)
shape <- read_sf(dsn = ".", layer = "mercator_32722_2014_2019")
projection(shape)
## [1] "+proj=utm +zone=22 +south +datum=WGS84 +units=m +no_defs"
projection(parqstemp) = projection(shape)

ccods = coordinates(parqs)
temps = as.data.frame(ccods)
cord.dec = SpatialPoints(cbind(temps$V1, temps$V2), proj4string=CRS("+proj=longlat"))
cord.UTM <- spTransform(cord.dec, CRS("+init=epsg:32722"))
ccods = as.data.frame(cord.UTM)
points = cbind(ccods[,1],ccods[,2])
head(points)
##          [,1]    [,2]
## [1,] 485287.0 6676398
## [2,] 485670.9 6680329
## [3,] 480549.0 6681996
## [4,] 485755.0 6681562
## [5,] 480202.3 6673730
## [6,] 483409.9 6674016
library(spdep)
distNeighbors = 400
dnb = dnearneigh(points,0,distNeighbors)
class(dnb)
## [1] "nb"
subsets = as.data.frame(matrix(dnb))
class(subsets)
## [1] "data.frame"
subsets = subsets$V1
lengths(subsets)
##    [1]  9  4  1  3  4  5  5  2  2  5  1  2  2  1  1  4  6  1  2  3  3  1  3  4
##   [25]  4  2  4  4  8  6  9  4  1  3  7  2  6  3  5  1  3  3  2  6  3  3  1  3
##   [49]  9  4  3  4  8  4  5  1  5  1  7  5  3  2  9  5  7  2 12  6  8  1  3  3
##   [73]  1  1  1  1  1  3  6  1  5  5  8  6 14  5  2  2  5  7  3  1  7  1  1  3
##   [97]  1  5  1  1  4  5  1  1  7  7  2  8  6  2  8  6  2  8  5  9  9  3  1  7
##  [121]  1  7  9  3  1  2  2  5  2  9 11  3  2  2  1  1  4 12  6  4  5  7  2  3
##  [145]  5  3  2  3  6  6  3  1  1  9  7  3  1  3 11  3  7  6 10 10  9  9  2  3
##  [169]  5  7  8  5  8  3  5  3  3  6  6  2  4  2  5  1  7  4  1  6  1  3  3  5
##  [193]  1 10  2  1  3  1  3  5  9  6  2 12  1  2  4  1  6  5  3  3  2  1  9  1
##  [217]  5  3  1  4  1  6  2  1  5  5  2  2  2  2  5  3  4  3  9  5  6  1  2  5
##  [241]  5  5  4  4  4  8  2  1  2  5  1  1  7  1  1  5  2  2  7  7  8  1  8  5
##  [265]  1  5  5  5  5  8  5  1  5 10  4  1  4  2  6  4  2  1  9  6  3  4  8  6
##  [289]  8  1  3  5 10  7  4  5  9  3 11  5  9  1  1  8  7  5  5  4  6  3 10  5
##  [313]  5  4  6  1  3  3  8  3  5  7  1  3  4  3  9  1  4  4  4  5  6  2  1  3
##  [337]  1  1  5  5  7  2  1 11  8  7  2  6  2  1  3  2  5  6 10  7 10 11  1  4
##  [361]  7  6  4  3  1  7  5  4  1  3  7  3  8  1  2  3  3  7  2  2  1  7  7  3
##  [385]  1  2  4  7  6  4  3 11  2  2  4  3  4  8  2  6  9  4  8  4  3  3  3  1
##  [409]  6  7  5  2  6  2 10  7  2  2  6  6  4  1  4  7 11  3  1  4  5  5  4  5
##  [433]  3  2  5  1  8  2  5  2  1  6  3  2  4  6  1  7  5  6  5  5  1  7  5  4
##  [457]  8  6  7  2  8  7  6  6  5  6  6  6  8  4  2 10  2  2  1  4  9  7  1  7
##  [481]  8  2  8  1  2  9  4  8  1  8  6 10  1  2  2  1  1  6  3  5  1  6 10  8
##  [505]  2  9  9  2  3  6  8  6  1  3  3  2  4  3  1  9  2  2  5  3  7  1  1  7
##  [529]  4  3  6  4  1  3  8  7  3  4  5  1  5  1  4  1  6  3  3  1  5  5  4 10
##  [553]  5  3  2  1  9  4 11  2  9  2  2 10  4  5 10  3  2  6  4  2 10  7  4  9
##  [577]  5  1  5  5  6  6  2  3  2  5  5  3  6  8  7  2  2  1  6  4  5  9  3  1
##  [601]  6  3  4  4  5 10  5  7  1  8  4  3  1 12 11  1  6  3  4  3  9  3 10  3
##  [625]  2  6  3  7  5  8  9  3  6  7  9  6  1  3  1  4  3  2  4  2  2  8  2  5
##  [649]  4  8  3  2  6  4  1  2  4  3  4  3  5  1  4  2  5  7  8  3  9  3  6  5
##  [673]  4  3  3 11  5  7  4 10  2  4  3  7  6  6  9  4  5  3  2  5  2  5  3  2
##  [697]  1  3  2  4  3  7  6  3  4  6  1  5  5  1  1  3  1  5  2  3  7  3  1  4
##  [721]  3  8  4  6  7  1  8  1  5  4 10  3  9  7  6  2  1  2  2  3  9  5  2  4
##  [745]  2  3  5  4  5  4  4  2  4  4  3  1  1  7  2  8  3  1  6  3  2  6  1  1
##  [769]  7  4  7  6  4  9  7  8  1  7  5  2  2  2  3  4  6  1  1  1  5  6  8  6
##  [793]  3  5  4  6  3  3  5  2  7  1  1 12  6  6  4  5  1  2  1  1  2  4  5  5
##  [817]  1  5  5  4  4  6  1  6  1  1  2  4  6  7  2  4  8  5  2  7  2  4  8  3
##  [841]  5  2  4  3  4  1 10  2  2  5  2  2  5  1  4  6  5  9  3  7  7  8  6  7
##  [865]  4  3  7  5  5  1  2 10  2  3  3  2  1  2  5  5  3  6  1  3  2  3  2  8
##  [889]  5  1  1  2  3  2  6  6 10  7  8  3  7  7  1  1 11  1 11  7  5  3  6  1
##  [913]  3  3  2  4  3  2  2  1  1  1  2 10  6  1  3  2  1  2  1  2  6  7  8  9
##  [937]  4  1  8  1  4  3  4  2  3  1  2  4  4  2  2  2  4  2  4  3  4  6  1  1
##  [961]  8  2  1  2  1  2  2  7  3  3  1  5  3  1  4  1 10  9  1  5  9  4  1  4
##  [985]  7  7  5  3  4  7  4  1  1 13  1  3  5  5  6  2  8  4  1  4  4  4  4  3
## [1009]  1  7  2  6  1  4  3  1  4  5  7  6  2  2  1  6  2 12  6  2  7  4  1 10
## [1033]  5  1  8  6  8  2  2  1  5  2  6  3  5  5  4  2 11  8  7  6  3  3 10  3
## [1057]  5  7  1  4  4  8  8  9  3  5  2  1  6  1  7  6  2  7  7  7  9  2  6  5
## [1081]  2  6  6  3  4  4  1  2  8  4  1  6  5  4  8  1  1  3  1  6  4  2  4  5
## [1105]  3  7  5  3  5  8  4  2  1 11  2  4  2  1  3  6  1  3  1  6  6  2  1  4
## [1129]  1  5  3  6  6  8  5  3  5  3  5  8  8  8  6  1  7  6  3  7  3  7  4  5
## [1153]  5  8  1  3  9  2  1  4  1  2  6  3  7  4  3 11  4  6  4  3  5  2  2  2
## [1177]  9  2  4  2  2  5  1  3  5  6  4  5  5  3  2  4  7  2  1  2  4  6  2  6
## [1201]  5  2  2  6  3  6  5 11  6  3  4  1  7  4  3  7  1  9 13  6  6  4  7  3
## [1225]  3  4  8  1  2  2
parqs$n = 1
sub = which(subsets == '0')
sub
##  [1]   22   40   47   56   70   74   76   97  100  121  135  152  153  157  187
## [16]  189  193  196  205  216  219  248  254  290  302  303  316  335  337  343
## [31]  359  365  381  408  422  427  453  479  484  544  556  578  594  613  639
## [46]  655  662  711  726  757  788  803  812  817  823  854  890  891  904  912
## [61]  920  921  946  963  965  971 1009 1016 1023 1031 1097 1113 1161 1195
parqs$n[sub] = 0
length(parqs)
## [1] 1230
parqs = parqs[parqs$n > 0,]
length(parqs)
## [1] 1156
length(dnb)
## [1] 1230
#dim(ccods)
ccods = ccods[-sub, ]
dim(ccods)
## [1] 1156    2
points = cbind(ccods[,1],ccods[,2])
head(points)
##          [,1]    [,2]
## [1,] 485287.0 6676398
## [2,] 485670.9 6680329
## [3,] 480549.0 6681996
## [4,] 485755.0 6681562
## [5,] 480202.3 6673730
## [6,] 483409.9 6674016
#dnb = dnearneigh(points,0,2000)
dnb = dnearneigh(points,0,distNeighbors)
dnb
## Neighbour list object:
## Number of regions: 1156 
## Number of nonzero links: 5220 
## Percentage nonzero weights: 0.3906203 
## Average number of links: 4.515571
length(dnb)
## [1] 1156

1.12 Matriz de Vizinhanca

1.12.1 Matriz Binária

W.Bin= nb2mat(neighbours = dnb, style = "B")
#parqs <- parqs[!sub,]

1.12.2 Matriz Normalizada

W.Normal= nb2mat(neighbours = dnb, style = "W")
#head(W.Normal)

1.13 KNN

vizinhos_4 <- knearneigh(points, k = 4)
class(vizinhos_4)
## [1] "knn"
head(vizinhos_4$nn)
##      [,1] [,2] [,3] [,4]
## [1,]  622  862 1069   36
## [2,]  611  887  205  215
## [3,]  454  970 1110  474
## [4,]  123  199 1148  899
## [5,]  172   71  871  764
## [6,]  549 1061  700  676
vizinhanca_4 <- knn2nb(vizinhos_4)
class(vizinhanca_4)
## [1] "nb"

Preparação para Análisis Global e Local

mv_simpl = st_as_sf(parqs)
plot(mv_simpl)
A caption

A caption

class(mv_simpl)
## [1] "sf"         "data.frame"
library(dplyr)
mv_simpl =  mv_simpl %>% dplyr::select(Acidentes.y)
#mv_simpl <- st_simplify(mv_simpl, preserveTopology = FALSE,                  dTolerance = 1)
class(mv_simpl)
## [1] "sf"         "data.frame"
mapview::mapview(mv_simpl)
sf::sf_use_s2(FALSE)#trips and tiks
## Spherical geometry (s2) switched off
mv_simpl = st_as_sf(mv_simpl)
vizinhanca_neig <- poly2nb(mv_simpl)
ShapeNEIG = parqs
ShapeNEIG$vizinhos = card(vizinhanca_neig)
ShapeNEIG <- subset(ShapeNEIG, parqs$vizinhos != 0)
#vizinhanca2neig <- poly2nb(ShapeNEIG)

1.14 Calculando o Índice de Moran Global

Os índices de autocorrelção espacial global calculados pelos testes de normalidade e permutação.

1.14.1 Pelo teste de Normalidade

moran.test(parqs$Acidentes.y,listw=nb2listw(dnb, style = "W"), randomisation= FALSE)
## 
##  Moran I test under normality
## 
## data:  parqs$Acidentes.y  
## weights: nb2listw(dnb, style = "W")    
## 
## Moran I statistic standard deviate = 6.6669, p-value = 1.306e-11
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic       Expectation          Variance 
##      0.1557432380     -0.0008658009      0.0005518080

1.14.2 Pelo teste de Permutação ou Teste de pseudo-significˆancia

 moran.test(parqs$Acidentes.y,listw=nb2listw(dnb, style = "W"), randomisation= TRUE)
## 
##  Moran I test under randomisation
## 
## data:  parqs$Acidentes.y  
## weights: nb2listw(dnb, style = "W")    
## 
## Moran I statistic standard deviate = 6.6756, p-value = 1.231e-11
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic       Expectation          Variance 
##      0.1557432380     -0.0008658009      0.0005503751

1.14.3 Por simulação de Monte-Carlo

moran.mc(parqs$Acidentes.y, listw=nb2listw(dnb, style = "W"), nsim=999)
## 
##  Monte-Carlo simulation of Moran I
## 
## data:  parqs$Acidentes.y 
## weights: nb2listw(dnb, style = "W")  
## number of simulations + 1: 1000 
## 
## statistic = 0.15574, observed rank = 1000, p-value = 0.001
## alternative hypothesis: greater

1.14.4 Pelo teste de Permutação

Diferente dos demais testes globais o teste para o EBI é exclusivo para taxas e tem-se apenas a opção de teste da permutação

EBImoran.mc(parqs$Acidentes.y,parqs$Area.y,
            nb2listw(dnb, style="B", zero.policy=TRUE), nsim=999, zero.policy=TRUE)
## 
##  Monte-Carlo simulation of Empirical Bayes Index (mean subtracted)
## 
## data:  cases: parqs$Acidentes.y, risk population: parqs$Area.y
## weights: nb2listw(dnb, style = "B", zero.policy = TRUE)
## number of simulations + 1: 1000
## 
## statistic = 0.13123, observed rank = 1000, p-value = 0.001
## alternative hypothesis: greater

1.14.5 Por simulação de Monte-Carlo

shapeCG.p=parqs$Acidentes.y/parqs$Area.y
moran.mc(shapeCG.p, nb2listw(dnb, style="B", zero.policy=TRUE),
         nsim=999, zero.policy=TRUE)
## 
##  Monte-Carlo simulation of Moran I
## 
## data:  shapeCG.p 
## weights: nb2listw(dnb, style = "B", zero.policy = TRUE)  
## number of simulations + 1: 1000 
## 
## statistic = 0.12462, observed rank = 1000, p-value = 0.001
## alternative hypothesis: greater

1.15 Calculando a Estatística C de Geary Global

1.15.1 Pelo teste de Normalidade

geary.test(parqs$Acidentes.y, listw=nb2listw(dnb, style = "W"), randomisation= FALSE)
## 
##  Geary C test under normality
## 
## data:  parqs$Acidentes.y 
## weights: nb2listw(dnb, style = "W") 
## 
## Geary C statistic standard deviate = 6.0692, p-value = 6.426e-10
## alternative hypothesis: Expectation greater than statistic
## sample estimates:
## Geary C statistic       Expectation          Variance 
##      0.8514728988      1.0000000000      0.0005988857

1.15.2 Pelo teste de Permutação

geary.test(parqs$Acidentes.y, listw=nb2listw(dnb, style = "W"), randomisation=TRUE)
## 
##  Geary C test under randomisation
## 
## data:  parqs$Acidentes.y 
## weights: nb2listw(dnb, style = "W") 
## 
## Geary C statistic standard deviate = 5.7405, p-value = 4.719e-09
## alternative hypothesis: Expectation greater than statistic
## sample estimates:
## Geary C statistic       Expectation          Variance 
##      0.8514728988      1.0000000000      0.0006694349

1.15.3 Por simulação de Monte-Carlo

geary.mc(parqs$Acidentes.y, listw=nb2listw(dnb, style = "W"),nsim=999)
## 
##  Monte-Carlo simulation of Geary C
## 
## data:  parqs$Acidentes.y 
## weights: nb2listw(dnb, style = "W") 
## number of simulations + 1: 1000 
## 
## statistic = 0.85147, observed rank = 1, p-value = 0.001
## alternative hypothesis: greater

1.16 Calculando Índice de Getis e Ord Global

Getis-Ord é um indicador que mede a concentração local de uma variável de atributo distribuída espacialmente

globalG.test(parqs$Acidentes.y, nb2listw(dnb, style="B"))
## 
##  Getis-Ord global G statistic
## 
## data:  parqs$Acidentes.y 
## weights: nb2listw(dnb, style = "B") 
## 
## standard deviate = 8.5427, p-value < 2.2e-16
## alternative hypothesis: greater
## sample estimates:
## Global G statistic        Expectation           Variance 
##       4.609752e-03       3.909585e-03       6.717645e-09

1.17 Getis e Ord Local

localG(parqs$Acidentes.y, nb2listw(dnb, style="B"), zero.policy=NULL, spChk=NULL, return_internals=FALSE)
##    [1]  0.9633108667  1.6377966338 -0.8220634488  0.3868558844  1.3963183863
##    [6]  0.0011388656  2.2811477515  0.4997229297 -0.1223367266 -0.8302360238
##   [11] -0.9201263775  1.1916453737 -0.9533616508 -0.6251462608  0.1568862196
##   [16] -0.1683556788  1.2694760525 -1.1154049199 -0.6066221898 -1.3658270675
##   [21] -0.9725412989  4.4426411011 -0.6170323028 -1.0088695713  2.9927772235
##   [26]  0.1698092858 -1.3007417192  1.1115866593 -0.2130683846  1.0343480867
##   [31]  1.0984403189 -1.1154049199 -0.3483698103 -0.5777747377  1.1915197822
##   [36]  0.9060813736 -0.2368170675  0.0914847387  0.7247013611  0.1059842698
##   [41] -0.2609060173  0.5057955672 -1.0831950692  1.0075818244  0.1025423779
##   [46]  0.8027322958  0.4149784392 -1.0266486147 -0.8605557794  2.8774848576
##   [51] -0.9561081071  4.1260655292 -0.3062076754  0.5485058039  0.8254798018
##   [56]  0.4401070147  4.1765973134 -1.2321084353  0.3472595544  0.1772516984
##   [61]  1.1646154751 -1.1628619148  2.1395142654 -1.3328758838  0.5494382235
##   [66]  0.7311760422  1.0653958182 -0.5270554951 -0.0381241213  0.8439473202
##   [71]  0.2740760628 -0.4140796704 -0.1368958176  0.0451724184  0.0888315150
##   [76] -0.1421404497  2.6281561189  1.6555118658 -1.2265582268  0.1526084891
##   [81] -1.3009339328  2.6783011376 -0.2854250323 -0.6318474553 -1.1157483493
##   [86]  0.7134608263 -1.1159340175 -0.6252355892  0.5564908678  1.7563212044
##   [91] -0.0383810417  0.0732221147  2.3691758054 -1.0163918969 -0.6249537939
##   [96]  0.5674789028  1.0122213809 -0.4005722760  0.5176888806  4.1905142199
##  [101] -1.5078952409  1.7317339636  0.5453356827 -0.2621132071  3.7625419597
##  [106] -1.3586660463 -0.0795132886 -0.2145408889 -0.3480911612 -0.4304835814
##  [111]  1.8282934155 -1.0300225544 -0.6068236052 -0.7460581989 -0.6255347259
##  [116] -0.8157965091 -0.8157965091  1.2725758034 -0.1218880126 -0.1475018283
##  [121]  1.6194731678 -0.1227828482 -0.3989686966 -1.4403363101 -0.4299189980
##  [126] -0.7631744257  3.1602989918 -0.8133968301 -0.9578162032 -0.7865886017
##  [131]  0.7927897436 -0.8860684939 -0.5761765129 -1.8819819074 -0.0665355958
##  [136]  0.7085819706 -0.3491170156 -0.4541149977 -1.5744454156 -0.9707917581
##  [141]  2.4742664502  1.5306503256  0.8501784060 -0.4052034368  0.4440956590
##  [146]  1.7429214317  0.2325994369 -0.2545869318  2.0070147721  1.6499221746
##  [151]  0.8326842782  0.7032589292 -0.1920234621 -0.1214801807 -0.2148996844
##  [156] -1.2884912624 -0.3168607979 -0.5674064943 -0.7355764634 -0.6305779481
##  [161]  2.3690624687  0.9514430068 -0.2910114198  0.1458050974  0.1474554724
##  [166] -0.2623807863  0.3728005660 -1.2325609015 -0.1746503790  1.0379653702
##  [171]  0.9793800211  0.4703835676 -0.4132424403  1.0644226545  1.8072044268
##  [176] -0.9187103552 -0.0594424878  0.0840344652  0.3295750135 -0.8220634488
##  [181] -1.2538544226 -0.4358972299  1.5231222612 -0.0921431043  0.4298080567
##  [186]  1.6546642585 -0.5369262006 -0.7631744257  0.6470045283 -0.0915353193
##  [191]  0.1323166458  1.5172731807 -0.0098404880 -0.9553207006  0.8427070896
##  [196]  1.7822216048 -0.6550793373 -0.3496001800  0.5104858864 -1.0176850576
##  [201]  0.5473800058 -1.3007396685 -0.0378700657 -1.2247641823  1.3219323605
##  [206] -0.7459119746 -0.1241641073 -0.3987925460 -0.3995764208  0.2672754999
##  [211] -0.9696136863  0.2163105974 -0.9162023538  0.2151536044  0.8840717970
##  [216] -0.1342558806 -0.3324765557 -0.9545750793  2.5341954622  1.3175596288
##  [221] -0.1742464333  0.9607862913  0.8071811353 -0.3221962070  1.2788959721
##  [226]  1.1919512624 -0.6755338391 -0.7880949796  0.3532043380 -0.3325886146
##  [231]  1.9010526244 -0.5278924430  1.6686474716 -0.3293626810 -0.9547495507
##  [236]  1.9007100307 -1.3979084258 -0.3531179743 -0.9196455091  1.3878853187
##  [241]  1.7603290924 -0.7238860983 -1.1399802390  0.7050800112 -0.7408890279
##  [246]  0.1761690780  0.6532111381  1.6246058513 -0.1362399891 -1.7533379354
##  [251]  0.2223131714 -0.9589481909 -0.8220634488 -0.7627668248 -0.9531790472
##  [256]  1.0676187910  2.8136489042  0.0154024747  0.3529843409 -0.4104157969
##  [261]  1.4691313963 -0.1791779039  1.1478447800  0.8639511888  1.1076531959
##  [266]  0.8639511888 -1.5355667048  0.1390702735  1.4035414610  0.9793800211
##  [271] -1.7916239784 -0.6537179246  1.1380075119 -1.0825001487  0.6739211595
##  [276]  0.1323166458  0.3806083823 -0.2099230471  0.3056872233 -1.2253605845
##  [281] -0.4809533956  1.2028580922 -0.0957180847 -1.0278501084  4.5081418429
##  [286] -0.7444872167  0.1759936911 -0.3704610627 -0.1734477592  0.2759465547
##  [291]  0.4416768612  0.5149269637  0.5558921035  0.9225058947  1.8261360279
##  [296] -0.9199584156 -0.0674416764  0.5619729487 -0.9135557058  0.1468240052
##  [301] -0.9189971883 -0.4695859201 -0.0273578706 -0.5651259818 -1.1386106630
##  [306] -1.0530937637 -0.2616042708 -1.3677787385 -0.8216272536  0.0882816250
##  [311]  0.3539363599 -1.6219678000 -0.2614824789  2.1840632693  1.0393871898
##  [316]  1.2725986075 -1.1633024319  0.1454045245 -0.7460311476 -1.1159340175
##  [321] -0.7430447418 -1.3011390255 -0.4790001997 -0.6955079082  0.6880310831
##  [326]  1.0518846308  2.0678426263  1.6531111573 -0.4699986098 -1.1758102746
##  [331] -0.6136188097 -1.4970481262  1.5198849992  1.4184542554 -0.7860410467
##  [336]  0.5591180644 -0.8222225576 -1.0278501084 -0.8418697364  0.8422129101
##  [341] -1.5326893995 -0.5277819153 -0.1242942203  1.9690635992 -1.1974938460
##  [346] -1.7711877350 -0.5396432303 -1.3015817340  0.1940003196  0.3466986778
##  [351]  0.2723511740  1.5275799199 -1.4403363101 -0.0278688580 -1.4751217813
##  [356] -0.2136917101 -0.5667940337 -1.4222141592  1.8598546585 -1.0930409150
##  [361] -0.1241641073 -1.2048983983  0.0468513500 -1.7439776726  1.0022811734
##  [366]  1.3303089458  0.3059619090 -1.5216819646  2.0790610774  1.5655386270
##  [371] -0.5673862350 -1.0840433041 -0.8591863622  1.3504426685  1.0670450358
##  [376] -0.4002382913 -1.3114219341  1.5381611938 -0.0048163967 -0.4700914156
##  [381]  3.3358036463  0.0467464739  2.2301391413  0.4993161953  1.6667431713
##  [386]  0.0678044645 -0.7600815854 -0.2739536859  4.4224429754  0.1970390599
##  [391] -0.6891042919  1.1963216876  1.7117473242  0.7022660880 -1.2517523598
##  [396] -1.1388655774 -0.8010074463  0.4998349642  0.0455479446  0.1571814578
##  [401] -0.6284340758  2.0921614271  1.0999046908 -1.1626562044 -0.6252355892
##  [406] -0.1319814117  1.2909621109 -0.8164491517  0.5102260098  2.7926890430
##  [411] -0.6257601431 -1.0267097208  0.2643915075 -0.2533088325 -1.4460743541
##  [416]  1.2831006039 -0.8789515588  2.3249940762  1.0507475335 -0.5236244263
##  [421]  0.5887681414  1.3094039175 -1.1626562044  1.5232712902  0.4564708816
##  [426] -0.2959977729 -0.8551749754  1.2307116859 -0.0893621743  0.6679171362
##  [431]  0.4657521180 -0.4537669314 -0.8115641624 -0.5374536988  0.9642417889
##  [436]  0.1529305959 -0.1923825222 -0.6262735151  0.6571493910  0.9640309385
##  [441]  0.3434067893 -0.0654118149  1.2796711220  0.7779815305 -0.6277303844
##  [446] -0.5394835095  2.7645015980  0.2170971310  1.1483907231 -0.9196455091
##  [451] -1.8432239206 -1.1354493958  2.6893748483 -1.0175252145 -1.2999305004
##  [456] -1.5073463019  0.8427995825 -0.9199584156 -0.5729905718  1.4610383108
##  [461]  0.2207267024  0.9402628188  0.3084766485  0.1277549976  0.4796126879
##  [466] -0.5398074604  0.7037528153  1.4875376069 -0.3989686966  1.4599409851
##  [471]  1.6672948080  1.4524303762  1.5489463823 -1.1140387407 -0.2351390550
##  [476] -1.5942548492 -1.2300882592  2.2271182014 -0.9162023538 -1.0173738456
##  [481]  1.7500700947 -1.0925482868 -0.6082608793  0.3087671448  3.3833563670
##  [486] -0.0624836476 -1.0180301975 -1.0176850576 -0.2481259741 -0.0779087620
##  [491] -1.7066411693  1.9076606304 -0.9575024899 -0.2343874777 -0.6318474553
##  [496]  1.2782904390 -1.0263728411  0.6689797970 -0.4699986098 -0.7444872167
##  [501] -0.8216272536  2.5934651459 -0.8216272536 -0.7115041583  0.3052047917
##  [506] -0.1807424050  0.1634290555 -0.6246182587  0.1780026035  0.0890162362
##  [511] -0.1262058875  2.1792133835  0.4414127172 -0.5761765129  0.0145667195
##  [516]  1.1928536090 -0.8596757530  0.6161729062 -0.4699446544  1.3249334894
##  [521]  3.3384738816 -0.1927559748  0.4111042700  0.1181361620 -1.6203681293
##  [526]  1.4339401294 -1.3093808870 -0.1923825222 -0.6920801729  2.8626464841
##  [531] -0.8164491517  0.1280240741 -1.8089462015  0.1184571638  0.6036961985
##  [536] -0.4368984256  0.6176168877 -0.8297103301  0.0296586447 -0.1321827151
##  [541] -0.0539390249 -1.1413417641 -1.1610311121 -0.2573158240 -0.2997872439
##  [546]  1.3475059577  1.1525425377 -0.5581004618  0.6043213351  2.6455397925
##  [551]  3.7548670348 -0.2950734164 -0.9596218234  1.4918217838 -0.2464775490
##  [556]  0.1033960827  0.8421891881  1.9873253041  0.0462706121 -0.4205823298
##  [561] -1.5948460593  0.2668621217  0.2802703065  0.6149874118 -0.1746186130
##  [566]  1.0379907587  2.0078827341 -0.3221962070 -1.2525532055  0.4103045303
##  [571]  1.5980700957  3.8773022884  0.2649694742  1.7496163408 -0.9095473963
##  [576]  0.1589671074  3.4521270188 -0.6891042919 -0.3363926396 -0.7454709721
##  [581]  1.1310629123  1.0738798480 -0.0099876832  1.7160543072  2.1060230886
##  [586] -0.0026140725 -0.6675312348 -1.5942548492  0.9940526890 -1.0249678019
##  [591] -0.6082981291  0.8674467668  1.7237133757 -0.9162023538  0.2674408201
##  [596] -1.2535109917  0.5689457630 -1.7421934612 -0.6781362713 -0.4010006884
##  [601]  2.8409322970 -0.8855126742  0.8873192752 -0.6645893115  0.4459308324
##  [606]  0.5557539443 -0.2622459009 -0.6960208799  0.4137158177 -0.6075886708
##  [611]  1.0526763610 -0.2357217506  1.0982903610 -0.6885626334 -0.0871425073
##  [616] -0.6662735188 -0.1925054030 -1.4896361752  0.8278379408  0.8277065214
##  [621] -0.8587729036  0.7027883476 -0.7443178395  1.1474880882 -0.0427074592
##  [626] -1.8413961526  2.0256245724 -0.4070377739  0.7050847090 -0.4332989962
##  [631] -0.2881905096  2.0787533021  2.7054401059 -0.3307037173  3.1077357956
##  [636] -0.5753013674 -0.1773929529  0.1864502383 -1.4931698967  0.1161453677
##  [641]  3.3548686292 -0.7429474247 -1.1397415236  0.8461779825  4.0796751217
##  [646] -0.4014592671  1.4496783090  0.8386113371 -0.8846000086 -0.8212526206
##  [651]  0.3853419157  0.0152827237 -0.5178008359 -0.7458562357  0.3081347015
##  [656] -1.4576199644 -1.3090372348 -0.2233165713  0.2673493493 -0.2329834673
##  [661] -0.0863590351 -1.6221036647  5.2482992424  0.6140886680 -0.8214955433
##  [666]  2.2375427786 -0.9553207006 -1.4816598366  0.2304742970  0.2160658542
##  [671] -0.4296232185 -0.3223843278 -0.5199914877  0.5500514748  0.9517408695
##  [676] -0.1311868903  0.1693201831 -1.8434931608  0.3553479080 -0.3920608085
##  [681] -0.1241648212  1.6517506829  0.2162039668  3.9457141863  1.2342298299
##  [686] -0.2931380069 -0.8162757978 -0.5283785483 -0.4007117366 -0.4005722760
##  [691]  0.3294360396  3.4193889125  0.3551919695  0.5689457630 -0.2711518748
##  [696] -0.6071416909  0.1595156476  0.9270053053  0.4129169995 -1.3568728789
##  [701] -1.1039648256  0.0201137000  0.0843859301 -1.2517523598  2.3284632486
##  [706]  1.4079595920 -0.9191115680  0.3827879482 -0.4707177366 -1.5630963727
##  [711]  0.1025423779 -0.7241684820  1.1095829909 -0.7433704928  1.8151546728
##  [716]  1.6672948080 -0.7238860983 -0.6261373422  1.3812117553 -0.5667940337
##  [721]  0.1966511049  1.1083077748  0.3656068322 -0.3401054559  0.4173605921
##  [726]  0.6552683571 -1.0172309436  0.3488361803 -0.4783301887 -0.6763666319
##  [731]  0.3608503596  0.7826711878 -0.9159725725 -0.5680210718  0.6674833315
##  [736] -0.1358057168  0.8423494846  0.1780026035 -0.2143310379  1.0007163225
##  [741] -0.9724282964  1.0724346515 -1.4006339989 -0.8596757530  1.7070028036
##  [746] -0.2357217506  0.6750729137 -0.3486119550  0.0851077197 -0.3233894903
##  [751] -0.3308721195  3.8404843108  1.1472980924 -0.5724394718 -0.8620884577
##  [756] -0.4818468049  0.0611369994 -0.4693967361 -0.3325886146 -0.9555270233
##  [761] -0.1233579405 -0.6122651264 -0.7011269702  1.6675019840 -0.4379907718
##  [766] -0.8613885237  0.4124787058  1.0670450358 -1.4566368449  0.1583789887
##  [771] -0.7241684820 -1.0936153943 -1.4505241320  0.3476330354  1.1225451085
##  [776] -0.5374536988  1.3445127851  1.5909722499 -0.4382202518 -0.9528209816
##  [781]  1.1625345870  0.1555057232 -0.7616961305  2.4753467610 -0.9139482863
##  [786] -1.3994722616 -0.3314788187 -1.4972412472  1.2341734465 -0.5680210718
##  [791] -0.2340989364 -0.1199585380  2.0921614271  0.0846237131 -0.5241640159
##  [796] -1.0223298430 -0.2625178673 -0.1298116421 -1.2054458154  0.0741781098
##  [801]  1.8425614986  0.4508368214  0.1619219473  0.5294688250  1.6055305385
##  [806] -0.9085371730 -0.3351168656 -1.9566229991 -0.4200011518  1.9125186318
##  [811] -1.5837031738  1.0144494694 -1.0514315893 -0.3323672767 -0.6075886708
##  [816]  0.4412598099  0.8473963226  0.8382310044  0.8951551839 -1.1626562044
##  [821]  1.0380247624  0.9844998122 -1.3151114332 -0.2605515441 -0.3497670687
##  [826] -0.1342558806 -0.8203380699  0.2745584774 -0.9555270233  0.8379132128
##  [831] -1.3691522491  1.4186965530  0.0003797403  0.2935984249  0.1033960827
##  [836]  0.6391959103  0.8261431606  1.3644347612 -0.4303556423 -0.2428855827
##  [841]  0.1680449997 -1.8754933057 -0.4341868216  1.0840539722 -1.0180301975
##  [846]  0.3473988810  0.7443876584  2.4856721613  0.8249059629  0.9666856147
##  [851] -0.1235422531  0.0645056658 -0.6313771565 -0.2932108648  0.9145861119
##  [856] -0.9573575432 -0.5157327377 -0.0521807632 -0.3307037173 -0.9196455091
##  [861] -0.2594994299  0.1943841142 -0.1738677069 -0.4304835814 -0.4037763259
##  [866] -1.4392055042 -0.4299189980 -1.0938251139  0.7462040703  1.5380654287
##  [871]  1.8677707716  1.1594196110  2.1294833272  1.2934288471 -0.2212329873
##  [876] -0.8222225576  1.9986452784 -1.0173738456 -0.0773824206 -0.3479561590
##  [881]  0.2712257893  1.4000811790  2.3650804947  0.0833500196 -1.3016920005
##  [886] -0.2717980178  0.9144888800  1.1228307020 -0.5394835095  0.0719912504
##  [891]  0.0154024747  0.5146950951  2.7042749435  2.3728909409 -0.5355772004
##  [896] -0.0367207301 -0.6257601431 -1.1807810623 -0.1239070334 -0.1921415548
##  [901]  0.0845044724 -1.2998010547 -0.6217494157  2.0843821484  0.2163438637
##  [906] -1.5758455721  0.6125783019 -0.7238860983  1.7394353444 -0.4302469821
##  [911]  1.1526275328  0.2510244328 -1.0178533825  2.3689687872  1.7291305231
##  [916]  1.0495253800 -0.1367026980 -1.3511453841 -1.1051523607  0.9034785050
##  [921] -1.3564620808 -0.2938900894  2.9663316626  0.2690256482  2.0801423978
##  [926] -0.1358057168 -0.4288130820  4.2260000699 -0.8217658037 -0.1220490660
##  [931] -0.4809533956 -0.3901554196  1.4266803031 -0.5378152292  1.2803638844
##  [936]  0.4126213939 -0.3327034576 -1.6444216509  1.7409018683 -1.4466869306
##  [941] -1.1055138642 -0.6873698880  1.4995445454 -0.7470389214  0.1462104396
##  [946]  0.0616047983  0.2199173873 -0.0077875347  0.1669927577  0.6206746537
##  [951]  0.3050728412  1.0692678434 -0.4705552537 -0.6776434079 -0.9326612935
##  [956] -0.8855126742  1.3157221309 -0.1329987623 -1.1630773206  1.1961190473
##  [961] -0.5673862350  1.3401197659 -0.3874285169  0.6465199583 -1.0446069997
##  [966] -0.4545565098  0.3112827647 -0.3310788708  0.9841864840 -1.1142440801
##  [971] -0.6114371909 -0.1902573130  0.8268273150 -0.9704917686 -0.3047745236
##  [976]  1.2295355780 -0.4695859201 -0.6774904138  0.6178178676  0.8644550146
##  [981]  0.9801171388  0.7073091474 -0.9725412989 -0.5199914877  3.5457699695
##  [986]  0.8943810838  1.1412678641 -0.2103996888 -0.7238860983  0.7085620750
##  [991]  1.1005109240  0.9331120871 -0.0385166365  0.4099528840 -0.9718853113
##  [996] -1.5343915985  1.8841795951 -0.7240242711  0.2738934328 -0.9195005496
## [1001] -0.8071952827  0.4654008992  0.1536087633  0.0879481266 -0.2881905096
## [1006]  0.8989066932  2.9827224448 -1.0245729072  1.5155109961  1.7548566343
## [1011] -0.7451812020 -0.5722652433 -0.5296116252  0.5587326067 -1.4970481262
## [1016]  0.4613466312 -0.7235077500 -1.0924022355  2.7393637241 -1.8906900396
## [1021] -0.8222225576  0.5451769175 -0.9158607077  1.4910418354  2.1553128143
## [1026] -0.3322607736 -0.9135557058  0.0619888388  1.0303528640 -0.0268489615
## [1031] -0.8157965091  1.6872807610 -0.3904728006 -0.2928860050  0.0858588252
## [1036]  0.3526056269 -0.8024161432  1.2784127917 -0.8385120937  0.4146004383
## [1041]  0.2940395530  3.9918711569  0.3611660528  0.7648792477 -0.4696628789
## [1046] -1.1157483493  0.2723511740  2.5671044785 -1.1155719864 -1.4234926864
## [1051] -0.0388157692  0.3057688559 -0.8120895049 -1.0221512063 -1.1149594195
## [1056]  0.1202922416 -0.1354785809  0.7034864003 -0.6325480881  0.6683731496
## [1061] -0.8959918439  1.6978851647  0.0905202825 -0.0652142400 -0.1261335088
## [1066] -1.5382285630  0.0447983129  0.7620041316 -0.2814238174 -0.7330347491
## [1071]  1.4696842617 -0.3327034576 -0.7304183897  0.9922213826 -0.9695401199
## [1076] -0.5465741568 -0.6331289405 -0.2510989073  0.6601779484 -0.9617454100
## [1081]  0.8351753809  0.4800108237 -1.0168529781 -0.9157504438  2.4378300100
## [1086]  1.0730651914 -1.0178533825  1.9826496123 -0.7457989894 -0.4136541883
## [1091] -0.1807424050  0.1935809029 -0.4675957938 -1.1983354962  1.8879899639
## [1096]  0.3631923169 -0.8927726065 -0.7638335376 -1.0280386904  2.1963327833
## [1101]  0.8463056517 -0.4005722760 -1.5082448273  0.8660222043 -1.5090945173
## [1106] -0.2701071158 -0.9553207006 -1.0936153943  1.6672185682  0.1568156424
## [1111] -0.2935455458 -0.6129396284  0.9127446244  0.0228222323 -0.3894182123
## [1116]  0.8404959870  1.2903628438  0.0150435910  2.2256060741  0.3802768561
## [1121] -0.5394835095 -0.2623807863 -0.7638335376 -0.5716156182  0.0155223527
## [1126]  0.7077322720  1.9757299221 -0.6090596671 -1.1620971550 -1.2573402762
## [1131] -0.9698826441  4.2520689583  1.2309434620  3.1649779190 -0.0149782709
## [1136] -1.2544477047 -1.7923957711  1.4294674206  0.2308841621 -1.2539056051
## [1141]  3.2133563814 -1.7708764187 -0.8212526206  0.3447536014  0.9584145590
## [1146] -0.2944061952 -0.0909341191  0.0229930806 -0.2114787565 -1.5355667048
## [1151] -0.0668353285 -1.5482456915  0.0979277241 -0.1357222488 -1.4396218466
## [1156] -1.1626562044
## attr(,"gstari")
## [1] FALSE
## attr(,"call")
## localG(x = parqs$Acidentes.y, listw = nb2listw(dnb, style = "B"), 
##     zero.policy = NULL, spChk = NULL, return_internals = FALSE)
## attr(,"class")
## [1] "localG"

1.18 Moran Local

Todas as análises feitas até o momento foram de escala global. No entanto, é necessário que seja feita também uma análise local do estudo. Essa análise pode ser feita pelo índice local de autocorrelaçãoo espacial (LISA). Para isso é preciso calcular o índice de Moran local.

ShapePB.mloc <- localmoran(parqs$Acidentes.y, listw=nb2listw(dnb, style="W")) 
head(ShapePB.mloc)
##              Ii          E.Ii     Var.Ii         Z.Ii Pr(z != E(Ii))
## 1 -0.3577780863 -1.076097e-03 0.13711285 -0.963310867      0.3353915
## 2 -0.4317744934 -2.409054e-04 0.06942394 -1.637796634      0.1014641
## 3  0.8350954204 -8.954134e-04 1.03417100  0.822063449      0.4110408
## 4  0.0570184992 -5.658953e-05 0.02176681  0.386855884      0.6988629
## 5  0.7913454324 -1.118688e-03 0.32209969  1.396318386      0.1626186
## 6 -0.0008207396 -4.529261e-04 0.10430627 -0.001138866      0.9990913

1.19 Mapa das probabilidades (Signific?ncias do I de Moral Local)

Por meio dos valor-p do éndice de Moran local é possível construir um mapa de probabilidades.

library(classInt)
INT4 <- classIntervals(ShapePB.mloc[,5], style="fixed", 
                       fixedBreaks=c(0,0.01, 0.05, 0.10))
CORES.4 <- c(rev(brewer.pal(3, "Reds")), brewer.pal(3, "Blues"))
COL4 <- findColours(INT4, CORES.4)
parqs$COL = COL4  
parqs$p_valor = ifelse(parqs$COL == "#DE2D26", "[0,0.01)", ifelse(parqs$COL == "#EEE5E4", "[0.01,0.05)", "[0.05,0.1]"))
plot(parqs, col=COL4)
title("P-valores do I de Moran Local por Distäncia de Centróides")
TB4 <- attr(COL4, "table")
legtext <- paste(names(TB4))
legend("bottomright", fill=attr(COL4, "palette"), legend=legtext, 
       bty="n", cex=0.7, y.inter=0.7)
A caption

A caption

mapview(parqs, zcol = "p_valor", col.regions=c("red", "orange", "green"))
temp = parqs[parqs$p_valor != "[0.05,0.1]", ]
mapview(temp, zcol = "p_valor", col.regions=c("red", "orange"))

1.19.1 Montando matrix W de vizinhança

ShapeCG.nb1.mat <- nb2mat(dnb)

1.19.2 Incidência de acidentes padronizada

Acidentes_SD <- scale(parqs$Acidentes.y)

1.19.3 Média das incidências de acedentes padronizada

Acidentes_W <- ShapeCG.nb1.mat %*% Acidentes_SD

2 Diagrama de espalhamento de Moran

plot(Acidentes_SD, Acidentes_W,xlab="Z",ylab="WZ")
abline(v=0, h=0)
title("Diagrama de Espalhamento de Moran por Distancia de Centróides")
A caption

A caption

Q <- vector(mode = "numeric", length = nrow(ShapePB.mloc))
Q[(Acidentes_SD>0  & Acidentes_W > 0)] <- 1            
Q[(Acidentes_SD<0  & Acidentes_W < 0)] <- 2
Q[(Acidentes_SD>=0 & Acidentes_W < 0)] <- 3
Q[(Acidentes_SD<0  & Acidentes_W >= 0)]<- 4
signif=0.05
parqs$Q = Q

3 Mapa LISA

Q[ShapePB.mloc[,5]>signif]<-5
CORES.5 <- c("blue", "green" , "red", "yellow", "gray", rgb(0.95,0.95,0.95))
#CORES.5 <- c(1:5, rgb(0.95,0.95,0.95))
parqs$cores5Q = CORES.5[Q]
plot(parqs, col=CORES.5[Q])
title("Mapa LISA por Distancia Centroides")
legend("bottomright", c("Q1(+/+)", "Q2(-/-)", "Q3(+/-)", "Q4(-/+)","NS"), 
       fill=CORES.5)
A caption

A caption

CORES.5[Q][1:5]
## [1] "gray" "gray" "gray" "gray" "gray"
head(CORES.5[Q])
## [1] "gray" "gray" "gray" "gray" "gray" "gray"
#save(parqs, file = "parqsFinal-975.Rds")
parqs$cores5 =  ifelse(parqs$cores5Q == "blue", "A-A", ifelse(parqs$cores5Q == "green", "B-B", 
            ifelse(parqs$cores5Q == "red", "A-B", ifelse(parqs$cores5Q == "yellow", "B-A", "NA"))))
mapview(parqs, zcol = "cores5", col.regions=c("red", "orange", "green", "yellow", "grey"))
temp = parqs[parqs$cores5 == "A-A", ]
mapview(temp, zcol = "cores5", col.regions=c("red", "orange", "green", "yellow", "grey"))
mapview(temp, zcol = "Acidentes.x", col.regions=brewer.pal(9, "YlOrRd"))