predict.gllvm: Predict Method for gllvm Fits

View source: R/predict.gllvm.R

predict.gllvmR Documentation

Predict Method for gllvm Fits

Description

Obtains predictions from a fitted generalized linear latent variable model object.

Usage

## S3 method for class 'gllvm'
predict(
  object,
  newX = NULL,
  newTR = NULL,
  newLV = NULL,
  type = "link",
  level = 1,
  offset = TRUE,
  se.fit = FALSE,
  alpha = 0.95,
  seed = 42,
  ordinal.cat = NULL,
  spp = NULL,
  ...
)

Arguments

object

an object of class 'gllvm'.

newX

A new data frame of environmental variables. If omitted, the original matrix of environmental variables is used.

newTR

A new data frame of traits for each response taxon. If omitted, the original matrix of traits is used.

newLV

A new matrix of latent variables. If omitted, the original matrix of latent variables is used. Note that number of rows/sites must be the same for newX (if X covariates are included in the model).

type

the type of prediction required. The default ("link") is on the scale of the linear predictors; the alternatives are "response" and "class" (predicted classes, works only for binomial model with 1 trial or ordinal model). "response" is on the scale of the response variable. That is, the predictions for the binomial model are predicted probabilities. In case of ordinal data, type = "response" gives predicted probabilities for each level of ordinal variable.

level

specification for how to predict. Level one (level = 1) attempts to use the predicted site scores from variational approximations or laplace approximation or given site scores in newLV. Level 0 sets the latent variable to zero. Defaults to 1.

offset

specification whether of not offset values are included to the predictions in case they are in the model, defaults to TRUE when offset values that are used to fit the model are included to the predictions. Alternatives are matrix/vector (number of rows must match with the newX) of new offset values or FALSE, when offsets are ignored.

se.fit

logical. If TRUE, performs 1000 simulations from the asymptotic covariance matrix for fixed effects, and from the CMSEP covariance matrix for the random effects. If an integer is provided, it is used as the number of simulations instead.

alpha

numerical between 0 and 1, defaults to 0.95. The confidence level for se.fit.

seed

numeric, defaults to 42. Seed used for simulation in se.fit.

ordinal.cat

integer or NULL, defaults to NULL. For ordinal models with type = "response", selects a single category to return instead of the full K×n×p array. When set, the return value is an n×p matrix of probabilities for that category. Primarily used internally by predictSR and goodnessOfFit to reduce memory use.

spp

integer vector or NULL, defaults to NULL. If provided, predictions are computed only for the selected species (column indices into the response matrix), returning an n \times \text{length(spp)} matrix. All parameter-level computations (beta0, Xcoef, theta, etc.) are subsetted accordingly, so no unnecessary computation is performed for the remaining species.

...

not used.

Details

If newX, newTR and newLV are omitted the predictions are based on the data used for fitting the model. Notice that newTR need to match with the number of species in the original data. Instead, new sites can be specified in newX. If predictors newX (and newTR) are given, and newLV is not, latent variables are not used in the predictions.

Value

A matrix containing requested predictor types.

Author(s)

Jenni Niku <jenni.m.e.niku@jyu.fi>, David Warton

Examples


# Load a dataset from the mvabund package
data(antTraits, package = "mvabund")
y <- as.matrix(antTraits$abund)
X <- scale(antTraits$env[, 1:3])
# Fit gllvm model
fit <- gllvm(y = y, X, family = poisson())
# fitted values
predfit <- predict(fit, type = "response")

# linear predictors
predlin <- predict(fit)
# Predict new sites:
# Generate matrix of environmental variables for 10 new sites
xnew <- cbind(rnorm(10), rnorm(10), rnorm(10))
colnames(xnew) <- colnames(X)
predfit <- predict(fit, newX = xnew, type = "response", level = 0)

TR <- (antTraits$tr[, 1:3])
fitt <- gllvm(y = y, X, TR, family = poisson())
# linear predictors
predlin <- predict(fitt)
# Predict new sites:
# Generate matrix of environmental variables for 10 new sites
xnew <- cbind(rnorm(10), rnorm(10), rnorm(10))
colnames(xnew) <- colnames(X)
# Generate matrix of traits for species
trnew <- data.frame(Femur.length = rnorm(41), No.spines = rnorm(41),
 Pilosity = factor(sample(0:3, 41, replace = TRUE)))
predfit <- predict(fitt, newX = xnew, newTR = trnew, type = "response", level = 0)


gllvm documentation built on May 10, 2026, 5:06 p.m.