vignettes/vignette3.rmd
vignette3.rmd
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
Package installation:
# From CRAN
install.packages(gllvm)
# OR
# From GitHub using devtools package's function install_github
devtools::install_github("JenniNiku/gllvm")
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
Additional tools: model checking, model selection, inference, visualization.
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 |
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", ...)
soil.dry
: Soil dry massbare.sand
: cover of bare sandfallen.leaves
: cover of fallen leaves/twigsmoss
: cover of mossherb.layer
: cover of herb layerreflection
: reflection of the soil surface with a
cloudless skyFitting basic GLLVM 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.6807
## Residual degrees of freedom: 289
## AIC: 1561.361
## AICc: 1577.028
## BIC: 1740.766
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.
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.
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
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:
E3. Explore the fitted model. Where are the estimates for parameters? What about predicted latent variables? Standard errors?
3. Lets explore the fitted model:
# Parameters:
coef(fitnb)
## $Species.scores
## LV1 LV2
## Alopacce 1.0000000 0.0000000
## Alopcune -0.2631740 1.0000000
## Alopfabr 0.9012964 -0.4840078
## Arctlute -0.7026951 2.0282163
## Arctperi 0.5941041 -1.5893485
## Auloalbi -0.6529384 1.5332130
## Pardlugu -0.8880584 -0.0283346
## Pardmont 0.7492841 0.5884815
## Pardnigr -0.5748022 1.7798474
## Pardpull -0.4149376 1.9481946
## Trocterr -0.4693903 0.7878657
## Zoraspin -0.6961561 1.1566005
##
## $sigma.lv
## LV1 LV2
## 1.803892 1.828690
##
## $Intercept
## Alopacce Alopcune Alopfabr Arctlute Arctperi Auloalbi Pardlugu
## 0.7737550 0.5527384 -0.1428596 -3.5862506 -3.3681967 -0.7299217 0.3189523
## Pardmont Pardnigr Pardpull Trocterr Zoraspin
## 1.6255407 -0.2729636 -0.1332579 2.6566655 0.2477211
##
## $inv.phi
## Alopacce Alopcune Alopfabr Arctlute Arctperi Auloalbi Pardlugu
## 1.3064181 1.4682482 0.7052467 0.8985537 2.3306414 1.5386146 1.1528918
## Pardmont Pardnigr Pardpull Trocterr Zoraspin
## 1.6180079 2.5401112 2.1949143 12.9753513 2.5724225
##
## $phi
## Alopacce Alopcune Alopfabr Arctlute Arctperi Auloalbi Pardlugu Pardmont
## 0.7654517 0.6810838 1.4179435 1.1128995 0.4290664 0.6499353 0.8673841 0.6180440
## Pardnigr Pardpull Trocterr Zoraspin
## 0.3936835 0.4555987 0.0770692 0.3887386
# Where are the predicted latent variable values? just fitp$lvs or
getLV(fitnb)
## LV1 LV2
## Row1 0.9280716 1.2703921608
## Row2 -0.6833799 0.7931112996
## Row3 0.6761008 1.2744024537
## Row4 -0.2474688 1.1495300096
## Row5 -0.3612717 1.2169150843
## Row6 -0.3523948 1.0083966313
## Row7 0.0290453 1.4259839281
## Row8 -1.5218059 -0.0718307843
## Row9 0.9102994 0.0008130686
## Row10 1.0371311 -0.4941576829
## Row11 1.4890401 0.3029948635
## Row12 1.3244847 0.8073754550
## Row13 0.1153634 1.3565782725
## Row14 -0.4371787 1.0141354167
## Row15 -1.3193982 -0.5458435954
## Row16 -1.1743660 -0.2435487204
## Row17 -1.1130624 -0.5141217705
## Row18 -1.1587204 -0.5439088158
## Row19 -1.3405175 -0.5038367050
## Row20 -1.3668834 -0.5930933717
## Row21 -1.1394851 -0.4765512978
## Row22 0.7642653 -1.3470526207
## Row23 0.9210288 -1.3815121496
## Row24 0.9791178 -1.3962675403
## Row25 0.6313729 0.5764522986
## Row26 0.2747960 -1.9652036873
## Row27 1.1157439 -1.3103619005
## Row28 1.0192948 -0.8263728575
# Standard errors for parameters:
fitnb$sd
## $theta
## LV1 LV2
## Alopacce 0.0000000 0.0000000
## Alopcune 0.2733177 0.0000000
## Alopfabr 0.3298680 0.2043612
## Arctlute 0.6735202 0.7836738
## Arctperi 0.4752967 0.5592892
## Auloalbi 0.4402796 0.3893924
## Pardlugu 0.2273684 0.2004821
## Pardmont 0.2195966 0.1816528
## Pardnigr 0.4727799 0.3799419
## Pardpull 0.4960265 0.4108329
## Trocterr 0.2095264 0.1569012
## Zoraspin 0.3474686 0.2796226
##
## $sigma.lv
## LV1 LV2
## 0.4163533 0.4258952
##
## $beta0
## Alopacce Alopcune Alopfabr Arctlute Arctperi Auloalbi Pardlugu Pardmont
## 0.4519226 0.4628608 0.5478520 1.7071988 1.4653249 0.7891611 0.4390375 0.3995752
## Pardnigr Pardpull Trocterr Zoraspin
## 0.7865322 0.8338770 0.3337800 0.5826921
##
## $inv.phi
## Alopacce Alopcune Alopfabr Arctlute Arctperi Auloalbi Pardlugu Pardmont
## 0.6026260 0.6024449 0.3958156 0.5792937 1.8032183 0.8380663 0.5810814 0.7757222
## Pardnigr Pardpull Trocterr Zoraspin
## 1.3144387 1.2462689 6.8906501 1.2848468
##
## $phi
## Alopacce Alopcune Alopfabr Arctlute Arctperi Auloalbi Pardlugu
## 0.35308842 0.27945917 0.79581245 0.71748153 0.33196891 0.35401255 0.43717961
## Pardmont Pardnigr Pardpull Trocterr Zoraspin
## 0.29630908 0.20372057 0.25868821 0.04092813 0.19416313
E4. Fit model with different numbers of latent variables.
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.81676413
## Row2 -0.94960125
## Row3 -0.91681940
## Row4 -1.10568473
## Row5 -1.22144979
## Row6 -1.03398484
## Row7 -1.26767644
## Row8 -0.53478360
## Row9 0.36592898
## Row10 1.00877957
## Row11 0.31276001
## Row12 -0.31676991
## Row13 -1.17619313
## Row14 -1.03496933
## Row15 -0.09008794
## Row16 -0.27458309
## Row17 -0.02447396
## Row18 -0.02079744
## Row19 -0.12968909
## Row20 -0.06889017
## Row21 -0.04324447
## Row22 1.54068229
## Row23 1.65008583
## Row24 1.68364476
## Row25 -0.28906403
## Row26 1.92897385
## Row27 1.62325289
## Row28 1.20009416
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.6807
## Residual degrees of freedom: 279
## AIC: 1581.361
## AICc: 1605.146
## BIC: 1798.937
getLV(fitnb3)
## LV1 LV2 LV3
## Row1 0.92855997 1.2704066937 2.490147e-04
## Row2 -0.68289382 0.7936434946 -4.250590e-04
## Row3 0.67654263 1.2745071649 2.713419e-04
## Row4 -0.24684270 1.1500860175 3.801664e-05
## Row5 -0.36042112 1.2176476998 5.541020e-04
## Row6 -0.35190586 1.0088261019 1.163737e-03
## Row7 0.03029708 1.4267390069 1.056439e-03
## Row8 -1.52100704 -0.0710999886 -2.349148e-03
## Row9 0.91092628 -0.0002631222 -2.949115e-04
## Row10 1.03719421 -0.4947825169 -3.210705e-04
## Row11 1.48909850 0.3020168483 -4.614553e-04
## Row12 1.32431104 0.8068430042 -7.479638e-04
## Row13 0.11566140 1.3569379287 -1.993838e-04
## Row14 -0.43645442 1.0147814740 2.241292e-04
## Row15 -1.31860748 -0.5455602127 4.306928e-04
## Row16 -1.17474725 -0.2435589343 1.754225e-04
## Row17 -1.11197409 -0.5137065542 5.233045e-04
## Row18 -1.15819895 -0.5438243299 3.008523e-04
## Row19 -1.34038762 -0.5036427180 1.120733e-04
## Row20 -1.36657978 -0.5927568262 -1.113443e-04
## Row21 -1.13970938 -0.4766759052 -3.724385e-04
## Row22 0.76499682 -1.3477413239 5.881954e-05
## Row23 0.92092061 -1.3819481717 -5.979814e-04
## Row24 0.97910691 -1.3962196048 -6.646905e-05
## Row25 0.63223200 0.5766255674 7.922041e-04
## Row26 0.27486244 -1.9659703788 -4.974432e-05
## Row27 1.11558856 -1.3109958578 -7.057136e-05
## Row28 1.01937654 -0.8279080554 1.168999e-04
E5. Include environmental variables to the GLLVM and explore the model fit.
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.65033064 1.000000e+00
## Alopfabr 4.60955070 5.839337e-01
## Arctlute -1.54308018 1.152119e+00
## Arctperi 1.57923376 3.724113e-07
## Auloalbi -1.24985864 6.094446e-01
## Pardlugu -8.27756341 1.960724e-01
## Pardmont 1.62553948 3.640396e-01
## Pardnigr -0.02217957 1.070667e+00
## Pardpull -0.31400009 7.281194e-01
## Trocterr -1.94940036 4.990269e-01
## Zoraspin 0.31162305 7.370960e-01
##
## $sigma.lv
## LV1 LV2
## 4.198566e-09 9.499576e-01
##
## $Intercept
## Alopacce Alopcune Alopfabr Arctlute Arctperi Auloalbi
## -1.8118034 -4.4229772 -1.6430780 -14.7012613 -14.9127415 -15.7161077
## Pardlugu Pardmont Pardnigr Pardpull Trocterr Zoraspin
## 7.9349699 -6.3785000 -5.1520389 -13.0281981 -0.1999952 -3.8441590
##
## $Xcoef
## soil.dry bare.sand fallen.leaves moss herb.layer
## Alopacce -0.9580696 -0.09050833 -0.3242683 0.10124549 0.73708820
## Alopcune 1.3493055 -0.45012971 0.0171826 -0.19948113 0.41919734
## Alopfabr -0.8697778 0.69550223 -0.4033527 0.54014731 0.30180782
## Arctlute 5.8764638 0.16535494 -1.6723058 0.34396786 -0.12417544
## Arctperi -1.7802096 0.23859650 -1.8760391 0.49600665 0.02128078
## Auloalbi 0.2248309 -0.04627880 0.6286176 0.06258915 4.05162146
## Pardlugu -2.5240359 -0.71558231 0.3620669 -0.37442348 0.58627107
## Pardmont 1.5850692 0.13313869 -0.5143564 0.88718490 0.73395038
## Pardnigr 2.5563703 -0.02664628 -0.8483469 -0.45644924 0.72835939
## Pardpull 3.0008808 -0.35252699 -0.4703709 0.36475658 1.95127965
## Trocterr 1.2400373 -0.23016156 -0.2275185 -0.20328000 0.44569773
## Zoraspin 2.1000129 0.11996378 -0.5721528 -0.09000736 0.61975217
## reflection
## Alopacce 0.86935167
## Alopcune 0.42105075
## Alopfabr 0.06149616
## Arctlute -0.64289561
## Arctperi 4.00723441
## Auloalbi -0.54358192
## Pardlugu -1.16980030
## Pardmont 0.07820141
## Pardnigr -0.53365350
## Pardpull -0.36801919
## Trocterr -0.29455636
## Zoraspin -0.93149392
##
## $inv.phi
## Alopacce Alopcune Alopfabr Arctlute Arctperi Auloalbi
## 9.146737e+00 2.937857e+00 7.757592e+00 2.022399e+00 4.385230e+06 1.022941e+01
## Pardlugu Pardmont Pardnigr Pardpull Trocterr Zoraspin
## 2.770437e+01 1.990873e+00 7.672767e+00 1.667735e+01 2.643112e+01 6.321633e+00
##
## $phi
## Alopacce Alopcune Alopfabr Arctlute Arctperi Auloalbi
## 1.093286e-01 3.403842e-01 1.289060e-01 4.944622e-01 2.280382e-07 9.775736e-02
## Pardlugu Pardmont Pardnigr Pardpull Trocterr Zoraspin
## 3.609539e-02 5.022923e-01 1.303311e-01 5.996155e-02 3.783419e-02 1.581870e-01
# confidence intervals for parameters:
confint(fitnbx)
## 2.5 % 97.5 %
## sigma.lv.LV1 -6.449473e-07 6.533444e-07
## sigma.lv.LV2 4.339237e-01 1.465992e+00
## theta.LV1.1 1.000000e+00 1.000000e+00
## theta.LV1.2 -1.369205e+08 1.369205e+08
## theta.LV1.3 -7.837880e+07 7.837881e+07
## theta.LV1.4 -1.248100e+08 1.248100e+08
## theta.LV1.5 -4.219775e+04 4.220090e+04
## theta.LV1.6 -5.925460e+07 5.925459e+07
## theta.LV1.7 -2.579623e+07 2.579621e+07
## theta.LV1.8 -1.211036e+08 1.211036e+08
## theta.LV1.9 -1.069053e+08 1.069053e+08
## theta.LV1.10 -7.661456e+07 7.661456e+07
## theta.LV1.11 -5.710665e+07 5.710664e+07
## theta.LV1.12 -8.664399e+07 8.664399e+07
## theta.LV2.1 0.000000e+00 0.000000e+00
## theta.LV2.2 1.000000e+00 1.000000e+00
## theta.LV2.3 -5.150183e-02 1.219369e+00
## theta.LV2.4 -1.944572e-01 2.498696e+00
## theta.LV2.5 -1.229696e-03 1.230441e-03
## theta.LV2.6 5.422453e-02 1.164665e+00
## theta.LV2.7 -1.285614e-01 5.207062e-01
## theta.LV2.8 -1.510936e-01 8.791728e-01
## theta.LV2.9 4.318875e-01 1.709446e+00
## theta.LV2.10 2.702054e-01 1.186033e+00
## Intercept.Alopacce 2.336750e-01 7.643787e-01
## Intercept.Alopcune 2.325701e-01 1.241622e+00
## Intercept.Alopfabr -5.315546e+00 1.691939e+00
## Intercept.Arctlute -9.576851e+00 7.308970e-01
## Intercept.Arctperi -7.967770e+00 4.681614e+00
## Intercept.Auloalbi -3.137398e+01 1.971456e+00
## Intercept.Pardlugu -2.630639e+01 -3.519095e+00
## Intercept.Pardmont -2.247640e+01 -8.955814e+00
## Intercept.Pardnigr 3.196537e+00 1.267340e+01
## Intercept.Pardpull -1.042939e+01 -2.327611e+00
## Intercept.Trocterr -1.019784e+01 -1.062381e-01
## Intercept.Zoraspin -1.860810e+01 -7.448300e+00
## Xcoef.soil.dry:Alopacce -2.429291e+00 2.029301e+00
## Xcoef.soil.dry:Alopcune -7.699380e+00 1.106180e-02
## Xcoef.soil.dry:Alopfabr -1.627819e+00 -2.883202e-01
## Xcoef.soil.dry:Arctlute -1.659364e-01 2.864547e+00
## Xcoef.soil.dry:Arctperi -1.961645e+00 2.220892e-01
## Xcoef.soil.dry:Auloalbi 4.213010e-01 1.133163e+01
## Xcoef.soil.dry:Pardlugu -3.435521e+00 -1.248979e-01
## Xcoef.soil.dry:Pardmont -9.613383e-01 1.411000e+00
## Xcoef.soil.dry:Pardnigr -3.776895e+00 -1.271176e+00
## Xcoef.soil.dry:Pardpull 6.322576e-01 2.537881e+00
## Xcoef.soil.dry:Trocterr 1.053284e+00 4.059457e+00
## Xcoef.soil.dry:Zoraspin 1.556373e+00 4.445389e+00
## Xcoef.bare.sand:Alopacce 5.869626e-01 1.893112e+00
## Xcoef.bare.sand:Alopcune 9.200349e-01 3.279991e+00
## Xcoef.bare.sand:Alopfabr -3.852030e-01 2.041863e-01
## Xcoef.bare.sand:Arctlute -9.739425e-01 7.368305e-02
## Xcoef.bare.sand:Arctperi 1.975735e-01 1.193431e+00
## Xcoef.bare.sand:Auloalbi -1.533905e+00 1.864615e+00
## Xcoef.bare.sand:Pardlugu -6.816265e-01 1.158820e+00
## Xcoef.bare.sand:Pardmont -4.622025e-01 3.696449e-01
## Xcoef.bare.sand:Pardnigr -1.248497e+00 -1.826675e-01
## Xcoef.bare.sand:Pardpull -1.853032e-01 4.515806e-01
## Xcoef.bare.sand:Trocterr -5.133315e-01 4.600389e-01
## Xcoef.bare.sand:Zoraspin -8.538039e-01 1.487499e-01
## Xcoef.fallen.leaves:Alopacce -4.443042e-01 -1.601889e-02
## Xcoef.fallen.leaves:Alopcune -2.625610e-01 5.024886e-01
## Xcoef.fallen.leaves:Alopfabr -9.896539e-01 3.411172e-01
## Xcoef.fallen.leaves:Arctlute -5.799276e-01 6.142928e-01
## Xcoef.fallen.leaves:Arctperi -1.537152e+00 7.304470e-01
## Xcoef.fallen.leaves:Auloalbi -3.798849e+00 4.542370e-01
## Xcoef.fallen.leaves:Pardlugu -1.673561e+02 1.636040e+02
## Xcoef.fallen.leaves:Pardmont 1.124858e-01 1.144749e+00
## Xcoef.fallen.leaves:Pardnigr 3.911008e-02 6.850237e-01
## Xcoef.fallen.leaves:Pardpull -1.026861e+00 -1.851484e-03
## Xcoef.fallen.leaves:Trocterr -1.464693e+00 -2.320013e-01
## Xcoef.fallen.leaves:Zoraspin -9.509773e-01 1.023561e-02
## Xcoef.moss:Alopacce -4.819962e-01 2.695925e-02
## Xcoef.moss:Alopcune -1.030295e+00 -1.140110e-01
## Xcoef.moss:Alopfabr -2.226192e-01 4.251102e-01
## Xcoef.moss:Arctlute -7.951878e-01 3.962256e-01
## Xcoef.moss:Arctperi 1.287561e-02 1.067419e+00
## Xcoef.moss:Auloalbi -9.128146e-01 1.600750e+00
## Xcoef.moss:Pardlugu -1.220231e-01 1.114036e+00
## Xcoef.moss:Pardmont -4.179853e-01 5.431636e-01
## Xcoef.moss:Pardnigr -8.457072e-01 9.686023e-02
## Xcoef.moss:Pardpull 4.272490e-01 1.347121e+00
## Xcoef.moss:Trocterr -1.039383e+00 1.264850e-01
## Xcoef.moss:Zoraspin -8.542468e-02 8.149378e-01
## Xcoef.herb.layer:Alopacce -4.720118e-01 6.545176e-02
## Xcoef.herb.layer:Alopcune -5.685103e-01 3.884955e-01
## Xcoef.herb.layer:Alopfabr 3.371456e-01 1.137031e+00
## Xcoef.herb.layer:Arctlute -3.383310e-01 1.176726e+00
## Xcoef.herb.layer:Arctperi -2.708919e-01 8.745076e-01
## Xcoef.herb.layer:Auloalbi -3.062448e+00 2.814097e+00
## Xcoef.herb.layer:Pardlugu -4.106232e-01 4.531848e-01
## Xcoef.herb.layer:Pardmont 2.457165e+00 5.646078e+00
## Xcoef.herb.layer:Pardnigr 2.739258e-01 8.986164e-01
## Xcoef.herb.layer:Pardpull 4.636978e-02 1.421531e+00
## Xcoef.herb.layer:Trocterr -1.339361e-01 1.590655e+00
## Xcoef.herb.layer:Zoraspin 9.272378e-01 2.975321e+00
## Xcoef.reflection:Alopacce 1.164534e-01 7.749420e-01
## Xcoef.reflection:Alopcune -1.629915e-02 1.255804e+00
## Xcoef.reflection:Alopfabr 3.390305e-01 1.399673e+00
## Xcoef.reflection:Arctlute -3.763276e-01 1.218429e+00
## Xcoef.reflection:Arctperi -9.861328e-01 1.109125e+00
## Xcoef.reflection:Auloalbi -1.930076e+00 6.442852e-01
## Xcoef.reflection:Pardlugu 1.461087e+00 6.553382e+00
## Xcoef.reflection:Pardmont -1.141760e+00 5.459662e-02
## Xcoef.reflection:Pardnigr -1.807600e+00 -5.320006e-01
## Xcoef.reflection:Pardpull -5.106481e-01 6.670509e-01
## Xcoef.reflection:Trocterr -1.302713e+00 2.354060e-01
## Xcoef.reflection:Zoraspin -8.994784e-01 1.634400e-01
## inv.phi.Alopacce -6.348769e-01 4.576424e-02
## inv.phi.Alopcune -1.527771e+00 -3.352165e-01
## inv.phi.Alopfabr -1.056783e+01 2.886131e+01
## inv.phi.Arctlute -7.217570e-02 5.947889e+00
## inv.phi.Arctperi -8.212985e+00 2.372817e+01
## inv.phi.Auloalbi -1.057543e+00 5.102341e+00
## inv.phi.Pardlugu -5.194026e+09 5.202797e+09
## inv.phi.Pardmont -3.755134e+00 2.421395e+01
## inv.phi.Pardnigr -4.815644e+01 1.035652e+02
## inv.phi.Pardpull 5.020224e-01 3.479723e+00
## inv.phi.Trocterr -1.346396e+00 1.669193e+01
## inv.phi.Zoraspin -4.389429e+00 3.774414e+01
## inv.phi.Trocterr -3.376966e+00 5.623921e+01
## inv.phi.Zoraspin -1.191355e+00 1.383462e+01
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.
GLLVMs can be used as a model-based approach to unconstrained ordination by including two latent variables in the model:
The latent variable term try to capture the underlying factors driving species abundances at sites.
Predictions for the two latent variables, , 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.
ordiplot()
produces ordination plots based on fitted
GLLVMs.biplot = TRUE
in ordiplot()
).
# 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:
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↩︎
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↩︎
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↩︎
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 .↩︎
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.↩︎
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.↩︎
Dunn, P. K., and Smyth, G. K. (1996). Randomized quantile residuals. Journal of Computational and Graphical Statistics, 5, 236-244.↩︎
Gabriel, K. R. (1971). The biplot graphic display of matrices with application to principal component analysis. Biometrika, 58, 453-467↩︎