residuals.sgdgmf: Extract the residuals of a GMF model

residuals.sgdgmfR Documentation

Extract the residuals of a GMF model

Description

Extract the residuals of a GMF model and, if required, compute the eigenvalues of the residuals covariance/correlation matrix. Moreover, if required, return the partial residual of the model obtained by excluding the matrix decomposition from the linear predictor.

Usage

## S3 method for class 'sgdgmf'
residuals(
  object,
  ...,
  type = c("deviance", "pearson", "working", "response", "link"),
  partial = FALSE,
  normalize = FALSE,
  fillna = FALSE,
  spectrum = FALSE,
  ncomp = 50
)

## S3 method for class 'sgdgmf'
resid(
  object,
  ...,
  type = c("deviance", "pearson", "working", "response", "link"),
  partial = FALSE,
  normalize = FALSE,
  fillna = FALSE,
  spectrum = FALSE,
  ncomp = 50
)

Arguments

object

an object of class sgdgmf

...

further arguments passed to or from other methods

type

the type of residuals which should be returned

partial

if TRUE, computes the residuals excluding the matrix factorization from the linear predictor

normalize

if TRUE, standardize the residuals column-by-column

fillna

if TRUE, fills NA values column-by-column

spectrum

if TRUE, returns the eigenvalues of the residual covariance matrix

ncomp

number of eigenvalues to be calculated (only if spectrum=TRUE)

Details

Let g(\mu) = \eta = X B^\top + \Gamma Z^\top + U V^\top be the linear predictor of a GMF model. Let R = (r_{ij}) be the correspondent residual matrix. The following residuals can be considered:

  • deviance: r_{ij}^{_D} = \textrm{sign}(y_{ij} - \mu_{ij}) \sqrt{D(y_{ij}, \mu_{ij})};

  • Pearson: r_{ij}^{_P} = (y_{ij} - \mu_{ij}) / \sqrt{\nu(\mu_{ij})};

  • working: r_{ij}^{_W} = (y_{ij} - \mu_{ij}) / \{g'(\mu_{ij}) \,\nu(\mu_{ij})\};

  • response: r_{ij}^{_R} = y_{ij} - \mu_{ij};

  • link: r_{ij}^{_G} = g(y_{ij}) - \eta_{ij}.

If partial=TRUE, mu is computed excluding the latent matrix decomposition from the linear predictor, so as to obtain the partial residuals.

Let \Sigma be the empirical variance-covariance matrix of R, being \sigma_{ij} = \textrm{Cov}(r_{:i}, r_{:j}). Then, the latent spectrum of the model is the collection of eigenvalues of \Sigma.

Notice that, in case of Gaussian data, the latent spectrum corresponds to the principal component analysis on the regression residuals, whose eigenvalues can be used to infer the amount of variance explained by each principal component. Similarly, we can use the (partial) latent spectrum in non-Gaussian data settings to infer the correct number of principal components to include into the GMF model or to detect some residual dependence structures not already explained by the model.

Value

If spectrum=FALSE, a matrix containing the selected residuals. If spectrum=TRUE, a list containing the residuals (res), the first ncomp eigenvalues of the residual covariance matrix, say (lambdas), the variance explained by the first ncomp principal component of the residuals (explained.var), the variance not explained by the first ncomp principal component of the residuals (residual.var), the total variance of the residuals (total.var).

Examples

# Load the sgdGMF package
library(sgdGMF)

# Generate data from a Poisson model
data = sim.gmf.data(n = 100, m = 20, ncomp = 5, family = poisson())

# Fit a GMF model with 3 latent factors
gmf = sgdgmf.fit(data$Y, ncomp = 3, family = poisson())

# Get the deviance residuals of a GMF model
str(residuals(gmf)) # returns the overall deviance residuals
str(residuals(gmf, partial = TRUE)) # returns the partial residuals
str(residuals(gmf, spectrum = TRUE)) # returns the eigenvalues of the residual var-cov matrix


sgdGMF documentation built on June 8, 2025, 12:05 p.m.