[R] How to get interction terms first in a model

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Feb 17 18:17:52 CET 2005


It can be done via an explicit call to terms[.formula] with
keep.order = TRUE.

summary( lm(terms(y ~ ns(x, knots=c(30, 50, 70), intercept=T):A - 1 + B,
                   keep.order = TRUE)) )

I've given other examples in the past, and I think this is in the White 
Book.

BTW, it is not quite the same as this drops Ba not Bd (as I think it 
should).


On Thu, 17 Feb 2005, BXC (Bendix Carstensen) wrote:

> Consider the following two specifications of a model:
>
> library( splines )
> x <- 1:100
> y <- rnorm( 100 )
> w <- rep( 1, 100 )
> A <- factor( sample( 1:2, 100, replace=T ) )
> B <- factor( sample( letters[1:4], 100, replace=T ) )
> summary( lm( y ~ ns( x, knots=c(30, 50, 70 ), intercept=T ):A - 1 + B )
> )
> summary( lm( y ~ ns( x, knots=c(30, 50, 70 ), intercept=T ):A - 1 + B:w
> ) )
>
> The interaction with the constant variable w is how I got to have the
> two spline terms as "proper" intercept terms.
>
> Is there another way to do this, or is it a featur of the model formulae
> that lower order terms appear before higher order terms regardless of
> the
> order they are specified in the model?
>
> Bendix Carstensen
> ----------------------
> Bendix Carstensen
> Senior Statistician
> Steno Diabetes Center
> Niels Steensens Vej 2
> DK-2820 Gentofte
> Denmark
> tel: +45 44 43 87 38
> mob: +45 30 75 87 38
> fax: +45 44 43 07 06
> bxc at steno.dk
> www.biostat.ku.dk/~bxc
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list