tvar {mra}R Documentation

Expand Time-varying covariates in models

Description

Expands a vector of time-varying values into a 2-d matrix of the appropriate size for use in MRA model formulas.

Usage

tvar(x, nan=attr(x,"nan"), drop.levels=1)

Arguments

x The vector of time varying values to expand. This can be a factor (see as.factor). It is assumed that length(x) = number of sampling occasions. If not, an error will occur in whatever routine called this function (e.g., F.3d.model.matrix).
nan Number of individuals. Default is to use the 'nan' attribute of x. If nan is not specifed or is not an attribute of x, an error is thrown.
drop.levels A vector of integers specifying which levels of a factor to drop. Only applicable if x is a factor. By default, the first level of all factors is dropped. drop.levels=length(levels(x)) does the SAS thing and drops the last level of a factor. Specifying a vector will drop all levels in the vector. E.g., drop.levels=c(1,2,7) drops the 1st, 2nd, and 7th levels of the factor, whatever they are. First level of a factor is first element of levels(x). Second level of a factor is second element of levels(x), and so on. Setting drop.levels to 0, a negative number, or a number greater than the number of levels will not drop any levels (this is so-called cell mean coding).

Value

A 2-d matrix of size nan x length(x) suitable for passing to the Fortran DLL of MRA for estimation. Values within columns are contant, values across columns vary according to x. If x is a factor, this matrix contains 0-1 indicator functions necessary to fit the factor.
If x is a factor, attributes of the returned matrix are "levels" = levels of the factor and "contr" = contrasts used in the coding (always contr.treatment). For other contrast coding of factors, make your own 2-d matrix with a call to the appropriate function (like contr.poly).

Author(s)

Trent McDonald, WEST-INC, tmcdonald@west-inc.com

See Also

F.cjs.estim, tvar

Examples


nan <- 30
ns <- 5
time.occ <- as.factor(paste("T",1:ns, sep=""))
attr(time.occ,"nan") <- nan
time.mat <- tvar(time.occ, drop=ns) # Last occasion is the reference, the SAS and MARK default.

dim(time.mat) <- c(nan,ns,length(levels(time.occ))-1)
print(time.mat)  # each page is the 2-d matrix used in the fit.
print(time.mat[1,,])

#  compare the above to 
tvar( 1:ns, nan )


[Package mra version 2.2 Index]