Hi Andrzej,

Thanks for your answer. However I need to be a bit clearer on what I 
exactly want.
The trick of removing the intercept works in this simple example. But it 
doesn't work if more variables are included. However, to make it easier 
I prefer staying with this simple example.
What I meant here, is having a way to use the model.matrix to implement 
the calculation to get the intercepts and slopes (not the differences) 
directly calculated from the model with the help of the model.matrix for 
any type of model.

I tried the following:

fm1<- lme(distance ~ Sex*age, random = ~ 1 + age | Subject, data = 
Orthodont)
newdat <- expand.grid(Sex=c("Male","Female"), age=c(0,1), distance = 0) 
# here age contains 0 for the calculation of the intercept and 1 for the 
calculation of the slope.
mm = model.matrix(terms(fm1),newdat)
newdat$distance = mm %*% fixef(fm1)
newdat
      Sex age distance
1   Male   0    16.34
2 Female   0    17.37
3 *Male   1    17.12*
4 *Female   1    17.85*

In this case the calculation for the intercept for both male and female 
is correct, but the calculation for the slope is wrong. This is because 
of two reasons. First the model.matrix (mm) contains a 1 in the 
(Intercept) when age contains 1 as well (used for the calculation of the 
slope) and so the intercept is included in the calculation of the slope. 
Second the SexFemale contains a 1 when age contains a 1 as well and so 
the difference in the intercept for female is included in the 
calculation of the slope for female.
   (Intercept) SexFemale age SexFemale:age
1           1         0   0             0
2           1         1   0             0
3 *1*         0   1             0
4 *1* *1*   1             1

Instead these 1s in the intercept should be 0s. I can add them manually 
like this.
mm <- round(solve(vcov(fm1)) %*% vcov(fm1))
mm[1:2,1] <- 1
mm
   (Intercept) SexFemale age SexFemale:age
1           1         0   0             0
2           1         1   0             0
3 *0*         0   1             0
4 *0* *0*   1             1

newdat$distance = mm %*% fixef(fm1)
newdat
      Sex age distance
1   Male   0  16.3406
2 Female   0  17.3727
3 *Male   1   0.7844*
4 *Female   1   0.4795*

But this trick is fare from being general to be applied to any model.
I was also thinking about using the vcov as follow
mm <- round(solve(vcov(fm1)) %*% vcov(fm1))
mm[2,1] <- 1
mm[4,3] <- 1
mm
               (Intercept) SexFemale age SexFemale:age
(Intercept)             1         0   0             0
SexFemale               1         1   0             0
age                     0         0   1             0
SexFemale:age           0         0   1             1

But again it's not general. As you can see the whole idea is to be able 
to create a matrix (mm), based on the model, to calculate the 
coefficients. The matrix should be flexible as to be used when we shift 
the order of the variables in the model for example age*Sex instead of 
Sex*age. Or without the interaction Sex+age. And of course with more 
variables.

Any suggestion would be welcome. Thanks in advance.
Yann

On 25.04.2012 18:14, Andrzej wrote:
> Hi Yann,
>
> Reparameterize the  model. This is probably what you are looking for:
>
> > fm1a <- lme(distance ~ -1 + Sex +age:Sex, random = ~ 1 + age | 
> Subject, data = Orthodont)
> > fixef(fm1a)
>       SexMale     SexFemale   SexMale:age SexFemale:age
>    16.3406250    17.3727273     0.7843750     0.4795455
>
> Hope it helps
>
> Andrzej
>
> PS. age is continuous not "sort of categorical"
>
> On 4/25/2012 11:36 AM, Yann Hautier wrote:
>> Dear all,
>>
>> On the FAQ page of http://glmm.wikidot.com/faq, Ben Bolker gives a nice
>> example of how to get predictions and/or confidence (or prediction)
>> intervals on predictions from the following model for lme:
>> fm1<- lme(distance ~ age*Sex, random = ~ 1 + age | Subject, data =
>> Orthodont)
>> and for lmer
>> fm1 = lmer(formula = distance ~ age*Sex + (age|Subject), data = 
>> Orthodont,)|
>>
>> |In this example, to calculate the predictions and confidence intervals,
>> the variable age is sort of considered as categorical,
>> age=c(8,10,12,14), and therefore the coefficients reported are the means
>> with their SE.
>> However, the fixef(fm1) output of the model shows that age is a
>> continuous variable and therefore the output
>> (Intercept)         age     SexMale age:SexMale
>>       17.3727      0.4795     -1.0321      0.3048
>> reports the intercept of Female (Intercept), the slope of Female (age),
>> the difference in the intercept for Male (SexMale) and the difference in
>> slope for Male (age:SexMale). The intercept and slope for Male have to
>> be calculated, the intercept is then 17.3727+(-1.0321)=16.3406 and the
>> slope is then 0.4795+0.3048=0.7843.
>>
>> Here is my question. In the same fashion as the means are calculated
>> with the model.matrix
>> newdat<- 
>> expand.grid(age=c(8,10,12,14),Sex=c("Male","Female"),distance = 0)
>> mm = model.matrix(terms(fm1),newdat)
>> newdat$distance = mm %*% fixef(fm1)
>> Is it possible to directly calculate the slopes and intercepts from this
>> model? I am of course talking about the estimates of each of the slope
>> and intercept for each gender (male and female). So with the calculation
>> from the fixef(lm1) output.
>>
>> Thanks in advance,
>> Yann
>>
>>     [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-mixed-models@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
>>
>>
>

	[[alternative HTML version deleted]]

