repeated measures ANOVA, crossover trial, R I am sorry for stating such a question. Unfortunatelly, it's very-very hard to go through all statistics.
I have a crossover study design with 3 treatments, 3 periods, and a baseline (covariate).
The data table looks like this:
SUBJECT     BASELINE   PERIOD1    PERIOD2    PERIOD3

subj_1      baseline   diet3      diet1      diet2   
subj_2      baseline   diet2      diet3      diet1
subj_3      baseline   diet1      diet2      diet3
subj_4      baseline   diet1      diet3      diet2
...         ...        ...        ...        ...
subj_27     baseline   diet2      diet1      diet3

PERIOD1, PERIOD2, PERIOD3 correspond to the time (week5, week11, week17). In between there is wash-out period.
My question is how to perform repeated measures ANOVA to such a experimental design?
I was trying to search for some examples and code but what I found out at the end is that I can apply mixed effect linear model to such data. Unfortunatelly, I did not find enough examples describing similar studies and the theory behind is very hard (I need time to understand).
I'm trying to find solution in R but SAS is also good. As I understood, in R there is a package nlme and I can apply a function lme() to fit an ANOVA model.
In order to do so, I found out that I have to divide my data according to treatment sequences (I will have 6 sequences: diet1-diet2-diet3, diet1-diet3-diet2, diet2-diet1-diet3, diet2-diet3-diet1, diet3-diet1-diet2, diet3-diet2-diet1).
Then I have to create a mixed effect linear model:
I assume (according to what I read and found) that probably it should look like this:
output_value ~ period(fixed effect) + diet(fixed effect) + sequence(fixed_effect)
                   + subject(random_effect) + baseline_value(random_effect)

In R it probably looks something like:
lme( output_value ~ period + diet + sequence, random ~ 1 | subject + baseline_value )

Is it right?
 A: In a crossover design, the effects that usually need to take into account are fixed sequence effect, period effect, treatment effect, and random subject effect. Note that by design the subject factor is nested within sequence (meaning that different subjects go through different sequences).
Basically you might want to keep all the effects in the model, and test whether they are significant or not. Prior to build the model, you also might want to test the carryover effect.
I've not used R to perform this kind of data analysis. But I guess there are a lot of sample code illustrating how to write R for a mixed-effects model. As far as SAS code, here are two references I think that both explains the experiment design and the SAS code very well.
1. Crossover Designs and Proc Mixed In SAS
2. The 2-by-2 crossover study and Grizzle Model
Hope it helps.
A: I am just suggesting following approach in R. I am not sure if it is the best way: 
> ddf
  subject_id baseline period1 period2 period3
1          1        5       6       7       5
2          2        6       8       7       5
3          3        8       9       9       7

> ddf$period1diff = ddf$period1 - ddf$baseline
    > ddf$period2diff = ddf$period2 - ddf$baseline
> ddf$period3diff = ddf$period3 - ddf$baseline
> 
> 
> ddf
  subject_id baseline period1 period2 period3 period1diff period2diff period3diff
1          1        5       6       7       5           1           2           0
2          2        6       8       7       5           2           1          -1
3          3        8       9       9       7           1           1          -1
> 
> melt(ddf[,c(1,6:8)], id='subject_id')
  subject_id    variable value
1          1 period1diff     1
2          2 period1diff     2
3          3 period1diff     1
4          1 period2diff     2
5          2 period2diff     1
6          3 period2diff     1
7          1 period3diff     0
8          2 period3diff    -1
9          3 period3diff    -1
> mm = melt(ddf[,c(1,6:8)], id='subject_id')
> aov.out = aov(value ~ variable + Error(subject_id/variable), data=mm)
> summary(aov.out)

Error: subject_id
          Df Sum Sq Mean Sq F value Pr(>F)
Residuals  1 0.6667  0.6667               

Error: subject_id:variable
         Df Sum Sq Mean Sq
variable  2  7.476   3.738

Error: Within
          Df Sum Sq Mean Sq F value Pr(>F)
variable   2 0.8571  0.4286   1.286  0.395
Residuals  3 1.0000  0.3333               
> 

A: Most of the terms are right. I wouldnt put the baseline value in the model though. Plus would add fixed effects for time (before vs after treatment) and time-treatment interaction, as well as a term for the data that you're modeling (say "x"). The following command should work fine in the nlme package: lme( output_value ~ period + diet + time + sequence + time * diet , random ~ 1 | subject, data=x)
