[R] Questions on formula in princomp

Gabor Grothendieck ggrothendieck at gmail.com
Fri Apr 14 01:12:38 CEST 2006

On 4/13/06, Sasha Pustota <popgen at gmail.com> wrote:
> I hope this time I'm using the "iris" dataset correctly:
> ir <- rbind(iris3[,,1], iris3[,,2], iris3[,,3])
> lir <- data.frame(log(ir))
> names(lir) <- c("a","b","c","d")
> I'm trying to understand the meaning of expressions like "~ a+b+c+d",
> used with princomp, e.g.
> princomp(~ a+b+c+d, data=lir, cor=T)
> By inspection, it looks like the result is the same as in
> princomp(lir, cor = T).

Yes, princomp.formula just takes the model matrix of the formula
and passes it to princomp.default.

> Do "a+b+c+d" simply specify the columns to be included? Could someone
> provide a meaningful example of princomp formula that uses operators
> other than "+"?

colnames(model.matrix(~., lir))
princomp(~., lir)

colnames(model.matrix(~(.)^2, lir))
princomp(~(.)^2, lir)

> In linear model, E(y)= xb, examples, "~" is usually placed between "y"
> and "x". What is the meaning of "~" here?

Its just a way to specify a formula that you can take a model matrix
of.  See ?model.matrix and try playing with it a bit on small examples.

More information about the R-help mailing list