Cluster Analysis

Grupo de Pesquisa - Prof. Dirceu Reis, UnB

by Cássio Rampinelli

13 de Outubro de 2021

OBJETIVOS

Os objetivos deste post são:

  • Apresentar um script inicial para uma análise de cluster para 63 bacias na bacia do rio São Francisco;

  • Variáveis consideradas: Coordenadas Geográficas, Média(w), Média (E0/P); Média (Q/P); Média(Q3[Q/P]-Q1[Q/P])

CARREGANDO PACOTES

#Clean R memory
rm(list=ls(all=TRUE))
############################################
#Loading packages to deal with maps#
############################################
#install.packages(maps)
library(maps)
#install.packages(mapdata)
library(mapdata)
############################################

############################################
#Loading clustering packages#
############################################
#install.packages((cluster))
library(cluster)
## 
## Attaching package: 'cluster'
## The following object is masked from 'package:maps':
## 
##     votes.repub
#install.packages(devtools)
library(devtools)
## Loading required package: usethis
devtools::install_github("kassambara/factoextra")
## Skipping install of 'factoextra' from a github remote, the SHA1 (1689fc74) has not changed since last install.
##   Use `force = TRUE` to force installation
library("factoextra")
## Loading required package: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa

ACESSANDO O BANCO DE DADOS E EXTRAINDO AS INFORMAÇÕES

