Introduction to gllvm Part 1: Ordination

Introduction to gllvm

R package gllvm

# From CRAN
install.packages(gllvm)
# OR
# From GitHub using devtools package's function install_github
devtools::install_github("JenniNiku/gllvm")
Problems?

gllvm package depends on R packages TMB and mvabund, try to install these first.

  • GLLVMs are computationally intensive to fit due the integral in log-likelihood.

  • gllvm package overcomes computational problems by applying closed form approximations to log-likelihood and using automatic differentiation in C++ to accelerate computation times (TMB2).

  • Estimation is performed using either variational approximation (VA3), extended variational approximation method (EVA4) or Laplace approximation (LA5) method implemented via R package TMB.

  • VA method is faster and more accurate than LA, but not applicable for all distributions and link functions.

  • Using gllvm we can fit

    • GLLVM without covariates gives model-based ordination and biplots
    • GLLVM with environmental covariates for studying factors explaining species abundance
    • Fourth corner models with latent variables for studying environmental-trait interactions
    • GLLVM without latent variables fits basic multivariate GLMs
  • Additional tools: model checking, model selection, inference, visualization.

Distributions

Response Distribution Method Link
Counts Poisson VA/LA log
NB VA/LA log
ZIP LA log
Binary Bernoulli VA/LA probit
EVA/LA logit
Ordinal Ordinal VA probit
Normal Gaussian VA/LA identity
Positive continuous Gamma VA/LA log
Non-negative continuous Exponential VA/LA log
Biomass Tweedie LA/EVA log
Percent cover beta LA/EVA probit/logit

Data input

Main function of the gllvm package is gllvm(), which can be used to fit GLLVMs for multivariate data with the most important arguments listed in the following:

gllvm(y = NULL, X = NULL, TR = NULL, family, num.lv = 2, 
 formula = NULL, method = "VA", row.eff = FALSE, n.init=1, starting.val ="res", ...)
  • y: matrix of abundances
  • X: matrix or data.frame of environmental variables
  • TR: matrix or data.frame of trait variables
  • family: distribution for responses
  • num.lv: number of latent variables
  • method: approximation used “VA” or “LA”
  • row.eff: type of community level row effects
  • n.init: number of random starting points for latent variables
  • starting.val: starting value method
library(gllvm)

Example: Spiders

  • Abundances of 12 hunting spider species measured as a count at 28 sites6.
  • Six environmental variables measured at each site.
    • soil.dry: Soil dry mass
    • bare.sand: cover of bare sand
    • fallen.leaves: cover of fallen leaves/twigs
    • moss: cover of moss
    • herb.layer: cover of herb layer
    • reflection: reflection of the soil surface with a cloudless sky

Data fitting

Fitting basic GLLVM g(E(yij)) = β0j + uiθj with gllvm:

data("spider", package = "mvabund")
library(gllvm)
fitnb <- gllvm(y = spider$abund, family = "negative.binomial", num.lv = 2)
fitnb
## Call: 
## gllvm(y = spider$abund, family = "negative.binomial", num.lv = 2)
## family: 
## [1] "negative.binomial"
## method: 
## [1] "VA"
## 
## log-likelihood:  -733.6808 
## Residual degrees of freedom:  289 
## AIC:  1561.362 
## AICc:  1577.028 
## BIC:  1740.766

Residual analysis

  • Residual analysis can be used to assess the appropriateness of the fitted model (eg. in terms of mean-variance relationship).

  • Randomized quantile/Dunn-Smyth residuals7 are used in the package, as they provide standard normal distributed residuals, even for discrete responses, in the case of a proper model.

par(mfrow = c(1,2))
plot(fitnb, which = 1:2)

Model selection

  • Information criteria can be used for model selection.
  • For example, compare distributions or choose suitable number of latent variables.
fitp <- gllvm(y = spider$abund, family = poisson(), num.lv = 2)
fitnb <- gllvm(y = spider$abund, family = "negative.binomial", num.lv = 2)
AIC(fitp)
## [1] 1761.655
AIC(fitnb)
## [1] 1561.361

Exercises

Try to do these exercises for the next 10 minutes, as many as time is enough for.

E1. Load spider data from mvabund package and take a look at the dataset.

library(gllvm)
data("spider", package = "mvabund")
# more info: 
# ?spider
Show the answers.

1. Print the data and covariates and draw a boxplot of the data.

