Carga de paquetes

library(ade4)
library(adegraphics)
## 
## Attaching package: 'adegraphics'
## The following objects are masked from 'package:ade4':
## 
##     kplotsepan.coa, s.arrow, s.class, s.corcircle, s.distri,
##     s.image, s.label, s.logo, s.match, s.traject, s.value,
##     table.value, triangle.class
library(adespatial)
## 
## Attaching package: 'adespatial'
## The following object is masked from 'package:ade4':
## 
##     multispati
library(cocorresp)
## Loading required package: vegan
## Loading required package: permute
## Loading required package: lattice
## This is vegan 2.5-4
## 
## Attaching package: 'cocorresp'
## The following object is masked from 'package:ade4':
## 
##     coinertia
library(vegan)
library(vegan3d)
library(ape)   
## 
## Attaching package: 'ape'
## The following object is masked from 'package:adegraphics':
## 
##     zoom
library(MASS)
library(ellipse)
## 
## Attaching package: 'ellipse'
## The following object is masked from 'package:graphics':
## 
##     pairs
library(FactoMineR)
## 
## Attaching package: 'FactoMineR'
## The following object is masked from 'package:ade4':
## 
##     reconst
library(rrcov)
## Loading required package: robustbase
## Scalable Robust Estimators with High Breakdown Point (version 1.4-7)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.0       ✔ purrr   0.3.1  
## ✔ tibble  2.0.1       ✔ dplyr   0.8.0.1
## ✔ tidyr   0.8.3       ✔ stringr 1.4.0  
## ✔ readr   1.3.1       ✔ forcats 0.4.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ✖ dplyr::select() masks MASS::select()

Funciones especiales

source("hcoplot.R")
source("triplot.rda.R")
source("plot.lda.R")
source("polyvars.R")
source("screestick.R")

Lectura de las bases de datos.

# Lectura de las bases de datos.
ambient       <- read.csv2("ambientales.csv", enc="latin1", row.names=1)
peces         <- read.csv2("peces.csv", enc="latin1", row.names=1)
locs          <- read.csv2("localidades.csv", enc="latin1", row.names=1)
nomEspec      <- read.csv2("nombresdeespecies.csv", enc="latin1")
nomVarAmbient <- read.csv2("Nombresdevariablesambientales.csv", enc="latin1")

AdecuaciĂ³n de la base de datos.

# AdecuaciĂ³n
# Quitar el sitio que no tiene ninguna especie
sumEsp  <- apply(peces, 1, sum)
peces   <- peces[sumEsp != 0, ]
ambient <- ambient[sumEsp != 0, ]
locs    <- locs[sumEsp != 0, ]

AnĂ¡lisis de redundancia.