setwd("E:/GA_Dirceu/Felipe")
data.Set<-read.table(file="Tabela_RHSF.txt",header=T,sep="\t")
knitr::kable(data.Set, col.names = gsub("[.]", " ", names(data.Set)))
Região por aproximação Estação Código da estação Latitude Longitude Mín w Q1 w Mediana w Média w Q3 w Máx w Q3 Q1 w Mín E0 P Q1 E0 P Mediana E0 P Média E0 P Q3 E0 P Máx E0 P Q3 Q1 E0 P Mín Q P Q1 Q P Mediana Q P Média Q P Q3 Q P Máx Q P Q3 Q1 Q P
5 1 40037000 -20.2156 -46.2322 1.514308 1.536707 1.579840 1.575616 1.607679 1.631433 0.0709723 0.9710513 0.9815990 0.9888933 0.9992123 1.015855 1.041346 0.0342563 0.5330215 0.5436427 0.5508239 0.5535076 0.5674175 0.5719827 0.0237748
5 2 40040000 -20.0953 -46.0622 1.619535 1.715655 1.741545 1.733955 1.754262 1.811112 0.0386071 1.0147138 1.0364808 1.0786599 1.0746477 1.103510 1.161930 0.0670288 0.4522004 0.4605616 0.4731951 0.4739446 0.4829042 0.5014871 0.0223427
5 3 40050000 -20.1700 -45.7158 1.705369 1.893871 1.953258 1.934295 2.000002 2.049997 0.1061315 0.9776253 1.0093356 1.0531283 1.0520865 1.073078 1.156257 0.0637420 0.3817590 0.3967063 0.4225908 0.4185116 0.4340151 0.4656242 0.0373088
5 4 40060001 -19.8875 -46.0169 1.989879 2.029046 2.313460 2.245233 2.406849 2.521074 0.3778029 0.8341509 0.8858713 0.9606404 0.9535782 1.019912 1.052831 0.1340403 0.3556270 0.3716833 0.3812388 0.3829062 0.3974917 0.4094330 0.0258084
5 5 40070000 -19.7761 -45.4792 1.910141 2.006979 2.066303 2.052491 2.093824 2.162296 0.0868446 0.9962446 1.0297097 1.0695675 1.0639161 1.083563 1.153334 0.0538529 0.3525980 0.3686143 0.3864845 0.3840770 0.4011784 0.4144422 0.0325641
5 6 40100000 -19.2814 -45.2753 1.982855 2.093417 2.150278 2.150088 2.217682 2.287789 0.1242646 1.0236328 1.0643297 1.1061220 1.1034485 1.127044 1.181647 0.0627146 0.3080925 0.3356518 0.3531145 0.3514682 0.3709236 0.3833278 0.0352718
5 7 40170000 -20.2169 -44.9181 2.095362 2.144747 2.290844 2.249889 2.335367 2.383232 0.1906203 1.0218082 1.0660717 1.1346429 1.1310330 1.183517 1.231119 0.1174454 0.2870190 0.2963476 0.3185987 0.3238982 0.3560507 0.3674961 0.0597032
5 8 40185000 -20.1847 -44.8933 2.101615 2.131448 2.315613 2.261954 2.370611 2.391608 0.2391628 1.0629806 1.1011511 1.1647353 1.1510392 1.191180 1.235631 0.0900285 0.2737914 0.2879834 0.2933297 0.3162593 0.3583206 0.3715590 0.0703372
5 9 40500000 -19.3317 -45.2214 2.434509 2.476277 2.516197 2.516847 2.542717 2.685808 0.0664392 1.1294491 1.1733024 1.2377641 1.2147470 1.245815 1.277073 0.0725126 0.2198984 0.2440026 0.2496291 0.2545899 0.2672528 0.2833291 0.0232503
5 10 40549998 -20.6039 -43.9086 1.911675 1.996725 2.184255 2.124448 2.245055 2.305338 0.2483296 1.0253092 1.0383729 1.0779720 1.0789612 1.112337 1.153791 0.0739642 0.3334946 0.3506574 0.3554934 0.3659193 0.3821844 0.4070809 0.0315270
5 11 40680000 -20.6611 -44.0722 1.786904 1.859932 1.978891 1.983275 2.055923 2.258575 0.1959914 0.9924341 1.0348249 1.0582386 1.0609784 1.086108 1.131769 0.0512826 0.3379897 0.3804245 0.4045835 0.4042459 0.4292902 0.4533129 0.0488657
5 12 40810350 -20.0942 -44.4936 2.334176 2.398727 2.481705 2.457067 2.510566 2.530437 0.1118387 1.0621306 1.0867832 1.1335631 1.1221612 1.154005 1.170884 0.0672219 0.2663502 0.2716938 0.2920581 0.2886778 0.3083017 0.3144959 0.0366079
5 13 40810800 -20.0869 -44.4381 2.243752 2.363645 2.437707 2.415805 2.467561 2.610738 0.1039152 1.0461397 1.1289098 1.1654396 1.1726441 1.201111 1.341734 0.0722009 0.2121116 0.2670024 0.2803145 0.2825989 0.3006141 0.3476788 0.0336117
5 14 40811100 -20.0475 -44.4089 2.354257 2.474307 2.518941 2.512879 2.577612 2.614108 0.1033058 1.0619492 1.1256531 1.1516081 1.1546558 1.178896 1.288485 0.0532424 0.2221193 0.2535258 0.2725484 0.2712882 0.2839606 0.3205327 0.0304349
5 15 40822995 -19.9850 -44.4389 2.846258 3.006445 3.072213 3.066218 3.175168 3.215936 0.1687228 1.0702626 1.1484539 1.1585635 1.1551734 1.165646 1.220968 0.0171919 0.1938964 0.1980361 0.2038756 0.2037303 0.2085280 0.2154808 0.0104918
5 16 40823500 -19.9614 -44.3661 2.231958 2.336278 2.436758 2.416162 2.521894 2.546832 0.1856156 1.0699792 1.1442133 1.1607895 1.1576522 1.171804 1.258683 0.0275903 0.2700190 0.2744489 0.2887116 0.2865738 0.2951240 0.3062768 0.0206750
5 17 41250000 -19.6875 -43.9206 2.108892 2.248207 2.376563 2.378434 2.487944 2.737786 0.2397365 1.1482069 1.1770942 1.1979084 1.2037977 1.224238 1.294013 0.0471441 0.2178118 0.2640819 0.2827658 0.2818165 0.3061328 0.3405824 0.0420509
5 18 41300000 -19.6536 -43.6875 1.904650 1.983605 2.125109 2.117803 2.247171 2.331037 0.2635658 1.0287058 1.0606989 1.0877581 1.0973767 1.129544 1.219204 0.0688450 0.2994813 0.3351561 0.3501586 0.3618233 0.3865687 0.4242917 0.0514126
5 19 41380000 -19.4619 -43.9036 2.140815 2.216303 2.312072 2.292173 2.357744 2.426671 0.1414404 1.1614035 1.2017107 1.2135837 1.2394606 1.259778 1.418617 0.0580672 0.2402201 0.2708004 0.2830931 0.2878840 0.3098751 0.3319541 0.0390747
5 20 41410000 -19.2311 -44.0247 2.193326 2.311757 2.351549 2.361852 2.407354 2.590115 0.0955962 1.0665885 1.0979739 1.1123550 1.1104888 1.122044 1.155116 0.0240704 0.2629945 0.3005322 0.3086743 0.3081567 0.3180335 0.3408165 0.0175014
5 21 41440005 -19.3714 -44.1528 2.191811 2.230237 2.239777 2.241930 2.248766 2.300257 0.0185288 1.1559190 1.1765961 1.2116910 1.2275920 1.254925 1.372477 0.0783290 0.2563367 0.2958097 0.3066496 0.3000981 0.3118148 0.3168879 0.0160051
5 22 41600000 -19.0111 -44.0383 2.221909 2.323748 2.367452 2.358921 2.394833 2.513205 0.0710845 1.0496923 1.0953074 1.1288377 1.1273712 1.150964 1.239782 0.0556562 0.2649687 0.2922425 0.3032259 0.3039077 0.3154021 0.3353028 0.0231596
5 23 41650002 -18.6728 -44.1939 2.298975 2.341423 2.378975 2.382701 2.408906 2.575260 0.0674827 1.0625463 1.1856270 1.2066755 1.1937121 1.222784 1.263692 0.0371572 0.2631533 0.2733676 0.2809910 0.2821308 0.2922887 0.2998254 0.0189211
5 24 41685000 -18.6042 -44.2847 2.439105 2.576166 2.654052 2.744172 2.972386 3.155575 0.3962196 1.3846444 1.4750933 1.5605683 1.5449299 1.608389 1.681457 0.1332961 0.0971907 0.1339512 0.1535039 0.1601713 0.1930586 0.2115293 0.0591074
5 25 41780002 -18.6450 -44.0506 1.663712 1.681722 1.700028 1.706406 1.725361 1.775812 0.0436388 1.0839505 1.2180279 1.2444975 1.2228430 1.258712 1.285038 0.0406840 0.4153381 0.4423792 0.4557255 0.4521717 0.4604370 0.4745437 0.0180578
5 26 41818000 -18.3061 -44.2258 2.221549 2.289974 2.323969 2.354570 2.418759 2.569612 0.1287854 1.0913363 1.1420048 1.2431647 1.2179510 1.268611 1.340268 0.1266066 0.2483141 0.2699008 0.2831574 0.2821658 0.2959835 0.3127786 0.0260827
5 27 41890000 -17.9964 -44.1775 1.505173 1.528945 1.580741 1.587310 1.628781 1.729148 0.0998366 1.4156376 1.5847562 1.6807927 1.6439816 1.739251 1.816555 0.1544951 0.3649021 0.4176105 0.4562867 0.4403152 0.4626616 0.4767280 0.0450511
5 28 41940000 -18.1908 -44.5556 2.057468 2.118544 2.171723 2.175578 2.189515 2.381318 0.0709707 1.5517847 1.5856979 1.6537796 1.6475256 1.701286 1.763775 0.1155882 0.2047873 0.2281686 0.2366753 0.2369349 0.2497933 0.2581432 0.0216246
5 29 41990000 -17.5961 -44.7133 2.048901 2.109099 2.162380 2.164503 2.228694 2.286264 0.1195950 1.1825618 1.2359782 1.3448563 1.3143586 1.372180 1.420743 0.1362015 0.2563092 0.2753206 0.2936344 0.2978712 0.3197810 0.3414997 0.0444604
4 30 42187000 -16.4669 -44.3761 2.803549 2.960975 3.044053 3.165465 3.316517 3.824952 0.3555414 1.2109087 1.4208002 1.6877790 1.6144071 1.801279 1.901557 0.3804793 0.0959398 0.1007321 0.1094364 0.1117385 0.1212790 0.1354252 0.0205469
4 31 42250000 -17.9150 -47.0108 1.759705 1.871084 2.017787 1.965856 2.058259 2.151770 0.1871751 1.0744705 1.1637799 1.1887995 1.1784629 1.208097 1.225254 0.0443173 0.3221079 0.3506077 0.3600251 0.3785874 0.4101080 0.4461181 0.0595004
4 32 42290000 -17.5025 -46.5711 2.326172 2.386720 2.541326 3.520066 4.979696 5.624542 2.5929762 0.8056469 0.8576098 1.1769600 1.0606651 1.219680 1.251033 0.3620704 0.1941090 0.2287885 0.2574404 0.2501165 0.2732785 0.2965104 0.0444899
4 33 42395000 -17.2550 -46.4728 2.231987 2.382786 2.499358 2.820177 3.447484 3.676991 1.0646984 0.8851943 0.9495739 1.1734189 1.1037237 1.217928 1.255022 0.2683542 0.2131885 0.2546480 0.2606980 0.2641674 0.2748538 0.3169062 0.0202058
4 34 42545500 -16.4925 -46.6686 2.148108 2.269585 2.399757 2.364695 2.435824 2.574133 0.1662394 1.1625366 1.2488323 1.3155713 1.2918215 1.336506 1.368799 0.0876737 0.2216459 0.2401188 0.2473997 0.2626503 0.2924071 0.3257587 0.0522883
4 35 42850000 -17.3506 -45.5325 1.973935 2.057805 2.148697 2.175229 2.296376 2.414711 0.2385713 1.1938066 1.2785651 1.3444988 1.3244732 1.385358 1.423222 0.1067927 0.2518607 0.2596138 0.2844879 0.2950978 0.3279781 0.3498327 0.0683643
4 36 43429998 -15.9178 -46.1192 2.176111 2.330913 2.396716 2.370069 2.449352 2.488509 0.1184392 1.1924039 1.2711702 1.3039207 1.3194054 1.371878 1.466439 0.1007078 0.2180381 0.2388239 0.2447706 0.2557020 0.2590814 0.3207860 0.0202575
4 37 43670000 -16.1331 -45.7417 2.143220 2.174365 2.394377 2.321714 2.441309 2.515792 0.2669440 1.2454935 1.3151364 1.3647082 1.3743657 1.426116 1.547877 0.1109798 0.1969771 0.2174281 0.2397954 0.2541605 0.2950561 0.3064042 0.0776280
4 38 43880000 -16.2811 -45.4142 2.131742 2.169285 2.276555 2.271624 2.373923 2.464526 0.2046379 1.2739936 1.3373391 1.4150484 1.4106048 1.469587 1.560664 0.1322482 0.2073884 0.2239410 0.2519424 0.2564434 0.2912866 0.3046215 0.0673456
3 39 45131000 -14.3136 -44.4594 1.665513 1.790494 2.146191 2.142952 2.411206 2.688815 0.6207127 1.1935463 1.2565536 1.4915576 1.4905064 1.678819 1.876982 0.4222652 0.2324815 0.2401407 0.2531564 0.2892216 0.3583853 0.3812054 0.1182446
3 40 45170001 -14.2642 -44.5225 1.630359 1.676468 1.704239 1.709900 1.730446 1.812601 0.0539777 1.7015457 1.7890147 1.9075050 1.8800405 1.961924 2.043961 0.1729093 0.2982958 0.3329458 0.3606038 0.3518615 0.3764750 0.3856534 0.0435292
3 41 45210000 -14.2808 -44.4097 1.749905 2.032102 2.182336 2.130017 2.300389 2.405188 0.2682876 1.3602291 1.3866542 1.4891758 1.5148809 1.614925 1.807869 0.2282704 0.2356783 0.2404171 0.2477178 0.2745861 0.3042772 0.3590583 0.0638601
3 42 45220000 -14.4236 -44.4831 2.470860 2.520062 2.555629 2.585864 2.652946 2.785651 0.1328834 1.5032007 1.6130636 1.8084487 1.7836743 1.886252 2.128338 0.2731881 0.1054233 0.1193607 0.1485695 0.1477808 0.1714351 0.1836208 0.0520744
3 43 45260000 -14.2600 -44.1522 1.870270 2.007008 2.215748 2.183693 2.325764 2.451793 0.3187557 1.4311951 1.4855888 1.6197847 1.6281422 1.732055 1.973120 0.2464662 0.2018244 0.2106444 0.2214075 0.2428125 0.2839807 0.3033746 0.0733363
2 44 45590000 -13.3406 -44.6386 2.062743 2.113210 2.138692 2.160351 2.225355 2.258837 0.1121448 1.4794173 1.6387350 1.6753068 1.6653616 1.715467 1.800450 0.0767319 0.2067417 0.2249801 0.2420105 0.2374772 0.2511937 0.2561981 0.0262136
2 45 45740001 -13.2853 -44.5617 2.548385 2.600731 2.647221 2.652476 2.679858 2.796913 0.0791275 1.4782666 1.6789165 1.7392901 1.7268287 1.781976 1.897780 0.1030591 0.1246298 0.1306465 0.1438269 0.1439483 0.1546699 0.1648175 0.0240233
2 46 45770000 -13.4528 -44.5689 1.867213 1.899723 1.912138 1.949033 2.003188 2.106252 0.1034659 1.4572503 1.6227370 1.6631854 1.6511241 1.691433 1.788270 0.0686963 0.2542405 0.2922527 0.3016133 0.2955246 0.3106238 0.3149410 0.0183711
2 47 45880000 -13.5586 -44.3031 1.864548 1.951984 2.008306 2.018202 2.092430 2.180514 0.1404462 1.5741052 1.7804613 1.8134898 1.7980465 1.839954 1.897572 0.0594932 0.2079219 0.2307568 0.2528259 0.2560917 0.2801553 0.2992697 0.0493985
2 48 45910001 -13.4006 -44.1989 2.242687 2.272755 2.295826 2.319680 2.368401 2.435912 0.0956463 1.4678998 1.6476290 1.7135022 1.6969020 1.763616 1.820660 0.1159867 0.1694414 0.1865364 0.2018562 0.2000571 0.2129062 0.2220193 0.0263699
2 49 45960001 -13.2914 -43.9089 2.257944 2.289650 2.304721 2.322550 2.347601 2.431273 0.0579511 1.4821512 1.6503227 1.7096058 1.6975752 1.769214 1.831012 0.1188918 0.1732228 0.1903574 0.2027041 0.1992312 0.2103161 0.2172976 0.0199587
1 50 46415000 -12.4306 -45.0856 2.552323 2.596106 2.637630 2.637881 2.660604 2.768277 0.0644981 1.4050008 1.5027074 1.5699464 1.5659530 1.640529 1.713869 0.1378217 0.1402341 0.1529638 0.1708958 0.1679395 0.1803428 0.1915442 0.0273789
1 51 46455000 -12.4106 -45.1222 2.347953 2.404361 2.596561 2.560382 2.697988 2.789708 0.2936262 1.2968585 1.3642780 1.4516226 1.4963121 1.621840 1.760965 0.2575622 0.1847375 0.1912866 0.1927485 0.1930110 0.1945153 0.2011608 0.0032287
1 52 46490000 -12.4044 -44.9531 3.635151 3.657063 3.692556 3.738067 3.814939 3.924747 0.1578756 1.7550477 1.9205472 1.9496876 1.9306663 1.984343 2.028442 0.0637956 0.0356145 0.0397720 0.0436643 0.0432425 0.0468768 0.0501672 0.0071048
1 53 46543000 -12.1358 -45.1033 2.309944 2.372440 2.403785 2.410527 2.434284 2.646361 0.0618435 1.2617681 1.3994531 1.4786166 1.4611177 1.539156 1.641932 0.1397028 0.2031773 0.2144883 0.2241529 0.2207324 0.2260715 0.2318241 0.0115832
1 54 46550000 -12.1525 -45.0094 2.964689 3.304702 3.353613 3.375623 3.451583 3.696462 0.1468815 1.3865526 1.4305749 1.5288948 1.5276515 1.610933 1.716522 0.1803580 0.0836955 0.0938716 0.1016092 0.1018720 0.1074592 0.1271868 0.0135876
1 55 46570000 -11.8953 -45.6081 3.448549 3.584449 3.712223 3.908116 4.193531 4.775177 0.6090821 1.1199757 1.2216202 1.3155380 1.2847388 1.358008 1.420500 0.1363879 0.1073133 0.1112881 0.1129394 0.1127737 0.1141148 0.1187805 0.0028267
1 56 46590000 -11.8561 -45.1200 2.497619 2.574294 2.620107 2.637440 2.684793 2.870494 0.1104987 1.3576027 1.4874201 1.5625869 1.5487977 1.624923 1.736735 0.1375034 0.1596200 0.1659452 0.1724848 0.1708757 0.1750351 0.1783061 0.0090899
1 57 46610000 -11.9794 -44.8772 2.656201 2.703562 2.733202 2.746495 2.765236 2.973240 0.0616740 1.4008771 1.5401697 1.5938861 1.5914007 1.672773 1.706997 0.1326032 0.1354397 0.1449634 0.1533130 0.1501381 0.1550218 0.1597645 0.0100585
1 58 46650000 -11.7208 -44.5022 2.707813 2.791443 2.852842 2.907103 3.021541 3.194093 0.2300988 1.4208014 1.4555698 1.5707097 1.5571878 1.644310 1.727508 0.1887401 0.1228230 0.1324660 0.1402007 0.1379248 0.1427256 0.1486539 0.0102596
1 59 46675000 -11.6106 -44.1567 2.782719 2.874605 2.934703 2.992835 3.131709 3.289594 0.2571034 1.4467090 1.4837984 1.6062790 1.5988230 1.687761 1.834545 0.2039629 0.1138691 0.1187160 0.1242287 0.1235812 0.1274820 0.1332269 0.0087661
1 60 46770000 -10.9936 -45.5278 2.219139 2.340967 2.492865 2.492633 2.540961 2.976663 0.1999939 1.6219831 1.7461938 1.8423587 1.8570552 1.990415 2.106780 0.2442210 0.1200686 0.1460980 0.1580429 0.1543102 0.1666259 0.1769410 0.0205279
1 61 46790000 -11.0514 -45.1969 1.942036 2.033424 2.124634 2.159790 2.184040 2.601803 0.1506161 1.6225127 1.7748936 1.8671018 1.8796119 2.020546 2.206691 0.2456526 0.1630395 0.2054144 0.2205263 0.2147308 0.2300581 0.2463092 0.0246437
1 62 46870000 -11.2356 -43.9494 2.265423 2.306601 2.392867 2.424634 2.497968 2.726866 0.1913664 1.7988364 1.8892153 1.9896523 1.9713547 2.049252 2.103254 0.1600365 0.1253737 0.1417972 0.1543022 0.1506524 0.1609103 0.1650709 0.0191131
1 63 46902000 -11.3450 -43.8261 2.790458 2.898036 2.981696 3.047905 3.200795 3.385257 0.3027596 1.4572261 1.4914865 1.6192059 1.6091154 1.703505 1.835663 0.2120181 0.1048976 0.1143988 0.1180859 0.1172190 0.1204625 0.1249270 0.0060637

