Problematização

  • Alvo: Distribuição do atendimento de doenças de tratamento contínuo.
  • Caso: Dados de atendimentos de hemodiálise em Curitiba/2016.
  • Demanda VS oferta.
  • OPORTUNIDADES:
    • 34% dos atendimentos foram na unidade mais próxima.
    • Economia de R$ 1.46 por atendimento apenas em Curitiba.
    • Ganho em qualidade de vida.
  • Objetivos da aplicação
    • Subsidiar o gestor público na tomada de decisão visando
    • Orientar a distribuição dos pacientes.
    • Redução de custos com transporte.
    • Alocação e/ou ampliação da rede de unidades de atendimento.
  • Metodologia
    • Dados do SIASUS.
    • Conhecimento em Estatística.
    • Análise exploratória gráfica e descritiva.
    • Software R de Computação Científica (livre de código aberto).
    • Enriquecimento dos dados com lat & long e distâncias.

Análise dos dados

Configuração do ambiente

#-----------------------------------------------------------------------

# Limpa ambiente de trabalho.
rm(list = ls())

# Pacotes.
library(lattice)      # Gráficos.
library(latticeExtra)
library(RColorBrewer) # Esquemas de cores.
library(plyr)         # Manipulação de dados.

# https://www.shanelynn.ie/massive-geocoding-with-r-and-google-maps/
library(ggmap)        # CEP para lat & lon.

# https://rstudio.github.io/leaflet/
library(leaflet)      # Gráficos interativos.

#-----------------------------------------------------------------------
# Configurações da lattice.

mycol <- c("#E41A1C", "#377EB8", "#4DAF4A",
           "#984EA3", "#FF7F00", "#FFFF33")

# Trellis graphical style.
ps <- list(box.rectangle = list(col = 1, fill = c("gray70")),
           box.umbrella = list(col = 1, lty = 1),
           dot.symbol = list(col = 1, pch = 19),
           dot.line = list(col = "gray50", lty = 3),
           plot.symbol = list(col = 19, cex = 0.8),
           plot.line = list(col = 1),
           plot.polygon = list(col = "gray50"),
           superpose.line = list(col = mycol, lty = 1),
           superpose.symbol = list(col = mycol, pch = 19, fill = mycol),
           superpose.region = list(col = mycol, pch = 19),
           superpose.polygon = list(col = mycol),
           strip.background = list(col = c("gray80", "gray50")),
           axis.text = list(cex = 0.8))
trellis.par.set(ps)
# lattice.options(default.args = list(as.table = TRUE))
# show.settings()

Importação dos dados

#-----------------------------------------------------------------------
# Importa dados de hemodialise.

# save.image("seguranca.RData")

# Exibe os arquivos no diretório de trabalho.
list.files()

# Importa a tabela.
nef <- read.csv2(file = "baseSIAOK.csv",
                 header = TRUE,
                 sep = ",",
                 stringsAsFactors = FALSE)
str(nef)
cat("Dimensões da tabela.\n")
dim(nef)

cat("Nome das colunas (variáveis).\n")
names(nef)
#-----------------------------------------------------------------------
# Gráfico de pareto do número de ocorrências por MUN.

barchart(sort(table(nef$AP_UFMUN),
              decreasing = FALSE),
         xlab = "Número de atendimentos",
         ylab = "Código do município") +
    latticeExtra::layer(panel.abline(v = seq(0, max(x), by = 1000),
                                     col = "gray70"),
                        under = TRUE)
# Códigos para filtrar os registros de Curitiba.
mun <- c(Curitiba = "410690")

# Aplicando o filtro.
nef <- subset(nef, AP_UFMUN %in% mun)

# Dimensões depois de filtrar.
dim(nef)

# Frequencias de dados ausentes.
table(is.na(nef$AP_CEPPCN))

Aquisição das coordenadas do CEP

#-----------------------------------------------------------------------
# Buscar o lat & lon a partir dos CEPs.

# Número de digitos do CEP.
table(nchar(nef$AP_CEPPCN))

# Formatando os CEPs para fazer a busca na API do Google.
cep <- gsub(x = nef$AP_CEPPCN,
            pattern = "(\\d{2})(\\d{3})(\\d{3})",
            replacement = "\\1.\\2-\\3")
cep <- sprintf("CEP %s, Curitiba-PR", cep)
# head(cep)

# Quantidade de CEPs únicos e duplicados.
table(duplicated(cep))

# Para recortar e colar no RStudio Web do LEG: rstudio.leg.ufpr.br.
# Com servidor do LEG a conexão com Google é mais rápida.
# dput(unique(cep))

# latlon <- geocode(location = cep)
# str(latlon)