peces.hel <- decostand(peces, "hellinger")
peces.rda <- rda(peces.hel ~ ., ambient)
peces.rda
## Call: rda(formula = peces.hel ~ das + alt + slo + flo + pH + har +
## pho + nit + amm + oxy + bdo, data = ambient)
## 
##               Inertia Proportion Rank
## Total          0.5025     1.0000     
## Constrained    0.3676     0.7314   11
## Unconstrained  0.1350     0.2686   17
## Inertia is variance 
## 
## Eigenvalues for constrained axes:
##    RDA1    RDA2    RDA3    RDA4    RDA5    RDA6    RDA7    RDA8    RDA9 
## 0.23020 0.05347 0.03396 0.02762 0.00680 0.00627 0.00315 0.00302 0.00142 
##   RDA10   RDA11 
## 0.00087 0.00078 
## 
## Eigenvalues for unconstrained axes:
##     PC1     PC2     PC3     PC4     PC5     PC6     PC7     PC8 
## 0.04383 0.02270 0.01774 0.01413 0.00932 0.00851 0.00522 0.00458 
## (Showing 8 of 17 unconstrained eigenvalues)
summary(peces.rda)  
## 
## Call:
## rda(formula = peces.hel ~ das + alt + slo + flo + pH + har +      pho + nit + amm + oxy + bdo, data = ambient) 
## 
## Partitioning of variance:
##               Inertia Proportion
## Total          0.5025     1.0000
## Constrained    0.3676     0.7314
## Unconstrained  0.1350     0.2686
## 
## Eigenvalues, and their contribution to the variance 
## 
## Importance of components:
##                         RDA1    RDA2    RDA3    RDA4     RDA5     RDA6
## Eigenvalue            0.2302 0.05347 0.03396 0.02762 0.006804 0.006271
## Proportion Explained  0.4581 0.10640 0.06758 0.05496 0.013540 0.012479
## Cumulative Proportion 0.4581 0.56451 0.63208 0.68704 0.700580 0.713058
##                           RDA7     RDA8     RDA9     RDA10     RDA11
## Eigenvalue            0.003154 0.003017 0.001417 0.0008703 0.0007768
## Proportion Explained  0.006277 0.006004 0.002820 0.0017319 0.0015459
## Cumulative Proportion 0.719335 0.725339 0.728159 0.7298905 0.7314363
##                           PC1     PC2     PC3     PC4      PC5      PC6
## Eigenvalue            0.04383 0.02270 0.01774 0.01413 0.009321 0.008515
## Proportion Explained  0.08722 0.04518 0.03529 0.02811 0.018550 0.016945
## Cumulative Proportion 0.81865 0.86384 0.89913 0.92724 0.945790 0.962735
##                            PC7      PC8      PC9     PC10     PC11
## Eigenvalue            0.005219 0.004581 0.002958 0.002013 0.001342
## Proportion Explained  0.010386 0.009117 0.005887 0.004006 0.002670
## Cumulative Proportion 0.973121 0.982238 0.988125 0.992131 0.994801
##                            PC12      PC13      PC14      PC15      PC16
## Eigenvalue            0.0009585 0.0007297 0.0003818 0.0003340 0.0001299
## Proportion Explained  0.0019073 0.0014520 0.0007597 0.0006646 0.0002586
## Cumulative Proportion 0.9967081 0.9981601 0.9989199 0.9995845 0.9998431
##                            PC17
## Eigenvalue            7.886e-05
## Proportion Explained  1.569e-04
## Cumulative Proportion 1.000e+00
## 
## Accumulated constrained eigenvalues
## Importance of components:
##                         RDA1    RDA2    RDA3    RDA4     RDA5     RDA6
## Eigenvalue            0.2302 0.05347 0.03396 0.02762 0.006804 0.006271
## Proportion Explained  0.6263 0.14547 0.09239 0.07513 0.018512 0.017060
## Cumulative Proportion 0.6263 0.77178 0.86417 0.93930 0.957814 0.974874
##                           RDA7     RDA8     RDA9     RDA10     RDA11
## Eigenvalue            0.003154 0.003017 0.001417 0.0008703 0.0007768
## Proportion Explained  0.008582 0.008208 0.003855 0.0023678 0.0021135
## Cumulative Proportion 0.983456 0.991664 0.995519 0.9978865 1.0000000
## 
## Scaling 2 for species and site scores
## * Species are scaled proportional to eigenvalues
## * Sites are unscaled: weighted dispersion equal on all dimensions
## * General scaling constant of scores:  1.93676 
## 
## 
## Species scores
## 
##          RDA1       RDA2     RDA3      RDA4      RDA5      RDA6
## Cogo  0.14261  0.1180253 -0.22904  0.086458 -0.028042 -0.013691
## Satr  0.63485  0.0273682  0.20085  0.163764  0.019805  0.005858
## Phph  0.48361  0.1077673 -0.07980 -0.133313  0.034833 -0.004144
## Neba  0.36118  0.1090800 -0.01143 -0.219383  0.029874  0.037263
## Thth  0.14244  0.1122444 -0.22054  0.106069 -0.044501 -0.008622
## Teso  0.07292  0.1280078 -0.18815  0.059441 -0.008623 -0.001556
## Chna -0.17236  0.0784750 -0.01415 -0.012202  0.024412  0.062130
## Chto -0.12548  0.1660939 -0.03644  0.002070  0.087969  0.023223
## Lele -0.07912  0.0398662 -0.02835 -0.050439  0.011284 -0.093644
## Lece -0.09581 -0.1434198 -0.13995 -0.122810 -0.079051 -0.011121
## Baba -0.17975  0.2151163 -0.04819  0.043810  0.015552  0.014657
## Spbi -0.15589  0.1641509 -0.01369  0.003517  0.059083  0.001573
## Gogo -0.20301  0.0348690 -0.03783 -0.027001  0.044723 -0.081508
## Eslu -0.11339  0.0291095  0.06240 -0.048558  0.031607 -0.069690
## Pefl -0.09975  0.1120913  0.04452 -0.095320  0.011882  0.011139
## Rham -0.21017  0.1602633  0.04184  0.021526  0.010718  0.001693
## Legi -0.23232  0.1103727  0.01809 -0.005716 -0.011545  0.043028
## Scer -0.16516 -0.0006841  0.03199  0.006260  0.013625 -0.098070
## Cyca -0.18069  0.1411915  0.03496  0.016758 -0.006660  0.003324
## Titi -0.14230  0.1179099  0.05249 -0.141851 -0.032298  0.007223
## Abbr -0.19435  0.1092121  0.07605  0.033536 -0.055275  0.006789
## Icme -0.15512  0.0727019  0.07999  0.034135 -0.088141 -0.009924
## Acce -0.31286  0.0113937  0.03276  0.017709 -0.001507 -0.043683
## Ruru -0.31310 -0.1517049 -0.05604 -0.140122  0.004038  0.040470
## Blbj -0.24750  0.0837801  0.06250  0.012999 -0.059178  0.048588
## Alal -0.43297 -0.2232880 -0.09319  0.124580  0.089120  0.048742
## Anan -0.19684  0.1387592  0.04844  0.020291 -0.007706 -0.002579
## 
## 
## Site scores (weighted sums of species scores)
## 
##        RDA1     RDA2     RDA3     RDA4       RDA5     RDA6
## 1   0.39199 -0.31267  0.98240  1.29491 -0.1258591  0.32865
## 2   0.52860 -0.04444  0.49719  0.10778  0.4259551  0.63121
## 3   0.48740 -0.02096  0.49331 -0.07076  0.5699500  0.29678
## 4   0.32937  0.02809  0.38254 -0.51181  0.2540256 -0.18083
## 5   0.02656 -0.18286  0.21678 -0.72427  0.0467813 -1.40635
## 6   0.24215 -0.08873  0.21046 -0.77964 -0.0138240 -0.32595
## 7   0.46183 -0.11999  0.28437 -0.18382  0.0403432  0.11466
## 9   0.04221 -0.52153 -0.30776 -1.06830 -1.1177967  0.90976
## 10  0.31401 -0.14722 -0.08142 -0.55441  0.0004456 -0.69622
## 11  0.48177 -0.03460 -0.30084  0.30647 -0.6211610  0.24013
## 12  0.49116  0.01863 -0.28336  0.28353 -0.5526465  0.38811
## 13  0.49826  0.19287 -0.45932  0.66506 -0.3554733  0.30170
## 14  0.38432  0.23745 -0.61472  0.44511 -0.3586002 -0.25255
## 15  0.29122  0.24047 -0.65399  0.13806 -0.5744138 -0.61858
## 16  0.09316  0.42148 -0.35490 -0.15339  0.5215487 -0.37988
## 17 -0.04925  0.45811 -0.39677 -0.01491  0.9780228  0.37292
## 18 -0.13761  0.42321 -0.38275 -0.05496  0.8161603  0.16502
## 19 -0.27972  0.31288 -0.11236 -0.33397  0.8933487  0.22061
## 20 -0.39479  0.22514  0.04995 -0.18906  0.5524046  0.07848
## 21 -0.42845  0.27228  0.18669 -0.06116  0.1685934 -0.05714
## 22 -0.46606  0.23214  0.22711  0.01341 -0.2537680 -0.02033
## 23 -0.27461 -1.14655 -0.45652  0.29007 -0.0135080  1.25334
## 24 -0.40481 -0.76490 -0.22822  0.37504 -0.0609283  1.09137
## 25 -0.34882 -0.79890 -0.18120  0.26592  0.6572636 -1.77138
## 26 -0.46948  0.07575  0.22954  0.02782 -0.3290586 -0.01789
## 27 -0.47071  0.19146  0.25713  0.03821 -0.3683865 -0.05750
## 28 -0.47379  0.20692  0.28560  0.06099 -0.5260879 -0.05532
## 29 -0.37500  0.36927  0.15499  0.19260 -0.2675599 -0.13575
## 30 -0.49089  0.27719  0.35606  0.19549 -0.3857710 -0.41707
## 
## 
## Site constraints (linear combinations of constraining variables)
## 
##        RDA1       RDA2     RDA3      RDA4     RDA5      RDA6
## 1   0.56175 -0.1760841  0.88764  0.754729  0.05699 -0.024789
## 2   0.27817  0.0278236  0.64854 -0.186273  0.71868  0.103142
## 3   0.40029 -0.1410937  0.44478  0.093254  0.06243  0.159225
## 4   0.38057  0.0304488  0.27060 -0.451187  0.11348 -0.163639
## 5   0.28434 -0.4313776 -0.07763 -0.624922 -0.31890 -0.121072
## 6   0.32365 -0.1717502  0.33216 -0.239434  0.10094  0.430241
## 7   0.43342 -0.1878085  0.23023 -0.249967 -0.43848 -0.112702
## 9   0.03876 -0.2453624 -0.06488 -1.063159 -0.28405 -0.165997
## 10  0.20896 -0.1304587  0.09944 -0.029712 -0.15900 -0.003754
## 11  0.40310  0.2072872 -0.38689  0.263810 -0.34896 -0.675910
## 12  0.31060  0.1676808 -0.35285  0.131850  0.23982  0.374171
## 13  0.36401  0.1083071 -0.45977  0.254110 -0.16169  0.154682
## 14  0.37243  0.1568990 -0.54561  0.280403 -0.29513  0.137795
## 15  0.30217  0.2952073 -0.51539  0.302620 -0.26909 -0.210679
## 16 -0.03361  0.2527439 -0.16089 -0.109020  0.07654 -0.063949
## 17 -0.04798  0.2826767 -0.42392 -0.163557  0.50606  0.277953
## 18 -0.04201  0.3220218 -0.27114 -0.101241  0.36862 -0.205147
## 19 -0.04238  0.3815154 -0.25768 -0.005306  0.34826 -0.051361
## 20 -0.22510  0.3772267 -0.06452  0.029576  0.73223  0.129096
## 21 -0.36748  0.2531034  0.06637 -0.113143  0.22778  0.340391
## 22 -0.31101  0.0588354  0.04318  0.184265 -0.22087  0.507177
## 23 -0.23685 -1.0546821 -0.31876  0.653124 -0.04596  0.560361
## 24 -0.50645 -0.5459548 -0.31521 -0.311281 -0.17450  0.594229
## 25 -0.37598 -0.9060399 -0.21468  0.249405  0.74559 -1.161192
## 26 -0.50636  0.0003718  0.12134 -0.254528 -0.36086 -0.278472
## 27 -0.57930  0.0667865  0.37489  0.211246 -0.22598  0.098458
## 28 -0.60704  0.3723919  0.35636 -0.090151 -0.04124 -0.118013
## 29 -0.34377  0.3294603  0.26629  0.309673 -0.30573 -0.235032
## 30 -0.43691  0.2998244  0.28800  0.274817 -0.64698 -0.275212
## 
## 
## Biplot scores for constraining variables
## 
##        RDA1     RDA2     RDA3     RDA4     RDA5     RDA6
## das -0.9128  0.12530 -0.14564  0.28559 -0.14025 -0.08596
## alt  0.8174 -0.19940  0.42653 -0.31363  0.04070  0.04191
## slo  0.7304 -0.18160  0.44131  0.12857 -0.03275  0.05724
## flo -0.7734  0.22945 -0.11776  0.35275 -0.20906 -0.20090
## pH   0.1032  0.17963 -0.23622  0.15717 -0.27998 -0.00646
## har -0.5651  0.06594 -0.58956  0.05411 -0.38278 -0.21829
## pho -0.4885 -0.66510 -0.22158  0.20580  0.19624 -0.37929
## nit -0.7699 -0.21306 -0.25074  0.19239  0.30094 -0.31772
## amm -0.4700 -0.70062 -0.19662  0.17917  0.34014 -0.29055
## oxy  0.7593  0.57745 -0.03543  0.21067  0.03237  0.15692
## bdo -0.5124 -0.80242 -0.19822  0.12204  0.05668 -0.05770
coef(peces.rda)
##              RDA1          RDA2          RDA3          RDA4          RDA5
## das -2.010177e-03 -6.608331e-04  4.071666e-03 -0.0007737831 -4.879723e-04
## alt -1.362522e-04 -1.940140e-04  1.584080e-03 -0.0007113863 -1.912051e-04
## slo  2.628938e-02 -4.812851e-02  5.088824e-02  0.1600880853 -1.247432e-01
## flo  6.300348e-05  4.629605e-05 -4.921894e-05  0.0001327932 -1.037798e-05
## pH   9.287651e-02 -8.634439e-02 -1.104621e-01  0.4085611837 -4.823312e-01
## har  9.775838e-04 -2.188270e-03 -4.710546e-03 -0.0064392451 -8.045440e-03
## pho  7.025002e-04  2.019299e-04  7.318641e-05 -0.0004476953 -1.972434e-03
## nit -1.321711e-04  8.279428e-04  2.744723e-04 -0.0002662408  5.496718e-04
## amm -9.834747e-04 -3.545150e-03 -3.541437e-05  0.0025904176  8.311865e-03
## oxy  3.172353e-03  9.545637e-04  7.456746e-04  0.0064171073  4.725933e-03
## bdo  8.490136e-04 -3.065095e-03 -1.780181e-03  0.0031954416 -2.120654e-03
##              RDA6          RDA7          RDA8          RDA9         RDA10
## das  0.0047839425  0.0046815522 -0.0015359027 -0.0040833159 -1.416482e-03
## alt  0.0005956930  0.0016706252  0.0016945643  0.0000687003 -1.567319e-03
## slo -0.1134485914 -0.2433342914 -0.1433130367  0.0634790412  8.982321e-02
## flo -0.0002228683 -0.0001670232  0.0002557783  0.0002140328  4.793737e-05
## pH  -0.1669999046  0.0247676509 -0.3568088308  0.9516657070  6.939359e-01
## har -0.0020686320 -0.0036608079  0.0050621827 -0.0020359947 -1.158263e-03
## pho -0.0044370161  0.0051546453 -0.0057318270 -0.0051168495 -3.561727e-03
## nit -0.0011216301 -0.0004445305  0.0017221099  0.0033539197 -3.521483e-03
## amm  0.0057241952 -0.0081100429  0.0041614434 -0.0024554952  2.195211e-02
## oxy  0.0119479055  0.0133260985  0.0104091231 -0.0041067968 -1.493132e-02
## bdo  0.0085989892  0.0020924330  0.0101797519  0.0078128921 -1.229218e-02
##             RDA11
## das  1.150295e-02
## alt  3.401140e-03
## slo -8.370854e-02
## flo -4.854237e-04
## pH  -2.898171e-01
## har  1.758118e-02
## pho  4.068172e-04
## nit  6.668115e-07
## amm  6.505853e-03
## oxy  2.177482e-02
## bdo -2.482311e-03
(R2 <- RsquareAdj(peces.rda)$r.squared)
## [1] 0.7314363
(R2adj <- RsquareAdj(peces.rda)$adj.r.squared)
## [1] 0.5576598
plot(peces.rda,
  scaling = 1,
  display = c("sp", "lc", "cn"),
  main = "Triplot RDA peces.hel ~ ambient - escalamiento 1 - puntajes ajustados"
)