VISUALIZANDO DADOS

############################################
#Plotting map of Brazil
############################################
map('worldHires','Brazil', xlim=c(-75.6,-33.6), ylim=c(-34.3,6), col='gray90', fill=TRUE)

############################################
#Plotting sites
############################################
coordinates<-cbind(data.Set$Longitude,data.Set$Latitude)
colnames(coordinates)<-c("long","lat")
coordinates<-as.data.frame(coordinates)
points(coordinates$long, coordinates$lat, pch=19, col="red", cex=0.5)  #plot my sample sites

CLUSTER ANALYSIS

Cluster by coordinates

Scaling data

df.scaled<-cbind(data.Set$Longitude,data.Set$Latitude)
col1<-(df.scaled[,1]-min(df.scaled[,1]))/(max(df.scaled[,1])-min(df.scaled[,1]))
col2<-(df.scaled[,2]-min(df.scaled[,2]))/(max(df.scaled[,2])-min(df.scaled[,2]))

df.scaled[,1]=col1
df.scaled[,2]=col2

df.scaled<-as.data.frame(df.scaled)
colnames(df.scaled)<-c("long","lat")
head(df.scaled)
##        long        lat
## 1 0.2342852 0.04608223
## 2 0.2854392 0.05852599
## 3 0.3896729 0.05079907
## 4 0.2990702 0.08002069
## 5 0.4608672 0.09154383
## 6 0.5222219 0.14271528

