# Re: [R] Linear Models with mean as Intercept.

From: Sundar Dorai-Raj <sundar.dorai-raj_at_pdf.com>
Date: Fri 01 Jul 2005 - 00:54:51 EST

Ghislain Vieilledent wrote:
>
> *******Objectif********
>
> I try to set up linear models with mean as intercept:
> Variable: x, as factor of two modalities: x(1), x(2).
>
> I would like to have a model as:
> y = mean(y)+A(i)+residuals,
> with i in (1,2) and A(1) coefficient for x(1) and A(2) coefficient for x(2).
>
> *******Trials in R*******
>
> ## Firstly:
>
> I write in R:
> >Model<-lm(y~x,Data)
> >summary(Model)
> ...
> I've got the coefficients for:
> - the intercept (x(1) as been choosen) that we can call B(1)
> - the second modality: x(2) that we can call B(2)
>
> If I have well understood we have for the model and predictions:
> if x(1): y=B(1)
> if x(2): y=B(1)+B(2)
> which is quite different as y=mean(y)+A(i)
>
> ## Secondly
> I tried to skip the intercept
> >Model2<-lm(y~0+x,Data)
> >summary(Model2)
> ...
> I've got the coefficients for:
> - the first modality: x(1) that we can call C(1)
> - the second modality: x(2) that we can call C(2)
>
> And the model and predictions, if I'm right, are:
> if x(1): y=C(1)
> if x(2): y=C(2)
>
> ******* Questions ***********
> How can I obtain a predictive model y=mean(y)+A(i) ?
> Is it possible to settle mean(y) as intercept?
>
>
> Ghislain V., retarded statistician.

You have to set the contrasts differently:

set.seed(1)
y <- rnorm(10)
x <- factor(rep(letters[1:5], each = 2)) fit <- lm(y ~ x, contrasts = list(x = "contr.sum")) all.equal(mean(y), coef(fit))
# TRUE "contr.helmert" will work too. The default "contr.treatment" does not have columns that sum to zero. Be forewarned: the coefficients need to be interpretted differently.

HTH, --sundar

R-help@stat.math.ethz.ch mailing list