plot(peces.rda,
   display = c("sp", "lc", "cn"),
   main = "Triplot RDA peces.hel ~ ambient - escalamiento 2 - puntajes ajustados"
  )
peces.sc2 <-
    scores(peces.rda,
    choices = 1:2,
    display = "sp"
)
arrows(0, 0,
       peces.sc2[, 1] * 0.92,
       peces.sc2[, 2] * 0.92,
       length = 0,
       lty = 1,
       col = "red"
)

peces.good <- goodness(peces.rda)
sel.sp <- which(peces.good[, 2] >= 0.6)

par(mfrow = c(2, 1))
triplot.rda(peces.rda, 
  site.sc = "lc", 
  scaling = 1, 
  cex.char2 = 0.7, 
  pos.env = 3, 
  pos.centr = 1, 
  mult.arrow = 1.1, 
  mar.percent = 0.05, 
  select.spe = sel.sp
)
## 
## -----------------------------------------------------------------------
## Site constraints (lc) selected. To obtain site scores that are weighted
## sums of species scores (default in vegan), argument site.sc must be set
## to wa.
## -----------------------------------------------------------------------
## No factor, hence levels cannot be plotted with symbols; 'plot.centr' is set to FALSE
text(-0.92, 0.62, "a", cex = 2)

triplot.rda(peces.rda, 
  site.sc = "lc", 
  scaling = 2, 
  cex.char2 = 0.7, 
  pos.env = 3, 
  pos.centr = 1, 
  mult.arrow = 1.1, 
  mar.percent = 0.05, 
  select.spe = sel.sp
)
## 
## -----------------------------------------------------------------------
## Site constraints (lc) selected. To obtain site scores that are weighted
## sums of species scores (default in vegan), argument site.sc must be set
## to wa.
## -----------------------------------------------------------------------
## No factor, hence levels cannot be plotted with symbols; 'plot.centr' is set to FALSE
text(-2.82, 2, "b", cex = 2)