Scaling data

df.scaled<-cbind(data.Set$Longitude,data.Set$Latitude)
col1<-(df.scaled[,1]-min(df.scaled[,1]))/(max(df.scaled[,1])-min(df.scaled[,1]))
col2<-(df.scaled[,2]-min(df.scaled[,2]))/(max(df.scaled[,2])-min(df.scaled[,2]))

df.scaled[,1]=col1
df.scaled[,2]=col2

df.scaled<-as.data.frame(df.scaled)
colnames(df.scaled)<-c("scaled.long","scaled.lat")
head(df.scaled)
##   scaled.long scaled.lat
## 1   0.2342852 0.04608223
## 2   0.2854392 0.05852599
## 3   0.3896729 0.05079907
## 4   0.2990702 0.08002069
## 5   0.4608672 0.09154383
## 6   0.5222219 0.14271528

Dissimilarity Matrix - Euclidian Distance

#Euclidean Distance
#################################################
dist.eucl<-dist(df.scaled,method="euclidean")
#Visualizing the distance matrices
fviz_dist(dist.eucl)

K-Means - Number of Clusters

#Number of Clusters: Elbow method
fviz_nbclust(df.scaled,kmeans,method="wss")+geom_vline(xintercept=3,linetype=2)+labs(subtitle="Elbow method")