# Valores de lat & lon retornados para API para cada CEP enviado.
latlon <-
structure(list(lon = c(-49.3069611, -49.291748, -49.291748, -49.2360011,
-49.2664018, -49.3602383, -49.2511995, -49.2778063, -49.291748,
-49.2851435, -49.2106794, -49.288142, -49.2511995, -49.325983,
-49.3297882, -49.3424738, -49.2829996, -49.2613339, -49.3323251,
-49.2410668, -49.3221781, -49.3526243, -49.2461329, -49.2106794,
-49.2816082, -49.3069611, -49.3500865, -49.1967573, -49.3221781,
-49.2892129, -49.3475488, -49.1803081, -49.2511995, -49.2220728,
-49.2106794, -49.155011, -49.3018896, -49.2816082, -49.2632284,
-49.3475488, -49.3272514, -49.3069611, -49.2511995, -49.2660851,
-49.2660851, -49.2396049, -49.2778063, -49.3018896, -49.2056164,
-49.3297882, -49.2106794, -49.3018896, -49.291748, -49.3323251,
-49.2714702, -49.2660851, -49.3069611, -49.2562665, -49.1803081,
-49.1803081, -49.1916955, -49.3018896, -49.2309359, -49.2461329,
-49.3323251, -49.2106794, -49.2106794, -49.3323251, -49.2660851,
-49.260067, -49.2056164, -49.3018896, -49.2714702, -49.291748,
-49.2052972, -49.2461329, -49.3373992, -49.2664018, -49.1771887,
-49.3412051, -49.2660851, -49.2511995, -49.2106794, -49.2968186,
-49.3272514, -49.2511995, -49.2660851, -49.3486495, -51.638382,
-49.2968186, -49.2778063, -49.2878973, -49.2410668, -49.2968186,
-49.2309359, -49.3373992, -49.2660851, -49.2410668, -49.2511995,
-49.2157429, -49.3221781, -49.1916955, -49.2461329, -49.2930156,
-49.195348, -49.260067, -49.2005539, -49.291748, -49.2258711,
-49.2660851, -49.3323251, -49.2660851, -49.2778063, -49.325983,
-49.3627765, -49.2613339, -49.2660851, -49.276539, -49.3424738,
-49.2461329, -49.291748, -49.3475488, -49.2461329, -49.2892129,
-49.2549997, -49.312033, -49.291748, -49.2106794, -49.2660851,
-49.291748, -49.2396049, -49.2845466, -49.3272514, -49.2157429,
-49.1916955, -49.2714702, -49.2461329, -48.4623764, -49.260067,
-49.2056164, -49.2220728, -49.2056164, -49.1752478, -49.3018896,
-49.2499328, -49.2220728, -49.1651285, -49.3069611, -49.3272514,
-49.2514329, -49.2664018, -49.3297882, -49.3526243, -49.2660851,
-49.2106794, -49.2106794, -49.2575333, -49.3018896, -49.2511995,
-49.2866779, -49.3412051, -49.3323251, -49.5076392, -49.2866779,
-49.3018896, -49.3069611, -49.3221781, -49.3323251, -49.2594132,
NA, -49.3323251, -49.224605, -49.2461329, -49.2562665, -49.2816082,
-49.2988176, -49.1967573, -49.291748, -49.2664018, -49.1777779,
-49.2660851, -49.1803081, -49.3424738, -49.2714702, -49.3627765,
-49.3729306, -49.3627765, -49.2410668, -49.2461329, -49.2499328,
-49.2220728, -48.5416503, -49.2106794, -49.1499529, -49.3272514,
-49.2461329, -49.2613339, -49.3221781, -49.1765128, -49.2511995,
-49.2660851, -49.2778063, -54.3372522, -49.291748, -49.2660851,
-49.2220728, -49.2664018, -49.291748, -49.2258711, -49.3323251,
-49.1904301, -49.2511995, -49.2136589, -49.2309359, -49.2968186,
-49.2056164, -49.2532217, -49.276539, -49.329004, -49.2575333,
-49.2157429, -49.224605, -49.2511995, -49.3323251, -49.3069611,
-49.2714702, -49.2461329, -49.3323251, -49.3171053, -49.2866779,
-49.1499529, -49.2309359, -49.3323251, -49.1651285, -49.2660851,
-49.2056164, -49.3412051, -49.2109844, -49.2157429, -49.2660851,
-49.2360011, -49.3297882, -49.2056164, -49.2816082, -49.2778063,
-49.2748814, -49.3627765, -49.2660851, -49.3412051, -49.260067,
-49.2182748, -49.1904301, -49.3526243, -49.2056164, NA, -49.2562665,
-49.3500865, -49.2660851, -49.3018896, -49.3500865, -49.3018896,
-49.2866779, -49.1853689, -49.2632284, -49.1588048, -49.3069611,
-49.3272514, -49.3221781, -49.3323251, -49.2816082, -49.2056164,
-49.2396049, -49.2816082, -49.3412051, -49.2892129, -49.260067,
-49.2993541, -49.2056164, -49.2056164, -49.2714702, -49.3018896,
-49.1904301, -49.2056164, -49.2410668, -49.2613339, -49.3196417,
-49.3412051, -49.2892129, -49.2461329, -49.210668, -49.2360011,
-49.3500865, -49.2714702, -49.2613339, -49.2993541, -49.2790736,
-49.3500865, -49.1803081, -49.3323251, -49.3323251, -49.3018896,
-49.3323251, -49.3627765, -49.2461329, -49.1499529, -49.2660851,
-49.3018896, -49.1499529, -49.2613339, -49.2056164, -49.1651285,
-49.2702031, -49.260067, -49.2816082, -49.2106794, -49.2220728,
-49.3069611, -49.2866779, -49.2511995, -49.2119453, -49.2461329,
-49.3221781, -49.3500865, -49.3018896, -49.3272514, -49.2660851,
-49.2660851, -49.2660851, -49.3101898, -49.2660851, -49.2360011,
-49.3069611, -49.3272514, -49.155011, -49.2660851, -49.3323251,
-49.2845466, -49.2511995, -49.2778063, -49.2660851, -49.2993541,
-49.2461329, -49.3412051, -49.2511995, -49.291748, -49.2360011,
-49.3323251, -49.2309359, -49.3805473, -49.0713868, -49.1967573,
-49.2499328, -49.2660851, -49.3069611, -49.2056164, -49.291748,
-49.2271373, -49.276539, -49.276539, -49.1803081, -49.2714702,
-49.2258711, -49.276539, -49.3323251, -49.3323251, -49.329004,
-49.3221781, -49.2461329, -49.2702031, -49.3475488, -49.3424738,
-49.3729306, -49.3323251, -49.3729306, -49.3221781, -49.2778063,
-49.2660851, -49.276539, -49.2119453, -49.2660851, -49.3272514,
-49.329004, -49.2816082, -49.329004, -49.2866779, -49.3018896,
-49.2816082, -49.2461329, -49.3196417, -49.2803409, -49.2660851,
-49.2396049, -49.3018896, -49.3221781, -49.1803081, -49.2157429,
-49.2613339, -49.2790736, -49.155011, -49.2511995, -49.3221781,
-49.312033, -49.3069611, -49.2892129, -49.2664018, -49.2866779,
-49.2660851, -49.2778063, -49.2136589, -49.2562665, -49.2702031,
-49.260067, -49.2549997, -49.2410668, -49.155011, -49.2220728,
-49.2056164, -49.2866779, -49.2410668, -49.2660851, -49.1904301,
-49.3323251, -49.3757156, -49.3412051, -49.2664018, -49.3757156,
-49.3323251, -49.2499328, -49.2461329, -49.2816082, -49.3500865,
-49.2258711, -49.2660851, -49.2499328, -49.2511995, -49.2866779,
-49.2714702, -49.2461329, -49.3069611, -49.3373992, -49.3373992,
-49.3272514, -49.2048357, -49.2511995, -49.276539, -49.3500865,
-49.3221781, -49.2499328, -49.291748, -49.2549997, -49.2660851,
-49.3272514, -49.2797692, -49.2660851, -49.260067, -49.2258711,
-49.3018896, -49.2613339, -49.337925, -49.2208068, -49.2309359,
-49.2461329, -49.224605, -49.3627765, -49.1499529, -49.2866779,
-49.3069611, -49.1499529, -49.1803081, -49.2549997, -49.2220728,
-49.2613339, -49.3424738, NA, -49.2760834, -49.2660851, -49.2651348,
-49.2892129, -49.325983, -49.2664018, -49.2499328, -49.2664018,
NA, -49.3526243, -49.2660851, -49.291748, -49.2220728, -49.1862508,
-49.1853689, -49.2660851, -49.3272514, -49.2461329, -49.1967573,
-49.2119453, -49.2660851, -49.3171053, -49.2714702, -49.2461329,
-49.2511995, -49.2208068, -49.2660851, -49.2816082, -49.3297882,
-49.2309359, -49.3627765, -49.2410668, -49.3297882, -49.276539,
-49.3069611, -49.2056164, -49.1765128, -49.2347348, -49.1803081,
-49.2660851, -49.2157429, -49.2005539, -49.1967573, -49.2660851,
-49.2664018, -49.2664018, -49.2461329, -49.2660851, -49.2511995,
-49.2778063, -49.2309359, -49.3475488, -49.2816082, -49.3069611,
-49.2660851, -49.3297882, -49.3475488, -49.2714702, -49.3069611,
-49.2660851, -49.1765128, -49.3171053, -49.2157429, -49.3323251,
-49.2660851, -49.2360011, -49.2660851, -49.3272514, -49.3373992,
-49.2816082, -49.1916955, -49.2968186, -49.3526243, -49.2106794,
-49.2613339, -49.2511995, -49.2664018, -49.2360011, -49.1588048,
-49.276539, -49.2714702, -49.2993541, -49.2056164, -49.3018896,
-49.3221781, -49.3171053, -49.2157429, -49.2309359, -49.2056164,
-49.2258711, -49.2309359, -49.3069611, -49.2511995, -49.2963092,
-49.3627765, -49.3323251, -49.2701383, -49.2968186, -49.312033,
-49.2511995, -49.2676689, -49.329004, -49.2816082, -49.2632284,
-49.3069611, -49.3069611, -49.2157429, -49.1967573, -49.1904301,
-49.2056164, -49.3475488, -49.2664018, -49.2309359, -49.1803081,
-49.291748, -49.2511995, -49.3297882, -49.2056164, -49.1137911,
-49.3475488, -49.2968186, -49.2258711, -49.2803409, -49.1803081,
-49.3272514, -49.2816082, -49.3018896, -49.2816082, -49.1803081,
-49.2363149, -49.3056932, -49.1916955, -49.2065637, -49.3221781,
-49.3805473, -49.3323251, -49.2208068, -49.3373992, -49.2461329,
-49.2309359, -49.2309359, -49.1967573, -49.2056164, -49.3272514,
-49.2613339, -49.2461329, -49.2660851, -49.3412051, -49.3323251,
-49.2660851, -49.2106794, -49.2309359, -49.3627765, -49.2778063,
-49.2511995, -49.2136589, -49.3412051, -49.2511995, -49.2660851,
-49.2651348, -49.260067, -49.2714702, -49.2511995, -49.2660851,
-49.3924167, -49.325983, -49.2461329, -49.2816082, -49.2866779,
-49.3481818, -49.2360011, -49.228295, -49.2660851, -49.3272514,
-49.2106794, -49.2660851, -49.3412051, -49.3221781, -49.3757156,
-49.3272514, -49.3272514, -49.2660851, -49.3272514, -49.3221781,
-49.276539, -49.2119453, -49.3627765, -49.1651285, -49.309497,
-49.3412051, -49.2816082, -49.2664018, -49.3323251, -49.2816082,
-49.2271373, -49.2660851, -49.2136589, -49.1499529, -49.2968186,
-49.1916955, -49.2056164, -49.2816082, -49.260067, -49.2136589,
-49.3018896, -49.3272514, -49.276539, -49.2056164, -49.2106794,
-49.2968186, -49.3221781, -49.2660851, -49.2461329, -49.1499529,
-49.2968186, -49.3297882, -49.2511995, -49.1960148, -49.3272514,
-49.2511995, -49.2499328, -49.1916955, -49.1904301, -49.1588048,
-49.3373992, -49.3069611, -49.2347348, -49.2258711, -49.2511995,
-49.2182748, -49.2106794, -49.3323251, -49.3475488, -49.3475488,
-49.312033, -49.3018896, -49.2106794, -49.3526243, -49.2258711,
-49.2660851, -49.2309359, -49.1904301, -49.2005539, -49.2157429,
-49.291748, -49.3373992, -49.2220728, -49.2461329, -49.2660851,
-49.3221781, -49.3272514, -49.393244, -49.2613339, -49.2157429,
-49.2816082, -49.2879454, -49.3221781, -49.2714702, -49.3323251,
-49.1499529, -49.2660851, NA, -49.2816082, -49.3757156, -49.2968186,
-49.2360011, -49.1916955, -49.2816082, -49.2651348, -49.2816082,
-49.2660851, -49.1651285, -49.3221781, -49.3526243, -49.3627765,
-49.3627765, -49.2760972, -49.3475488, -49.2816082, -49.2660851,
-49.2968186, -49.2664018, -49.1803081, -49.2664018, -49.2461329,
-49.2778063, -49.2106794, -49.1853689, -49.2816082, -49.2660851,
-49.2410668, -49.2983729, -49.3526243, -49.2360011, -49.260067,
-49.2613339, -49.2664018, -49.2816082, -49.2866779, -49.2461329,
-49.2511995, -49.276539, -49.2499328, -49.2056164, -49.2714702,
-49.2106794, -49.2119453, -49.2714702, -49.2056164, -49.2511995,
-49.2208068, -49.3272514, -49.1651285, -49.2660851, -49.3757156,
-49.2136589, -49.2360011, -49.276539, -49.2309359, -49.3069611,
-49.2106794, -49.1904301, -49.291748, -49.155011, -49.106767,
-49.3475488, -49.2106794, -49.2660851, -49.2461329, -49.291748,
-49.2309359, -49.2660851, -49.2220728, -49.3297882, -49.2056164,
-49.2511995, -49.3018896, -49.2461329, -49.2106794, -49.2664018,
-49.3424738, -49.2511995, -49.260067, -49.3526243, -49.1651285,
-53.8447133, -49.2157429, -49.2660851, -49.2258711, -49.1904301,
-49.3221781, -49.2714702, -49.3171053, -49.3323251, -49.2993541,
-49.2396049, -49.2660851, -49.2136589, -49.291748, -49.312033,
-49.2410668, -49.3018896, -49.276539, -49.2461329, -49.2660851,
-49.3323251, -49.2499328, -49.2823316, -49.2676689, -49.276539,
-49.3526243, -49.2106794, -49.2664018, -49.2056164, -49.2613339,
-49.3729306, -49.2664018, -49.3069611, -49.2879454, -49.2816082,
-49.2005539, -49.2056164, -49.2511995, -49.1853689, -49.2702031,
-49.291748, -49.1904301, -49.3323251, -49.2930156, -49.3526243,
-49.2499328, -49.276539, -49.2664018, -49.2660851, -49.3424738,
-49.3526243, -49.3323251, -49.1967573, -49.2511995, -49.3069611,
-49.1967573, -49.2511995, -49.2562665, -49.2106794, -49.2562665,
-49.2220728, -49.3297882, -49.3272514, -49.3272514, -49.3424738,
-49.4148395, -49.2106794, -49.3297882, -49.3221781, -49.3323251,
-49.1980228, -49.1803081, -49.2613339, -49.1904301, -49.1803081,
-49.2220728, -49.2660851, -49.2613339, -49.291748, -49.2157429,
-49.2660851, -49.178307, -49.1588048, -49.3323251, -49.2664018,
-49.2220728, -49.3323251, -49.2613339, -49.2660851, -49.276539,
-49.2347348, -49.2220728, -49.2511995, -49.1916955, -49.3272514,
-49.2106794, -49.3323251, -49.3221781, -49.2258711, -49.2613339,
-49.2511995, -49.3196417, -49.2613339, -49.1575402, -49.2182748,
-49.3221781, -49.2879454, -49.2660851, -49.2271373, -49.2220728,
-49.3323251, -49.2660851, -49.3272514, -49.2664018, -49.2106794,
-49.3475488, -49.2816082, -49.2106794, -49.1916955, -49.2511995,
-49.3196417, -49.2511995, -49.2106794, -49.2613339, -49.2613339,
-49.3297882, -49.3323251, -49.276539, -49.2816082, -49.3475488,
-49.2664018, -49.2714702, -49.1499529, -49.2866779, -49.3018896,
-49.260067, -49.2391529, -49.312033, -49.2660851, -49.2106794,
-49.2309359, -49.2685838, -49.3373992, -49.2208068, -49.2410668,
-49.1904301, -49.2660851, -49.2660851, -49.2362777, -49.2660851,
-49.3323251, -49.3323251, -49.3323251, -49.1651285, -49.3475488,
-49.2660851, -49.3475488, -49.312033, -49.3602383, -49.3018896,
-49.2816082, -49.0935683, -49.2056164, -49.224605, -49.2208068,
-49.2258711, -49.3272514, -49.2461329, -49.2660851, -49.2660851,
-49.3526243, -49.3373992, -49.2660851, -49.3272514, -49.2410668,
-49.2511995, -49.329004, -49.176598, -49.2660851, -48.5096116,
-49.2613339, -49.2157429, -49.2461329, -49.2660851, -49.2461329,
-49.3272514, -49.2751417, -49.2410668, -49.3221781, -49.2702031,
-49.2660851, -49.3306037, -49.2660851, -49.2396049, -49.2511995,
-49.3373992, -49.2632284, -49.2157429, -49.3272514, -49.3627765,
-49.2660851, -49.2511995, -49.2930156, -49.2423333, -49.3323251,
-49.1499529, -49.3272514, -49.2056164, -49.2106794, -49.2056164,
-49.2660851, -49.1765128, NA, -49.3272514, -49.2660851, -49.291748,
NA, NA, -49.2866779, -49.2660851, -49.2562665, -49.3018896, -49.2660851,
NA, -49.2660851, -49.3018896, -49.323688, -49.260067, -49.2879454,
-49.3221781, -49.2660851, -49.1967573, -49.2660851, -49.2707125,
-49.3323251, -49.2499328, -49.2511995, -49.2714702, -49.2660851,
-49.2816082, -49.1967573, -49.291748, -49.3373992, -49.2660851,
-49.2106794, -49.3221781, -49.3069611, -49.2790736, -49.393244,
-49.1803081, -49.2157429, -49.2660851, -49.3323251, -49.3056932,
-49.2106794, NA, -49.2660851, -49.3272514, -49.2816082, -49.3424738,
-49.2613339, -49.291748, -49.3018896, -49.2968186, -49.3323251,
-49.2816082, -49.2714702, NA, -49.2220728, NA, -49.2664018, -49.3221781,
-49.2182748, -49.2866779, -49.2106794, -49.2778063, -49.2660851,
NA, -49.3018896, -49.3018896, -49.2660851, -49.1853689, -49.2816082,
-49.2423333, -49.3272514, -49.2660851, -49.3475488, -49.2258711,
-49.2182748, -49.2499328, -49.3323251, -49.2360011, -49.2664018,
-49.2309359, -49.2511995, -49.2660851, -49.2879454, -49.2664018,
-49.2892129, -49.329004, -49.276539, -49.1499529, -49.291748,
-49.3323251, -49.3009596, -49.2499328, -49.3272514, -49.1803081,
-49.2660851, -49.2660851, NA, -49.2056164, -49.291748, -49.2660851,
-49.3272514, -49.3203244, -49.2664018, -49.2803409, -49.1803081,
-49.2660851, -49.2511995, -49.2816082, -49.291748, -52.3978261,
-49.2660851, -49.2660851, -49.2714702, -49.2613339, -49.3627765,
NA, -49.3272514, -49.393244, -49.276539, -49.2892129, -49.2119453,
-49.329004, -49.3221781, -49.2562665, -49.3018896, -49.3323251,
-49.3323251, -49.2575333, -49.2056164, -49.260067, -49.2660851,
NA, -49.291748, -49.2136589, -49.3221781, -49.2816082, -49.2660851,
-49.3323251, -49.2866779, -49.2866779, -49.2660851, -49.3297882,
-49.3171053, -49.2660851, -49.2461329, -49.291748, -49.2660851,
-49.3729306, -49.276539, -49.3018896, -49.147424, -49.2660851,
-49.2892129, -49.2660851, -49.2664018, -49.2119453, -49.2220728,
-49.1803081, -50.61765, -49.2968186, -49.2511995, -49.2660851,
-49.2714702, -49.2660851, -49.2845466, -49.3221781, -49.2660851,
NA, -49.2816082, -49.3729306, -49.2778063, -49.2664018, -49.2778063,
-49.3526243, -49.325983, -49.1651285, -49.3221781, -49.2575333,
-49.2660851, -49.2499328, -49.3069611, -49.276539, -49.2461329,
-49.2511995, -49.2660851, -49.1575402, -49.3323251, -49.2660851,
-49.2660851, -49.3323251, -49.3323251, -49.2660851, -49.276539,
-49.1651285, -49.2660851, -49.3323251, -49.2660851, -49.2664018,
-49.2660851, -49.2816082, -49.2660851, -49.2613339, -49.2056164,
-49.2660851, -49.1499529, -49.3323251, -49.1960148, -49.1853689,
-49.2360011, -49.2702031, NA, -49.2660851, -49.2410668, -49.2660851,
-49.2866779, -49.2816082, -49.1980228, -49.2106794, -49.1765128,
-49.3373992, -49.3297882, -49.2866779, -49.3221781, -49.2660851,
-49.2182748, -49.2660851, -49.1588048, -49.2208068, -49.2856508,
-49.2778063, -49.329004, -49.2056164, -49.260067, -49.291748,
-49.2660851, -49.3412051, -49.2660851, -49.2660851), lat = c(-25.371596,
-25.4741948, -25.4007895, -25.4897917, -25.4461553, -25.5435704,
-25.5584612, -25.5123456, -25.542697, -25.4482171, -25.3541729,
-25.4814604, -25.5437894, -25.5039825, -25.5529765, -25.4715469,
-25.442495, -25.4835974, -25.5954269, -25.3985569, -25.4256165,
-25.4798276, -25.4149238, -25.3541729, -25.5441946, -25.4694879,
-25.4618445, -25.4607546, -25.4549875, -25.314294, -25.404689,
-25.3782501, -25.5584612, -25.4570094, -25.4568622, -25.4137719,
-25.4922636, -25.5588709, -25.3355143, -25.3899986, -25.4370981,
-25.645646, -25.5584612, -25.4237277, -25.4237277, -25.3923531,
-25.5123456, -25.4922636, -25.445388, -25.5529765, -25.4128549,
-25.4922636, -25.542697, -25.5709555, -25.3597747, -25.4237277,
-25.4792761, -25.3547073, -25.4417996, -25.3782501, -25.4712796,
-25.511838, -25.5125457, -25.5176381, -25.4632669, -25.4568622,
-25.3737343, -25.5954269, -25.4237277, -25.533311, -25.4747233,
-25.4922636, -25.4967722, -25.503554, -25.3981207, -25.5176381,
-25.538389, -25.5488845, -25.4302181, -25.4821393, -25.4237277,
-25.485098, -25.4128549, -25.3877968, -25.520314, -25.4997715,
-25.4237277, -25.6214954, -23.7053068, -25.4612077, -25.4511883,
-25.449246, -25.5257215, -25.3877968, -25.5125457, -25.538389,
-25.4237277, -25.3985569, -25.485098, -25.5025633, -25.4549875,
-25.4712796, -25.3855732, -25.3804118, -25.3145131, -25.533311,
-25.3850202, -25.4007895, -25.4863988, -25.4237277, -25.4975339,
-25.4237277, -25.5123456, -25.5039825, -25.5027987, -25.4835974,
-25.4237277, -25.4691128, -25.4225859, -25.5176381, -25.4007895,
-25.404689, -25.3415442, -25.314294, -25.3922016, -25.441814,
-25.4497275, -25.4128549, -25.4237277, -25.5769445, -25.3923531,
-25.3387476, -25.4860503, -25.5025633, -25.4712796, -25.4282779,
-25.4442728, -25.6803592, -25.533311, -25.4893903, -25.4570094,
-25.4893903, -25.4009993, -25.511838, -25.5348061, -25.4570094,
-25.4367121, -25.4792761, -25.4860503, -25.4958191, -25.5488845,
-25.5529765, -25.4798276, -25.4237277, -25.3737343, -25.4128549,
-25.4370769, -25.5216248, -25.485098, -25.5263222, -25.4821393,
-25.4975339, -25.2346654, -25.4920729, -25.5216248, -25.645646,
-25.4941462, -25.5709555, -25.4588316, NA, -25.5954269, -25.4505217,
-25.3708973, -25.3547073, -25.5441946, -25.3452194, -25.4607546,
-25.5769445, -25.5488845, -25.414067, -25.4237277, -25.4417996,
-25.4715469, -25.5163403, -25.5027987, -25.5404629, -25.5027987,
-25.5257215, -25.3415442, -25.5348061, -25.4447841, -25.8106077,
-25.3737343, -25.3241055, -25.520314, -25.4442728, -25.4297854,
-25.4549875, -25.3659813, -25.5584612, -25.4237277, -25.4511883,
-24.8582476, -25.542697, -25.4237277, -25.4447841, -25.5488845,
-25.503554, -25.4863988, -25.5954269, -25.4158585, -25.5437894,
-25.4778939, -25.3609346, -25.3388506, -25.401382, -25.4088246,
-25.527821, -25.38426, -25.4370769, -25.5025633, -25.4505217,
-25.485098, -25.5709555, -25.4694879, -25.5163403, -25.4149238,
-25.5954269, -25.3945546, -25.5263222, -25.3241055, -25.5125457,
-25.5954269, -25.3829477, -25.4237277, -25.401382, -25.4821393,
-25.3619385, -25.5025633, -25.4237277, -25.5386937, -25.4061214,
-25.464945, -25.5441946, -25.5123456, -25.4326404, -25.4048578,
-25.4237277, -25.4821393, -25.533311, -25.3395906, -25.4354136,
-25.4406557, -25.4893903, NA, -25.3547073, -25.4618445, -25.4237277,
-25.4922636, -25.4618445, -25.5216248, -25.5263222, -25.3408298,
-25.3355143, -25.3796091, -25.4792761, -25.5105246, -25.4745672,
-25.4975339, -25.5441946, -25.401382, -25.3923531, -25.5588709,
-25.4821393, -25.314294, -25.533311, -25.6602153, -25.464945,
-25.445388, -25.5163403, -25.511838, -25.3669676, -25.4893903,
-25.5257215, -25.4835974, -25.667025, -25.4821393, -25.314294,
-25.3415442, -25.3071029, -25.5386937, -25.4618445, -25.5163403,
-25.5227299, -25.6602153, -25.3402858, -25.4618445, -25.3782501,
-25.5709555, -25.5954269, -25.4922636, -25.4975339, -25.5027987,
-25.3855732, -25.3241055, -25.4237277, -25.4922636, -25.4023029,
-25.4053236, -25.401382, -25.3829477, -25.4070604, -25.533311,
-25.5441946, -25.3737343, -25.4447841, -25.4792761, -25.5263222,
-25.485098, -25.4389499, -25.3855732, -25.4256165, -25.4618445,
-25.511838, -25.4468889, -25.4237277, -25.4237277, -25.4237277,
-25.6628855, -25.4237277, -25.4897917, -25.371596, -25.5105246,
-25.4528675, -25.4237277, -25.4975339, -25.3387476, -25.4019404,
-25.5123456, -25.4237277, -25.6602153, -25.4149238, -25.4821393,
-25.5437894, -25.4007895, -25.4897917, -25.5709555, -25.3609346,
-25.5993177, -25.4426113, -25.4607546, -25.5348061, -25.4237277,
-25.4792761, -25.4893903, -25.4007895, -25.4733752, -25.527821,
-25.4691128, -25.4564638, -25.3597747, -25.4863988, -25.3712508,
-25.5954269, -25.4632669, -25.38426, -25.4745672, -25.3415442,
-25.4070604, -25.4977191, -25.4715469, -25.5404629, -25.4975339,
-25.5404629, -25.4256165, -25.5123456, -25.4237277, -25.4691128,
-25.4389499, -25.4237277, -25.4860503, -25.38426, -25.4023021,
-25.38426, -25.5263222, -25.5216248, -25.5588709, -25.5176381,
-25.667025, -25.4202295, -25.4237277, -25.3923531, -25.4922636,
-25.4745672, -25.4417996, -25.5025633, -25.4835974, -25.3402858,
-25.4528675, -25.485098, -25.4745672, -25.3194337, -25.4694879,
-25.314294, -25.5488845, -25.5263222, -25.4237277, -25.5123456,
-25.4778939, -25.3693852, -25.4070604, -25.533311, -25.3922016,
-25.5257215, -25.4137719, -25.4447841, -25.464945, -25.4920729,
-25.5257215, -25.4237277, -25.4354136, -25.5709555, -25.4712653,
-25.4821393, -25.4608322, -25.4712653, -25.5709555, -25.5348061,
-25.5176381, -25.5588709, -25.4618445, -25.4863988, -25.4237277,
-25.5348061, -25.5437894, -25.5263222, -25.5163403, -25.4442728,
-25.4694879, -25.538389, -25.538389, -25.6426652, -25.4073656,
-25.4997715, -25.527821, -25.4618445, -25.4941462, -25.5348061,
-25.4007895, -25.3922016, -25.4237277, -25.4860503, -25.4428498,
-25.4237277, -25.533311, -25.4863988, -25.511838, -25.5227299,
-25.4985021, -25.465143, -25.3218022, -25.5176381, -25.4505217,
-25.4048578, -25.3241055, -25.4382479, -25.645646, -25.3241055,
-25.4564638, -25.3922016, -25.4570094, -25.5227299, -25.4715469,
NA, -25.429094, -25.4237277, -25.3172938, -25.314294, -25.5039825,
-25.5488845, -25.4761145, -25.4608322, NA, -25.4406557, -25.4237277,
-25.5769445, -25.4447841, -25.4595484, -25.3408298, -25.4237277,
-25.520314, -25.3415442, -25.4607546, -25.4389499, -25.4237277,
-25.3945546, -25.4967722, -25.3855732, -25.5584612, -25.3820079,
-25.4237277, -25.5441946, -25.5529765, -25.5125457, -25.4048578,
-25.5257215, -25.5529765, -25.527821, -25.371596, -25.4893903,
-25.3659813, -25.4734736, -25.4417996, -25.4237277, -25.5025633,
-25.3850202, -25.4607546, -25.4237277, -25.5488845, -25.5488845,
-25.3855732, -25.4237277, -25.5584612, -25.5123456, -25.5125457,
-25.404689, -25.5441946, -25.4792761, -25.4237277, -25.5529765,
-25.4977191, -25.4282779, -25.4548053, -25.4237277, -25.3659813,
-25.3945546, -25.5025633, -25.5709555, -25.4237277, -25.4897917,
-25.4237277, -25.5105246, -25.538389, -25.5441946, -25.4712796,
-25.4612077, -25.4798276, -25.3737343, -25.4297854, -25.4997715,
-25.4608322, -25.5386937, -25.3796091, -25.4788979, -25.3597747,
-25.6602153, -25.445388, -25.4922636, -25.4256165, -25.3945546,
-25.5025633, -25.5125457, -25.4893903, -25.4863988, -25.3218022,
-25.4792761, -25.5437894, -25.4543626, -25.4195501, -25.4632669,
-25.3461586, -25.4612077, -25.3194337, -25.4557498, -25.4355706,
-25.38426, -25.5588709, -25.3355143, -25.371596, -25.4792761,
-25.3949874, -25.4607546, -25.5625038, -25.4893903, -25.4977191,
-25.5488845, -25.5125457, -25.3782501, -25.4741948, -25.485098,
-25.5529765, -25.401382, -25.491988, -25.4977191, -25.4612077,
-25.4863988, -25.430016, -25.3782501, -25.520314, -25.461021,
-25.4922636, -25.5588709, -25.3782501, -25.4373835, -25.4335813,
-25.4712796, -25.4064768, -25.4549875, -25.5993177, -25.5709555,
-25.3820079, -25.538389, -25.3415442, -25.3609346, -25.5125457,
-25.4607546, -25.4893903, -25.4860503, -25.5227299, -25.5176381,
-25.4237277, -25.4821393, -25.4975339, -25.4237277, -25.3737343,
-25.3609346, -25.4195501, -25.5123456, -25.5584612, -25.4778939,
-25.4821393, -25.4557498, -25.4237277, -25.3172938, -25.533311,
-25.3597747, -25.4997715, -25.4237277, -25.4600921, -25.5039825,
-25.5176381, -25.5588709, -25.4920729, -25.4372477, -25.3772995,
-25.363444, -25.4237277, -25.4468889, -25.4568622, -25.4237277,
-25.4821393, -25.4941462, -25.4712653, -25.4468889, -25.4370981,
-25.4237277, -25.6426652, -25.4158258, -25.527821, -25.4389499,
-25.4195501, -25.3829477, -25.4091527, -25.4821393, -25.3876214,
-25.4608322, -25.5709555, -25.5441946, -25.4733752, -25.4237277,
-25.4778939, -25.431624, -25.4416325, -25.4712796, -25.464945,
-25.5441946, -25.533311, -25.4778939, -25.4237477, -25.520314,
-25.4788979, -25.401382, -25.3541729, -25.3388506, -25.4745672,
-25.4237277, -25.3855732, -25.431624, -25.3877968, -25.5529765,
-25.5584612, -25.3531282, -25.6426652, -25.5584612, -25.5348061,
-25.4712796, -25.4158585, -25.3796091, -25.538389, -25.4792761,
-25.4734736, -25.4863988, -25.4997715, -25.3395906, -25.3737343,
-25.5709555, -25.4977191, -25.4977191, -25.3194337, -25.511838,
-25.4275244, -25.4798276, -25.4863988, -25.4237277, -25.5125457,
-25.3669676, -25.3850202, -25.5416763, -25.4741948, -25.538389,
-25.4570094, -25.3855732, -25.4237277, -25.4745672, -25.4860503,
-25.5570359, -25.4297854, -25.5025633, -25.5441946, -25.4301074,
-25.4158258, -25.3597747, -25.4632669, -25.3241055, -25.4237277,
NA, -25.4023021, -25.4712653, -25.3877968, -25.4311031, -25.4712796,
-25.4023021, -25.3172938, -25.461021, -25.4237277, -25.3585076,
-25.4745672, -25.4798276, -25.4195501, -25.4195501, -25.4414357,
-25.404689, -25.5441946, -25.4237277, -25.3877968, -25.5488845,
-25.4417996, -25.5488845, -25.5176381, -25.5123456, -25.4128549,
-25.3408298, -25.5588709, -25.4237277, -25.4621431, -25.4338299,
-25.4406557, -25.3772995, -25.533311, -25.5129471, -25.5488845,
-25.461021, -25.5263222, -25.5176381, -25.5437894, -25.3712508,
-25.5348061, -25.401382, -25.5163403, -25.4568622, -25.4389499,
-25.5163403, -25.4893903, -25.4997715, -25.4309125, -25.520314,
-25.3585076, -25.4237277, -25.4712653, -25.4778939, -25.4311031,
-25.4788979, -25.5125457, -25.4694879, -25.4568622, -25.3669676,
-25.4007895, -25.4137719, -25.448836, -25.4977191, -25.4128549,
-25.4237277, -25.5176381, -25.503554, -25.4147371, -25.4237277,
-25.4447841, -25.5529765, -25.401382, -25.5584612, -25.511838,
-25.3708973, -25.3737343, -25.3972295, -25.4715469, -25.4997715,
-25.533311, -25.4798276, -25.3829477, -25.1495815, -25.5025633,
-25.4237277, -25.4863988, -25.4354136, -25.4256165, -25.4967722,
-25.3945546, -25.5954269, -25.6602153, -25.3923531, -25.4237277,
-25.4778939, -25.4741948, -25.441814, -25.5257215, -25.511838,
-25.527821, -25.3855732, -25.4237277, -25.5709555, -25.4761145,
-25.350195, -25.4355706, -25.4788979, -25.4406557, -25.4568622,
-25.4608322, -25.4893903, -25.5227299, -25.5404629, -25.4608322,
-25.371596, -25.4301074, -25.3288944, -25.3850202, -25.445388,
-25.5437894, -25.3408298, -25.4070604, -25.542697, -25.4354136,
-25.4975339, -25.3804118, -25.4798276, -25.4761145, -25.4691128,
-25.4608322, -25.4237277, -25.4715469, -25.4406557, -25.5954269,
-25.4607546, -25.5437894, -25.645646, -25.4607546, -25.4997715,
-25.3693852, -25.3737343, -25.3693852, -25.4447841, -25.4061214,
-25.6426652, -25.6426652, -25.4715469, -25.5835712, -25.5253105,
-25.5529765, -25.4941462, -25.5954269, -25.5381718, -25.4564638,
-25.5129471, -25.3669676, -25.4564638, -25.4447841, -25.4237277,
-25.4835974, -25.4741948, -25.3949874, -25.4237277, -25.4253119,
-25.3796091, -25.5709555, -25.4608322, -25.4570094, -25.5954269,
-25.4297854, -25.4237277, -25.527821, -25.4734736, -25.4570094,
-25.4019404, -25.4712796, -25.4370981, -25.4568622, -25.5954269,
-25.4745672, -25.4863988, -25.5129471, -25.5437894, -25.667025,
-25.4297854, -25.5147985, -25.3395906, -25.4256165, -25.4301074,
-25.4237277, -25.4733752, -25.4447841, -25.5709555, -25.4237277,
-25.520314, -25.5488845, -25.4128549, -25.3899986, -25.5441946,
-25.4568622, -25.4712796, -25.5437894, -25.667025, -25.4997715,
-25.3541729, -25.4835974, -25.4297854, -25.5529765, -25.5954269,
-25.4788979, -25.5441946, -25.3899986, -25.5488845, -25.3597747,
-25.3241055, -25.4382479, -25.4922636, -25.533311, -25.3935639,
-25.441814, -25.4237277, -25.3541729, -25.5125457, -25.4579084,
-25.538389, -25.465143, -25.5257215, -25.3669676, -25.4237277,
-25.4237277, -25.5042838, -25.4237277, -25.5954269, -25.4632669,
-25.5709555, -25.3585076, -25.404689, -25.4237277, -25.4977191,
-25.5054406, -25.5435704, -25.5216248, -25.5441946, -25.4730761,
-25.445388, -25.4505217, -25.4309125, -25.4863988, -25.5105246,
-25.5176381, -25.4237277, -25.4237277, -25.4798276, -25.538389,
-25.4237277, -25.5105246, -25.5257215, -25.5437894, -25.38426,
-25.4316868, -25.4237277, -25.5106183, -25.4835974, -25.5025633,
-25.3415442, -25.4237277, -25.3855732, -25.4860503, -25.3710218,
-25.5257215, -25.4549875, -25.4070604, -25.4237277, -25.4824311,
-25.4237277, -25.3923531, -25.485098, -25.538389, -25.3355143,
-25.5025633, -25.520314, -25.4195501, -25.4237277, -25.485098,
-25.3804118, -25.4735715, -25.4975339, -25.3241055, -25.4370981,
-25.4893903, -25.4568622, -25.464945, -25.4237277, -25.3659813,
NA, -25.4468889, -25.4237277, -25.542697, NA, NA, -25.4920729,
-25.4237277, -25.3693852, -25.4237477, -25.4237277, NA, -25.4237277,
-25.511838, -25.5832769, -25.533311, -25.4301074, -25.4745672,
-25.4237277, -25.4607546, -25.4237277, -25.483406, -25.5954269,
-25.5348061, -25.5584612, -25.3597747, -25.4237277, -25.5441946,
-25.4607546, -25.4497275, -25.538389, -25.4237277, -25.4128549,
-25.4941462, -25.4694879, -25.3402858, -25.5570359, -25.5004538,
-25.5416763, -25.4237277, -25.5954269, -25.4335813, -25.4128549,
NA, -25.4237277, -25.4370981, -25.5441946, -25.4715469, -25.4297854,
-25.503554, -25.4237477, -25.4612077, -25.5709555, -25.5441946,
-25.5163403, NA, -25.4570094, NA, -25.4608322, -25.4549875, -25.3395906,
-25.4920729, -25.3541729, -25.5123456, -25.4237277, NA, -25.511838,
-25.511838, -25.4237277, -25.3408298, -25.461021, -25.4735715,
-25.4370981, -25.4237277, -25.404689, -25.4863988, -25.3395906,
-25.4761145, -25.4632669, -25.4897917, -25.4461553, -25.3218022,
-25.4997715, -25.4237277, -25.4301074, -25.4608322, -25.314294,
-25.38426, -25.4691128, -25.3241055, -25.4497275, -25.4632669,
-25.4979285, -25.5348061, -25.5105246, -25.4564638, -25.4237277,
-25.4237277, NA, -25.4893903, -25.4154713, -25.4237277, -25.520314,
-25.4765441, -25.4461553, -25.430016, -25.4417996, -25.4237277,
-25.4997715, -25.5588709, -25.503554, -25.3943643, -25.4237277,
-25.4237277, -25.3597747, -25.5129471, -25.4195501, NA, -25.4860503,
-25.5570359, -25.527821, -25.314294, -25.4389499, -25.38426,
-25.4256165, -25.3693852, -25.5216248, -25.5709555, -25.4975339,
-25.4370769, -25.401382, -25.533311, -25.4237277, NA, -25.4007895,
-25.4778939, -25.4745672, -25.5441946, -25.4237277, -25.4632669,
-25.4920729, -25.4920729, -25.4237277, -25.5529765, -25.3945546,
-25.4237277, -25.5176381, -25.503554, -25.4237277, -25.6139081,
-25.4691128, -25.5216248, -25.4918548, -25.4237277, -25.314294,
-25.4237277, -25.4461553, -25.4389499, -25.4447841, -25.4417996,
-24.3287937, -25.3388506, -25.5437894, -25.4237277, -25.5163403,
-25.4237277, -25.3387476, -25.4256165, -25.4237277, NA, -25.5441946,
-25.5404629, -25.5123456, -25.5488845, -25.4144905, -25.4406557,
-25.5039825, -25.4367121, -25.4941462, -25.4370769, -25.4237277,
-25.5348061, -25.4548053, -25.4788979, -25.3855732, -25.4557498,
-25.4237277, -25.5147985, -25.4632669, -25.4237277, -25.4237277,
-25.5954269, -25.4975339, -25.4237277, -25.3712508, -25.3829477,
-25.4237277, -25.4975339, -25.4237277, -25.5488845, -25.4237277,
-25.5441946, -25.4237277, -25.4835974, -25.464945, -25.4237277,
-25.3241055, -25.4975339, -25.3531282, -25.3408298, -25.3772995,
-25.4070604, NA, -25.4237277, -25.5257215, -25.4237277, -25.4920729,
-25.461021, -25.5381718, -25.4128549, -25.3659813, -25.538389,
-25.4061214, -25.4920729, -25.4158258, -25.4237277, -25.3395906,
-25.4237277, -25.3796091, -25.3820079, -25.4367711, -25.5123456,
-25.38426, -25.4893903, -25.533311, -25.4497275, -25.4237277,
-25.4821393, -25.4237277, -25.4237277)),
.Names = c("lon", "lat"), class = "data.frame", row.names = c(NA, -1271L))

# Mantém apenas os CEPs dentro da delimitação.
latlon <- cbind(cep = unique(nef$AP_CEPPCN),
                latlon)
head(latlon)

# Dois cliques para delimitar um retangulo só com dados de CWB.
# plot(lat ~ lon, data = latlon)
# bb <- locator(n = 2, type = "p", pch = 3)
# dput(bb)

# Coordenadas que definem um retângulo contendo dados de Curitiba.
bb <- structure(list(x = c(-49.4651796798985, -48.9478976959142),
                     y = c(-25.2595489355597, -25.7423278381743)),
                .Names = c("x", "y"))

# Exibe a delimitação.
plot(lat ~ lon, data = latlon)
rect(xleft = bb$x[1],
     xright = bb$x[2],
     ybottom = bb$y[1],
     ytop = bb$y[2])

# Mantém apenas as coordenadas dentro da delimitação.
latlon <- subset(latlon,
                 lon >= min(bb$x) &
                 lon <= max(bb$x) &
                 lat >= min(bb$y) &
                 lat <= max(bb$y))
str(latlon)

# Verifica.
# plot(lat ~ lon, data = latlon)
#-----------------------------------------------------------------------
# Acrescentar o lat & lon nos registros da tabela.

dim(latlon)
dim(nef)

# Faz o pareamento.
i <- match(x = nef$AP_CEPPCN, table = latlon$cep)
length(i)