# response matrix:
spider$abund
##       Alopacce Alopcune Alopfabr Arctlute Arctperi Auloalbi Pardlugu Pardmont
##  [1,]       25       10        0        0        0        4        0       60
##  [2,]        0        2        0        0        0       30        1        1
##  [3,]       15       20        2        2        0        9        1       29
##  [4,]        2        6        0        1        0       24        1        7
##  [5,]        1       20        0        2        0        9        1        2
##  [6,]        0        6        0        6        0        6        0       11
##  [7,]        2        7        0       12        0       16        1       30
##  [8,]        0       11        0        0        0        7       55        2
##  [9,]        1        1        0        0        0        0        0       26
## [10,]        3        0        1        0        0        0        0       22
## [11,]       15        1        2        0        0        1        0       95
## [12,]       16       13        0        0        0        0        0       96
## [13,]        3       43        1        2        0       18        1       24
## [14,]        0        2        0        1        0        4        3       14
## [15,]        0        0        0        0        0        0        6        0
## [16,]        0        3        0        0        0        0        6        0
## [17,]        0        0        0        0        0        0        2        0
## [18,]        0        1        0        0        0        0        5        0
## [19,]        0        1        0        0        0        0       12        0
## [20,]        0        2        0        0        0        0       13        0
## [21,]        0        1        0        0        0        0       16        1
## [22,]        7        0       16        0        4        0        0        2
## [23,]       17        0       15        0        7        0        2        6
## [24,]       11        0       20        0        5        0        0        3
## [25,]        9        1        9        0        0        2        1       11
## [26,]        3        0        6        0       18        0        0        0
## [27,]       29        0       11        0        4        0        0        1
## [28,]       15        0       14        0        1        0        0        6
##       Pardnigr Pardpull Trocterr Zoraspin
##  [1,]       12       45       57        4
##  [2,]       15       37       65        9
##  [3,]       18       45       66        1
##  [4,]       29       94       86       25
##  [5,]      135       76       91       17
##  [6,]       27       24       63       34
##  [7,]       89      105      118       16
##  [8,]        2        1       30        3
##  [9,]        1        1        2        0
## [10,]        0        0        1        0
## [11,]        0        1        4        0
## [12,]        1        8       13        0
## [13,]       53       72       97       22
## [14,]       15       72       94       32
## [15,]        0        0       25        3
## [16,]        2        0       28        4
## [17,]        0        0       23        2
## [18,]        0        0       25        0
## [19,]        1        0       22        3
## [20,]        0        0       22        2
## [21,]        0        1       18        2
## [22,]        0        0        1        0
## [23,]        0        0        1        0
## [24,]        0        0        0        0
## [25,]        6        0       16        6
## [26,]        0        0        1        0
## [27,]        0        0        0        0
## [28,]        0        0        2        0
# Environmental variables
spider$x
##    soil.dry bare.sand fallen.leaves   moss herb.layer reflection
## 1    2.3321    0.0000        0.0000 3.0445     4.4543     3.9120
## 2    3.0493    0.0000        1.7918 1.0986     4.5643     1.6094
## 3    2.5572    0.0000        0.0000 2.3979     4.6052     3.6889
## 4    2.6741    0.0000        0.0000 2.3979     4.6151     2.9957
## 5    3.0155    0.0000        0.0000 0.0000     4.6151     2.3026
## 6    3.3810    2.3979        3.4340 2.3979     3.4340     0.6931
## 7    3.1781    0.0000        0.0000 0.6931     4.6151     2.3026
## 8    2.6247    0.0000        4.2627 1.0986     3.4340     0.6931
## 9    2.4849    0.0000        0.0000 4.3307     3.2581     3.4012
## 10   2.1972    3.9318        0.0000 3.4340     3.0445     3.6889
## 11   2.2192    0.0000        0.0000 4.1109     3.7136     3.6889
## 12   2.2925    0.0000        0.0000 3.8286     4.0254     3.6889
## 13   3.5175    1.7918        1.7918 0.6931     4.5109     3.4012
## 14   3.0865    0.0000        0.0000 1.7918     4.5643     1.0986
## 15   3.2696    0.0000        4.3944 0.6931     3.0445     0.6931
## 16   3.0301    0.0000        4.6052 0.6931     0.6931     0.0000
## 17   3.3322    0.0000        4.4543 0.6931     3.0445     1.0986
## 18   3.1224    0.0000        4.3944 0.0000     3.0445     1.0986
## 19   2.9232    0.0000        4.5109 1.6094     1.6094     0.0000
## 20   3.1091    0.0000        4.5951 0.6931     0.6931     0.0000
## 21   2.9755    0.0000        4.5643 0.6931     1.7918     0.0000
## 22   1.2528    3.2581        0.0000 4.3307     0.6931     3.9120
## 23   1.1939    3.0445        0.0000 4.0254     3.2581     4.0943
## 24   1.6487    3.2581        0.0000 4.0254     3.0445     4.0073
## 25   1.8245    3.5835        0.0000 1.0986     4.1109     2.3026
## 26   0.9933    4.5109        0.0000 1.7918     1.7918     4.3820
## 27   0.9555    2.3979        0.0000 3.8286     3.4340     3.6889
## 28   0.9555    3.4340        0.0000 3.7136     3.4340     3.6889
# Plot data using boxplot:
boxplot(spider$abund)

E2. Fit GLLVM to spider data with a suitable distribution. Data consists of counts of spider species.

# Take a look at the function documentation for help: 
?gllvm
Show the answers.

2. Response variables in spider data are counts, so Poisson, negative binomial and zero inflated Poisson are possible. However, ZIP is implemented only with Laplace method, so it need to be noticed, that if models are fitted with different methods they can not be compared with information criteria. Let’s try just with a Poisson and NB. NOTE THAT the results may not be exactly the same as below, as the initial values for each model fit are slightly different, so the results may also differ slightly.