K-Means - 3 Clusters

#Compute k-means k=3
km.res<-kmeans(df.scaled,3,nstart=15)
km.res
## K-means clustering with 3 clusters of sizes 15, 23, 25
## 
## Cluster means:
##   scaled.long scaled.lat
## 1   0.3135057  0.2504012
## 2   0.8242933  0.1460240
## 3   0.7104384  0.8251742
## 
## Clustering vector:
##  [1] 1 1 1 1 1 1 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1
## [39] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
## 
## Within cluster sum of squares by cluster:
## [1] 0.7810200 0.4592320 0.8504986
##  (between_SS / total_SS =  80.7 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"
clusters<-km.res$cluster

fviz_cluster(km.res, data = df.scaled)

df.scaled.cluster<-cbind(clusters,coordinates$long, coordinates$lat)

colnames(df.scaled.cluster)<-c("clusters","long","lat")

df.scaled.cluster<-as.data.frame(df.scaled.cluster)

head(df.scaled.cluster)
##   clusters     long      lat
## 1        1 -46.2322 -20.2156
## 2        1 -46.0622 -20.0953
## 3        1 -45.7158 -20.1700
## 4        1 -46.0169 -19.8875
## 5        1 -45.4792 -19.7761
## 6        1 -45.2753 -19.2814
points.cluster.1<-df.scaled.cluster[which(df.scaled.cluster$clusters==1),]
colnames(points.cluster.1)<-c("cluster","long","lat")
points.cluster.1<-as.data.frame(points.cluster.1)

points.cluster.2<-df.scaled.cluster[which(df.scaled.cluster$clusters==2),]
colnames(points.cluster.2)<-c("cluster","long","lat")
points.cluster.2<-as.data.frame(points.cluster.2)


points.cluster.3<-df.scaled.cluster[which(df.scaled.cluster$clusters==3),]
colnames(points.cluster.3)<-c("cluster","long","lat")
points.cluster.3<-as.data.frame(points.cluster.3)

map('worldHires','Brazil', xlim=c(-75.6,-33.6), ylim=c(-34.3,6), col='gray90', fill=TRUE)
points(points.cluster.1$long, points.cluster.1$lat, pch=19, col="blue", cex=0.5)  #plot my sample sites
points(points.cluster.2$long, points.cluster.2$lat, pch=21, col="red", cex=0.5)  #plot my sample sites
points(points.cluster.3$long, points.cluster.3$lat, pch=22, col="green", cex=0.5)  #plot my sample sites


#Configurando a legenda
legend("topright", c("Cluster 1", "Cluster 2","Cluster 3"), col = c("blue","red","green"),
       pch = c(19,21,22))

K-Means - 2 Clusters

#Compute k-means k=2
km.res<-kmeans(df.scaled,2,nstart=15)
km.res
## K-means clustering with 2 clusters of sizes 25, 38
## 
## Cluster means:
##   scaled.long scaled.lat
## 1   0.7104384  0.8251742
## 2   0.6226666  0.1872255
## 
## Clustering vector:
##  [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## [39] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## 
## Within cluster sum of squares by cluster:
## [1] 0.8504986 3.7078972
##  (between_SS / total_SS =  57.8 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"
clusters<-km.res$cluster

fviz_cluster(km.res, data = df.scaled)

df.scaled.cluster<-cbind(clusters,coordinates$long, coordinates$lat)

colnames(df.scaled.cluster)<-c("clusters","long","lat")

df.scaled.cluster<-as.data.frame(df.scaled.cluster)

head(df.scaled.cluster)
##   clusters     long      lat
## 1        2 -46.2322 -20.2156
## 2        2 -46.0622 -20.0953
## 3        2 -45.7158 -20.1700
## 4        2 -46.0169 -19.8875
## 5        2 -45.4792 -19.7761
## 6        2 -45.2753 -19.2814
points.cluster.1<-df.scaled.cluster[which(df.scaled.cluster$clusters==1),]
colnames(points.cluster.1)<-c("cluster","long","lat")
points.cluster.1<-as.data.frame(points.cluster.1)

points.cluster.2<-df.scaled.cluster[which(df.scaled.cluster$clusters==2),]
colnames(points.cluster.2)<-c("cluster","long","lat")
points.cluster.2<-as.data.frame(points.cluster.2)

map('worldHires','Brazil', xlim=c(-75.6,-33.6), ylim=c(-34.3,6), col='gray90', fill=TRUE)
points(points.cluster.1$long, points.cluster.1$lat, pch=19, col="blue", cex=0.5)  #plot my sample sites
points(points.cluster.2$long, points.cluster.2$lat, pch=21, col="red", cex=0.5)  #plot my sample sites


#Configurando a legenda
legend("topright", c("Cluster 1", "Cluster 2"), col = c("blue","red"),
       pch = c(19,21))

Cluster by w; E0/P; Q/P

Scaling data

subs.set<-cbind(data.Set$Média..w.,data.Set$Média..E0.P.,data.Set$Média..Q.P.)

df.scaled<-0

df.scaled<-subs.set

col1=col2=col3=0

col1<-(df.scaled[,1]-min(df.scaled[,1]))/(max(df.scaled[,1])-min(df.scaled[,1]))
col2<-(df.scaled[,2]-min(df.scaled[,2]))/(max(df.scaled[,2])-min(df.scaled[,2]))
col3<-(df.scaled[,3]-min(df.scaled[,3]))/(max(df.scaled[,3])-min(df.scaled[,3]))


df.scaled[,1]=col1
df.scaled[,2]=col2
df.scaled[,3]=col3

colnames(df.scaled)<-c("w","E0/P","Q0/P")

head(df.scaled)
##               w       E0/P      Q0/P
## [1,] 0.00000000 0.04483707 1.0000000
## [2,] 0.06788383 0.11895490 0.8440751
## [3,] 0.15377450 0.09678773 0.7354396
## [4,] 0.28708123 0.00000000 0.6656613
## [5,] 0.20444814 0.10841078 0.6679557
## [6,] 0.24629062 0.14725268 0.6040502

Dissimilarity Matrix - Euclidian Distance

#Euclidean Distance
#################################################
dist.eucl<-dist(df.scaled,method="euclidean")
#Visualizing the distance matrices
fviz_dist(dist.eucl)

K-Means - Number of Clusters

#Number of Clusters: Elbow method
fviz_nbclust(df.scaled,kmeans,method="wss")+geom_vline(xintercept=3,linetype=2)+labs(subtitle="Elbow method")

K-Means - 3 Clusters

#Compute k-means k=3
km.res<-kmeans(df.scaled,3,nstart=15)
km.res
## K-means clustering with 3 clusters of sizes 15, 34, 14
## 
## Cluster means:
##           w      E0/P      Q0/P
## 1 0.2444478 0.7322860 0.4070958
## 2 0.3063365 0.2093014 0.5456739
## 3 0.6014762 0.6337619 0.1787336
## 
## Clustering vector:
##  [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 1 1 2 3 2 2 2 2 2 2 2 2
## [39] 1 1 1 3 1 1 3 1 1 1 1 3 3 3 1 3 3 3 3 3 3 1 1 1 3
## 
## Within cluster sum of squares by cluster:
## [1] 0.7720936 1.8391153 0.7871033
##  (between_SS / total_SS =  64.1 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"
clusters<-km.res$cluster

fviz_cluster(km.res, data = df.scaled)

df.scaled.cluster<-cbind(clusters,coordinates$long, coordinates$lat)

colnames(df.scaled.cluster)<-c("clusters","long","lat")

df.scaled.cluster<-as.data.frame(df.scaled.cluster)

head(df.scaled.cluster)
##   clusters     long      lat
## 1        2 -46.2322 -20.2156
## 2        2 -46.0622 -20.0953
## 3        2 -45.7158 -20.1700
## 4        2 -46.0169 -19.8875
## 5        2 -45.4792 -19.7761
## 6        2 -45.2753 -19.2814
points.cluster.1<-df.scaled.cluster[which(df.scaled.cluster$clusters==1),]
colnames(points.cluster.1)<-c("cluster","long","lat")
points.cluster.1<-as.data.frame(points.cluster.1)

points.cluster.2<-df.scaled.cluster[which(df.scaled.cluster$clusters==2),]
colnames(points.cluster.2)<-c("cluster","long","lat")
points.cluster.2<-as.data.frame(points.cluster.2)


points.cluster.3<-df.scaled.cluster[which(df.scaled.cluster$clusters==3),]
colnames(points.cluster.3)<-c("cluster","long","lat")
points.cluster.3<-as.data.frame(points.cluster.3)

map('worldHires','Brazil', xlim=c(-75.6,-33.6), ylim=c(-34.3,6), col='gray90', fill=TRUE)
points(points.cluster.1$long, points.cluster.1$lat, pch=19, col="blue", cex=0.5)  #plot my sample sites
points(points.cluster.2$long, points.cluster.2$lat, pch=21, col="red", cex=0.5)  #plot my sample sites
points(points.cluster.3$long, points.cluster.3$lat, pch=22, col="green", cex=0.5)  #plot my sample sites


#Configurando a legenda
legend("topright", c("Cluster 1", "Cluster 2","Cluster 3"), col = c("blue","red","green"),
       pch = c(19,21,22))

K-Means - 2 Clusters

#Compute k-means k=2
km.res<-kmeans(df.scaled,2,nstart=15)
km.res
## K-means clustering with 2 clusters of sizes 27, 36
## 
## Cluster means:
##           w      E0/P      Q0/P
## 1 0.4384866 0.6907801 0.2721658
## 2 0.2962135 0.2311706 0.5503650
## 
## Clustering vector:
##  [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 1 2 1 2 2 2 2 2 2 2 2
## [39] 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## 
## Within cluster sum of squares by cluster:
## [1] 2.409625 2.291579
##  (between_SS / total_SS =  50.3 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"
clusters<-km.res$cluster

fviz_cluster(km.res, data = df.scaled)

df.scaled.cluster<-cbind(clusters,coordinates$long, coordinates$lat)

colnames(df.scaled.cluster)<-c("clusters","long","lat")

df.scaled.cluster<-as.data.frame(df.scaled.cluster)

head(df.scaled.cluster)
##   clusters     long      lat
## 1        2 -46.2322 -20.2156
## 2        2 -46.0622 -20.0953
## 3        2 -45.7158 -20.1700
## 4        2 -46.0169 -19.8875
## 5        2 -45.4792 -19.7761
## 6        2 -45.2753 -19.2814
points.cluster.1<-df.scaled.cluster[which(df.scaled.cluster$clusters==1),]
colnames(points.cluster.1)<-c("cluster","long","lat")
points.cluster.1<-as.data.frame(points.cluster.1)

points.cluster.2<-df.scaled.cluster[which(df.scaled.cluster$clusters==2),]
colnames(points.cluster.2)<-c("cluster","long","lat")
points.cluster.2<-as.data.frame(points.cluster.2)

map('worldHires','Brazil', xlim=c(-75.6,-33.6), ylim=c(-34.3,6), col='gray90', fill=TRUE)
points(points.cluster.1$long, points.cluster.1$lat, pch=19, col="blue", cex=0.5)  #plot my sample sites
points(points.cluster.2$long, points.cluster.2$lat, pch=21, col="red", cex=0.5)  #plot my sample sites


#Configurando a legenda
legend("topright", c("Cluster 1", "Cluster 2"), col = c("blue","red"),
       pch = c(19,21))

Cluster by w; E0/P; Q/P; Q3(Q/P)-Q2(Q/P)

Scaling data

subs.set<-cbind(data.Set$Média..w.,data.Set$Média..E0.P.,data.Set$Média..Q.P.,data.Set$Q3.Q1..Q.P.)


df.scaled<-0

df.scaled<-subs.set

col1=col2=col3=0

col1<-(df.scaled[,1]-min(df.scaled[,1]))/(max(df.scaled[,1])-min(df.scaled[,1]))
col2<-(df.scaled[,2]-min(df.scaled[,2]))/(max(df.scaled[,2])-min(df.scaled[,2]))
col3<-(df.scaled[,3]-min(df.scaled[,3]))/(max(df.scaled[,3])-min(df.scaled[,3]))
col4<-(df.scaled[,4]-min(df.scaled[,4]))/(max(df.scaled[,4])-min(df.scaled[,4]))


df.scaled[,1]=col1
df.scaled[,2]=col2
df.scaled[,3]=col3
df.scaled[,4]=col4

colnames(df.scaled)<-c("w","E0/P","Q0/P","Q3-Q1")

head(df.scaled)
##               w       E0/P      Q0/P     Q3-Q1
## [1,] 0.00000000 0.04483707 1.0000000 0.1814977
## [2,] 0.06788383 0.11895490 0.8440751 0.1690895
## [3,] 0.15377450 0.09678773 0.7354396 0.2987587
## [4,] 0.28708123 0.00000000 0.6656613 0.1991175
## [5,] 0.20444814 0.10841078 0.6679557 0.2576499
## [6,] 0.24629062 0.14725268 0.6040502 0.2811095

