gllvm | R Documentation |
Fits generalized linear latent variable model for multivariate data. The model can be fitted using Laplace approximation method or variational approximation method.
gllvm(
y = NULL,
X = NULL,
TR = NULL,
data = NULL,
formula = NULL,
family,
num.lv = NULL,
num.lv.c = 0,
num.RR = 0,
lv.formula = NULL,
lvCor = NULL,
studyDesign = NULL,
dist = matrix(0),
corWithin = FALSE,
quadratic = FALSE,
row.eff = FALSE,
sd.errors = TRUE,
offset = NULL,
method = "VA",
randomB = FALSE,
randomX = NULL,
dependent.row = FALSE,
beta0com = FALSE,
zeta.struc = "species",
plot = FALSE,
link = "probit",
Ntrials = 1,
Power = 1.1,
seed = NULL,
scale.X = TRUE,
return.terms = TRUE,
gradient.check = FALSE,
disp.formula = NULL,
control = list(reltol = 1e-10, reltol.c = 1e-08, TMB = TRUE, optimizer = ifelse((num.RR
+ num.lv.c) == 0 | randomB != FALSE, "optim", "alabama"), max.iter = 6000, maxit =
6000, trace = FALSE, optim.method = NULL),
control.va = list(Lambda.struc = "unstructured", Ab.struct = "unstructured", Ar.struc =
"diagonal", diag.iter = 1, Ab.diag.iter = 0, Lambda.start = c(0.3, 0.3, 0.3), NN = 3),
control.start = list(starting.val = "res", n.init = 1, n.init.max = 10, jitter.var = 0,
start.fit = NULL, start.lvs = NULL, randomX.start = "zero", quad.start = 0.01,
start.struc = "LV", scalmax = 10, MaternKappa = 1.5, rangeP = NULL),
setMap = NULL,
...
)
y |
(n x m) matrix of responses. |
X |
matrix or data.frame of environmental covariates. |
TR |
matrix or data.frame of trait covariates. |
data |
data in long format, that is, matrix of responses, environmental and trait covariates and row index named as "id". When used, model needs to be defined using formula. This is alternative data input for y, X and TR. |
formula |
an object of class "formula" (or one that can be coerced to that class): a symbolic description of the model to be fitted (for fixed-effects predictors). |
family |
distribution function for responses. Options are |
num.lv |
number of latent variables, d, in gllvm model. Non-negative integer, less than number of response variables (m). Defaults to 2, if |
num.lv.c |
number of latent variables, d, in gllvm model to constrain, with residual term. Non-negative integer, less than number of response (m) and equal to, or less than, the number of predictor variables (k). Defaults to 0. Requires specification of "lv.formula" in combination with "X" or "datayx". Can be used in combination with num.lv and fixed-effects, but not with traits. |
num.RR |
number of latent variables, d, in gllvm model to constrain, without residual term (reduced rank regression). Cannot yet be combined with traits. |
lv.formula |
an object of class "formula" (or one that can be coerced to that class): a symbolic description of the model to be fitted (for latent variables). |
lvCor |
(Under development, not to be used at the moment!) correlation structure for latent variables, defaults to |
studyDesign |
variables related to eg. sampling/study design, used for defining correlation structure of the latent variables and row effects. |
dist |
matrix of coordinates or time points used for row parameters correlation structure |
corWithin |
logical. If |
quadratic |
either |
row.eff |
|
sd.errors |
logical. If |
offset |
vector or matrix of offset terms. |
method |
model can be fitted using Laplace approximation method ( |
randomB |
either |
randomX |
formula for species specific random effects of environmental variables in fourth corner model. Defaults to |
dependent.row |
logical. Whether or not random row effects are correlated (dependent) with the latent variables. Defaults to |
beta0com |
logical. If |
zeta.struc |
structure for cut-offs in the ordinal model. Either "common", for the same cut-offs for all species, or "species" for species-specific cut-offs. For the latter, classes are arbitrary per species, each category per species needs to have at least one observations. Defaults to "species". |
plot |
logical. If |
link |
link function for binomial family if |
Ntrials |
number of trials for binomial family. |
Power |
fixed power parameter in Tweedie model. Scalar from interval (1,2). Defaults to 1.1. If set to NULL it is estimated (note: experimental). |
seed |
a single seed value, defaults to |
scale.X |
logical. If |
return.terms |
logical. If |
gradient.check |
logical. If |
disp.formula |
formula, or alternatively a vector of indices, for the grouping of dispersion parameters (e.g. in a negative-binomial distribution). Defaults to NULL so that all species have their own dispersion parameter. Is only allowed to include categorical variables. If a formula, data should be included as named rows in y. |
control |
A list with the following arguments controlling the optimization:
|
control.va |
A list with the following arguments controlling the variational approximation method:
|
control.start |
A list with the following arguments controlling the starting values:
|
setMap |
UNDER DEVELOPMENT, DO NOT USE! list of a set of parameters to be fixed |
... |
Not used. |
Fits generalized linear latent variable models as in Hui et al. (2015 and 2017) and Niku et al. (2017).
Method can be used with two types of latent variable models depending on covariates. If only
site related environmental covariates are used, the expectation of response Y_{ij}
is determined by
g(\mu_{ij}) = \eta_{ij} = \alpha_i + \beta_{0j} + x_i'\beta_j + u_i'\theta_j,
where g(.)
is a known link function, u_i
are d
-variate latent variables (d
<<m
), \alpha_i
is an optional row effect
at site i
, and it can be fixed or random effect (also other structures are possible, see below), \beta_{0j}
is an intercept term for species j
, \beta_j
and \theta_j
are column
specific coefficients related to covariates and the latent variables, respectively.
Alternatively, a more complex version of the model can be fitted with quadratic = TRUE
, where species are modeled as a quadratic function of the latent variables:
g(\mu_{ij}) = \eta_{ij} = \alpha_i + \beta_{0j} + x_i'\beta_j + u_i'\theta_j - u_i' D_j u_i
. Here, D_j is a diagonal matrix of positive only quadratic coefficients, so that the model generates concave shapes only. This implementation follows the ecological theoretical model where species are generally recognized to exhibit non-linear response curves. For a model with quadratic responses, quadratic coefficients are assumed to be the same for all species:
D_j = D
. This model requires less information
per species and can be expected to be more applicable to most datasets. The quadratic coefficients D can be used to calculate the length of
ecological gradients.
For quadratic responses, it can be useful to provide the latent variables estimated with a GLLVM with linear responses, or estimated with (Detrended) Correspondence Analysis.
The latent variables can then be passed to the start.lvs
argument inside the control.start
list, which in many cases gives good results.
For GLLVMs with both linear and quadratic response model, a series of predictors x_{lv}
can be included to explain the latent variables:
g(\mu_{ij}) = \alpha_i + \beta_{0j} + x_i'\beta_j + (B' x_{lv,i} + \epsilon_i)' \gamma_j - (B' x_{lv,i} + \epsilon_i)' D_j (B' x_{lv,i} + \epsilon_i) ,
where z_i = B' x_{lv,i} + \epsilon_i
are latent variables informed by the predictors, but not constrained compared to unconstrained ordination as in methods such as CCA or RDA.
Omitting the predictors results in an unconstrained ordination, and omitting \epsilon_i
in the usual constrained ordination, which can also be fitted.
An alternative model is the fourth corner model (Brown et al., 2014, Warton et al., 2015) which will be fitted if also trait covariates
are included. The expectation of response Y_{ij}
is
g(\mu_{ij}) = \alpha_i + \beta_{0j} + x_i'(\beta_x + b_j) + TR_j'\beta_t + vec(B)*kronecker(TR_j,X_i) + u_i'\theta_j - u_i'D_ju_i
where g(.), u_i
, \beta_{0j}
and \theta_j
are defined as above. Vectors \beta_x
and \beta_t
are the main effects
or coefficients related to environmental and trait covariates, respectively, matrix B
includes interaction terms. Vectors b_j
are
optional species-specific random slopes for environmental covariates.
The interaction/fourth corner terms are optional as well as are the main effects of trait covariates.
In addition to the site-specific random effects, \alpha_i
, it is also possible to set arbitrary structure/design for the row effects.
That is, assume that observations / rows i=1,...,n
in the data matrix are from groups t=1,...,T
, so that each row i
belongs to one of the groups, denote G(i) \in \{1,...,T\}
. Each group t
has a number of observations n_t
, so that \sum_{t=1}^{T} n_t =n
.
Now we can set random intercept for each group t
, (see argument 'row.eff
'):
g(\mu_{ij}) = \eta_{ij} = \alpha_{G(i)} + \beta_{0j} + x_i'\beta_j + u_i'\theta_j,
There is also a possibility to set correlation structure for the random intercepts between groups, so that (\alpha_{1},...,\alpha_{T})^\top \sim N(0, \Sigma_r)
. That might be the case, for example, when the groups are spatially or temporally dependent.
Another option is to set row specific random intercepts \alpha_i
, but to set the correlation structure for the observations within groups, (see argument 'corWithin
'). That is, we can set corr(\alpha_{i},\alpha_{i'}) = C(i,i') \neq 0
according to some correlation function C
, when G(i)=G(i')
.
This model is restricted to the case, where each group has equal number of observations (rows), that is n_t=n_{t'}
for all t,t' \in \{1,...,T\}
.
The correlation structures available in the package are
corAR1
autoregressive process of order 1.
corExp
exponentially decaying, see argument 'dist
'.
corCS
compound symmetry.
The method is sensitive for the choices of initial values of the latent variables. Therefore it is
recommendable to use multiple runs and pick up the one giving the highest log-likelihood value (see argument 'n.init
').
However, sometimes this is computationally too demanding, and default option
starting.val = "res"
is recommended. For more details on different starting value methods, see Niku et al., (2018).
Models are implemented using TMB (Kristensen et al., 2015) applied to variational approximation (Hui et al., 2017), extended variational approximation (Korhonen et al., 2021) and Laplace approximation (Niku et al., 2017).
With ordinal family response classes must start from 0 or 1.
Mean and variance for distributions are defined as follows.
For count data family = poisson()
: Expectation E[Y_{ij}] = \mu_{ij}
, variance V(\mu_{ij}) = \mu_{ij}
, or
family = "negative.binomial"
: Expectation E[Y_{ij}] = \mu_{ij}
, variance V(\mu_{ij}) = \mu_{ij}+\mu_{ij}^2\phi_j
, or
family = "ZIP"
: Expectation E[Y_{ij}] = (1-p)\mu_{ij}
, variance V(\mu_{ij}) = \mu_{ij}(1-p_j)(1+\mu_{ij}p)
.
family = "ZINB"
: Expectation E[Y_{ij}] = (1-p)\mu_{ij}
, variance V(\mu_{ij}) = \mu_{ij}(1-p_j)(1+\mu_{ij}(\phi_j+p_j))
.
For binary data family = binomial()
: Expectation E[Y_{ij}] = \mu_{ij}
, variance V(\mu_{ij}) = \mu_{ij}(1-\mu_{ij})
.
For percent cover data 0 < Y_{ij} < 1
family = "beta"
: Expectation E[Y_{ij}] = \mu_{ij}
, variance V(\mu_{ij}) = \mu_{ij}(1-\mu_{ij})//(1+\phi_j)
.
For positive continuous data family = "gamma"
:Expectation E[Y_{ij}] = \mu_{ij}
, variance V(\mu_{ij}) = \mu_{ij}^2/\phi_j
, where \phi_j
is species specific shape parameter.
For non-negative continuous data family = "exponential"
:Expectation E[Y_{ij}] = \mu_{ij}
, variance V(\mu_{ij}) = \mu_{ij}^2
.
For non-negative continuous or biomass data family = "tweedie"
Expectation E[Y_{ij}] = \mu_{ij}
, variance V(\mu_{ij}) = \phi_j*\mu_{ij}^\nu
, where \nu
is a power parameter of Tweedie distribution. See details Dunn and Smyth (2005).
For ordinal data family = "ordinal"
: Cumulative probit model, see Hui et.al. (2016).
For normal distributed data family = gaussian()
: Expectation E[Y_{ij}] = \mu_{ij}
, variance V(y_{ij}) = \phi_j^2.
An object of class "gllvm" includes the following components:
call |
function call |
y |
(n x m) matrix of responses. |
X |
matrix or data.frame of environmental covariates. |
lv.X |
matrix or data.frame of environmental covariates for latent variables. |
TR |
Trait matrix |
formula |
Formula for predictors |
lv.formula |
Formula of latent variables in constrained and concurrent ordination |
randomX |
Formula for species specific random effects in fourth corner model |
randomB |
Boolean flag for random slopes in constrained and concurrent ordination |
num.lv |
Number of unconstrained latent variables |
num.lv.c |
Number of latent variables in concurrent ordination |
num.RR |
Number of latent variables in constrained ordination |
Ntrials |
Number of trials in a binomial model |
method |
Method used for integration |
family |
Response distribution |
row.eff |
Type of row effect used |
n.init |
Number of model runs for best fit |
disp.group |
Groups for dispersion parameters |
sd |
List of standard errors |
lvs |
Latent variables |
params |
List of parameters
|
Power |
power parameter |
sd |
list of standard errors of parameters |
prediction.errors |
list of prediction covariances for latent variables and variances for random row effects when method |
A, Ar, Ab_lv |
covariance matrices for variational densities of latent variables, random row effects, and random slopes respectively |
seed |
Seed used for calculating starting values |
TMBfn |
TMB objective and derivative functions |
logL |
log likelihood |
convergence |
convergence code of optimizer |
quadratic |
flag for quadratic model |
Hess |
List holding matrices of second derivatives |
beta0com |
Flag for common intercept in fourth corner models |
rstruc |
Integer that indicates which type of row structure is included |
cstruc |
Correlation structure for row effects |
dist |
Matrix of coordinates or time points used for row effects |
terms |
Terms object for main predictors |
start |
starting values for model |
optim.method |
Optimization method when using 'optim', 'alabama', or 'nloptr' |
If function gives warning: 'In f(x, order = 0) : value out of range in 'lgammaā€¯, optimizer have visited an area where gradients become too big. It is automatically fixed by trying another step in the optimization process, and can be ignored if errors do not occur.
Jenni Niku <jenni.m.e.niku@jyu.fi>, Wesley Brooks, Riki Herliansyah, Francis K.C. Hui, Pekka Korhonen, Sara Taskinen, Bert van der Veen, David I. Warton
Brown, A. M., Warton, D. I., Andrew, N. R., Binns, M., Cassis, G., and Gibb, H. (2014). The fourth-corner solution - using predictive models to understand how species traits interact with the environment. Methods in Ecology and Evolution, 5:344-352.
Dunn, P. K. and Smyth, G. K. (2005). Series evaluation of tweedie exponential dispersion model densities. Statistics and Computing, 15:267-280.
Hui, F. K. C., Taskinen, S., Pledger, S., Foster, S. D., and Warton, D. I. (2015). Model-based approaches to unconstrained ordination. Methods in Ecology and Evolution, 6:399-411.
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.
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.
Korhonen, P., Hui, F. K. C., Niku, J., and Taskinen, S. (2021). Fast, universal estimation of latent variable models using extended variational approximations. Stat Comput 33, 26 (2023).
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.
Niku, J., Brooks, W., Herliansyah, R., Hui, F. K. C., Taskinen, S., and Warton, D. I. (2018). Efficient estimation of generalized linear latent variable models. PLoS One, 14(5):1-20.
Warton, D. I., Guillaume Blanchet, F., O'Hara, R. B., Ovaskainen, O., Taskinen, S., Walker, S. C. and Hui, F. K. C. (2015). So many variables: Joint modeling in community ecology. Trends in Ecology & Evolution, 30:766-779.
coefplot.gllvm
, confint.gllvm
, ordiplot.gllvm
, plot.gllvm
, summary.gllvm
.
# Extract subset of the microbial data to be used as an example
data(microbialdata)
X <- microbialdata$Xenv
y <- microbialdata$Y[, order(colMeans(microbialdata$Y > 0),
decreasing = TRUE)[21:40]]
fit <- gllvm(y, X, formula = ~ pH + Phosp, family = poisson())
fit$logL
ordiplot(fit)
coefplot(fit)
# Inclusion of structured random row effect
sDesign<-data.frame(Site = microbialdata$Xenv$Site)
fit <- gllvm(y, X, formula = ~ pH + Phosp, family = poisson(),
studyDesign=sDesign, row.eff=~(1|Site))
## Load a dataset from the mvabund package
library(mvabund)
data(antTraits)
y <- as.matrix(antTraits$abund)
X <- as.matrix(antTraits$env)
TR <- antTraits$traits
# Fit model with environmental covariates Bare.ground and Shrub.cover
fit <- gllvm(y, X, formula = ~ Bare.ground + Shrub.cover,
family = poisson())
ordiplot(fit)
coefplot(fit)
## Example 1: Fit model with two unconstrained latent variables
# Using variational approximation:
fitv0 <- gllvm(y, family = "negative.binomial", method = "VA")
ordiplot(fitv0)
plot(fitv0, mfrow = c(2,2))
summary(fitv0)
confint(fitv0)
## Example 1a: Fit concurrent ordination model with two latent variables and with
# quadratic response model
# We scale and centre the predictors to improve convergence
fity1 <- gllvm(y, X = scale(X), family = "negative.binomial",
num.lv.c=2, method="VA")
ordiplot(fity1, biplot = TRUE)
#'## Example 1b: Fit constrained ordination model with two latent variables and with
# random canonical coefficients
fity2 <- gllvm(y, X = scale(X), family = "negative.binomial",
num.RR=2, randomB="LV", method="VA")
# Using Laplace approximation: (this line may take about 30 sec to run)
fitl0 <- gllvm(y, family = "negative.binomial", method = "LA")
ordiplot(fitl0)
# Poisson family:
fit.p <- gllvm(y, family = poisson(), method = "LA")
ordiplot(fit.p)
# Use poisson model as a starting parameters for ZIP-model, this line
# may take few minutes to run
fit.z <- gllvm(y, family = "ZIP", method = "LA",
control.start = list(start.fit = fit.p))
ordiplot(fit.z)
## Example 2: gllvm with environmental variables
# Fit model with two latent variables and all environmental covariates,
fitvX <- gllvm(formula = y ~ X, family = "negative.binomial")
ordiplot(fitvX, biplot = TRUE)
coefplot(fitvX)
# Fit model with environmental covariates Bare.ground and Shrub.cover
fitvX2 <- gllvm(y, X, formula = ~ Bare.ground + Shrub.cover,
family = "negative.binomial")
ordiplot(fitvX2)
coefplot(fitvX2)
# Use 5 initial runs and pick the best one
fitvX_5 <- gllvm(y, X, formula = ~ Bare.ground + Shrub.cover,
family = "negative.binomial", control.start=list(n.init = 5, jitter.var = 0.1))
ordiplot(fitvX_5)
coefplot(fitvX_5)
## Example 3: Data in long format
# Reshape data to long format:
datalong <- reshape(data.frame(cbind(y,X)), direction = "long",
varying = colnames(y), v.names = "y")
head(datalong)
fitvLong <- gllvm(data = datalong, formula = y ~ Bare.ground + Shrub.cover,
family = "negative.binomial")
## Example 4: Fourth corner model
# Fit fourth corner model with two latent variables
fitF1 <- gllvm(y = y, X = X, TR = TR, family = "negative.binomial")
coefplot(fitF1)
# Fourth corner can be plotted also with next lines
#fourth = fitF1$fourth.corner
#library(lattice)
#a = max( abs(fourth) )
#colort = colorRampPalette(c("blue","white","red"))
#plot.4th = levelplot(t(as.matrix(fourth)), xlab = "Environmental Variables",
# ylab = "Species traits", col.regions = colort(100),
# at = seq( -a, a, length = 100), scales = list( x = list(rot = 45)))
#print(plot.4th)
# Specify model using formula
fitF2 <- gllvm(y = y, X = X, TR = TR,
formula = ~ Bare.ground + Canopy.cover * (Pilosity + Webers.length),
family = "negative.binomial")
ordiplot(fitF2)
coefplot(fitF2)
## Include species specific random slopes to the fourth corner model
fitF3 <- gllvm(y = y, X = X, TR = TR,
formula = ~ Bare.ground + Canopy.cover * (Pilosity + Webers.length),
family = "negative.binomial", randomX = ~ Bare.ground + Canopy.cover,
control.start = list(n.init = 3))
ordiplot(fitF3)
coefplot(fitF3)
## Example 5: Fit Tweedie model
# Load coral data
data(tikus)
ycoral <- tikus$abund
# Let's consider only years 1981 and 1983
ycoral <- ycoral[((tikus$x$time == 81) + (tikus$x$time == 83)) > 0, ]
# Exclude species which have observed at less than 4 sites
ycoral <- ycoral[-17, (colSums(ycoral > 0) > 4)]
# Fit Tweedie model for coral data (this line may take few minutes to run)
fit.twe <- gllvm(y = ycoral, family = "tweedie", method = "LA")
ordiplot(fit.twe)
## Example 6: Random row effects
fitRand <- gllvm(y, family = "negative.binomial", row.eff = "random")
ordiplot(fitRand, biplot = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.