# Fit a GLLVM to data
fitp <- gllvm(y = spider$abund, family = poisson(), num.lv = 2)
fitp
## Call: 
## gllvm(y = spider$abund, family = poisson(), num.lv = 2)
## family: 
## [1] "poisson"
## method: 
## [1] "VA"
## 
## log-likelihood:  -845.8277 
## Residual degrees of freedom:  301 
## AIC:  1761.655 
## AICc:  1770.055 
## BIC:  1895.254
fitnb <- gllvm(y = spider$abund, family = "negative.binomial", num.lv = 2)
fitnb
## Call: 
## gllvm(y = spider$abund, family = "negative.binomial", num.lv = 2)
## family: 
## [1] "negative.binomial"
## method: 
## [1] "VA"
## 
## log-likelihood:  -733.6807 
## Residual degrees of freedom:  289 
## AIC:  1561.361 
## AICc:  1577.028 
## BIC:  1740.766

Based on AIC, NB distribution suits better. How about residual analysis: NOTE THAT The package uses randomized quantile residuals so each time you plot the residuals, they look a little different.

# Fit a GLLVM to data
plot(fitp)

plot(fitnb)

You could do these comparisons with Laplace method as well, using the code below, and it would give the same conclusion that NB distribution suits best:

fitLAp <- gllvm(y = spider$abund, family = poisson(), method = "LA", num.lv = 2)
fitLAnb <- gllvm(y = spider$abund, family = "negative.binomial", method = "LA", num.lv = 2)
fitLAzip <- gllvm(y = spider$abund, family = "ZIP", method = "LA", num.lv = 2)
AIC(fitLAp)
AIC(fitLAnb)
AIC(fitLAzip)

E3. Explore the fitted model. Where are the estimates for parameters? What about predicted latent variables? Standard errors?

Show the answers.

3. Lets explore the fitted model:

# Parameters:
coef(fitnb)
## $Species.scores
##                 LV1         LV2
## Alopacce  1.0000000  0.00000000
## Alopcune -0.2625952  1.00000000
## Alopfabr  0.9012292 -0.48393657
## Arctlute -0.7014034  2.02781205
## Arctperi  0.5935267 -1.59008553
## Auloalbi -0.6517388  1.53322932
## Pardlugu -0.8877750 -0.02802721
## Pardmont  0.7491447  0.58844215
## Pardnigr -0.5737413  1.78006353
## Pardpull -0.4138221  1.94817301
## Trocterr -0.4689025  0.78798829
## Zoraspin -0.6953949  1.15685550
## 
## $sigma.lv
##      LV1      LV2 
## 1.804775 1.828086 
## 
## $Intercept
##   Alopacce   Alopcune   Alopfabr   Arctlute   Arctperi   Auloalbi   Pardlugu 
##  0.7724584  0.5535169 -0.1440744 -3.5833152 -3.3696063 -0.7282448  0.3193900 
##   Pardmont   Pardnigr   Pardpull   Trocterr   Zoraspin 
##  1.6249501 -0.2716193 -0.1315820  2.6574475  0.2488348 
## 
## $inv.phi
##   Alopacce   Alopcune   Alopfabr   Arctlute   Arctperi   Auloalbi   Pardlugu 
##  1.3063417  1.4683250  0.7050551  0.8983128  2.3293583  1.5391612  1.1529986 
##   Pardmont   Pardnigr   Pardpull   Trocterr   Zoraspin 
##  1.6176190  2.5404289  2.1947241 12.9711499  2.5724273 
## 
## $phi
##   Alopacce   Alopcune   Alopfabr   Arctlute   Arctperi   Auloalbi   Pardlugu 
## 0.76549648 0.68104812 1.41832885 1.11319802 0.42930278 0.64970455 0.86730377 
##   Pardmont   Pardnigr   Pardpull   Trocterr   Zoraspin 
## 0.61819253 0.39363432 0.45563813 0.07709417 0.38873790
# Where are the predicted latent variable values? just fitp$lvs or
getLV(fitnb)
##               LV1           LV2
## Row1   0.92899212  1.270177e+00
## Row2  -0.68264026  7.933739e-01
## Row3   0.67659567  1.274112e+00
## Row4  -0.24632080  1.149802e+00
## Row5  -0.36031069  1.217240e+00
## Row6  -0.35130546  1.008677e+00
## Row7   0.02959233  1.426032e+00
## Row8  -1.52129877 -7.146171e-02
## Row9   0.91118764  8.564613e-05
## Row10  1.03760034 -4.948931e-01
## Row11  1.48956791  3.020294e-01
## Row12  1.32523567  8.068165e-01
## Row13  0.11634963  1.356741e+00
## Row14 -0.43649448  1.014297e+00
## Row15 -1.31903718 -5.458637e-01
## Row16 -1.17381988 -2.433557e-01
## Row17 -1.11264013 -5.139948e-01
## Row18 -1.15863723 -5.438850e-01
## Row19 -1.33994790 -5.036119e-01
## Row20 -1.36645355 -5.930544e-01
## Row21 -1.13878611 -4.768060e-01
## Row22  0.76452404 -1.347489e+00
## Row23  0.92134341 -1.381656e+00
## Row24  0.97924395 -1.396907e+00
## Row25  0.63183976  5.761502e-01
## Row26  0.27514642 -1.965126e+00
## Row27  1.11608599 -1.310384e+00
## Row28  1.01949361 -8.272304e-01
# Standard errors for parameters:
fitnb$sd
## $theta
##                LV1       LV2
## Alopacce 0.0000000 0.0000000
## Alopcune 0.2731255 0.0000000
## Alopfabr 0.3299811 0.2043314
## Arctlute 0.6730201 0.7831302
## Arctperi 0.4752866 0.5590446
## Auloalbi 0.4399469 0.3891129
## Pardlugu 0.2272968 0.2005683
## Pardmont 0.2195492 0.1815787
## Pardnigr 0.4725330 0.3796300
## Pardpull 0.4957877 0.4104299
## Trocterr 0.2094407 0.1567081
## Zoraspin 0.3473111 0.2794486
## 
## $sigma.lv
##       LV1       LV2 
## 0.4167065 0.4249198 
## 
## $beta0
##  Alopacce  Alopcune  Alopfabr  Arctlute  Arctperi  Auloalbi  Pardlugu  Pardmont 
## 0.4521704 0.4625265 0.5481233 1.7058571 1.4658594 0.7887834 0.4390544 0.3996152 
##  Pardnigr  Pardpull  Trocterr  Zoraspin 
## 0.7862743 0.8334884 0.3337012 0.5825491 
## 
## $inv.phi
##  Alopacce  Alopcune  Alopfabr  Arctlute  Arctperi  Auloalbi  Pardlugu  Pardmont 
## 0.6026268 0.6024617 0.3957004 0.5791360 1.8019720 0.8383588 0.5811212 0.7754628 
##  Pardnigr  Pardpull  Trocterr  Zoraspin 
## 1.3145865 1.2461651 6.8876477 1.2847792 
## 
## $phi
##   Alopacce   Alopcune   Alopfabr   Arctlute   Arctperi   Auloalbi   Pardlugu 
## 0.35313017 0.27943771 0.79601329 0.71767107 0.33210502 0.35388467 0.43712859 
##   Pardmont   Pardnigr   Pardpull   Trocterr   Zoraspin 
## 0.29635241 0.20369251 0.25871148 0.04093681 0.19415218

E4. Fit model with different numbers of latent variables.

Show the answers.

4. Default number of latent variables is 2. Let’s try 1 and 3 latent variables as well:

# In exercise 2, we fitted GLLVM with two latent variables 
fitnb
## Call: 
## gllvm(y = spider$abund, family = "negative.binomial", num.lv = 2)
## family: 
## [1] "negative.binomial"
## method: 
## [1] "VA"
## 
## log-likelihood:  -733.6807 
## Residual degrees of freedom:  289 
## AIC:  1561.361 
## AICc:  1577.028 
## BIC:  1740.766
# How about 1 or 3 LVs
fitnb1 <- gllvm(y = spider$abund, family = "negative.binomial", num.lv = 1)
fitnb1
## Call: 
## gllvm(y = spider$abund, family = "negative.binomial", num.lv = 1)
## family: 
## [1] "negative.binomial"
## method: 
## [1] "VA"
## 
## log-likelihood:  -759.4012 
## Residual degrees of freedom:  300 
## AIC:  1590.802 
## AICc:  1599.712 
## BIC:  1728.218
getLV(fitnb1)
##               LV1
## Row1  -0.81688484
## Row2  -0.94967922
## Row3  -0.91694864
## Row4  -1.10578698
## Row5  -1.22160104
## Row6  -1.03408584
## Row7  -1.26781605
## Row8  -0.53480075
## Row9   0.36608315
## Row10  1.00898159
## Row11  0.31292713
## Row12 -0.31673237
## Row13 -1.17630959
## Row14 -1.03505726
## Row15 -0.09010850
## Row16 -0.27463504
## Row17 -0.02447312
## Row18 -0.02078795
## Row19 -0.12972943
## Row20 -0.06890800
## Row21 -0.04323658
## Row22  1.54074337
## Row23  1.65004616
## Row24  1.68354807
## Row25 -0.28912586
## Row26  1.92860060
## Row27  1.62317325
## Row28  1.20028997
fitnb3 <- gllvm(y = spider$abund, family = "negative.binomial", num.lv = 3)
fitnb3
## Call: 
## gllvm(y = spider$abund, family = "negative.binomial", num.lv = 3)
## family: 
## [1] "negative.binomial"
## method: 
## [1] "VA"
## 
## log-likelihood:  -733.6809 
## Residual degrees of freedom:  279 
## AIC:  1581.362 
## AICc:  1605.146 
## BIC:  1798.937
getLV(fitnb3)
##              LV1           LV2           LV3
## Row1   0.9293665  1.2678426697 -9.552357e-06
## Row2  -0.6831070  0.7927320473  5.907683e-07
## Row3   0.6768147  1.2719764628 -1.889553e-06
## Row4  -0.2467654  1.1484834598 -5.926319e-06
## Row5  -0.3612141  1.2167213749  2.743362e-06
## Row6  -0.3519693  1.0077996722 -5.107830e-07
## Row7   0.0297057  1.4247349127  5.823804e-07
## Row8  -1.5212016 -0.0694065000  2.012703e-05
## Row9   0.9115155 -0.0009235735  9.047841e-06
## Row10  1.0380189 -0.4953690981  7.975277e-06
## Row11  1.4892044  0.2999484073  5.828945e-06
## Row12  1.3244207  0.8041756387  8.723991e-06
## Row13  0.1155115  1.3549059265 -1.318466e-06
## Row14 -0.4359253  1.0137532187 -1.098639e-05
## Row15 -1.3195449 -0.5443661743 -7.921439e-06
## Row16 -1.1746140 -0.2424748871 -1.052035e-06
## Row17 -1.1132730 -0.5129674261 -7.215096e-06
## Row18 -1.1592678 -0.5428359496 -4.256836e-06
## Row19 -1.3403216 -0.5022217602 -1.574452e-06
## Row20 -1.3669302 -0.5911671603 -1.020324e-06
## Row21 -1.1388464 -0.4750563932  1.341218e-06
## Row22  0.7646072 -1.3472676568  2.241688e-07
## Row23  0.9214671 -1.3818788981  1.308602e-06
## Row24  0.9790019 -1.3967103213  3.013644e-06
## Row25  0.6317808  0.5752247189 -7.934527e-06
## Row26  0.2746214 -1.9645668023 -1.929076e-06
## Row27  1.1158206 -1.3112695817  2.562168e-06
## Row28  1.0196674 -0.8280277693 -3.546697e-07

