Skip to content

Instantly share code, notes, and snippets.

@kguidonimartins
Created July 15, 2020 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kguidonimartins/a51e02a0c47b0c9810fd93a417a64166 to your computer and use it in GitHub Desktop.
Save kguidonimartins/a51e02a0c47b0c9810fd93a417a64166 to your computer and use it in GitHub Desktop.
if (!require("ade4")) install.packages("ade4")
#> Loading required package: ade4
# Where am I?
getwd()
#> [1] "/tmp/RtmpK0kjbc/reprex2437bf19e2d4b1"
folder <- "kleyer-files"
data_zip <- paste0(folder, "/", "data.zip")
if (!dir.exists(folder)) {
dir.create(folder)
}
if (!file.exists(data_zip)) {
url <- "https://onlinelibrary.wiley.com/action/downloadSupplement?doi=10.1111%2Fj.1654-1103.2012.01402.x&file=jvs1402-sup-0004-AppendixS4.zip"
download.file(url = url, destfile = data_zip)
unzip(zipfile = data_zip, exdir = folder)
}
traits <- read.table(file = "kleyer-files/Appendix S4 scripts and data/data/Species_traits.txt", sep = "\t")
spe <- read.table(file = "kleyer-files/Appendix S4 scripts and data/data/Site_species.txt", sep = "\t")
env <- read.table(file = "kleyer-files/Appendix S4 scripts and data/data/Site_env.txt", sep = "\t")
coa <- dudi.coa(spe, scannf = F)
coa
#> Duality diagramm
#> class: coa dudi
#> $call: dudi.coa(df = spe, scannf = F)
#>
#> $nf: 2 axis-components saved
#> $rank: 42
#> eigen values: 0.7197 0.6659 0.6099 0.5452 0.5012 ...
#> vector length mode content
#> 1 $cw 50 numeric column weights
#> 2 $lw 43 numeric row weights
#> 3 $eig 42 numeric eigen values
#>
#> data.frame nrow ncol content
#> 1 $tab 43 50 modified array
#> 2 $li 43 2 row coordinates
#> 3 $l1 43 2 row normed scores
#> 4 $co 50 2 column coordinates
#> 5 $c1 50 2 column normed scores
#> other elements: N
traits[, 1] <- as.numeric(traits[, 1])
pca.traits <- dudi.pca(traits, row.w = coa$cw, scannf = FALSE)
pca.env <- dudi.pca(env, row.w = coa$lw, scannf = FALSE)
rlq1 <- rlq(pca.env, coa, pca.traits, scannf = FALSE)
summary(rlq1)
#> RLQ analysis
#>
#> Class: rlq dudi
#> Call: rlq(dudiR = pca.env, dudiL = coa, dudiQ = pca.traits, scannf = FALSE)
#>
#> Total inertia: 0.6076
#>
#> Eigenvalues:
#> Ax1 Ax2 Ax3
#> 0.347425 0.253025 0.007172
#>
#> Projected inertia (%):
#> Ax1 Ax2 Ax3
#> 57.18 41.64 1.18
#>
#> Cumulative projected inertia (%):
#> Ax1 Ax1:2 Ax1:3
#> 57.18 98.82 100.00
#>
#>
#> Eigenvalues decomposition:
#> eig covar sdR sdQ corr
#> 1 0.3474245 0.5894273 0.9994426 1.316734 0.4478929
#> 2 0.2530248 0.5030157 1.1859679 1.136397 0.3732319
#>
#> Inertia & coinertia R (pca.env):
#> inertia max ratio
#> 1 0.9988856 1.581479 0.6316149
#> 12 2.4054053 2.552662 0.9423125
#>
#> Inertia & coinertia Q (pca.traits):
#> inertia max ratio
#> 1 1.733789 2.222861 0.7799810
#> 12 3.025187 3.195351 0.9467464
#>
#> Correlation L (coa):
#> corr max ratio
#> 1 0.4478929 0.8483415 0.5279630
#> 2 0.3732319 0.8160086 0.4573872
plot(rlq1)
#' ![](reprex_reprex_files/figure-markdown_strict/reprex-body-1.png)
## Percentage of co-Inertia for each axis
100*rlq1$eig/sum(rlq1$eig)
#> [1] 57.177840 41.641884 1.180276
## weighted correlations axes / env.
t(pca.env$tab)%*%(diag(pca.env$lw))%*%as.matrix(rlq1$mR)
#> NorS1 NorS2
#> dist.int 0.09551403 0.9599589
#> SOIL.P -0.99445098 -0.1150855
#> SOIL.WHC 0.17425053 0.7637516
## weighted correlations axes / traits.
t(pca.traits$tab)%*%(diag(pca.traits$lw))%*%as.matrix(rlq1$mQ)
#> NorS1 NorS2
#> Polycarpic 0.7171193 0.1551089
#> Cnratio 0.5582854 -0.6262489
#> seed.mass.log 0.4932320 0.1147673
#> SLA -0.6396080 0.6372234
#> height 0.5220954 -0.3724864
#> Onset.flower 0.4782056 -0.7687494
## correlations traits / env.
rlq1$tab
#> Polycarpic Cnratio seed.mass.log SLA height
#> dist.int 0.1467609 -0.2182632 0.13662287 0.17193497 -0.08406364
#> SOIL.P -0.4311989 -0.1728592 -0.25391352 0.16158906 -0.11784596
#> SOIL.WHC 0.1243845 -0.1152605 0.09662426 0.05832776 0.05671719
#> Onset.flower
#> dist.int -0.30211986
#> SOIL.P -0.14185350
#> SOIL.WHC -0.08218956
devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.0.2 (2020-06-22)
#> os Arch Linux
#> system x86_64, linux-gnu
#> ui X11
#> language (EN)
#> collate en_US.UTF-8
#> ctype pt_BR.UTF-8
#> tz America/Sao_Paulo
#> date 2020-07-15
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date lib source
#> ade4 * 1.7-15 2020-02-13 [1] CRAN (R 4.0.0)
#> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.0.0)
#> backports 1.1.5 2019-10-02 [1] CRAN (R 4.0.2)
#> callr 3.4.3 2020-03-28 [1] CRAN (R 4.0.0)
#> cli 2.0.2 2020-02-28 [1] CRAN (R 4.0.0)
#> crayon 1.3.4 2017-09-16 [1] CRAN (R 4.0.0)
#> desc 1.2.0 2018-05-01 [1] CRAN (R 4.0.0)
#> devtools 2.3.0 2020-04-10 [1] CRAN (R 4.0.0)
#> digest 0.6.25 2020-02-23 [1] CRAN (R 4.0.0)
#> ellipsis 0.3.0 2019-09-20 [1] CRAN (R 4.0.2)
#> evaluate 0.14 2019-05-28 [1] CRAN (R 4.0.0)
#> fansi 0.4.1 2020-01-08 [1] CRAN (R 4.0.0)
#> fs 1.4.2 2020-06-30 [1] CRAN (R 4.0.2)
#> glue 1.4.0 2020-04-03 [1] CRAN (R 4.0.2)
#> highr 0.8 2019-03-20 [1] CRAN (R 4.0.0)
#> htmltools 0.5.0 2020-06-16 [1] CRAN (R 4.0.1)
#> knitr 1.29 2020-06-23 [1] CRAN (R 4.0.1)
#> magrittr 1.5 2014-11-22 [1] CRAN (R 4.0.0)
#> MASS 7.3-51.6 2020-04-26 [2] CRAN (R 4.0.2)
#> memoise 1.1.0 2017-04-21 [1] CRAN (R 4.0.0)
#> pkgbuild 1.0.8 2020-05-07 [1] CRAN (R 4.0.0)
#> pkgload 1.1.0 2020-05-29 [1] CRAN (R 4.0.0)
#> prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.0.0)
#> processx 3.4.3 2020-07-05 [1] CRAN (R 4.0.2)
#> ps 1.3.3 2020-05-08 [1] CRAN (R 4.0.0)
#> R6 2.4.1 2019-11-12 [1] CRAN (R 4.0.0)
#> remotes 2.1.1 2020-02-15 [1] CRAN (R 4.0.0)
#> rlang 0.4.6 2020-05-02 [1] CRAN (R 4.0.2)
#> rmarkdown 2.3 2020-06-18 [1] CRAN (R 4.0.1)
#> rprojroot 1.3-2 2018-01-03 [1] CRAN (R 4.0.0)
#> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.0.0)
#> stringi 1.4.3 2019-03-12 [1] CRAN (R 4.0.2)
#> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.0.0)
#> testthat 2.3.2 2020-03-02 [1] CRAN (R 4.0.0)
#> usethis 1.6.1 2020-04-29 [1] CRAN (R 4.0.0)
#> withr 2.2.0 2020-04-20 [1] CRAN (R 4.0.0)
#> xfun 0.15 2020-06-21 [1] CRAN (R 4.0.1)
#> yaml 2.2.1 2020-02-01 [1] CRAN (R 4.0.0)
#>
#> [1] /home/karlo/.local/lib/R/library/4.0
#> [2] /usr/lib/R/library
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment