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)
library(car)
library(lubridate)
library(tidyr)
library(stringr)
dados = read.dbf("/Users/fagne/OneDrive/r-files/CIET/acidentes2020/_Base/acidentes_2014a2020_WGS84.dbf")
dados = dados[dados$ANO > 2014, ]
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] 75102 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
Os dados já foram previamente clusterizados conforme o documento https://rpubs.com/fagnersutel/837844. Dessa maneira, as seçõs de preparação e treino não reprocessam os dados e apenas fazem a carga do modelo previamente obtido:
x1 <- x2
load("data/AZURE/x2-20000-99925.rda")
load("data/AZURE/apres2-20000-99925.rda")
names(x2) = c("LONGITUDE", "LATITUDE" )
head(x2)
dim(x1)
## [1] 75102 2
dim(x2)
## [1] 18776 2
plot(apres, x2)
summary(apres)
## Length Class Mode
## 3639 APResult S4
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] 3639 3
exemplars = poly
frame = exemplars
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)
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("Classified into ", summary(apres)[1], "clusters"), title="Accidents in Porto Alegre") %>%
addProviderTiles(providers$CartoDB)