var_cov: Variance Covariance matrix of for g(n)ls and (n)lme models

View source: R/var_cov.R

var_covR Documentation

Variance Covariance matrix of for g(n)ls and (n)lme models

Description

Extracts the variance covariance matrix (residuals, random or all)

Usage

var_cov(
  object,
  type = c("residual", "random", "all", "conditional", "marginal"),
  aug = FALSE,
  sparse = FALSE,
  data = NULL
)

Arguments

object

object which inherits class lm, gls or lme

type

“residual” for the variance-covariance for the residuals, “random” for the variance-covariance of the random effects or “all” for the sum of both.

aug

whether to augment the matrix of the random effects to the dimensions of the data

sparse

whether to return a sparse matrix (default is FALSE)

data

optional passing of ‘data’, probably needed when using this function inside other functions.

Details

Variance Covariance matrix for (non)linear mixed models

Value

It returns a matrix or a sparse matrix Matrix.

Note

See Chapter 5 of Pinheiro and Bates. This returns potentially a very large matrix of N x N, where N is the number of rows in the data.frame. The function getVarCov only works well for lme objects.
The equivalence is more or less:
getVarCov type = “random.effects” equivalent to var_cov type = “random”.
getVarCov type = “conditional” equivalent to var_cov type = “residual”.
getVarCov type = “marginal” equivalent to var_cov type = “all”.
The difference is that getVarCov has an argument that specifies the individual for which the matrix is being retrieved and var_cov returns the full matrix only.

See Also

getVarCov

Examples


require(graphics)
require(nlme)
data(ChickWeight)
## First a linear model
flm <- lm(weight ~ Time, data = ChickWeight)
vlm <- var_cov(flm)
## First model with no modeling of the Variance-Covariance
fit0 <- gls(weight ~ Time, data = ChickWeight)
v0 <- var_cov(fit0)
## Only modeling the diagonal (weights)
fit1 <- gls(weight ~ Time, data = ChickWeight, weights = varPower())
v1 <- var_cov(fit1)
## Only the correlation structure is defined and there are no groups
fit2 <- gls(weight ~ Time, data = ChickWeight, correlation = corAR1())
v2 <- var_cov(fit2)
## The correlation structure is defined and there are groups present
fit3 <- gls(weight ~ Time, data = ChickWeight, correlation = corCAR1(form = ~ Time | Chick))
v3 <- var_cov(fit3)
## There are both weights and correlations
fit4 <- gls(weight ~ Time, data = ChickWeight, 
            weights = varPower(),
            correlation = corCAR1(form = ~ Time | Chick))
v4 <- var_cov(fit4)
## Tip: you can visualize these matrices using
image(log(v4[,ncol(v4):1]))


nlraa documentation built on July 9, 2023, 6:08 p.m.