# Adiciona o lat & lon.
nef$lat <- latlon$lat[i]
nef$lon <- latlon$lon[i]

# Remove registros do hospital Pequeno Príncipe.
nef <- subset(nef, nef$AP_CODUNI != 15563)

# nef$AP_PRIPAL
# Troca código da causa principal pelo significado.
pp <- c("305010107" = "Hemodiálise",
        "305010115" = "Hemodiálise - HIV",
        "305010166" = "Acompanhamento domiciliar",
        "305010182" = "Treinamento de paciente ")
nef$AP_PRIPAL <- pp[match(nef$AP_PRIPAL, as.integer(names(pp)))]

Dados dos hospitais que atendem hemodialise

#-----------------------------------------------------------------------
# Dados com coordenadas das unidades de atendimento de Curitiba.

hos <- read.csv2(file = "hosp.txt",
                 header = TRUE,
                 sep = ";",
                 fileEncoding = "latin1",
                 dec = ".",
                 colClasses = c(NA,
                                "character",
                                "numeric",
                                "numeric"))

# Remove hospital Pequeno Príncipe.
hos <- hos[-3, ]
hos

# Adiciona uma coluna com o nome do hospital do atendimento.
nef$hospname <- hos$name[match(x = nef$AP_CODUNI, table = hos$cod)]

