Creditos: diegokjkjj
library(factoextra)
## Warning: package 'factoextra' was built under R version 4.1.2
## Carregando pacotes exigidos: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v tibble 3.1.6 v dplyr 1.0.7
## v tidyr 1.1.4 v stringr 1.4.0
## v readr 2.0.2 v forcats 0.5.1
## v purrr 0.3.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(ggplot2)
library(vegan)
## Carregando pacotes exigidos: permute
## Carregando pacotes exigidos: lattice
## This is vegan 2.5-7
setwd("G:/Meu Drive/UFPE/2021.1/Ecologia Numérica")
load("G:/Meu Drive/UFPE/2021.1/Ecologia Numérica/NEwR-2ed_code_data (2)/NEwR-2ed_code_data/NEwR2-Data/Doubs.RData")
Env<-env[,-c(1,4)]
Env.simp<-Env[,6:9]
PCA.Env<-princomp(Env.simp)
summary(PCA.Env)
## Importance of components:
## Comp.1 Comp.2 Comp.3 Comp.4
## Standard deviation 4.3678227 1.08858700 0.98383428 0.1241205577
## Proportion of Variance 0.8979416 0.05577561 0.04555772 0.0007251127
## Cumulative Proportion 0.8979416 0.95371717 0.99927489 1.0000000000
PCA.Env$scores %>%
as.tibble() %>%
ggplot(aes(Comp.1))+
geom_histogram(fill="green",color="black")
## 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<=-2,"medio-baixo", "alto"))-> Env.set
fviz_pca_ind(PCA.Env,
geom.ind = "point",
col.ind = Env.set$setor,
palette = c("#00AFBB", "#E7B800"),
addEllipses = TRUE,
legend.title = "Groups"
)
PCA.Env$scores %>%
as.tibble() %>%
mutate(setor=ifelse(Comp.1<=0,"medio-baixo", "alto"))-> Env.set
fviz_pca_ind(PCA.Env,
geom.ind = "point",
col.ind = Env.set$setor,
palette = c("#00AFBB", "#E7B800"),
addEllipses = TRUE,
legend.title = "Groups"
)
PCA.Env$scores %>%
as.tibble() %>%
mutate(setor=ifelse(Comp.1<=3,"medio-baixo", "alto"))-> Env.set
fviz_pca_ind(PCA.Env,
geom.ind = "point",
col.ind = Env.set$setor,
palette = c("#00AFBB", "#E7B800"),
addEllipses = TRUE,
legend.title = "Groups"
)
Env.nmds<-metaMDS(spe[-8,-c(20:30)])
## Run 0 stress 0.08903472
## Run 1 stress 0.08903466
## ... New best solution
## ... Procrustes: rmse 8.855098e-05 max resid 0.0004174451
## ... Similar to previous best
## Run 2 stress 0.0917871
## Run 3 stress 0.1286932
## Run 4 stress 0.08903488
## ... Procrustes: rmse 0.0003509997 max resid 0.001685598
## ... Similar to previous best
## Run 5 stress 0.116465
## Run 6 stress 0.1364542
## Run 7 stress 0.1114361
## Run 8 stress 0.08903489
## ... Procrustes: rmse 0.0003306562 max resid 0.001583819
## ... Similar to previous best
## Run 9 stress 0.1298665
## Run 10 stress 0.08903511
## ... Procrustes: rmse 0.0003234448 max resid 0.001550634
## ... Similar to previous best
## Run 11 stress 0.1117094
## Run 12 stress 0.08903501
## ... Procrustes: rmse 0.000242027 max resid 0.001148632
## ... Similar to previous best
## Run 13 stress 0.1336898
## Run 14 stress 0.1117098
## Run 15 stress 0.08903486
## ... Procrustes: rmse 0.0001969684 max resid 0.0009451049
## ... Similar to previous best
## Run 16 stress 0.1330487
## Run 17 stress 0.08903475
## ... Procrustes: rmse 0.0001215198 max resid 0.0005805838
## ... Similar to previous best
## Run 18 stress 0.1195107
## Run 19 stress 0.1321452
## Run 20 stress 0.0934953
## *** Solution reached
Env.nmds$points
## MDS1 MDS2
## 1 -1.74981752 0.71717240
## 2 -1.01673065 -0.16007274
## 3 -0.89877077 -0.02127737
## 4 -0.51932637 -0.06249251
## 5 0.14986608 -0.07712334
## 6 -0.38314478 -0.13221163
## 7 -0.74381865 -0.17455805
## 9 -0.18219957 -0.93261542
## 10 -0.31239313 -0.33501267
## 11 -0.87609951 -0.36141505
## 12 -0.86070178 -0.10283158
## 13 -1.08420204 0.14543687
## 14 -0.69394121 0.18596647
## 15 -0.37092159 0.23063750
## 16 -0.09193541 0.38987948
## 17 0.06182781 0.37589109
## 18 0.25996654 0.32059329
## 19 0.33676511 0.36626696
## 20 0.68813734 0.36785167
## 21 0.77834480 0.40937242
## 22 0.90293021 0.46857968
## 23 0.40217633 -1.71524988
## 24 0.81937989 -1.06500307
## 25 0.98484163 -0.72905064
## 26 0.85369912 0.05946252
## 27 0.91754141 0.28174015
## 28 0.92745054 0.36412481
## 29 0.68657752 0.59273006
## 30 1.01449865 0.59320855
## attr(,"centre")
## [1] TRUE
## attr(,"pc")
## [1] TRUE
## attr(,"halfchange")
## [1] TRUE
## attr(,"internalscaling")
## [1] 1.06514
Env.nmds.dat<-data.frame(Env.nmds$points,Env.set$setor[-8])
colnames(Env.nmds.dat)<-c("MDS1","MDS2","setor")
Env.nmds.dat %>%
ggplot(aes(MDS1,MDS2,color=setor))+
geom_point(size=5)
K.M.Env<-kmeans(Env.simp, centers = 2, nstart=25)
fviz_cluster(K.M.Env, data = Env)