Learn R Programming

nlraa (version 1.2)

var_cov: 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
)

Value

It returns a matrix or a sparse matrix Matrix.

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

See Also

Examples

Run this code
# \donttest{
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]))
# }

Run the code above in your browser using DataLab