Dissimilarity Matrix - Euclidian Distance

#Euclidean Distance
#################################################
dist.eucl<-dist(df.scaled,method="euclidean")
#Visualizing the distance matrices
fviz_dist(dist.eucl)

K-Means - Number of Clusters

#Number of Clusters: Elbow method
fviz_nbclust(df.scaled,kmeans,method="wss")+geom_vline(xintercept=3,linetype=2)+labs(subtitle="Elbow method")

K-Means - 3 Clusters

#Compute k-means k=3
km.res<-kmeans(df.scaled,3,nstart=15)
km.res
## K-means clustering with 3 clusters of sizes 10, 31, 22
## 
## Cluster means:
##           w      E0/P      Q0/P     Q3-Q1
## 1 0.2029375 0.6072437 0.4945840 0.5171310
## 2 0.3077453 0.1899790 0.5557471 0.2650719
## 3 0.4969702 0.6823372 0.2267099 0.1425506
## 
## Clustering vector:
##  [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 1 3 2 3 2 2 2 2 1 2 1 1
## [39] 1 1 1 3 1 3 3 1 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
## 
## Within cluster sum of squares by cluster:
## [1] 0.9770914 2.1588440 1.9312151
##  (between_SS / total_SS =  56.8 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"
clusters<-km.res$cluster

fviz_cluster(km.res, data = df.scaled)

df.scaled.cluster<-cbind(clusters,coordinates$long, coordinates$lat)

colnames(df.scaled.cluster)<-c("clusters","long","lat")

df.scaled.cluster<-as.data.frame(df.scaled.cluster)

head(df.scaled.cluster)
##   clusters     long      lat
## 1        2 -46.2322 -20.2156
## 2        2 -46.0622 -20.0953
## 3        2 -45.7158 -20.1700
## 4        2 -46.0169 -19.8875
## 5        2 -45.4792 -19.7761
## 6        2 -45.2753 -19.2814
points.cluster.1<-df.scaled.cluster[which(df.scaled.cluster$clusters==1),]
colnames(points.cluster.1)<-c("cluster","long","lat")
points.cluster.1<-as.data.frame(points.cluster.1)

points.cluster.2<-df.scaled.cluster[which(df.scaled.cluster$clusters==2),]
colnames(points.cluster.2)<-c("cluster","long","lat")
points.cluster.2<-as.data.frame(points.cluster.2)


points.cluster.3<-df.scaled.cluster[which(df.scaled.cluster$clusters==3),]
colnames(points.cluster.3)<-c("cluster","long","lat")
points.cluster.3<-as.data.frame(points.cluster.3)

map('worldHires','Brazil', xlim=c(-75.6,-33.6), ylim=c(-34.3,6), col='gray90', fill=TRUE)
points(points.cluster.1$long, points.cluster.1$lat, pch=19, col="blue", cex=0.5)  #plot my sample sites
points(points.cluster.2$long, points.cluster.2$lat, pch=21, col="red", cex=0.5)  #plot my sample sites
points(points.cluster.3$long, points.cluster.3$lat, pch=22, col="green", cex=0.5)  #plot my sample sites


#Configurando a legenda
legend("topright", c("Cluster 1", "Cluster 2","Cluster 3"), col = c("blue","red","green"),
       pch = c(19,21,22))

K-Means - 2 Clusters

#Compute k-means k=2
km.res<-kmeans(df.scaled,2,nstart=15)
km.res
## K-means clustering with 2 clusters of sizes 26, 37
## 
## Cluster means:
##           w      E0/P      Q0/P     Q3-Q1
## 1 0.4462097 0.6961370 0.2651960 0.1783792
## 2 0.2946316 0.2398282 0.5477438 0.3212647
## 
## Clustering vector:
##  [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 1 2 1 2 2 2 2 2 2 2 2
## [39] 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## 
## Within cluster sum of squares by cluster:
## [1] 2.927807 3.728618
##  (between_SS / total_SS =  43.2 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"
clusters<-km.res$cluster

fviz_cluster(km.res, data = df.scaled)

df.scaled.cluster<-cbind(clusters,coordinates$long, coordinates$lat)

colnames(df.scaled.cluster)<-c("clusters","long","lat")

df.scaled.cluster<-as.data.frame(df.scaled.cluster)

head(df.scaled.cluster)
##   clusters     long      lat
## 1        2 -46.2322 -20.2156
## 2        2 -46.0622 -20.0953
## 3        2 -45.7158 -20.1700
## 4        2 -46.0169 -19.8875
## 5        2 -45.4792 -19.7761
## 6        2 -45.2753 -19.2814
points.cluster.1<-df.scaled.cluster[which(df.scaled.cluster$clusters==1),]
colnames(points.cluster.1)<-c("cluster","long","lat")
points.cluster.1<-as.data.frame(points.cluster.1)

points.cluster.2<-df.scaled.cluster[which(df.scaled.cluster$clusters==2),]
colnames(points.cluster.2)<-c("cluster","long","lat")
points.cluster.2<-as.data.frame(points.cluster.2)

map('worldHires','Brazil', xlim=c(-75.6,-33.6), ylim=c(-34.3,6), col='gray90', fill=TRUE)
points(points.cluster.1$long, points.cluster.1$lat, pch=19, col="blue", cex=0.5)  #plot my sample sites
points(points.cluster.2$long, points.cluster.2$lat, pch=21, col="red", cex=0.5)  #plot my sample sites


#Configurando a legenda
legend("topright", c("Cluster 1", "Cluster 2"), col = c("blue","red"),
       pch = c(19,21))

```