Carregando pacotes
library(ggplot2)
library(dplyr)
library(knitr)
library(rmarkdown)
library(readxl)
library(kableExtra)
library(factoextra)
library(gridExtra)
Gerando os dados
## [1] "Município" "X0_a_6" "X6_a_9" "X9_a_12" "X12_a_15" "X15_a_18"
## [7] "maior_18"
## [1] "Município" "X0_a_6" "X6_a_9" "X9_a_12" "X12_a_15" "X15_a_18"
## [7] "maior_18"
Tabela com os dados
kable(head(dat.graf, 10), col.names = c("Municipio", "0 - 6","6 - 9","9 - 12","12 - 15",
"15 - 18","maior 18"))%>%
kable_styling(full_width = F, bootstrap_options = c("striped", "hover", "condensed", "responsive"))
| Municipio | 0 - 6 | 6 - 9 | 9 - 12 | 12 - 15 | 15 - 18 | maior 18 |
|---|---|---|---|---|---|---|
| Ubatuba | 37 | 46 | 7 | 7 | 0 | 0 |
| Caraguatatuba | 41 | 41 | 15 | 2 | 0 | 0 |
| Ilhabela | 50 | 39 | 9 | 0 | 0 | 0 |
| São Sebastião | 43 | 40 | 14 | 1 | 0 | 0 |
| Bertioga | 0 | 52 | 28 | 18 | 0 | 0 |
| Santos/Guarujá | 6 | 27 | 16 | 16 | 4 | 28 |
| São Vicente | 75 | 12 | 12 | 0 | 0 | 0 |
| Praia Grande | 60 | 39 | 0 | 0 | 0 | 0 |
| Mongaguá | 16 | 83 | 0 | 0 | 0 | 0 |
| Itanhaém | 0 | 85 | 14 | 0 | 0 | 0 |
Retirando e colocando o nome na primeira coluna
dados.quanti<- dat.graf[,-1] # Tirando o nome
dados.quanti
Colocando o nome
row.names(dados.quanti)= dat.graf[,1]
dados.quanti
Transformando em matriz
dados.quanti<-as.matrix(dados.quanti)
dados.quanti
## 0 - 6 6 - 9 9 - 12 12 - 15 15 - 18 maior 18
## Ubatuba 37 46 7 7 0 0
## Caraguatatuba 41 41 15 2 0 0
## Ilhabela 50 39 9 0 0 0
## São Sebastião 43 40 14 1 0 0
## Bertioga 0 52 28 18 0 0
## Santos/Guarujá 6 27 16 16 4 28
## São Vicente 75 12 12 0 0 0
## Praia Grande 60 39 0 0 0 0
## Mongaguá 16 83 0 0 0 0
## Itanhaém 0 85 14 0 0 0
## Peruíbe 31 43 25 0 0 0
## Iguape 38 60 0 0 0 0
## Ilha Comprida 66 34 0 0 0 0
## Cananéia 34 40 10 11 2 0
Cálculo das distâncias entre os casos
d<-dist(dados.quanti, method="euclidean")
d
## Ubatuba Caraguatatuba Ilhabela São Sebastião Bertioga
## Caraguatatuba 11.401754
## Ilhabela 16.462078 11.180340
## São Sebastião 12.529964 2.645751 8.717798
## Bertioga 44.350874 47.191101 57.913729 49.779514
## Santos/Guarujá 47.791213 49.173163 56.435804 50.665570 40.112342
## São Vicente 51.710734 44.833024 36.918830 42.579338 88.345911
## Praia Grande 26.000000 24.372115 13.453624 22.068076 69.835521
## Mongaguá 43.680659 51.166395 56.329388 52.678269 48.218254
## Itanhaém 54.662601 60.183054 68.124885 62.249498 40.112342
## Peruíbe 20.445048 14.422205 25.159491 16.583124 37.080992
## Iguape 17.175564 24.474477 25.806976 24.939928 51.146847
## Ilha Comprida 32.908965 30.049958 19.026298 27.604347 76.078906
## Cananéia 8.602325 12.649111 19.570386 14.177447 40.951190
## Santos/Guarujá São Vicente Praia Grande Mongaguá Itanhaém
## Caraguatatuba
## Ilhabela
## São Sebastião
## Bertioga
## Santos/Guarujá
## São Vicente 77.833155
## Praia Grande 66.121101 33.136083
## Mongaguá 67.438861 93.091353 62.225397
## Itanhaém 66.783231 104.680466 76.889531 21.354157
## Peruíbe 44.922155 55.371473 38.496753 49.497475 53.347915
## Iguape 58.523500 61.781874 30.413813 31.827661 47.592016
## Ilha Comprida 70.434367 26.627054 7.810250 70.007142 84.575410
## Cananéia 42.449971 50.931326 30.033315 48.969378 57.636794
## Peruíbe Iguape Ilha Comprida
## Caraguatatuba
## Ilhabela
## São Sebastião
## Bertioga
## Santos/Guarujá
## São Vicente
## Praia Grande
## Mongaguá
## Itanhaém
## Peruíbe
## Iguape 31.032241
## Ilha Comprida 43.943145 38.209946
## Cananéia 19.183326 25.317978 35.846897
Considerando a distância euclidiana ao quadrado
## Ubatuba Caraguatatuba Ilhabela São Sebastião Bertioga
## Caraguatatuba 130
## Ilhabela 271 125
## São Sebastião 157 7 76
## Bertioga 1967 2227 3354 2478
## Santos/Guarujá 2284 2418 3185 2567 1609
## São Vicente 2674 2010 1363 1813 7805
## Praia Grande 676 594 181 487 4877
## Mongaguá 1908 2618 3173 2775 2325
## Itanhaém 2988 3622 4641 3875 1609
## Peruíbe 418 208 633 275 1375
## Iguape 295 599 666 622 2616
## Ilha Comprida 1083 903 362 762 5788
## Cananéia 74 160 383 201 1677
## Santos/Guarujá São Vicente Praia Grande Mongaguá Itanhaém
## Caraguatatuba
## Ilhabela
## São Sebastião
## Bertioga
## Santos/Guarujá
## São Vicente 6058
## Praia Grande 4372 1098
## Mongaguá 4548 8666 3872
## Itanhaém 4460 10958 5912 456
## Peruíbe 2018 3066 1482 2450 2846
## Iguape 3425 3817 925 1013 2265
## Ilha Comprida 4961 709 61 4901 7153
## Cananéia 1802 2594 902 2398 3322
## Peruíbe Iguape Ilha Comprida
## Caraguatatuba
## Ilhabela
## São Sebastião
## Bertioga
## Santos/Guarujá
## São Vicente
## Praia Grande
## Mongaguá
## Itanhaém
## Peruíbe
## Iguape 963
## Ilha Comprida 1931 1460
## Cananéia 368 641 1285
\[ d_E(x,y) =\sqrt{(x_1-y_1)^2+...+(x_n-y_n)^2}=\sqrt{{\sum_{i=1}^n}(x_i-Y_i)^2} \]
Simples
grupos.s<- hclust(d2, method="single")
Completo
grupos.c<- hclust(d2, method="complete")
Ligação Média
grupos.m<- hclust(d2, method="average")
Método de ward ou ward.D (Depende da versão do R)
grupos.w<- hclust(d2, method="ward.D")
Considerando o método de ligação simples
clstr<- grupos.s
plot(clstr)
groups<-cutree(clstr, k=4)
rect.hclust(grupos.s, k=4, border = "red")
groups
## Ubatuba Caraguatatuba Ilhabela São Sebastião Bertioga
## 1 1 1 1 2
## Santos/Guarujá São Vicente Praia Grande Mongaguá Itanhaém
## 3 1 1 4 4
## Peruíbe Iguape Ilha Comprida Cananéia
## 1 1 1 1
Existe a opção de substituir no dendrograma o nome do objeto pelo número do grupo
plot(clstr, labels = as.character(groups))
Finalizando a Analise
plot(clstr)
r <- rect.hclust(clstr, 4)