mat.random: Calculates the variance matrix for the random effects from a...

mat.randomR Documentation

Calculates the variance matrix for the random effects from a mixed model, based on a supplied formula or a matrix.

Description

For n observations, compute the variance matrix of the random effects. The matrix can be specified using a formula for the random effects and a list of values of the variance components for the terms specified in the random formula. If a matrix specifying the variances of the nuisance random effects is supplied then it is returned as the value from the function.

Usage

mat.random(random, G, design, keep.order = TRUE)

Arguments

random

A formula or a matrix. If a formula, it specifies the random effects from which the matrix for the contribution of the random effects to the variance matrix can be generated. If it is a matrix, it must be an n x n matrix and will be passed through as the required variance matrix for the random effects. The default is 0, which implies that there are no random effects.

G

This term only needs to be set if random is a formula. Then it is set to a list, in which each component is either a single value or a matrix; there needs to be a component for each term in the expanded formula, with the order of the terms and components matching. If it is a single value, a diagonal matrix of dimension equal to the product of the numbers of levels of the factors in its term. If it is a matrix, its dimension must be equal to the product of the numbers of levels of the factors in its term.

design

A data.frame containing the design to be used in an experiment and for which the variane matrix is required. It is not required when the only formula specified is an intercept-only formula.

keep.order

A logical indicating whether the terms should keep their position in the expanded formula projector, or reordered so that main effects precede two-factor interactions, which precede three-factor interactions and so on.

Details

If \bold{Z}_i is the is incidence matrix for the random nuisance effects in \bold{u}_i for a term in random and \bold{u}_i has variance matrix \bold{G}_i so that the contribution of the random effectst to the variance matrix for \bold{Y} is \bold{V}_u = \Sigma (\bold{Z}_i\bold{G}_i(\bold{Z}_i)^T).

Value

A n x n matrix containing the variance matrix for the random effects.

Author(s)

Chris Brien

See Also

mat.Vpredicts.

Examples

## Reduced example from Smith et al. (2015)
## Generate two-phase design
mill.fac <- fac.gen(list(Mrep = 2, Mday = 2, Mord = 3))
field.lay <- fac.gen(list(Frep = 2, Fplot = 4))
field.lay$Variety <- factor(c("D","E","Y","W","G","D","E","M"), 
                            levels = c("Y","W","G","M","D","E"))
start.design <- cbind(mill.fac, field.lay[c(3,4,5,8,1,7,3,4,5,8,6,2),])
rownames(start.design) <- NULL

## Set gammas
terms <- c("Variety", "Frep", "Frep:Fplot", "Mrep", "Mrep:Mday", "Mrep:Mday:Mord")
gammas <- c(1, 0.1, 0.2, 0.3, 0.2, 1)
names(gammas) <- terms

## Specify matrices to calculate the variance matrix of the predicted fixed Variety effects 
Vu <- with(start.design, fac.vcmat(Mrep, gammas["Mrep"]) + 
                         fac.vcmat(fac.combine(list(Mrep,Mday)), gammas["Mrep:Mday"]) + 
                         fac.vcmat(Frep, gammas["Frep"]) + 
                         fac.vcmat(fac.combine(list(Frep,Fplot)), gammas["Frep:Fplot"]))

## Calculate the variance matrix of the predicted random Variety effects using formulae
Vu <- mat.random(random = ~ -1 + Mrep/Mday + Frep/Fplot, 
                 G = as.list(gammas[c(4,5,2,3)]), 
                 design = start.design)

dae documentation built on June 22, 2024, 9:07 a.m.