Learn R Programming

lfe (version 1.4-708)

demeanlist: Centre vectors on multiple groups

Description

Uses the method of alternating projections to centre a (model) matrix on multiple groups, as specified by a list of factors. This function is called by felm, but it has been made available as standalone in case it's needed.

Usage

demeanlist(mtx,fl,icpt=0,eps=getOption('lfe.eps'),
           threads=getOption('lfe.threads'),progress=getOption('lfe.pint'))

Arguments

Value

If mtx is a matrix, a matrix of the same shape, possibly with column icpt deleted. If mtx is a list of vectors and matrices, a list of the same length is returned, with the same vector and matrix-pattern, but the matrices have the column icpt deleted.

Details

For each column y in mtx, the equivalent of the following centering is performed, with cy as the result. cy <- y; oldy <- y-1 while(sqrt(sum((cy-oldy)**2)) >= eps) { oldy <- cy for(f in fl) cy <- cy - tapply(cy,f,mean)[f] }

Examples

Run this code
## create a 15x3 matrix
mtx <- matrix(rnorm(45),15,3)

## a list of factors
fl <- list(g1=factor(sample(2,nrow(mtx),replace=TRUE)),
           g2=factor(sample(3,nrow(mtx),replace=TRUE)))

## centre on both means and print result
mtx0 <- demeanlist(mtx,fl)
cbind(mtx0,g1=fl[[1]],g2=fl[[2]],comp=compfactor(fl))

for(i in 1:ncol(mtx0))
   for(n in names(fl))
    cat('col',i,'group',n,'level sums:',tapply(mtx0[,i],fl[[n]],mean),'')

Run the code above in your browser using DataLab