s-news
[Top] [All Lists]

Re: Linear regressions with constraints PLUS a question on C

To: Prof Brian Ripley <ripley@stats.ox.ac.uk>
Subject: Re: Linear regressions with constraints PLUS a question on C
From: Greg Snow <snow@fisher.byu.edu>
Date: Wed, 20 Feb 2002 12:18:22 -0700 (MST)
Cc: Rene Holst <rene@constat.dk>, s-news@lists.biostat.wustl.edu
In-reply-to: <Pine.LNX.4.31.0202201713400.24184-100000@gannet.stats>
On Wed, 20 Feb 2002, Prof Brian Ripley wrote:

> On Wed, 20 Feb 2002, Greg Snow wrote:
> 
> [...]
> 
> 
> > Now my question for the group:
> >
> > I originally thought that this could also be done using the contrasts or C
> > functions, but if I try the following code (S-PLUS 6.0.3, R 1.3.1)
> >
> > b <- a
> > contrasts(b,2) <- cbind( c(1,0,1), c(0,1,0) )
> > attributes(b)
> >
> > temp6 <- lm( y ~ -1 + a + b:x, x=T)
> > temp6$x
> >
> > then there is no evidence that the contrasts were ever used, I expected
> > the same x-matrix as temp3$x above.  Why are the contrasts not used in
> > creating the interaction terms?

[...]
> 
> You'll do better to look at the model matrix (here with treatment
> contrasts in use):
> 
> model.matrix(y ~ -1 + a + b:x)
>   aa ab ac ba:x bb:x bc:x
> 1  1  0  0    1    0    0
> 2  1  0  0    2    0    0
> 3  1  0  0    3    0    0
> 4  0  1  0    0    1    0
> 5  0  1  0    0    2    0
> 6  0  1  0    0    3    0
> 7  0  0  1    0    0    1
> 8  0  0  1    0    0    2
> 9  0  0  1    0    0    3
> 
> Note, three columns for b:x, which is correct as there is no x main
> effect.
> 
> model.matrix.default(y ~ -1 + a + x + b:x)
>   aa ab ac x x:b1 x:b2
> 1  1  0  0 1    1    0
> 2  1  0  0 2    2    0
> 3  1  0  0 3    3    0
> 4  0  1  0 1    0    1
> 5  0  1  0 2    0    2
> 6  0  1  0 3    0    3
> 7  0  0  1 1    1    0
> 8  0  0  1 2    2    0
> 9  0  0  1 3    3    0
> 
> does use the contrasts.  So, contrasts are only used when less than the
> number of levels is required.

OK, that makes sense and I can see the sense of it.  This leads to another
alternative for doing the constraints:

b <- a
contrasts(b,2) <- cbind( c(1,0,1), c(0,1,0) )

 model.matrix( y~ -1 + a + x/b - x )
or
 temp <- lm( y~ -1 + a + x/b - x, x=T)

but I don't know if that is simpler or more complex than my first answer.

I'm guessing that simmilar logic also explains why S-PLUS adds the
intercept column back and R just uses the dummy variables.  Unfortunately
there does not seem to be a way to tell either one to fit the main effects
assuming that there is an intercept, then take out the intercept (the main
effect version of above where it computes the interactions using the fact
that the x main effect is present, then takes out the x main effect).

This leads to another possible solution to the original question (which
may be simpler or more complex depending on the data and constraints):

b <- a
b[b=="c"] <- "a"

temp <- lm( y ~ -1 + a + b:x) # a and c have same slope

tempnew <- lm( y ~ -1 + b + a:x) # a and c have same intercept

tempnew2 <- lm( y ~ -1 + b/x) # a and c have same both

Constraining an intercept to be equal to 0 still seems to require creating
your own matrix like temp5 in my original answer.

Thank you Dr. Ripley

-- 
Greg Snow, PhD                Office: 223A TMCB
Department of Statistics      Phone:  (801) 378-7049
Brigham Young University      Dept.:  (801) 378-4505
Provo, UT  84602              email:  gls@byu.edu


<Prev in Thread] Current Thread [Next in Thread>