# Gráfico de pareto de casos por hospital.
barchart(~sort(table(nef$hospname)),
         xlab = "Número de atendimentos",
         ylab = "Unidade de atendimento") +
    latticeExtra::layer(panel.abline(v = seq(0, max(x), by = 1000),
                                     col = "gray70"),
                        under = TRUE)

Distribuição cronológica dos atendimentos

#-----------------------------------------------------------------------
# Série temporal de atendimentos mensais.

# tbd <- as.data.frame(xtabs(~hospname + AP_CMP,
#                            data = subset(nef, AP_CMP > 201600)))
# str(tbd)

xtabs(~AP_CMP, data = subset(nef, AP_CMP > 201600))

tbd <- as.data.frame(xtabs(~hospname + AP_CMP + AP_PRIPAL,
                           data = subset(nef, AP_CMP > 201600)))
str(tbd)

xyplot(Freq ~ AP_CMP | AP_PRIPAL,
       groups = hospname,
       data = tbd,
       xlab = "Meses",
       ylab = "Número de atendimentos",
       as.table = TRUE,
       scales = list(x = list(rot = 90)),
       layout = c(NA, 2),
       auto.key = list(title = "Unidade de atendimento",
                       cex.title = 1.1,
                       columns = 2),
       type = "o")
xyplot(Freq ~ AP_CMP | hospname,
       groups = AP_PRIPAL,
       data = tbd,
       jitter.x = TRUE,
       layout = c(NA, 2),
       xlab = "Meses",
       ylab = "Número de atendimentos",
       as.table = TRUE,
       scales = list(x = list(rot = 90)),
       auto.key = list(title = "Causa principal",
                       cex.title = 1.1,
                       columns = 2),
       type = "o")

