design_mat {lazy.sasef}R Documentation

Generation of Various Design Matrices

Description

Generation of Various Design Matrices

Usage

design_mat(
  data = NULL,
  fnames = NULL,
  levels = NULL,
  type = 0,
  drop = "last",
  maxorder = 3,
  sort = 0,
  delim = "",
  pattern = "A",
  print = 0,
  debug = 0
)

Arguments

data

Matrix of data frame containing the combination of factor levels
The column names of data will be used as the factor names.

fnames

A vector of factor names

levels

A vector of levels of each factor

type

The type of design matrix
-2 : cell mean model
-1 : SAS GLM style non-full-rank model
0 : zero sum constraints
1 : drop one column

drop

= "first" if the first level is dropped when type >= 0

maxorder

The maximum order of effects

sort

= 1 to sort the columns according to the order of effect

delim

NOT yet available

pattern

The pattern of factor names:
"A" or "a", or "Aa", or "aA" or "AA", or "aa"

print

= 1 to print the design info
= 2 to print the design matrix

debug

= 1 to print the intermediate result

Details

When data is NULL, levels and fnames will be used.
When data is given, the colums specified by fnames will be used.
If fnames is NULL, all the columns will be used and the column names will be used as factor names.
Data has priority over levels.


Note that an effect is defined as a combination of each levels
of factors with order 1 (main), 2 (two way interaction), ..., maxorder.


To retrieve the information of the design matrix, use get_design_info.
To extract the subset of the columns of the design matrix, use get_design_subset.
To rename the columns of the design matrix, use change_enames.

Value

A design object which is a list of the design matrix (X), infomation list (info) and parameter list (param) where

X is the design matrix
and
info is a list whose length is the number of effects.

info[[i]] contains the following:
ename name of effect[i]
order order of effect[i]: 0, 1, ..., maxorder
elevels # of levels of effect[i]
df degrees of freedom of effect[i]
erange range of effect[i] in the (full rank) estimable functions
range=cbind(from,to) where
from is the starting independent colmn of effect[i] in X
to is the ending independent column of effect[i] in X
rangef=cbind(from,to) where
from is the starting colmn of effect[i] in X
to is the ending column of effect[i] in X
menum vector of main effect number involved in effect[i] contained vector of effect numbers which contains effect[i]
where effect number is defined according to the order of column of X.
contains vector of effects which effect[i] contais.
param is a list consisting of

type, drop, maxorder, sort, pattern

Information of estimable function may be added to this object later.

Examples

# generate various types of design matrices
res0 <- design_mat( levels=c(2:3), type=0 )$X
res10 <- design_mat( levels=c(2:3), type=1 )$X
res11 <- design_mat( levels=c(2:3), type=1, drop="first" )$X
resnf <- design_mat( levels=c(2:3), type=-1 )$X
rescm <- design_mat( levels=c(2:3), type=-2 )$X
Print(res0, res10, res11)
Print(resnf,rescm)

# projecting all to type=0 matrix and checking if they are equivalent.
P0 <- res0%*%solve(t(res0)%*%res0)%*%t(res0)
maxad10 <- max(abs(res10-P0%*%res10))
maxad11 <- max(abs(res11-P0%*%res11))
maxadnf <- max(abs(resnf-P0%*%resnf))
maxadcm <- max(abs(rescm-P0%*%rescm))
Print( maxad10, maxad11, maxadnf, maxadcm )

# sorting the columns
levels <- c(2,3,2)
ll <- lapply( levels,function(x) seq(1:x) )
data <- cprod(ll); data <- rbind(data,c(1,1,1))
rownames(data) <- 1:nrow(data); colnames(data)=c("A","B","C")
res1 <- design_mat( data )
res2 <- design_mat( data, sort=1 )
Print(res1$X)
Print(res2$X)


[Package lazy.sasef version 0.1.4 Index]