E5. Include environmental variables to the GLLVM and explore the model fit.

Show the answers.

5. Environmental variables can be included with an argument X:

fitnbx <- gllvm(y = spider$abund, X = spider$x, family = "negative.binomial", seed = 123, num.lv = 2)
fitnbx
## Call: 
## gllvm(y = spider$abund, X = spider$x, family = "negative.binomial", 
##     num.lv = 2, seed = 123)
## family: 
## [1] "negative.binomial"
## method: 
## [1] "VA"
## 
## log-likelihood:  -593.675 
## Residual degrees of freedom:  217 
## AIC:  1425.35 
## AICc:  1557.572 
## BIC:  1879.586
coef(fitnbx)
## $Species.scores
##                 LV1          LV2
## Alopacce  1.0000000 0.000000e+00
## Alopcune  0.4629835 1.000000e+00
## Alopfabr  1.3335160 5.840465e-01
## Arctlute -1.1491195 1.151988e+00
## Arctperi  1.1437539 1.628019e-06
## Auloalbi  0.9573392 6.094261e-01
## Pardlugu -6.8694877 1.962162e-01
## Pardmont  1.5847039 3.640404e-01
## Pardnigr -0.3413011 1.070902e+00
## Pardpull  1.6495898 7.283101e-01
## Trocterr -2.2544699 4.991855e-01
## Zoraspin -0.1820456 7.373605e-01
## 
## $sigma.lv
##          LV1          LV2 
## 5.426615e-08 9.495749e-01 
## 
## $Intercept
##    Alopacce    Alopcune    Alopfabr    Arctlute    Arctperi    Auloalbi 
##  -1.8100187  -4.4266967  -1.6414980 -14.7088352 -14.9041892 -15.7070899 
##    Pardlugu    Pardmont    Pardnigr    Pardpull    Trocterr    Zoraspin 
##   7.9304888  -6.3847285  -5.1545533 -13.0279685  -0.2015223  -3.8462784 
## 
## $Xcoef
##            soil.dry   bare.sand fallen.leaves        moss herb.layer
## Alopacce -0.9582832 -0.09062849   -0.32435132  0.10118158  0.7369952
## Alopcune  1.3494591 -0.44989324    0.01752089 -0.19913585  0.4198230
## Alopfabr -0.8700221  0.69545126   -0.40330007  0.54005072  0.3019104
## Arctlute  5.8747669  0.16534287   -1.67071488  0.34424354 -0.1214404
## Arctperi -1.7803424  0.23854863   -1.41584238  0.49568329  0.0212277
## Auloalbi  0.2248742 -0.04627419    0.62830523  0.06245445  4.0495963
## Pardlugu -2.5230942 -0.71528993    0.36228688 -0.37412872  0.5864604
## Pardmont  1.5855371  0.13344012   -0.51400509  0.88764055  0.7347535
## Pardnigr  2.5562580 -0.02638632   -0.84807041 -0.45619844  0.7289500
## Pardpull  3.0006899 -0.35227946   -0.47029439  0.36481733  1.9513745
## Trocterr  1.2400968 -0.23002117   -0.22739824 -0.20316859  0.4459694
## Zoraspin  2.1000028  0.12013086   -0.57196404 -0.08984428  0.6202207
##           reflection
## Alopacce  0.86916634
## Alopcune  0.42106670
## Alopfabr  0.06122226
## Arctlute -0.64300313
## Arctperi  4.00551154
## Auloalbi -0.54335807
## Pardlugu -1.16961846
## Pardmont  0.07832973
## Pardnigr -0.53369940
## Pardpull -0.36807364
## Trocterr -0.29457771
## Zoraspin -0.93157295
## 
## $inv.phi
##     Alopacce     Alopcune     Alopfabr     Arctlute     Arctperi     Auloalbi 
## 9.157555e+00 2.937613e+00 7.758681e+00 2.022703e+00 2.458177e+06 1.023033e+01 
##     Pardlugu     Pardmont     Pardnigr     Pardpull     Trocterr     Zoraspin 
## 2.769764e+01 1.990591e+00 7.673397e+00 1.667698e+01 2.643388e+01 6.321980e+00 
## 
## $phi
##     Alopacce     Alopcune     Alopfabr     Arctlute     Arctperi     Auloalbi 
## 1.091995e-01 3.404125e-01 1.288879e-01 4.943881e-01 4.068056e-07 9.774853e-02 
##     Pardlugu     Pardmont     Pardnigr     Pardpull     Trocterr     Zoraspin 
## 3.610416e-02 5.023634e-01 1.303204e-01 5.996289e-02 3.783024e-02 1.581783e-01
# confidence intervals for parameters:
confint(fitnbx)
##                                      2.5 %        97.5 %
## sigma.lv.LV1                 -1.246132e-04  1.247218e-04
## sigma.lv.LV2                  4.336985e-01  1.465451e+00
## theta.LV1.1                   1.000000e+00  1.000000e+00
## theta.LV1.2                  -3.741657e+08  3.741657e+08
## theta.LV1.3                  -2.185385e+08  2.185385e+08
## theta.LV1.4                  -4.309521e+08  4.309521e+08
## theta.LV1.5                  -2.744232e+03  2.746520e+03
## theta.LV1.6                  -2.279834e+08  2.279834e+08
## theta.LV1.7                  -7.341023e+07  7.341022e+07
## theta.LV1.8                  -1.360222e+08  1.360222e+08
## theta.LV1.9                  -4.006678e+08  4.006678e+08
## theta.LV1.10                 -2.724992e+08  2.724992e+08
## theta.LV1.11                 -1.867747e+08  1.867747e+08
## theta.LV1.12                 -2.758763e+08  2.758763e+08
## theta.LV2.1                   0.000000e+00  0.000000e+00
## theta.LV2.2                   1.000000e+00  1.000000e+00
## theta.LV2.3                  -5.152737e-02  1.219620e+00
## theta.LV2.4                  -1.946451e-01  2.498622e+00
## theta.LV2.5                  -2.411382e-03  2.414638e-03
## theta.LV2.6                   5.419826e-02  1.164654e+00
## theta.LV2.7                  -1.285423e-01  5.209746e-01
## theta.LV2.8                  -1.512912e-01  8.793721e-01
## theta.LV2.9                   4.319015e-01  1.709903e+00
## theta.LV2.10                  2.702314e-01  1.186389e+00
## theta.LV2.11                  2.336922e-01  7.646789e-01
## theta.LV2.12                  2.325839e-01  1.242137e+00
## Intercept.Alopacce           -5.312975e+00  1.692938e+00
## Intercept.Alopcune           -9.579763e+00  7.263698e-01
## Intercept.Alopfabr           -7.964720e+00  4.681724e+00
## Intercept.Arctlute           -3.138874e+01  1.971067e+00
## Intercept.Arctperi           -2.629651e+01 -3.511867e+00
## Intercept.Auloalbi           -2.246385e+01 -8.950328e+00
## Intercept.Pardlugu            3.192753e+00  1.266822e+01
## Intercept.Pardmont           -1.043612e+01 -2.333336e+00
## Intercept.Pardnigr           -1.020004e+01 -1.090650e-01
## Intercept.Pardpull           -1.860723e+01 -7.448702e+00
## Intercept.Trocterr           -2.430753e+00  2.027708e+00
## Intercept.Zoraspin           -7.701690e+00  9.132759e-03
## Xcoef.soil.dry:Alopacce      -1.627841e+00 -2.887250e-01
## Xcoef.soil.dry:Alopcune      -1.655133e-01  2.864431e+00
## Xcoef.soil.dry:Alopfabr      -1.961758e+00  2.217138e-01
## Xcoef.soil.dry:Arctlute       4.207355e-01  1.132880e+01
## Xcoef.soil.dry:Arctperi      -3.435499e+00 -1.251856e-01
## Xcoef.soil.dry:Auloalbi      -9.609802e-01  1.410729e+00
## Xcoef.soil.dry:Pardlugu      -3.775683e+00 -1.270505e+00
## Xcoef.soil.dry:Pardmont       6.326174e-01  2.538457e+00
## Xcoef.soil.dry:Pardnigr       1.053322e+00  4.059194e+00
## Xcoef.soil.dry:Pardpull       1.556423e+00  4.444957e+00
## Xcoef.soil.dry:Trocterr       5.870455e-01  1.893148e+00
## Xcoef.soil.dry:Zoraspin       9.200160e-01  3.279990e+00
## Xcoef.bare.sand:Alopacce     -3.852634e-01  2.040064e-01
## Xcoef.bare.sand:Alopcune     -9.735637e-01  7.377725e-02
## Xcoef.bare.sand:Alopfabr      1.975983e-01  1.193304e+00
## Xcoef.bare.sand:Arctlute     -1.534236e+00  1.864922e+00
## Xcoef.bare.sand:Arctperi     -6.815203e-01  1.158618e+00
## Xcoef.bare.sand:Auloalbi     -4.620590e-01  3.695107e-01
## Xcoef.bare.sand:Pardlugu     -1.248063e+00 -1.825173e-01
## Xcoef.bare.sand:Pardmont     -1.850487e-01  4.519289e-01
## Xcoef.bare.sand:Pardnigr     -5.129598e-01  4.601872e-01
## Xcoef.bare.sand:Pardpull     -8.533998e-01  1.488408e-01
## Xcoef.bare.sand:Trocterr     -4.441392e-01 -1.590314e-02
## Xcoef.bare.sand:Zoraspin     -2.623534e-01  5.026152e-01
## Xcoef.fallen.leaves:Alopacce -9.896743e-01  3.409716e-01
## Xcoef.fallen.leaves:Alopcune -5.794167e-01  6.144585e-01
## Xcoef.fallen.leaves:Alopfabr -1.536973e+00  7.303733e-01
## Xcoef.fallen.leaves:Arctlute -3.795623e+00  4.541928e-01
## Xcoef.fallen.leaves:Arctperi -1.109120e+02  1.080803e+02
## Xcoef.fallen.leaves:Auloalbi  1.122802e-01  1.144330e+00
## Xcoef.fallen.leaves:Pardlugu  3.931960e-02  6.852542e-01
## Xcoef.fallen.leaves:Pardmont -1.026471e+00 -1.539328e-03
## Xcoef.fallen.leaves:Pardnigr -1.464324e+00 -2.318164e-01
## Xcoef.fallen.leaves:Pardpull -9.508691e-01  1.028028e-02
## Xcoef.fallen.leaves:Trocterr -4.818560e-01  2.705948e-02
## Xcoef.fallen.leaves:Zoraspin -1.030080e+00 -1.138483e-01
## Xcoef.moss:Alopacce          -2.225804e-01  4.249436e-01
## Xcoef.moss:Alopcune          -7.947123e-01  3.964406e-01
## Xcoef.moss:Alopfabr           1.284141e-02  1.067260e+00
## Xcoef.moss:Arctlute          -9.125744e-01  1.601061e+00
## Xcoef.moss:Arctperi          -1.222869e-01  1.113654e+00
## Xcoef.moss:Auloalbi          -4.180240e-01  5.429329e-01
## Xcoef.moss:Pardlugu          -8.453708e-01  9.711331e-02
## Xcoef.moss:Pardmont           4.275967e-01  1.347684e+00
## Xcoef.moss:Pardnigr          -1.039056e+00  1.266589e-01
## Xcoef.moss:Pardpull          -8.531934e-02  8.149540e-01
## Xcoef.moss:Trocterr          -4.718831e-01  6.554593e-02
## Xcoef.moss:Zoraspin          -5.683276e-01  3.886391e-01
## Xcoef.herb.layer:Alopacce     3.371270e-01  1.136863e+00
## Xcoef.herb.layer:Alopcune    -3.375486e-01  1.177195e+00
## Xcoef.herb.layer:Alopfabr    -2.707449e-01  8.745657e-01
## Xcoef.herb.layer:Arctlute    -3.059809e+00  2.816928e+00
## Xcoef.herb.layer:Arctperi    -4.106630e-01  4.531184e-01
## Xcoef.herb.layer:Auloalbi     2.456029e+00  5.643164e+00
## Xcoef.herb.layer:Pardlugu     2.741184e-01  8.988023e-01
## Xcoef.herb.layer:Pardmont     4.716383e-02  1.422343e+00
## Xcoef.herb.layer:Pardnigr    -1.332867e-01  1.591187e+00
## Xcoef.herb.layer:Pardpull     9.274449e-01  2.975304e+00
## Xcoef.herb.layer:Trocterr     1.167391e-01  7.751997e-01
## Xcoef.herb.layer:Zoraspin    -1.586853e-02  1.256310e+00
## Xcoef.reflection:Alopacce     3.389879e-01  1.399345e+00
## Xcoef.reflection:Alopcune    -3.761264e-01  1.218260e+00
## Xcoef.reflection:Alopfabr    -9.862204e-01  1.108665e+00
## Xcoef.reflection:Arctlute    -1.930134e+00  6.441274e-01
## Xcoef.reflection:Arctperi     1.459683e+00  6.551340e+00
## Xcoef.reflection:Auloalbi    -1.141336e+00  5.461980e-02
## Xcoef.reflection:Pardlugu    -1.807420e+00 -5.318174e-01
## Xcoef.reflection:Pardmont    -5.105917e-01  6.672512e-01
## Xcoef.reflection:Pardnigr    -1.302666e+00  2.352668e-01
## Xcoef.reflection:Pardpull    -8.994909e-01  1.633436e-01
## Xcoef.reflection:Trocterr    -6.348731e-01  4.571772e-02
## Xcoef.reflection:Zoraspin    -1.527850e+00 -3.352957e-01
## inv.phi.Alopacce             -1.059876e+01  2.891387e+01
## inv.phi.Alopcune             -7.201361e-02  5.947239e+00
## inv.phi.Alopfabr             -8.212085e+00  2.372945e+01
## inv.phi.Arctlute             -1.057832e+00  5.103237e+00
## inv.phi.Arctperi             -2.809637e+09  2.814553e+09
## inv.phi.Auloalbi             -3.756512e+00  2.421718e+01
## inv.phi.Pardlugu             -4.813201e+01  1.035273e+02
## inv.phi.Pardmont              5.019926e-01  3.479189e+00
## inv.phi.Pardnigr             -1.346000e+00  1.669279e+01
## inv.phi.Pardpull             -4.389057e+00  3.774302e+01
## inv.phi.Trocterr             -3.376475e+00  5.624424e+01
## inv.phi.Zoraspin             -1.191390e+00  1.383535e+01
Problems? See hints:

I have problems in model fitting. My model converges to infinity or local maxima: GLLVMs are complex models where starting values have a big role. Choosing a different starting value method (see argument starting.val) or use multiple runs and pick up the one giving highest log-likelihood value using argument n.init. More variation to the starting points can be added with jitter.var.

My results does not look the same as in answers: The results may not be exactly the same as in the answers, as the initial values for each model fit are slightly different, so the results may also differ slightly.

Ordination

GLLVM as a model based ordination method

  • GLLVMs can be used as a model-based approach to unconstrained ordination by including two latent variables in the model: g(E(yij)) = β0j + uiθj

  • The latent variable term try to capture the underlying factors driving species abundances at sites.

  • Predictions for the two latent variables, i = (i1, i2), then provide coordinates for sites in the ordination plot and then provides a graphical representation of which sites are similar in terms of their species composition.

Ordination plot

  • ordiplot() produces ordination plots based on fitted GLLVMs.
  • Uncertainty of the ordination points in model based ordination can be assessed with prediction regions based on the prediction errors of latent variables.
  • Prediction regions may also help interpreting which differences between ordination points are really a sign of the difference between species composition at those sites.

Biplot

  • Between species correlations can be visualized with biplot8 by adding latent variable loadings θj to the ordination of sites, by producing a biplot, (argument biplot = TRUE in ordiplot()).
  • In a biplot latent variables and their loadings are rotated so that the LV loadings of the species are in the same direction with the sites where they are most abundant.
  • Biplot can be used for finding groups of correlated species or finding indicator species common at specific sites.
  • For example, species named Pardlugu is common in the group of sites located on the bottom (eg sites 8, 19, 20 and 21).
  • x and y axes may help the interpretation.