anova(peces.rda, permutations = how(nperm = 999))
## Permutation test for rda under reduced model
## Permutation: free
## Number of permutations: 999
## 
## Model: rda(formula = peces.hel ~ das + alt + slo + flo + pH + har + pho + nit + amm + oxy + bdo, data = ambient)
##          Df Variance      F Pr(>F)    
## Model    11  0.36755 4.2091  0.001 ***
## Residual 17  0.13496                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
anova(peces.rda, by = "axis", permutations = how(nperm = 999))
## Permutation test for rda under reduced model
## Forward tests for axes
## Permutation: free
## Number of permutations: 999
## 
## Model: rda(formula = peces.hel ~ das + alt + slo + flo + pH + har + pho + nit + amm + oxy + bdo, data = ambient)
##          Df Variance       F Pr(>F)    
## RDA1      1 0.230202 28.9978  0.001 ***
## RDA2      1 0.053468  6.7353  0.001 ***
## RDA3      1 0.033958  4.2776  0.102    
## RDA4      1 0.027616  3.4787  0.335    
## RDA5      1 0.006804  0.8571  1.000    
## RDA6      1 0.006271  0.7899  1.000    
## RDA7      1 0.003154  0.3973  1.000    
## RDA8      1 0.003017  0.3800  1.000    
## RDA9      1 0.001417  0.1785  1.000    
## RDA10     1 0.000870  0.1096  1.000    
## RDA11     1 0.000777  0.0979  1.000    
## Residual 17 0.134956                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
peces.rda$CA$eig[peces.rda$CA$eig > mean(peces.rda$CA$eig)]
##         PC1         PC2         PC3         PC4         PC5         PC6 
## 0.043827430 0.022704284 0.017735831 0.014126067 0.009321390 0.008514941
vif.cca(peces.rda)
##        das        alt        slo        flo         pH        har 
## 119.360074  47.145809   5.324581  41.000764   1.792699   4.026741 
##        pho        nit        amm        oxy        bdo 
##  27.187637  16.461081  30.583561  16.606532  18.148646
ggplot(ambient, aes(das, alt)) +
  geom_line()

ggplot(ambient, aes(das, flo)) +
  geom_line()

(R2a.all <- RsquareAdj(peces.rda)$adj.r.squared)
## [1] 0.5576598
forward.sel(peces.hel, ambient, adjR2thresh = R2a.all)
## Testing variable 1
## Testing variable 2
## Testing variable 3
## Testing variable 4
## Procedure stopped (adjR2thresh criteria) adjR2cum = 0.575186 with 4 variables (> 0.557660)
##   variables order         R2     R2Cum  AdjR2Cum         F pvalue
## 1       das     1 0.38972513 0.3897251 0.3671224 17.242359  0.001
## 2       oxy    10 0.11822033 0.5079455 0.4700951  6.246724  0.001
## 3       bdo    11 0.07794818 0.5858936 0.5362009  4.705807  0.001