[R] Re-Post: Combining Factors in model.matrix

Paul Boutros Paul.Boutros at utoronto.ca
Mon Jan 26 09:51:17 CET 2004


> > > You will need to explain to us why the object you list is `the design
> > > matrix': have *you* a reference for that?  R is doing the conventional
> > > thing, and I at least have no idea where your example comes from.
> >
> > Perhaps I have used the wrong terminology?  My understanding of a design
> > matrix is that it identifies the factors are present for a
> > given experiment.
>
> The design matrix is X in the regression usually represented by
>
> y = Xb + e
>
> and is called a model matrix in S/R.

Right, that's how I understood it.

> > Here, I have a two factor experiment, where each factor has two levels.
> > In the case I gave:
> >    t1 t2
> > 1   1  0
> > 2   1  1
> > 3   0  0
> > 4   0  1
> >
> > I had expected this to represent four distinct experiments where
> > factor one is present in the first two and absent in the second two.
>
> You can't have factors that are present/absent.  (You can have levels of
> treatments which are present/absent.)  We understand the rows to represent
> the individuals runs of a single experiment, but what do the columns
> represent?

Yes, I mis-spoke.  I thought the columns represent individual factors, with
a
0 = level 1 for this factor
1 = level 2 for this factor
Hence the encoding I gave above would indicate that factor 1 is at level 1
for
the first pair of experiments, but at level 0 for the second pair.

> > > You seem to have coded variables t1 and t2 the opposite ways (the
> > > reference level is 2 for t1 and 1 for t2), and your model has
> the fit at
> > > levels t1=2,t1=1 constrained to pass through the origin.  I
> don't think R
> > > has a simple syntax for that (although you can fake
> anything), and I find
> > > it hard to believe that is actually what you want.
> >
> > That wasn't my intention, I want to retain the intercept term and
> > not constrain it to pass through the origin.
>
> So why did you use ~ -1 + (t1+t2) ?  That explicitly removes the
> intercept.

Ahh, I had misunderstood the -1 as explicitly specifying an intercept.
So now:

> t1 <- factor(c(1,1,2,2));
> t2 <- factor(c(1,2,1,2));
> design <- model.matrix(~ t1+t2);
> design;
  (Intercept) t12 t22
1           1   0   0
2           1   0   1
3           1   1   0
4           1   1   1

Which is what I had been looking for!

Thank you for your patient help,
Paul




More information about the R-help mailing list