I have seen how to use ~ operator in formula. For example y~x means: y is distributed as x.
However I am really confused of what does ~0+a means in this code:
require(limma)
a = factor(1:3)
model.matrix(~0+a)
Why just model.matrix(a) does not work? Why the result of model.matrix(~a) is different from model.matrix(~0+a)? And finally what is the meaning of ~ operator here?
~creates a formula – it separates the righthand and lefthand sides of a formulaFrom
?`~`Quoting from the help for formula
So regarding specific issue with
~a+0ais a factor,model.matrix(~a)will return an intercept column which isa1(You needn-1indicators to fully specifynclasses)The help files for each function are well written, detailed and easy to find!
why doesn’t
model.matrix(a)workmodel.matrix(a)doesn’t work becauseais afactorvariable, not a formula or terms objectFrom the help for
model.matrixRis looking for a particular class of object, by passing a formula~ayou are passing an object that is of classformula.model.matrix(terms(~a))would also work, (passing the terms object corresponding to the formula~ageneral note
@BenBolker helpfully notes in his comment, This is a modified version of Wilkinson-Rogers notation.
There is a good description in the Introduction to R.