[R] How to specified contrasts in anova (lm)

Davide Risso davide.risso at berkeley.edu
Wed Jul 1 21:30:55 CEST 2015


Dear list,

I have the following anova that I want to fit in R:

y_{ijk} = \mu + \alpha_i + \beta_{j(i)} + \epsilon_{ijk}

This is an application in biology, in which we are measuring a certain
(continuous) characteristic of a group of cells.
Hence y_{ijk} corresponds to the measurement on cell k of type i in batch j.

Batch is nested in type, meaning that we have multiple batches for each
cell type and each batch contains only cells of a given type. To complicate
things, the design is unbalanced.

I can "manually" fit the model, with the following constraints:
\sum_{i=1}^m \alpha_i = 0
and
\sum_{j=1}^{n_i} \beta_{j(i)} = 0.

This gives me m  + 1 constraints, where m is the number of types.

A minimal (toy) example in R is:

a <- as.factor(c(rep(1, 4), rep(2, 6)))
b <- as.factor(rep(1:5, each=2))
y <- rnorm(10)

fit <- lm(y ~ a + b)

This call to lm will fit the wrong model, using two constraints (with the
"contr.sum" specification), \sum_{i=1}^m \alpha_i = 0 and \sum_{j=1}^{n}
\beta_{j} = 0, and resulting in a singular design matrix.
Is there a way to specify the right constraints in lm?

Thanks in advance for any help,
davide

-- 
Davide Risso, PhD
Post Doctoral Scholar
Division of Biostatistics
School of Public Health
University of California, Berkeley
344 Li Ka Shing Center, #3370
Berkeley, CA 94720-3370
E-mail: davide.risso at berkeley.edu

	[[alternative HTML version deleted]]



More information about the R-help mailing list