# Introduction to gllvm Part 1: Ordination

#### Jenni Niku

#### 2025-07-11

# Introduction to gllvm

## R package gllvm

- R package gllvm fits Generalized linear latent variable models (GLLVM) for multivariate data1.

- Developed by J. Niku, W.Brooks, R. Herliansyah, F.K.C. Hui, S. Taskinen, D.I. Warton, B. van der Veen.

- The package available in

  - GitHub: https://github.com/JenniNiku/gllvm
  - CRAN: https://CRAN.R-project.org/package=gllvm

- Package installation:

    # 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 (TMB 2).

- 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(y_{ij})) = \beta_{0j} + \boldsymbol{u}_i'\boldsymbol{\theta}_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.6806
    ## Residual degrees of freedom:  289
    ## AIC:  1561.361
    ## AICc:  1577.028
    ## BIC:  1740.765

## 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.6806
    ## Residual degrees of freedom:  289
    ## AIC:  1561.361
    ## AICc:  1577.028
    ## BIC:  1740.765

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.2631462  1.00000000
    ## Alopfabr  0.9015820 -0.48406199
    ## Arctlute -0.7036368  2.03033896
    ## Arctperi  0.5939626 -1.58944775
    ## Auloalbi -0.6527009  1.53310844
    ## Pardlugu -0.8886154 -0.02804361
    ## Pardmont  0.7498094  0.58844268
    ## Pardnigr -0.5748804  1.78025447
    ## Pardpull -0.4146996  1.94808823
    ## Trocterr -0.4694630  0.78799409
    ## Zoraspin -0.6964340  1.15697365
    ##
    ## $sigma.lv
    ##      LV1      LV2
    ## 1.803169 1.829259
    ##
    ## $Intercept
    ##   Alopacce   Alopcune   Alopfabr   Arctlute   Arctperi   Auloalbi   Pardlugu
    ##  0.7740451  0.5514852 -0.1420836 -3.5927087 -3.3660009 -0.7311463  0.3187527
    ##   Pardmont   Pardnigr   Pardpull   Trocterr   Zoraspin
    ##  1.6247798 -0.2755878 -0.1351114  2.6556839  0.2459122
    ##
    ## $inv.phi
    ##   Alopacce   Alopcune   Alopfabr   Arctlute   Arctperi   Auloalbi   Pardlugu
    ##  1.3062309  1.4686466  0.7051241  0.8975809  2.3318851  1.5391822  1.1528620
    ##   Pardmont   Pardnigr   Pardpull   Trocterr   Zoraspin
    ##  1.6178950  2.5410471  2.1962286 12.9778757  2.5728473
    ##
    ## $phi
    ##   Alopacce   Alopcune   Alopfabr   Arctlute   Arctperi   Auloalbi   Pardlugu
    ## 0.76556145 0.68089898 1.41819014 1.11410576 0.42883759 0.64969565 0.86740653
    ##   Pardmont   Pardnigr   Pardpull   Trocterr   Zoraspin
    ## 0.61808708 0.39353856 0.45532601 0.07705421 0.38867445
    # Where are the predicted latent variable values? just fitp$lvs or
    getLV(fitnb)
    ##               LV1          LV2
    ## Row1   0.92852503  1.270519624
    ## Row2  -0.68266927  0.793809268
    ## Row3   0.67620458  1.274449856
    ## Row4  -0.24715896  1.149852343
    ## Row5  -0.36105229  1.217231036
    ## Row6  -0.35231979  1.008689806
    ## Row7   0.02922192  1.426054168
    ## Row8  -1.52132855 -0.070716810
    ## Row9   0.91074390  0.001240128
    ## Row10  1.03736629 -0.493610376
    ## Row11  1.48871264  0.303009071
    ## Row12  1.32413036  0.807371133
    ## Row13  0.11539642  1.356631554
    ## Row14 -0.43681663  1.014589907
    ## Row15 -1.31925711 -0.544673233
    ## Row16 -1.17465444 -0.242686783
    ## Row17 -1.11253373 -0.512793595
    ## Row18 -1.15894626 -0.542953624
    ## Row19 -1.34067007 -0.502779621
    ## Row20 -1.36734645 -0.592082329
    ## Row21 -1.13971090 -0.475631155
    ## Row22  0.76441679 -1.345933439
    ## Row23  0.92079151 -1.380621447
    ## Row24  0.97904211 -1.395235935
    ## Row25  0.63125423  0.576833461
    ## Row26  0.27410136 -1.964090061
    ## Row27  1.11556946 -1.309369381
    ## Row28  1.01881182 -0.825795887
    # Standard errors for parameters:
    fitnb$sd
    ## $theta
    ##                LV1       LV2
    ## Alopacce 0.0000000 0.0000000
    ## Alopcune 0.2736322 0.0000000
    ## Alopfabr 0.3301737 0.2043821
    ## Arctlute 0.6749244 0.7848886
    ## Arctperi 0.4757444 0.5592173
    ## Auloalbi 0.4407933 0.3892486
    ## Pardlugu 0.2275827 0.2005400
    ## Pardmont 0.2196744 0.1816351
    ## Pardnigr 0.4734941 0.3799094
    ## Pardpull 0.4966260 0.4106308
    ## Trocterr 0.2098719 0.1568668
    ## Zoraspin 0.3480405 0.2796503
    ##
    ## $sigma.lv
    ##       LV1       LV2
    ## 0.4163185 0.4257548
    ##
    ## $beta0
    ##  Alopacce  Alopcune  Alopfabr  Arctlute  Arctperi  Auloalbi  Pardlugu  Pardmont
    ## 0.4518389 0.4630190 0.5478106 1.7104475 1.4645938 0.7892999 0.4391379 0.3996812
    ##  Pardnigr  Pardpull  Trocterr  Zoraspin
    ## 0.7870424 0.8340642 0.3339027 0.5830988
    ##
    ## $inv.phi
    ##  Alopacce  Alopcune  Alopfabr  Arctlute  Arctperi  Auloalbi  Pardlugu  Pardmont
    ## 0.6025230 0.6026002 0.3957344 0.5787020 1.8042611 0.8383782 0.5810477 0.7756852
    ##  Pardnigr  Pardpull  Trocterr  Zoraspin
    ## 1.3150119 1.2472503 6.8923678 1.2851045
    ##
    ## $phi
    ##   Alopacce   Alopcune   Alopfabr   Arctlute   Arctperi   Auloalbi   Pardlugu
    ## 0.35312931 0.27937956 0.79592609 0.71830326 0.33180665 0.35388314 0.43717687
    ##   Pardmont   Pardnigr   Pardpull   Trocterr   Zoraspin
    ## 0.29633630 0.20365931 0.25858215 0.04092241 0.19413795

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.6806
    ## Residual degrees of freedom:  289
    ## AIC:  1561.361
    ## AICc:  1577.028
    ## BIC:  1740.765
    # 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.81680676
    ## Row2  -0.94960771
    ## Row3  -0.91687374
    ## Row4  -1.10569138
    ## Row5  -1.22147286
    ## Row6  -1.03402761
    ## Row7  -1.26769619
    ## Row8  -0.53476229
    ## Row9   0.36601425
    ## Row10  1.00887867
    ## Row11  0.31287765
    ## Row12 -0.31672654
    ## Row13 -1.17621037
    ## Row14 -1.03499118
    ## Row15 -0.09012064
    ## Row16 -0.27460765
    ## Row17 -0.02449633
    ## Row18 -0.02083003
    ## Row19 -0.12972078
    ## Row20 -0.06891625
    ## Row21 -0.04325989
    ## Row22  1.54079152
    ## Row23  1.65021922
    ## Row24  1.68377006
    ## Row25 -0.28904118
    ## Row26  1.92917592
    ## Row27  1.62336607
    ## Row28  1.20014747
    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.6806
    ## Residual degrees of freedom:  279
    ## AIC:  1581.361
    ## AICc:  1605.145
    ## BIC:  1798.937
    getLV(fitnb3)
    ##               LV1          LV2           LV3
    ## Row1   0.92852263  1.270537491  6.081525e-07
    ## Row2  -0.68267389  0.793840057  5.642729e-08
    ## Row3   0.67620701  1.274474299  4.226692e-07
    ## Row4  -0.24715323  1.149884105  1.333225e-07
    ## Row5  -0.36105310  1.217265008  2.744672e-07
    ## Row6  -0.35231928  1.008723428 -2.975587e-07
    ## Row7   0.02921203  1.426078820 -1.146904e-07
    ## Row8  -1.52134967 -0.070684613 -2.226569e-07
    ## Row9   0.91074040  0.001258462 -3.138688e-07
    ## Row10  1.03735281 -0.493600126 -2.155974e-07
    ## Row11  1.48870242  0.303020351 -3.850318e-07
    ## Row12  1.32414360  0.807389025 -1.635435e-07
    ## Row13  0.11538550  1.356654834  1.947549e-07
    ## Row14 -0.43681587  1.014621043 -2.755372e-07
    ## Row15 -1.31925518 -0.544638922  2.137836e-07
    ## Row16 -1.17468669 -0.242666503  7.098039e-08
    ## Row17 -1.11254219 -0.512765173  2.617992e-07
    ## Row18 -1.15895625 -0.542926903  2.898839e-07
    ## Row19 -1.34067828 -0.502747264  9.723774e-08
    ## Row20 -1.36736049 -0.592054717  1.452689e-07
    ## Row21 -1.13971320 -0.475598636 -1.015886e-08
    ## Row22  0.76439150 -1.345944132  6.710058e-11
    ## Row23  0.92078354 -1.380622425 -3.157505e-07
    ## Row24  0.97904496 -1.395235063 -3.937788e-08
    ## Row25  0.63126521  0.576858429  2.547340e-07
    ## Row26  0.27406904 -1.964101106 -1.255146e-08
    ## Row27  1.11554971 -1.309362409  6.238233e-09
    ## Row28  1.01880533 -0.825785411  2.124748e-08

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.6749
    ## Residual degrees of freedom:  217
    ## AIC:  1425.35
    ## AICc:  1557.572
    ## BIC:  1879.586
    coef(fitnbx)
    ## $Species.scores
    ##                  LV1          LV2
    ## Alopacce  1.00000000 0.000000e+00
    ## Alopcune  0.70590977 1.000000e+00
    ## Alopfabr -3.78755978 5.841054e-01
    ## Arctlute -1.70202883 1.151848e+00
    ## Arctperi -6.39740814 3.135798e-09
    ## Auloalbi  0.63406682 6.094515e-01
    ## Pardlugu -4.43736539 1.961041e-01
    ## Pardmont  0.34498774 3.640925e-01
    ## Pardnigr -0.07066453 1.070880e+00
    ## Pardpull  2.57613515 7.282404e-01
    ## Trocterr -5.18231298 4.991127e-01
    ## Zoraspin  0.79796364 7.372577e-01
    ##
    ## $sigma.lv
    ##          LV1          LV2
    ## 1.360108e-12 9.497877e-01
    ##
    ## $Intercept
    ##    Alopacce    Alopcune    Alopfabr    Arctlute    Arctperi    Auloalbi
    ##  -1.8103134  -4.4232906  -1.6436169 -14.6900476 -14.9071474 -15.7117265
    ##    Pardlugu    Pardmont    Pardnigr    Pardpull    Trocterr    Zoraspin
    ##   7.9323827  -6.3805612  -5.1535471 -13.0274657  -0.2004536  -3.8446415
    ##
    ## $Xcoef
    ##            soil.dry   bare.sand fallen.leaves        moss  herb.layer
    ## Alopacce -0.9582468 -0.09060390   -0.32435141  0.10121108  0.73702212
    ## Alopcune  1.3490206 -0.45006523    0.01729742 -0.19941357  0.41950551
    ## Alopfabr -0.8698245  0.69555626   -0.40325721  0.54008018  0.30193852
    ## Arctlute  5.8729070  0.16617755   -1.67207548  0.34344074 -0.12397028
    ## Arctperi -1.7801684  0.23853994   -2.51504872  0.49575099  0.02125324
    ## Auloalbi  0.2248037 -0.04630748    0.62844360  0.06252726  4.05071740
    ## Pardlugu -2.5235378 -0.71541799    0.36222267 -0.37421535  0.58639700
    ## Pardmont  1.5852002  0.13323462   -0.51424357  0.88735878  0.73424429
    ## Pardnigr  2.5563642 -0.02661124   -0.84827008 -0.45635810  0.72877687
    ## Pardpull  3.0006796 -0.35244780   -0.47038677  0.36473717  1.95132558
    ## Trocterr  1.2400077 -0.23013006   -0.22748046 -0.20324652  0.44584275
    ## Zoraspin  2.0998820  0.11998134   -0.57210256 -0.08996277  0.62001139
    ##           reflection
    ## Alopacce  0.86917546
    ## Alopcune  0.42097856
    ## Alopfabr  0.06157631
    ## Arctlute -0.64318458
    ## Arctperi  4.00612925
    ## Auloalbi -0.54352701
    ## Pardlugu -1.16972866
    ## Pardmont  0.07820906
    ## Pardnigr -0.53379011
    ## Pardpull -0.36809979
    ## Trocterr -0.29459853
    ## Zoraspin -0.93160575
    ##
    ## $inv.phi
    ##     Alopacce     Alopcune     Alopfabr     Arctlute     Arctperi     Auloalbi
    ## 9.155763e+00 2.937740e+00 7.756738e+00 2.022811e+00 1.945564e+09 1.022815e+01
    ##     Pardlugu     Pardmont     Pardnigr     Pardpull     Trocterr     Zoraspin
    ## 2.772336e+01 1.990647e+00 7.672034e+00 1.667529e+01 2.643105e+01 6.321726e+00
    ##
    ## $phi
    ##     Alopacce     Alopcune     Alopfabr     Arctlute     Arctperi     Auloalbi
    ## 1.092208e-01 3.403978e-01 1.289202e-01 4.943616e-01 5.139898e-10 9.776943e-02
    ##     Pardlugu     Pardmont     Pardnigr     Pardpull     Trocterr     Zoraspin
    ## 3.607066e-02 5.023491e-01 1.303435e-01 5.996897e-02 3.783428e-02 1.581846e-01
    # confidence intervals for parameters:
    confint(fitnbx)
    ##                                      2.5 %        97.5 %
    ## sigma.lv.LV1                 -5.253095e-10  5.280297e-10
    ## sigma.lv.LV2                  4.336746e-01  1.465901e+00
    ## theta.LV1.1                   1.000000e+00  1.000000e+00
    ## theta.LV1.2                  -5.721750e+11  5.721750e+11
    ## theta.LV1.3                  -2.400872e+11  2.400872e+11
    ## theta.LV1.4                  -5.097004e+11  5.097004e+11
    ## theta.LV1.5                  -6.172954e+06  6.172942e+06
    ## theta.LV1.6                  -2.837221e+11  2.837221e+11
    ## theta.LV1.7                  -9.715346e+10  9.715346e+10
    ## theta.LV1.8                  -3.641011e+11  3.641011e+11
    ## theta.LV1.9                  -4.705534e+11  4.705534e+11
    ## theta.LV1.10                 -3.025283e+11  3.025283e+11
    ## theta.LV1.11                 -1.574245e+11  1.574245e+11
    ## theta.LV1.12                 -3.005948e+11  3.005948e+11
    ## theta.LV2.1                   0.000000e+00  0.000000e+00
    ## theta.LV2.2                   1.000000e+00  1.000000e+00
    ## theta.LV2.3                  -5.150877e-02  1.219719e+00
    ## theta.LV2.4                  -1.944687e-01  2.498164e+00
    ## theta.LV2.5                  -1.874842e-05  1.875469e-05
    ## theta.LV2.6                   5.415548e-02  1.164748e+00
    ## theta.LV2.7                  -1.285936e-01  5.208018e-01
    ## theta.LV2.8                  -1.511677e-01  8.793527e-01
    ## theta.LV2.9                   4.318004e-01  1.709960e+00
    ## theta.LV2.10                  2.701450e-01  1.186336e+00
    ## theta.LV2.11                  2.336325e-01  7.645928e-01
    ## theta.LV2.12                  2.325206e-01  1.241995e+00
    ## Intercept.Alopacce           -5.313406e+00  1.692780e+00
    ## Intercept.Alopcune           -9.576625e+00  7.300442e-01
    ## Intercept.Alopfabr           -7.968708e+00  4.681474e+00
    ## Intercept.Arctlute           -3.133616e+01  1.956065e+00
    ## Intercept.Arctperi           -2.629977e+01 -3.514528e+00
    ## Intercept.Auloalbi           -2.247053e+01 -8.952925e+00
    ## Intercept.Pardlugu            3.194495e+00  1.267027e+01
    ## Intercept.Pardmont           -1.043175e+01 -2.329372e+00
    ## Intercept.Pardnigr           -1.019969e+01 -1.074015e-01
    ## Intercept.Pardpull           -1.860706e+01 -7.447867e+00
    ## Intercept.Trocterr           -2.429768e+00  2.028861e+00
    ## Intercept.Zoraspin           -7.700028e+00  1.074534e-02
    ## Xcoef.soil.dry:Alopacce      -1.627836e+00 -2.886573e-01
    ## Xcoef.soil.dry:Alopcune      -1.660344e-01  2.864076e+00
    ## Xcoef.soil.dry:Alopfabr      -1.961754e+00  2.221046e-01
    ## Xcoef.soil.dry:Arctlute       4.238522e-01  1.132196e+01
    ## Xcoef.soil.dry:Arctperi      -3.435303e+00 -1.250337e-01
    ## Xcoef.soil.dry:Auloalbi      -9.612412e-01  1.410849e+00
    ## Xcoef.soil.dry:Pardlugu      -3.776246e+00 -1.270830e+00
    ## Xcoef.soil.dry:Pardmont       6.323338e-01  2.538067e+00
    ## Xcoef.soil.dry:Pardnigr       1.053201e+00  4.059527e+00
    ## Xcoef.soil.dry:Pardpull       1.556293e+00  4.445066e+00
    ## Xcoef.soil.dry:Trocterr       5.869311e-01  1.893084e+00
    ## Xcoef.soil.dry:Zoraspin       9.198846e-01  3.279879e+00
    ## Xcoef.bare.sand:Alopacce     -3.852495e-01  2.040417e-01
    ## Xcoef.bare.sand:Alopcune     -9.738202e-01  7.368976e-02
    ## Xcoef.bare.sand:Alopfabr      1.975876e-01  1.193525e+00
    ## Xcoef.bare.sand:Arctlute     -1.529830e+00  1.862185e+00
    ## Xcoef.bare.sand:Arctperi     -6.815216e-01  1.158601e+00
    ## Xcoef.bare.sand:Auloalbi     -4.621850e-01  3.695700e-01
    ## Xcoef.bare.sand:Pardlugu     -1.248243e+00 -1.825928e-01
    ## Xcoef.bare.sand:Pardmont     -1.852343e-01  4.517035e-01
    ## Xcoef.bare.sand:Pardnigr     -5.132998e-01  4.600774e-01
    ## Xcoef.bare.sand:Pardpull     -8.536659e-01  1.487703e-01
    ## Xcoef.bare.sand:Trocterr     -4.442687e-01 -1.599142e-02
    ## Xcoef.bare.sand:Zoraspin     -2.625472e-01  5.025099e-01
    ## Xcoef.fallen.leaves:Alopacce -9.896866e-01  3.409838e-01
    ## Xcoef.fallen.leaves:Alopcune -5.797428e-01  6.143376e-01
    ## Xcoef.fallen.leaves:Alopfabr -1.537078e+00  7.305637e-01
    ## Xcoef.fallen.leaves:Arctlute -3.797511e+00  4.533600e-01
    ## Xcoef.fallen.leaves:Arctperi -2.955983e+02  2.905682e+02
    ## Xcoef.fallen.leaves:Auloalbi  1.123335e-01  1.144554e+00
    ## Xcoef.fallen.leaves:Pardlugu  3.927958e-02  6.851658e-01
    ## Xcoef.fallen.leaves:Pardmont -1.026752e+00 -1.735120e-03
    ## Xcoef.fallen.leaves:Pardnigr -1.464631e+00 -2.319087e-01
    ## Xcoef.fallen.leaves:Pardpull -9.510085e-01  1.023494e-02
    ## Xcoef.fallen.leaves:Trocterr -4.819565e-01  2.699561e-02
    ## Xcoef.fallen.leaves:Zoraspin -1.030252e+00 -1.139528e-01
    ## Xcoef.moss:Alopacce          -2.225648e-01  4.249870e-01
    ## Xcoef.moss:Alopcune          -7.950524e-01  3.962253e-01
    ## Xcoef.moss:Alopfabr           1.278889e-02  1.067371e+00
    ## Xcoef.moss:Arctlute          -9.123139e-01  1.599195e+00
    ## Xcoef.moss:Arctperi          -1.222031e-01  1.113705e+00
    ## Xcoef.moss:Auloalbi          -4.180182e-01  5.430727e-01
    ## Xcoef.moss:Pardlugu          -8.454485e-01  9.701783e-02
    ## Xcoef.moss:Pardmont           4.273672e-01  1.347350e+00
    ## Xcoef.moss:Pardnigr          -1.039307e+00  1.265908e-01
    ## Xcoef.moss:Pardpull          -8.543276e-02  8.149071e-01
    ## Xcoef.moss:Trocterr          -4.719749e-01  6.548182e-02
    ## Xcoef.moss:Zoraspin          -5.684716e-01  3.885461e-01
    ## Xcoef.herb.layer:Alopacce     3.371435e-01  1.136901e+00
    ## Xcoef.herb.layer:Alopcune    -3.379490e-01  1.176960e+00
    ## Xcoef.herb.layer:Alopfabr    -2.708354e-01  8.747124e-01
    ## Xcoef.herb.layer:Arctlute    -3.060514e+00  2.812573e+00
    ## Xcoef.herb.layer:Arctperi    -4.106409e-01  4.531474e-01
    ## Xcoef.herb.layer:Auloalbi     2.456621e+00  5.644813e+00
    ## Xcoef.herb.layer:Pardlugu     2.740884e-01  8.987056e-01
    ## Xcoef.herb.layer:Pardmont     4.665398e-02  1.421835e+00
    ## Xcoef.herb.layer:Pardnigr    -1.335813e-01  1.591135e+00
    ## Xcoef.herb.layer:Pardpull     9.273110e-01  2.975340e+00
    ## Xcoef.herb.layer:Trocterr     1.165955e-01  7.750900e-01
    ## Xcoef.herb.layer:Zoraspin    -1.608230e-02  1.256105e+00
    ## Xcoef.reflection:Alopacce     3.389783e-01  1.399373e+00
    ## Xcoef.reflection:Alopcune    -3.763172e-01  1.218274e+00
    ## Xcoef.reflection:Alopfabr    -9.861043e-01  1.109257e+00
    ## Xcoef.reflection:Arctlute    -1.929959e+00  6.435902e-01
    ## Xcoef.reflection:Arctperi     1.460219e+00  6.552040e+00
    ## Xcoef.reflection:Auloalbi    -1.141645e+00  5.459085e-02
    ## Xcoef.reflection:Pardlugu    -1.807510e+00 -5.319478e-01
    ## Xcoef.reflection:Pardmont    -5.106952e-01  6.671133e-01
    ## Xcoef.reflection:Pardnigr    -1.302885e+00  2.353048e-01
    ## Xcoef.reflection:Pardpull    -8.995632e-01  1.633636e-01
    ## Xcoef.reflection:Trocterr    -6.349174e-01  4.572037e-02
    ## Xcoef.reflection:Zoraspin    -1.527919e+00 -3.352922e-01
    ## inv.phi.Alopacce             -1.059345e+01  2.890498e+01
    ## inv.phi.Alopcune             -7.210031e-02  5.947580e+00
    ## inv.phi.Alopfabr             -8.210298e+00  2.372377e+01
    ## inv.phi.Arctlute             -1.057750e+00  5.103371e+00
    ## inv.phi.Arctperi             -3.175542e+13  3.175931e+13
    ## inv.phi.Auloalbi             -3.754890e+00  2.421118e+01
    ## inv.phi.Pardlugu             -4.820755e+01  1.036543e+02
    ## inv.phi.Pardmont              5.019663e-01  3.479328e+00
    ## inv.phi.Pardnigr             -1.345719e+00  1.668979e+01
    ## inv.phi.Pardpull             -4.388606e+00  3.773918e+01
    ## inv.phi.Trocterr             -3.376275e+00  5.623838e+01
    ## inv.phi.Zoraspin             -1.191284e+00  1.383474e+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(y_{ij})) = \beta_{0j} + \boldsymbol{u}_i'\boldsymbol{\theta}_j\)

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

- Predictions for the two latent variables, \(\boldsymbol{\hat u}_i=(\hat u_{i1}, \hat u_{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 \(\boldsymbol{\theta}_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(y_{ij})) = \beta_{0j} + \boldsymbol{x}_i'\boldsymbol{\beta}_{j} + \boldsymbol{u}_i'\boldsymbol{\theta}_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↩︎