library(vegan)
## Carregando pacotes exigidos: permute
## Carregando pacotes exigidos: lattice
## This is vegan 2.6-2
library(tidyverse)
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.8     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.1
## ✔ readr   2.1.2     ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(factoextra)
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
load("C:/Users/vahum/AppData/Local/R/win-library/4.2/rmarkdown/rmarkdown/templates/github_document/NEwR-2ed_code_data/NEwR2-Data/Doubs.RData")
spe
##    Cogo Satr Phph Babl Thth Teso Chna Pato Lele Sqce Baba Albi Gogo Eslu Pefl
## 1     0    3    0    0    0    0    0    0    0    0    0    0    0    0    0
## 2     0    5    4    3    0    0    0    0    0    0    0    0    0    0    0
## 3     0    5    5    5    0    0    0    0    0    0    0    0    0    1    0
## 4     0    4    5    5    0    0    0    0    0    1    0    0    1    2    2
## 5     0    2    3    2    0    0    0    0    5    2    0    0    2    4    4
## 6     0    3    4    5    0    0    0    0    1    2    0    0    1    1    1
## 7     0    5    4    5    0    0    0    0    1    1    0    0    0    0    0
## 8     0    0    0    0    0    0    0    0    0    0    0    0    0    0    0
## 9     0    0    1    3    0    0    0    0    0    5    0    0    0    0    0
## 10    0    1    4    4    0    0    0    0    2    2    0    0    1    0    0
## 11    1    3    4    1    1    0    0    0    0    1    0    0    0    0    0
## 12    2    5    4    4    2    0    0    0    0    1    0    0    0    0    0
## 13    2    5    5    2    3    2    0    0    0    0    0    0    0    0    0
## 14    3    5    5    4    4    3    0    0    0    1    1    0    1    1    0
## 15    3    4    4    5    2    4    0    0    3    3    2    0    2    0    0
## 16    2    3    3    5    0    5    0    4    5    2    2    1    2    1    1
## 17    1    2    4    4    1    2    1    4    3    2    3    4    1    1    2
## 18    1    1    3    3    1    1    1    3    2    3    3    3    2    1    3
## 19    0    0    3    5    0    1    2    3    2    1    2    2    4    1    1
## 20    0    0    1    2    0    0    2    2    2    3    4    3    4    2    2
## 21    0    0    1    1    0    0    2    2    2    2    4    2    5    3    3
## 22    0    0    0    1    0    0    3    2    3    4    5    1    5    3    4
## 23    0    0    0    0    0    0    0    0    0    1    0    0    0    0    0
## 24    0    0    0    0    0    0    1    0    0    2    0    0    1    0    0
## 25    0    0    0    0    0    0    0    0    1    1    0    0    2    1    0
## 26    0    0    0    1    0    0    1    0    1    2    2    1    3    2    1
## 27    0    0    0    1    0    0    1    1    2    3    4    1    4    4    1
## 28    0    0    0    1    0    0    1    1    2    4    3    1    4    3    2
## 29    0    1    1    1    1    1    2    2    3    4    5    3    5    5    4
## 30    0    0    0    0    0    0    1    2    3    3    3    5    5    4    5
##    Rham Legi Scer Cyca Titi Abbr Icme Gyce Ruru Blbj Alal Anan
## 1     0    0    0    0    0    0    0    0    0    0    0    0
## 2     0    0    0    0    0    0    0    0    0    0    0    0
## 3     0    0    0    0    0    0    0    0    0    0    0    0
## 4     0    0    0    0    1    0    0    0    0    0    0    0
## 5     0    0    2    0    3    0    0    0    5    0    0    0
## 6     0    0    0    0    2    0    0    0    1    0    0    0
## 7     0    0    0    0    0    0    0    0    0    0    0    0
## 8     0    0    0    0    0    0    0    0    0    0    0    0
## 9     0    0    0    0    1    0    0    0    4    0    0    0
## 10    0    0    0    0    0    0    0    0    0    0    0    0
## 11    0    0    0    0    0    0    0    0    0    0    0    0
## 12    0    0    0    0    0    0    0    0    0    0    0    0
## 13    0    0    0    0    0    0    0    0    0    0    0    0
## 14    0    0    0    0    0    0    0    0    0    0    0    0
## 15    0    0    0    0    1    0    0    0    0    0    0    0
## 16    0    1    0    1    1    0    0    0    1    0    0    0
## 17    1    1    0    1    1    0    0    0    2    0    2    1
## 18    2    1    0    1    1    0    0    1    2    0    2    1
## 19    2    1    1    1    2    1    0    1    5    1    3    1
## 20    3    2    2    1    4    1    0    2    5    2    5    2
## 21    3    2    2    2    4    3    1    3    5    3    5    2
## 22    3    3    2    3    4    4    2    4    5    4    5    2
## 23    0    0    0    0    0    0    0    0    1    0    2    0
## 24    0    1    0    0    0    0    0    2    2    1    5    0
## 25    0    0    1    0    0    0    0    1    1    0    3    0
## 26    2    2    1    1    3    2    1    4    4    2    5    2
## 27    3    3    1    2    5    3    2    5    5    4    5    3
## 28    4    4    2    4    4    3    3    5    5    5    5    4
## 29    5    5    2    3    3    4    4    5    5    4    5    4
## 30    5    3    5    5    5    5    5    5    5    5    5    5
env
##      dfs ele  slo   dis  pH har  pho  nit  amm  oxy  bod
## 1    0.3 934 48.0  0.84 7.9  45 0.01 0.20 0.00 12.2  2.7
## 2    2.2 932  3.0  1.00 8.0  40 0.02 0.20 0.10 10.3  1.9
## 3   10.2 914  3.7  1.80 8.3  52 0.05 0.22 0.05 10.5  3.5
## 4   18.5 854  3.2  2.53 8.0  72 0.10 0.21 0.00 11.0  1.3
## 5   21.5 849  2.3  2.64 8.1  84 0.38 0.52 0.20  8.0  6.2
## 6   32.4 846  3.2  2.86 7.9  60 0.20 0.15 0.00 10.2  5.3
## 7   36.8 841  6.6  4.00 8.1  88 0.07 0.15 0.00 11.1  2.2
## 8   49.1 792  2.5  1.30 8.1  94 0.20 0.41 0.12  7.0  8.1
## 9   70.5 752  1.2  4.80 8.0  90 0.30 0.82 0.12  7.2  5.2
## 10  99.0 617  9.9 10.00 7.7  82 0.06 0.75 0.01 10.0  4.3
## 11 123.4 483  4.1 19.90 8.1  96 0.30 1.60 0.00 11.5  2.7
## 12 132.4 477  1.6 20.00 7.9  86 0.04 0.50 0.00 12.2  3.0
## 13 143.6 450  2.1 21.10 8.1  98 0.06 0.52 0.00 12.4  2.4
## 14 152.2 434  1.2 21.20 8.3  98 0.27 1.23 0.00 12.3  3.8
## 15 164.5 415  0.5 23.00 8.6  86 0.40 1.00 0.00 11.7  2.1
## 16 185.9 375  2.0 16.10 8.0  88 0.20 2.00 0.05 10.3  2.7
## 17 198.5 349  0.5 24.30 8.0  92 0.20 2.50 0.20 10.2  4.6
## 18 211.0 333  0.8 25.00 8.0  90 0.50 2.20 0.20 10.3  2.8
## 19 224.6 310  0.5 25.90 8.1  84 0.60 2.20 0.15 10.6  3.3
## 20 247.7 286  0.8 26.80 8.0  86 0.30 3.00 0.30 10.3  2.8
## 21 282.1 262  1.0 27.20 7.9  85 0.20 2.20 0.10  9.0  4.1
## 22 294.0 254  1.4 27.90 8.1  88 0.20 1.62 0.07  9.1  4.8
## 23 304.3 246  1.2 28.80 8.1  97 2.60 3.50 1.15  6.3 16.4
## 24 314.7 241  0.3 29.76 8.0  99 1.40 2.50 0.60  5.2 12.3
## 25 327.8 231  0.5 38.70 7.9 100 4.22 6.20 1.80  4.1 16.7
## 26 356.9 214  0.5 39.10 7.9  94 1.43 3.00 0.30  6.2  8.9
## 27 373.2 206  1.2 39.60 8.1  90 0.58 3.00 0.26  7.2  6.3
## 28 394.7 195  0.3 43.20 8.3 100 0.74 4.00 0.30  8.1  4.5
## 29 422.0 183  0.6 67.70 7.8 110 0.45 1.62 0.10  9.0  4.2
## 30 453.0 172  0.2 69.00 8.2 109 0.65 1.60 0.10  8.2  4.4