ordiplot(fitnb, biplot = TRUE)
abline(h = 0, v = 0, lty=2)

Environmental gradients

  • The potential impact of environmental variables on species communities can be viewed by coloring ordination points according to the variables.
  • For example, species named Pardlugu seems to prefer sites with lot of dry soil mass and low reflection of the soil surface with a cloudless sky.
# Arbitrary color palette, a vector length of 20. Can use, for example, colorRampPalette from package grDevices
rbPal <- c("#00FA9A", "#00EC9F", "#00DFA4", "#00D2A9", "#00C5AF", "#00B8B4", "#00ABB9", "#009DBF", "#0090C4", "#0083C9", "#0076CF", "#0069D4", "#005CD9", "#004EDF", "#0041E4", "#0034E9", "#0027EF", "#001AF4", "#000DF9", "#0000FF")
X <- spider$x
par(mfrow = c(3,2), mar=c(4,4,2,2))
for(i in 1:ncol(X)){
Col <- rbPal[as.numeric(cut(X[,i], breaks = 20))]
ordiplot(fitnb, symbols = T, s.colors = Col, main = colnames(X)[i], 
         biplot = TRUE)
}

  • Here environmental gradients stand out quite clearly, indicating that, at least, some of the differences in species compositions at sites can be explained by the differences in environmental conditions.

  • The next step would be to include covariates to the model to study more precisely the effects of environmental variables: g(E(yij)) = β0j + xiβj + uiθj


  1. Niku, J., F.K.C. Hui, S. Taskinen, and D.I. Warton. 2019. Gllvm - Fast Analysis of Multivariate Abundance Data with Generalized Linear Latent Variable Models in R. 10. Methods in Ecology and Evolution: 2173–82↩︎

  2. Kasper Kristensen, Anders Nielsen, Casper W. Berg, Hans Skaug, Bradley M. Bell (2016). TMB: Automatic Differentiation and Laplace Approximation. Journal of Statistical Software, 70(5), 1-21↩︎

  3. Hui, F. K. C., Warton, D., Ormerod, J., Haapaniemi, V., and Taskinen, S. (2017). Variational approximations for generalized linear latent variable models. Journal of Computational and Graphical Statistics. Journal of Computational and Graphical Statistics, 26:35-43↩︎

  4. Korhonen, P., Hui, F. K. C., Niku, J., and Taskinen, S. (2021). Fast, universal estimation of latent variable models using extended variational approximations, arXiv:2107.02627 .↩︎

  5. Niku, J., Warton, D. I., Hui, F. K. C., and Taskinen, S. (2017). Generalized linear latent variable models for multivariate count and biomass data in ecology. Journal of Agricultural, Biological, and Environmental Statistics, 22:498-522.↩︎

  6. van der Aart, P. J. M., and Smeenk-Enserink, N. (1975) Correlations between distributions of hunting spiders (Lycosidae, Ctenidae) and environmental characteristics in a dune area. Netherlands Journal of Zoology 25, 1-45.↩︎

  7. Dunn, P. K., and Smyth, G. K. (1996). Randomized quantile residuals. Journal of Computational and Graphical Statistics, 5, 236-244.↩︎

  8. Gabriel, K. R. (1971). The biplot graphic display of matrices with application to principal component analysis. Biometrika, 58, 453-467↩︎