Distribuição espacial dos atendimentos

#-----------------------------------------------------------------------
# Distribuição espacial dos atendidos e hospitais.

# Define o esquema de cores.
# display.brewer.all()
nh <- unique(nef$AP_CODUNI)
col <- brewer.pal(n = length(nh), name = "Set1")
pal <- colorFactor(palette = col, domain = nh)

mymap <- leaflet(data = subset(nef,
                               complete.cases(cbind(lat, lon)))) %>%
    addTiles() %>%
    addMarkers(lng = hos$lon,
               lat = hos$lat,
               popup = hos$name) %>%
    addCircleMarkers(
        radius = 6,
        color = ~pal(AP_CODUNI),
        stroke = FALSE,
        fillOpacity = 1) %>%
    addCircleMarkers(
        lng = hos$lon, lat = hos$lat,
        radius = 15,
        color = ~pal(hos$cod),
        stroke = TRUE,
        fillOpacity = 1)
mymap
#-----------------------------------------------------------------------
# Remove atendimentos sem lat lon.

table(is.na(nef$lat))
nef <- subset(nef, !is.na(nef$lat))

table(duplicated(nef[, c("lat", "lon")]))

table(duplicated(nef$AP_CNSPCN))

aten <- table(nef$AP_CNSPCN)
hist(aten,
     breaks = seq(min(aten, na.rm = TRUE),
                  max(aten, na.rm = TRUE) + 1,
                  by = 1) - 0.5,
     col = "seagreen",
     xlab = "Número de atendimentos no ano",
     ylab = "Frequência absoluta")
box()

Distâncias percorridas

#-----------------------------------------------------------------------
# Distâncias.

# Calcula a distância de cada paciente para hospital em que fez o
# procedimento.
nef$unidist <- NA
for (i in seq_len(nrow(nef))) {
    coordh <- hos[match(x = nef$AP_CODUNI[i], table = hos$cod),
                  c("lat", "lon")]
    coordp <- nef[i, c("lat", "lon")]
    if (!any(is.na(coordp))) {
        nef$unidist[i] <- dist(rbind(coordh, coordp)) * 100
    } else {
        nef$unidist[i] <- NA
    }
}

hist(nef$unidist,
     col = "gray50",
     main = NA,
     xlab = "Distância percorrida (km)",
     ylab = "Frequência absoluta")
rug(nef$unidist)
box()
plot(ecdf(nef$unidist), main = NA)
q <- c(0.5, 0.7, 0.9, 0.95)
qq <- quantile(nef$unidist, probs = q, na.rm = TRUE)
segments(y0 = 0, x0 = qq, y1 = q, x1 = qq, lty = 2)
segments(y0 = q, x0 = par()$usr[1], y1 = q, x1 = qq, lty = 2)
#-----------------------------------------------------------------------
# Calcula a distância de cada paciente para cada hospital.

alldist <- matrix(NA, nrow = nrow(nef), ncol = nrow(hos))
coordh <- hos[, c("lat", "lon")]

# i <- 10
for (i in seq_len(nrow(nef))) {
    coordp <- nef[i, c("lat", "lon")]
    if (!any(is.na(coordp))) {
        alldist[i, ] <-
            100 * apply(coordh,
                        MARGIN = 1,
                        FUN = function(x) {
                            dist(rbind(coordp, x))
                        })
    }
}

str(alldist)

# Menor distância.
nef$mindist <- apply(alldist, MARGIN = 1, min, na.rm = TRUE)
nef$mindist[!is.finite(nef$mindist)] <- NA
head(nef$mindist, n = 30)

# Distância mediana.
nef$meddist <- apply(alldist, MARGIN = 1, median, na.rm = TRUE)
nef$meddist[!is.finite(nef$meddist)] <- NA
head(nef$meddist, n = 30)
# d <- d[is.finite(d)]

# Custos.
nrow(nef)
a <- sum(nef$mindist) * 0.76
b <- sum(nef$unidist) * 0.76
b/a
(b - a)/nrow(nef)

# plot(ecdf(d))
# lines(ecdf(nef$unidist), col = 2)

# ecdfplot(~unidist + mindist + meddist | factor(AP_PRIPAL),
#          # layout = c(NA, 1),
#          data = nef,
#          xlab = "Distância (km)",
#          ylab = "Proporção acumulada",
#          xlim = c(0, 30),
#          auto.key = list(title = "Distância percorrida",
#                          cex.title = 1.1,
#                          text = c("Real", "Mínima", "Mediana")),
#          as.table = TRUE) +
#     latticeExtra::layer(panel.abline(v = seq(5, 25, 5),
#                                      h = seq(0, 1, 0.2),
#                                      col = "gray50", lty = 2),
#                         under = TRUE)

# ecdfplot(~unidist + mindist + meddist,
#          # layout = c(NA, 1),
#          data = nef,
#          xlab = "Distância (km)",
#          ylab = "Proporção acumulada",
#          xlim = c(0, 30),
#          lwd = c(2, 1, 1),
#          auto.key = list(title = "Distância percorrida",
#                          cex.title = 1.1,
#                          text = c("Real", "Mínima", "Mediana")),
#          as.table = TRUE) +
#     latticeExtra::layer(panel.abline(v = seq(5, 25, 5),
#                                      h = seq(0, 1, 0.2),
#                                      col = "gray50", lty = 2),
#                         under = TRUE)

ecdfplot(~unidist + mindist,
         # layout = c(NA, 1),
         data = nef,
         xlab = "Distância (km)",
         ylab = "Proporção acumulada",
         xlim = c(0, 30),
         lwd = c(2, 1),
         auto.key = list(title = "Distância percorrida",
                         cex.title = 1.1,
                         text = c("Real", "Mínima")),
         as.table = TRUE) +
    latticeExtra::layer(panel.abline(v = seq(5, 25, 5),
                                     h = seq(0, 1, 0.2),
                                     col = "gray50",
                                     lty = 2),
                        under = TRUE)

# Razão da soma das distância.
with(nef, {
    sum(unidist, na.rm = TRUE)/sum(mindist, na.rm = TRUE)
})

# Razão entre a distância atual e a distância mínima.
nef$ratiodist <- with(nef, {
    unidist/mindist
})

# Percentual de pessoas que vão no hospital mais próximo.
mean(nef$ratiodist <= 1, na.rm = TRUE)

# plot(ecdf(nef$ratiodist))

# ecdfplot(~ratiodist,
#          groups = AP_PRIPAL,
#          data = nef,
#          auto.key = TRUE,
#          as.table = TRUE)

# Média por principal causa da hemodialise.
ddply(nef,
      .(AP_PRIPAL),
      summarise,
      md = mean(ratiodist <= 1, na.rm = TRUE),
      N = length(ratiodist))

Alocação dos atendimentos

#-----------------------------------------------------------------------
# Matriz de confusão.

# xt <- xtabs(~AP_CMP + AP_CODUNI, data = nef)
# mosaicplot(t(xt))

# barplot(sort(prop.table(xtabs(~hospname, data = nef))))

# dim(alldist)
# dim(nef)

# Para onde deveria ter ido.
y <- hos$cod[apply(alldist,
                   MARGIN = 1,
                   FUN = function(x) {
                       i <- which.min(x)
                       if (length(i)) {
                           i
                       } else {
                           NA
                       }
                   })]

# Para onde ele foi.
x <- nef$AP_CODUNI

length(x) == length(y)

# sum(is.na(nef$AP_CODUNI))
# sum(is.na(y))

xtb <- table(x, y)

# Matriz de confusão.
xtb
dim(xtb)

rownames(xtb) <-
    hos$name[match(rownames(xtb), hos$cod)]
colnames(xtb) <-
    hos$name[match(colnames(xtb), hos$cod)]

# dim(xtb)
# xtb <- xtb[, -3]

# mosaicplot(xtb, col = col, off = 2)

sum(diag(xtb))
sum(xtb)
sum(diag(xtb))/sum(xtb)

xtb

#-----------------------------------------------------------------------

tbx <- merge(as.data.frame(table(x)),
             as.data.frame(table(y)),
             by.x = "x",
             by.y = "y")

tbx$hosname <- hos$name[match(tbx$x, hos$cod)]
# tbx$hosname <- hos$name

barchart(Freq.x + Freq.y ~ hosname,
         data = tbx,
         xlab = "Unidades de atendimento",
         ylab = "Número de atendimentos",
         scales = list(x = list(rot = 15)),
         auto.key = list(title = "Alocação",
                         cex.title = 1.1,
                         columns = 2,
                         text = c("Real", "Ideal"))) +
    latticeExtra::layer(panel.abline(h = seq(0, max(y), by = 500),
                                     col = "gray50",
                                     lty = 2),
                        under = TRUE)

# sum(with(tbx, Freq.x - Freq.y))
# sum(tbx$Freq.x) == sum(tbx$Freq.y)
# sum(tbx$Freq.x) == nrow(nef)

#-----------------------------------------------------------------------

# mosaicplot(xtb, off = FALSE)
# barplot(xtb, beside = TRUE)

xtbd <- as.data.frame(xtb)
str(xtbd)

barchart(Freq ~ y | x,
         data = xtbd,
         as.table = TRUE,
         scales = list(x = list(rot = 30)),
         ylab = "Total de atendimentos",
         xlab = "Unidades de atendimento",
         key = list(title = "Alocação",
                    cex.title = 1.1,
                    columns = 2,
                    text = list(c("Inadequado", "Adequado")),
                    rect = list(col = c("gray90", "gray50"))),
         panel = function(...) {
             col <- rep("gray90", 7)
             col[which.packet()] <- "gray50"
             panel.barchart(..., col = col)
         }) +
    latticeExtra::layer(panel.abline(h = seq(0, max(xtbd$Freq),
                                             by = 500),
                                     col = "gray50",
                                     lty = 2),
                        under = TRUE)

#-----------------------------------------------------------------------

z <- nef$AP_PRIPAL
xtb <- table(x, y, z)
xtb <- as.data.frame(xtb)
str(xtb)

xtb$x <- hos$name[match(xtb$x, hos$cod)]
xtb$y <- hos$name[match(xtb$y, hos$cod)]

str(xtb)
xtb$x <- factor(xtb$x, levels = hos$name)
xtb$y <- factor(xtb$y, levels = hos$name)

# combineLimits(
#     useOuterStrips(
#         barchart(Freq ~ y | x + z,
#                  data = xtb,
#                  as.table = TRUE,
#                  scales = list(relation = "free",
#                                x = list(rot = 30)),
#                  ylab = "Total de atendimentos",
#                  xlab = "Unidades de atendimento",
#                  key = list(title = "Alocação",
#                             cex.title = 1.1,
#                             columns = 2,
#                             text = list(c("Inadequado", "Adequado")),
#                             rect = list(col = mycol[1:2])),
#                  panel = function(...) {
#                      col <- rep(mycol[1], nrow(hos))
#                      col[which.packet()[1]] <- mycol[2]
#                      panel.barchart(..., col = col)
#                  })))

#-----------------------------------------------------------------------

Considerações finais

  • Subsidiar a distribuição dos pacientes nas UA.
  • Distribuição otimizada:
    • Redução de custos e riscos com transporte.
    • Aumento da qualidade e expectativa de vida.
  • Orientar investimentos:
    • Ampliação da capacidade das UA.
    • Criação de novas UA.
  • Aplicação portável e baseada em software livre.
  • Soluções de problemas sociais com Estatística.