1)

env_simp<-env[,-c(1,4)]
princomp(env_simp)-> pca_env
summary(pca_env)
## Importance of components:
##                             Comp.1       Comp.2       Comp.3       Comp.4
## Standard deviation     267.1096679 11.533521173 6.9246972888 4.0343429098
## Proportion of Variance   0.9972202  0.001859241 0.0006702139 0.0002274875
## Cumulative Proportion    0.9972202  0.999079484 0.9997496976 0.9999771850
##                              Comp.5       Comp.6       Comp.7       Comp.8
## Standard deviation     1.018804e+00 7.091927e-01 2.482529e-01 1.600339e-01
## Proportion of Variance 1.450751e-05 7.029758e-06 8.613915e-07 3.579612e-07
## Cumulative Proportion  9.999917e-01 9.999987e-01 9.999996e-01 9.999999e-01
##                              Comp.9
## Standard deviation     6.461191e-02
## Proportion of Variance 5.834949e-08
## Cumulative Proportion  1.000000e+00
pca_env$scores %>% 
  as.tibble() %>% 
  ggplot(aes(Comp.1))+geom_histogram()
## Warning: `as.tibble()` was deprecated in tibble 2.0.0.
## Please use `as_tibble()` instead.
## The signature and semantics have changed, see `?as_tibble`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

pca_env$scores %>% 
  as.tibble() %>% 
  mutate(setor=ifelse(Comp.1<0,"medio-baixo", "alto"))-> env_set
fviz_pca_ind(pca_env,
             geom.ind = "point", # show points only (nbut not "text")
             col.ind = env_set$setor, # color by groups
             palette = c("#00AFBB", "#E7B800"),
             addEllipses = TRUE, # Concentration ellipses
             legend.title = "Groups"
)

É possivel observar que o rio foi dividido em duas porções.

2)

spe_simp<-spe 
princomp(spe_simp)-> pca_spe
summary(pca_spe)
## Importance of components:
##                           Comp.1    Comp.2     Comp.3     Comp.4     Comp.5
## Standard deviation     6.5380634 2.8562806 1.92951554 1.62519017 1.36425689
## Proportion of Variance 0.6469085 0.1234657 0.05634315 0.03997173 0.02816676
## Cumulative Proportion  0.6469085 0.7703742 0.82671731 0.86668904 0.89485580
##                            Comp.6     Comp.7     Comp.8     Comp.9     Comp.10
## Standard deviation     1.30481115 1.08392781 0.98318157 0.79659944 0.699842885
## Proportion of Variance 0.02576558 0.01778055 0.01462891 0.00960339 0.007412175
## Cumulative Proportion  0.92062137 0.93840193 0.95303084 0.96263423 0.970046405
##                            Comp.11    Comp.12     Comp.13     Comp.14
## Standard deviation     0.664210686 0.59361733 0.573125360 0.492457727
## Proportion of Variance 0.006676614 0.00533283 0.004971001 0.003670139
## Cumulative Proportion  0.976723019 0.98205585 0.987026850 0.990696988
##                            Comp.15     Comp.16     Comp.17     Comp.18
## Standard deviation     0.440035770 0.320905598 0.314801834 0.270003099
## Proportion of Variance 0.002930357 0.001558473 0.001499751 0.001103271
## Cumulative Proportion  0.993627345 0.995185818 0.996685569 0.997788839
##                            Comp.19      Comp.20      Comp.21      Comp.22
## Standard deviation     0.244788845 0.1702064108 0.1359103790 0.1270213711
## Proportion of Variance 0.000906834 0.0004384261 0.0002795438 0.0002441733
## Cumulative Proportion  0.998695673 0.9991340994 0.9994136431 0.9996578164
##                             Comp.23      Comp.24      Comp.25      Comp.26
## Standard deviation     0.1013829076 7.470389e-02 7.100332e-02 3.248053e-02
## Proportion of Variance 0.0001555514 8.445611e-05 7.629602e-05 1.596581e-05
## Cumulative Proportion  0.9998133679 9.998978e-01 9.999741e-01 9.999901e-01
##                             Comp.27
## Standard deviation     2.559509e-02
## Proportion of Variance 9.914208e-06
## Cumulative Proportion  1.000000e+00
pca_spe$scores %>% 
  as.tibble() %>% 
  ggplot(aes(Comp.1))+geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

pca_spe$scores %>% 
  as.tibble() %>% 
  mutate(setor=ifelse(Comp.1<0,"especies 1", "especies 2"))-> spev_set
fviz_pca_ind(pca_spe,
             geom.ind = "point", # show points only (nbut not "text")
             col.ind = env_set$setor, # color by groups
             palette = c("#00AFBB", "#E7B800"),
             addEllipses = TRUE, # Concentration ellipses
             legend.title = "Groups"
)

nmds<-metaMDS(spe[-8,-c(20:30)])
## Run 0 stress 0.08903472 
## Run 1 stress 0.1117096 
## Run 2 stress 0.09349518 
## Run 3 stress 0.1274673 
## Run 4 stress 0.09178727 
## Run 5 stress 0.09178723 
## Run 6 stress 0.08903508 
## ... Procrustes: rmse 0.0005374486  max resid 0.00257864 
## ... Similar to previous best
## Run 7 stress 0.1336582 
## Run 8 stress 0.09178715 
## Run 9 stress 0.1158582 
## Run 10 stress 0.1348621 
## Run 11 stress 0.08903492 
## ... Procrustes: rmse 0.0001412584  max resid 0.0006756296 
## ... Similar to previous best
## Run 12 stress 0.08903471 
## ... New best solution
## ... Procrustes: rmse 1.098389e-05  max resid 4.037948e-05 
## ... Similar to previous best
## Run 13 stress 0.1336898 
## Run 14 stress 0.1161219 
## Run 15 stress 0.08903476 
## ... Procrustes: rmse 4.890967e-05  max resid 0.0002347463 
## ... Similar to previous best
## Run 16 stress 0.1269522 
## Run 17 stress 0.0917871 
## Run 18 stress 0.1298099 
## Run 19 stress 0.08903477 
## ... Procrustes: rmse 0.0003603548  max resid 0.001730208 
## ... Similar to previous best
## Run 20 stress 0.08903488 
## ... Procrustes: rmse 0.0001317221  max resid 0.000631469 
## ... Similar to previous best
## *** Solution reached
nmds$points
##           MDS1        MDS2
## 1  -1.75069944  0.71575978
## 2  -1.01676226 -0.15977122
## 3  -0.89873700 -0.02095081
## 4  -0.51932523 -0.06226539
## 5   0.14981488 -0.07717252
## 6  -0.38320407 -0.13209054
## 7  -0.74384974 -0.17434051
## 9  -0.18248545 -0.93250200
## 10 -0.31245033 -0.33487696
## 11 -0.87614268 -0.36119814
## 12 -0.86070620 -0.10251152
## 13 -1.08401822  0.14607515
## 14 -0.69387564  0.18634022
## 15 -0.37090227  0.23093415
## 16 -0.09187837  0.39003000
## 17  0.06190630  0.37598062
## 18  0.25994537  0.32058347
## 19  0.33683137  0.36628662
## 20  0.68834507  0.36773187
## 21  0.77853960  0.40922180
## 22  0.90321476  0.46841072
## 23  0.40167399 -1.71549517
## 24  0.81918144 -1.06520851
## 25  0.98469241 -0.72931031
## 26  0.85381846  0.05922881
## 27  0.91773440  0.28149288
## 28  0.92768118  0.36392196
## 29  0.68679290  0.59265143
## 30  1.01486477  0.59304413
## attr(,"centre")
## [1] TRUE
## attr(,"pc")
## [1] TRUE
## attr(,"halfchange")
## [1] TRUE
## attr(,"internalscaling")
## [1] 1.065066
nmds_dat<-data.frame(nmds$points, env_set$setor[-8])
colnames(nmds_dat) <- c("MDS1","MDS2","setor")
nmds_dat %>% 
  ggplot(aes(MDS1, MDS2, color=setor))+geom_point()

Nessa análise é possivel encontrar uma coincidência com a primeira. Entretando, ela fica menos evidente que a anterior, sendo preferível usar os dados env para dividir naturalmente.

3)

fviz_nbclust(spe, kmeans, method = "silhouette")

Os métodos coincidiram demonstrando ser melhor dividir esse rio em 2 áreas.