Ir para Fase 2.

rm(list = ls(all.names = TRUE))
library(leaflet.extras)
## Loading required package: leaflet
library(apcluster)
## 
## Attaching package: 'apcluster'
## The following object is masked from 'package:stats':
## 
##     heatmap
library(magrittr) # need to run every time you start R and want to use %>%
library(dplyr)    # alternative, this also loads %>%
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
##           [,1]      [,2]
## [1,] -51.22665 -30.08557
## [2,] -51.20434 -30.06956
## [3,] -51.22628 -30.03048
## [4,] -51.17905 -30.05931
## [5,] -51.18213 -30.00723
## [6,] -51.21227 -30.12198
## [1] 20000     2
##   Length    Class     Mode 
##      136 APResult       S4

centroides = unique(apres@exemplars)
centroides
##   [1]    46    72   259   269   297   393   442   557   581   651   737
##  [12]   741   746   801   927   929  1051  1057  1182  1320  1372  1413
##  [23]  1445  1520  1681  1835  2072  2101  2200  2591  2711  2751  2948
##  [34]  2986  3000  3018  3085  3177  3348  3390  3481  3664  3731  4061
##  [45]  4142  4332  4456  4852  4857  4859  4907  4929  5128  5142  5499
##  [56]  5618  5654  5825  6127  6317  6512  6840  6960  6992  7041  7062
##  [67]  7069  7599  7799  7835  8214  8804  8886  9065  9258  9279  9467
##  [78]  9483  9908 10167 10344 10921 10982 11096 11412 11423 11536 11753
##  [89] 11774 12030 12094 12190 12309 12388 12519 13029 13244 13602 14097
## [100] 14244 14381 14505 14536 14609 14742 14842 15021 15409 15622 15714
## [111] 15717 15895 15909 15915 16201 16430 17128 17296 17354 17592 17980
## [122] 17981 18292 18426 18490 18577 18630 18659 18824 18887 19272 19373
## [133] 19493 19747 19948 19960
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] 136   3
exemplars = poly
save(exemplars, file = "exemplars.rda")
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()
filenames <- list.files(path = paste(workdirectory, "TCC/geo/consolidado", sep = "")) 
filenames
## [1] "geo_1552880779.08177.csv" "geo_1552942262.57111.csv"
## [3] "geo_1552942779.00958.csv" "geo_1552994851.82868.csv"
## [5] "geo_1553008141.58904.csv" "geo_1553070736.4915.csv" 
## [7] "geo_1553116059.10188.csv" "geo_1553140648.17926.csv"
## [9] "geo_1553234908.16836.csv"
setwd(paste(workdirectory, "/TCC/geo/consolidado", sep = "")) 
data <- do.call("rbind", lapply(filenames, read.csv, header = TRUE, sep = ";")) 
names(data)
## [1] "lat"               "long"              "accuracy"         
## [4] "formatted_address" "index"             "tipo"             
## [7] "default"           "atuacao"
head(data)
tail(data)
alvaras = data
dim(alvaras)
## [1] 177312      8
alvaras <- cbind(alvaras$long, alvaras$lat, as.character(alvaras$default), as.character(alvaras$tipo), as.character(alvaras$atuacao))
alvaras <- as.data.frame(alvaras)
alvaras$V1 <- as.numeric(as.character(alvaras$V1))
alvaras$V2 <- as.numeric(as.character(alvaras$V2))
alvaras <- alvaras[alvaras$V2 < 0, ]
alvaras <- subset(alvaras, !is.na(V1))
dim(alvaras)
## [1] 177200      5
head(alvaras)
tail(alvaras)
setwd(paste(workdirectory, "/TCC/", sep = "")) 
save(alvaras, file = "alvarasnc.rda")
alvaras$cluster = 0
head(alvaras)
tail(alvaras)
alvarasfim = alvaras[1:length(alvaras$cluster),]
head(alvarasfim)
length(alvarasfim$V1)
## [1] 177200
aa= length(alvarasfim$V1)/1000
aa = aa+1
aa
## [1] 178.2
for(i in seq(from=1, to=length(alvarasfim$V1)-1000, by=1000)){
  #print(paste(i, (i+999), sep = " - "))
  inicio = i
  final = i+999
  print(paste(inicio, final, sep = " - "))
  resultado = predict.apcluster(negDistMat(r=2), x2[apres@exemplars, ],  alvarasfim[inicio:final, 1:2])
  alvaras$cluster[inicio:final] = resultado
}
## [1] "1 - 1000"
## [1] "1001 - 2000"
## [1] "2001 - 3000"
## [1] "3001 - 4000"
## [1] "4001 - 5000"
## [1] "5001 - 6000"
## [1] "6001 - 7000"
## [1] "7001 - 8000"
## [1] "8001 - 9000"
## [1] "9001 - 10000"
## [1] "10001 - 11000"
## [1] "11001 - 12000"
## [1] "12001 - 13000"
## [1] "13001 - 14000"
## [1] "14001 - 15000"
## [1] "15001 - 16000"
## [1] "16001 - 17000"
## [1] "17001 - 18000"
## [1] "18001 - 19000"
## [1] "19001 - 20000"
## [1] "20001 - 21000"
## [1] "21001 - 22000"
## [1] "22001 - 23000"
## [1] "23001 - 24000"
## [1] "24001 - 25000"
## [1] "25001 - 26000"
## [1] "26001 - 27000"
## [1] "27001 - 28000"
## [1] "28001 - 29000"
## [1] "29001 - 30000"
## [1] "30001 - 31000"
## [1] "31001 - 32000"
## [1] "32001 - 33000"
## [1] "33001 - 34000"
## [1] "34001 - 35000"
## [1] "35001 - 36000"
## [1] "36001 - 37000"
## [1] "37001 - 38000"
## [1] "38001 - 39000"
## [1] "39001 - 40000"
## [1] "40001 - 41000"
## [1] "41001 - 42000"
## [1] "42001 - 43000"
## [1] "43001 - 44000"
## [1] "44001 - 45000"
## [1] "45001 - 46000"
## [1] "46001 - 47000"
## [1] "47001 - 48000"
## [1] "48001 - 49000"
## [1] "49001 - 50000"
## [1] "50001 - 51000"
## [1] "51001 - 52000"
## [1] "52001 - 53000"
## [1] "53001 - 54000"
## [1] "54001 - 55000"
## [1] "55001 - 56000"
## [1] "56001 - 57000"
## [1] "57001 - 58000"
## [1] "58001 - 59000"
## [1] "59001 - 60000"
## [1] "60001 - 61000"
## [1] "61001 - 62000"
## [1] "62001 - 63000"
## [1] "63001 - 64000"
## [1] "64001 - 65000"
## [1] "65001 - 66000"
## [1] "66001 - 67000"
## [1] "67001 - 68000"
## [1] "68001 - 69000"
## [1] "69001 - 70000"
## [1] "70001 - 71000"
## [1] "71001 - 72000"
## [1] "72001 - 73000"
## [1] "73001 - 74000"
## [1] "74001 - 75000"
## [1] "75001 - 76000"
## [1] "76001 - 77000"
## [1] "77001 - 78000"
## [1] "78001 - 79000"
## [1] "79001 - 80000"
## [1] "80001 - 81000"
## [1] "81001 - 82000"
## [1] "82001 - 83000"
## [1] "83001 - 84000"
## [1] "84001 - 85000"
## [1] "85001 - 86000"
## [1] "86001 - 87000"
## [1] "87001 - 88000"
## [1] "88001 - 89000"
## [1] "89001 - 90000"
## [1] "90001 - 91000"
## [1] "91001 - 92000"
## [1] "92001 - 93000"
## [1] "93001 - 94000"
## [1] "94001 - 95000"
## [1] "95001 - 96000"
## [1] "96001 - 97000"
## [1] "97001 - 98000"
## [1] "98001 - 99000"
## [1] "99001 - 1e+05"
## [1] "100001 - 101000"
## [1] "101001 - 102000"
## [1] "102001 - 103000"
## [1] "103001 - 104000"
## [1] "104001 - 105000"
## [1] "105001 - 106000"
## [1] "106001 - 107000"
## [1] "107001 - 108000"
## [1] "108001 - 109000"
## [1] "109001 - 110000"
## [1] "110001 - 111000"
## [1] "111001 - 112000"
## [1] "112001 - 113000"
## [1] "113001 - 114000"
## [1] "114001 - 115000"
## [1] "115001 - 116000"
## [1] "116001 - 117000"
## [1] "117001 - 118000"
## [1] "118001 - 119000"
## [1] "119001 - 120000"
## [1] "120001 - 121000"
## [1] "121001 - 122000"
## [1] "122001 - 123000"
## [1] "123001 - 124000"
## [1] "124001 - 125000"
## [1] "125001 - 126000"
## [1] "126001 - 127000"
## [1] "127001 - 128000"
## [1] "128001 - 129000"
## [1] "129001 - 130000"
## [1] "130001 - 131000"
## [1] "131001 - 132000"
## [1] "132001 - 133000"
## [1] "133001 - 134000"
## [1] "134001 - 135000"
## [1] "135001 - 136000"
## [1] "136001 - 137000"
## [1] "137001 - 138000"
## [1] "138001 - 139000"
## [1] "139001 - 140000"
## [1] "140001 - 141000"
## [1] "141001 - 142000"
## [1] "142001 - 143000"
## [1] "143001 - 144000"
## [1] "144001 - 145000"
## [1] "145001 - 146000"
## [1] "146001 - 147000"
## [1] "147001 - 148000"
## [1] "148001 - 149000"
## [1] "149001 - 150000"
## [1] "150001 - 151000"
## [1] "151001 - 152000"
## [1] "152001 - 153000"
## [1] "153001 - 154000"
## [1] "154001 - 155000"
## [1] "155001 - 156000"
## [1] "156001 - 157000"
## [1] "157001 - 158000"
## [1] "158001 - 159000"
## [1] "159001 - 160000"
## [1] "160001 - 161000"
## [1] "161001 - 162000"
## [1] "162001 - 163000"
## [1] "163001 - 164000"
## [1] "164001 - 165000"
## [1] "165001 - 166000"
## [1] "166001 - 167000"
## [1] "167001 - 168000"
## [1] "168001 - 169000"
## [1] "169001 - 170000"
## [1] "170001 - 171000"
## [1] "171001 - 172000"
## [1] "172001 - 173000"
## [1] "173001 - 174000"
## [1] "174001 - 175000"
## [1] "175001 - 176000"
## [1] "176001 - 177000"
controle = length(alvarasfim$cluster)  - final
controle
## [1] 200
resultado = predict.apcluster(negDistMat(r=2), x2[apres@exemplars, ],  alvarasfim[(final + 1):length(alvarasfim$cluster), 1:2])
alvaras$cluster[(final + 1):length(alvaras$cluster)] = resultado
length(resultado)
## [1] 200
alvaras = alvaras[alvaras$V1 < -49, ]
alvaras = alvaras[alvaras$V2 < -29.954337664276498, ]
head(alvaras)
tail(alvaras)
rm(alvarasfim)

save(alvaras, file = "alvaras.rda")
## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette Dark2 is 8
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette Dark2 is 8
## Returning the palette you asked for with that many colors

Ir para Fase 4.

Fim