Skip to content

Instantly share code, notes, and snippets.

@perrygeo
Last active September 27, 2022 15:03
Show Gist options
  • Save perrygeo/7572735 to your computer and use it in GitHub Desktop.
Save perrygeo/7572735 to your computer and use it in GitHub Desktop.
Canonical Correspondence Analysis in R using the vegan library
library(vegan)
library(labdsv)
data(varespec)
data(varechem)
vare.cca <- cca(varespec ~ Baresoil+Humdepth+pH+N+P+K+Ca+Mg+S+Al+Fe, data=varechem)
vare.cca
plot(vare.cca)
summary(vare.cca)
// See also http://ecology.msu.montana.edu/labdsv/R/labs/lab12/lab12.html
> vare.cca
Call: cca(formula = varespec ~ Baresoil + Humdepth + pH + N + P + K + Ca + Mg + S + Al + Fe, data = varechem)
Inertia Proportion Rank
Total 2.0832 1.0000
Constrained 1.2154 0.5834 11
Unconstrained 0.8678 0.4166 12
Inertia is mean squared contingency coefficient
Eigenvalues for constrained axes:
CCA1 CCA2 CCA3 CCA4 CCA5 CCA6 CCA7 CCA8 CCA9 CCA10 CCA11
0.410865 0.255619 0.146158 0.131515 0.091575 0.078525 0.060261 0.019134 0.008944 0.006849 0.005946
Eigenvalues for unconstrained axes:
CA1 CA2 CA3 CA4 CA5 CA6 CA7 CA8 CA9 CA10 CA11 CA12
0.253513 0.151934 0.126683 0.093370 0.075393 0.049632 0.037929 0.026435 0.021758 0.014304 0.009052 0.007806
> plot(vare.cca)
> summary(vare.cca)
Call:
cca(formula = varespec ~ Baresoil + Humdepth + pH + N + P + K + Ca + Mg + S + Al + Fe, data = varechem)
Partitioning of mean squared contingency coefficient:
Inertia Proportion
Total 2.0832 1.0000
Constrained 1.2154 0.5834
Unconstrained 0.8678 0.4166
Eigenvalues, and their contribution to the mean squared contingency coefficient
Importance of components:
CCA1 CCA2 CCA3 CCA4 CCA5 CCA6 CCA7 CCA8
Eigenvalue 0.4109 0.2556 0.14616 0.13152 0.09158 0.07852 0.06026 0.01913
Proportion Explained 0.1972 0.1227 0.07016 0.06313 0.04396 0.03769 0.02893 0.00918
Cumulative Proportion 0.1972 0.3199 0.39009 0.45322 0.49718 0.53488 0.56381 0.57299
CCA9 CCA10 CCA11 CA1 CA2 CA3 CA4 CA5
Eigenvalue 0.008944 0.006849 0.005946 0.2535 0.15193 0.12668 0.09337 0.07539
Proportion Explained 0.004290 0.003290 0.002850 0.1217 0.07293 0.06081 0.04482 0.03619
Cumulative Proportion 0.577280 0.580570 0.583430 0.7051 0.77805 0.83886 0.88368 0.91988
CA6 CA7 CA8 CA9 CA10 CA11 CA12
Eigenvalue 0.04963 0.03793 0.02643 0.02176 0.01430 0.009052 0.007806
Proportion Explained 0.02382 0.01821 0.01269 0.01044 0.00687 0.004350 0.003750
Cumulative Proportion 0.94370 0.96191 0.97460 0.98504 0.99191 0.996250 1.000000
Accumulated constrained eigenvalues
Importance of components:
CCA1 CCA2 CCA3 CCA4 CCA5 CCA6 CCA7 CCA8
Eigenvalue 0.4109 0.2556 0.1462 0.1315 0.09158 0.07852 0.06026 0.01913
Proportion Explained 0.3381 0.2103 0.1203 0.1082 0.07535 0.06461 0.04958 0.01574
Cumulative Proportion 0.3381 0.5484 0.6686 0.7768 0.85218 0.91679 0.96637 0.98211
CCA9 CCA10 CCA11
Eigenvalue 0.008944 0.006849 0.005946
Proportion Explained 0.007360 0.005630 0.004890
Cumulative Proportion 0.989470 0.995110 1.000000
Scaling 2 for species and site scores
* Species are scaled proportional to eigenvalues
* Sites are unscaled: weighted dispersion equal on all dimensions
Species scores
CCA1 CCA2 CCA3 CCA4 CCA5 CCA6
Cal.vul 0.2579826 -0.80872 1.94560 -0.35626 0.697113 -0.74258
Emp.nig -0.2704679 -0.05299 -0.14437 -0.32584 -0.170125 -0.04192
Led.pal -0.9762750 -0.37214 -0.06644 -0.75402 -0.697058 0.06306
Vac.myr -1.3055169 -0.11479 0.17137 0.27104 -0.599337 0.07048
Vac.vit -0.1929037 0.08757 -0.21288 -0.35303 -0.066669 -0.10932
Pin.syl 0.2380445 0.22966 0.05686 -0.39593 -0.019382 0.07840
Des.fle -1.4062903 0.38398 0.39272 0.39030 -0.101391 0.30965
Bet.pub -0.5960443 -0.95617 -0.72698 -1.67618 -1.588060 -0.32047
Vac.uli 0.3926825 -0.94198 -0.39399 0.58151 -0.228095 -0.14642
Dip.mon 0.0229110 -0.49673 -0.89897 -0.40174 -0.174258 -0.26730
Dic.sp -0.4868957 0.65851 -1.26929 0.02094 1.419951 -0.57922
Dic.fus -0.4861256 -0.47490 0.52892 -0.51872 0.229919 0.59969
Dic.pol -0.3223530 -0.20367 -1.05718 -0.83490 -0.119395 -0.73626
Hyl.spl -1.8827323 0.65465 0.49959 1.53295 -0.308169 0.16247
Ple.sch -0.9252145 0.28944 0.09530 0.27152 0.053829 -0.02879
Pol.pil 0.2594323 -0.52462 -0.49858 0.08717 0.163650 0.43618
Pol.jun -0.7366157 0.12464 -0.27517 -0.14649 -0.314192 0.31794
Pol.com -0.9458638 -0.14755 -0.27373 -0.71188 -0.855045 -0.28475
Poh.nut -0.0348872 0.21735 -0.33522 -0.36923 0.179971 -0.30020
Pti.cil -0.5533110 -0.80045 -0.73937 -1.41351 -1.316239 -0.52927
Bar.lyc -0.5655746 -1.05360 -0.79808 -1.74760 -1.740288 -0.58626
Cla.arb 0.3082534 -0.56549 -0.03583 0.08219 -0.096060 -0.13535
Cla.ran 0.4695611 -0.53152 -0.16623 0.29331 0.037571 0.13895
Cla.ste 0.7627157 0.70289 0.12199 -0.02713 -0.053315 0.10624
Cla.unc -0.0840257 -0.04106 -0.16775 -0.54249 0.443329 -0.24384
Cla.coc 0.1998745 -0.11892 0.05746 -0.11248 0.073434 0.09607
Cla.cor -0.3377207 0.00630 -0.30418 -0.13082 0.007785 -0.09738
Cla.gra -0.0591848 -0.14946 -0.27214 -0.18229 0.113776 -0.19158
Cla.fim -0.0005966 -0.11638 0.03219 -0.27266 -0.185276 0.01141
Cla.cri -0.2353142 -0.13014 0.04825 -0.44966 -0.082457 -0.30900
Cla.chl 0.2960198 0.43031 -0.44880 -0.45712 -0.026098 -0.32614
Cla.bot -0.6121890 -0.67705 -0.17531 -1.05814 -0.701290 -0.74162
Cla.ama 0.0339234 -0.89659 -1.10581 -0.06753 0.078437 0.24184
Cla.sp 0.6893702 0.73032 0.49452 -0.39382 0.126202 -0.21620
Cet.eri 0.2703176 0.10341 -0.30842 -0.06482 0.593267 -0.44989
Cet.isl 0.1448207 0.10344 -0.31552 -0.70305 -0.482515 -0.22317
Cet.niv 1.3069886 -0.01489 0.54845 1.61791 -1.099398 -2.15267
Nep.arc -0.8040582 0.39185 -0.06934 -0.02375 -0.746439 1.04971
Ste.sp 0.2935602 -1.44392 -0.78477 0.79952 0.694834 0.72962
Pel.aph -0.6296777 0.05166 -0.34405 -0.15437 -0.058790 -0.21384
Ich.eri 0.3228067 -1.83898 -0.44899 0.64843 0.595119 0.78301
Cla.cer 0.9088477 0.22411 0.50813 1.00260 -0.582433 -0.84668
Cla.def -0.3460925 -0.21145 -0.11090 -0.54258 0.007175 -0.33724
Cla.phy 0.7223026 1.12784 -0.05001 -0.23188 0.099363 0.06092
Site scores (weighted averages of species scores)
CCA1 CCA2 CCA3 CCA4 CCA5 CCA6
18 0.2368 -1.0455 -0.79083 -0.149031 -0.49251 -0.35212
15 -0.9799 -0.2750 0.34407 0.111139 0.63356 0.30849
24 -1.0983 0.8478 -2.51016 -0.080535 4.40120 -2.69293
27 -1.6399 0.4846 0.31083 1.033926 -0.68668 0.16045
23 -0.6573 -0.3813 -0.70171 -0.837942 -0.50965 -0.35934
19 -0.2220 0.6886 -0.03737 -0.002568 -0.65742 0.07187
22 -1.1371 -0.7562 1.68441 -1.251373 0.68408 1.82825
16 -0.7322 -0.9683 1.38438 -0.848066 1.09474 1.52365
28 -2.4224 0.8605 0.77192 2.459293 -1.07010 0.02053
13 0.5930 -1.4057 2.91790 -0.312366 1.59649 -2.08999
14 -0.3296 -0.8773 0.53603 -1.974878 1.81909 -0.90880
20 -0.5440 -0.3236 -0.58922 -0.198890 0.30072 -0.50529
25 -1.2792 0.4454 -1.17274 -0.360068 1.82086 0.33613
7 0.7264 -1.9319 -0.98918 1.155440 -0.49232 -0.08616
5 0.9323 -2.3201 -1.39730 2.072908 0.83123 1.69279
6 0.7454 -1.2134 -0.56548 0.372969 -0.26442 -0.15445
3 1.3118 0.5893 -0.06894 0.401225 -0.39972 0.94502
4 1.3726 -0.1616 0.83676 1.878597 -1.28329 -3.05924
2 1.3068 1.3075 0.11566 -0.055773 -0.47463 1.05104
9 1.1513 1.9030 0.25604 -0.576516 -0.64079 0.50208
12 0.9003 1.2149 0.03147 -0.437409 -0.44022 0.54418
10 1.2566 1.7341 0.25151 -0.507033 -0.60207 0.74947
11 0.4809 0.1232 -0.01664 0.339108 0.09187 0.24062
21 -0.8258 -0.3306 -1.27405 -2.712570 -3.96294 -1.33584
Site constraints (linear combinations of constraining variables)
CCA1 CCA2 CCA3 CCA4 CCA5 CCA6
18 -0.51001 -0.86277 -1.265868 -0.84832 0.00585 -0.55362
15 -0.11359 0.47962 0.292164 -0.71983 -1.13275 -0.12336
24 -0.22365 0.90898 -2.201280 0.15538 2.96165 -1.67268
27 -1.64472 0.76232 0.420020 0.74263 -0.06008 0.22434
23 -1.32539 -0.30282 -0.919558 -0.37197 0.23629 -0.78394
19 -1.07981 0.11303 -0.113499 -0.60422 -0.19793 -1.29585
22 -0.80792 -0.42650 1.084574 -0.98064 1.21790 1.50140
16 -0.09523 -1.62392 0.407793 -0.06884 -0.07282 0.33619
28 -2.23615 0.64537 0.663728 2.48585 -0.46791 0.25333
13 0.42015 -1.10098 3.235920 -0.54956 1.29688 -1.38806
14 -0.14558 -0.43109 0.310536 -0.84389 0.19863 -0.03026
20 -0.46785 0.19132 -0.309976 -1.55804 -0.35312 -0.83013
25 -0.94343 0.48338 -0.020181 -0.08962 -0.75201 1.14075
7 1.13410 -1.71592 -0.683778 1.28206 -0.45511 -0.70628
5 0.46572 -2.30802 -1.130203 1.25890 1.45704 1.70523
6 0.86525 0.07408 -0.448437 -0.03867 -0.80286 1.09713
3 1.05262 0.42467 -0.654573 0.68815 -1.10660 0.89967
4 1.39514 -0.07478 0.728546 1.89627 -1.33345 -2.59477
2 1.33321 0.54940 0.492977 0.24130 0.78232 0.74187
9 1.29092 1.95150 0.005342 -0.81565 0.26355 0.18074
12 0.41778 0.20237 0.558481 -0.57975 -0.17713 0.78064
10 0.48239 1.37874 -0.125838 0.29170 0.38215 -0.13758
11 0.56717 0.36508 -0.101642 -0.04929 -0.36747 0.15785
21 -0.55130 -1.11370 -0.928444 -1.85875 -1.87942 -0.58312
Biplot scores for constraining variables
CCA1 CCA2 CCA3 CCA4 CCA5 CCA6
Baresoil -0.52916 -0.39394 -0.08617 -0.48990 -0.17135 -0.47822
Humdepth -0.74502 0.09330 0.17115 -0.23030 -0.12889 -0.16495
pH 0.50738 0.14614 -0.26885 0.17600 -0.10593 0.17747
N -0.18023 -0.58693 0.04837 0.36820 0.15022 0.16997
P -0.36138 0.63661 0.04617 0.32849 0.17792 -0.38565
K -0.39232 0.31726 0.55953 0.12491 0.25046 -0.48506
Ca -0.48559 0.41273 0.01284 -0.06856 0.28471 -0.24271
Mg -0.45563 0.35145 -0.07675 -0.07944 0.47484 -0.50687
S -0.03901 0.50851 0.36649 0.02615 0.41656 -0.63216
Al 0.80742 0.10327 0.25002 0.22841 -0.03898 -0.35763
Fe 0.67570 0.02563 0.09560 0.24078 -0.08059 -0.04175
@rezacheloei
Copy link

Dear Matthew,
I am thankful for subjects that you present.
I have a question about "Canonical Correspondence Analysis in R using the vegan library".
If it is possible for you please interpret about plot of cca!
I look forward to reply from you
Sincerely

@sfpravelo
Copy link

Hi, do you know why Im getting this error when I run the cca "Error in eval(predvars, data, env) : object 'Turb' not found"

@ahma077
Copy link

ahma077 commented Aug 17, 2022

Hi, do you know why Im getting this error when I run the cca "Error in eval(predvars, data, env) : object 'Turb' not found"

it's too late for replying, but if you have an error like that it means that the Turb is not in your R environment yet. Therefore, to solve the problem, make sure that you have already loaded the object Turb and the name in your environment and your script are the same (i.e. Turb or turb) since R is case sensitive.

@najouamghazli
Copy link

Thank you for this code.
I am applying this to my data, but I don't get the OTUs and sites names. Any idea on how to resolve this ?

Thank you,

Najoua

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment