title: How do I extract lmer fixed effects by observation?
body: <p>I have a lme object, constructed from some repeated measures nutrient intake data (two 24-hour intake periods per RespondentID):</p>
  
  <pre><code>Male.lme2 &lt;- lmer(BoxCoxXY ~ -1 + AgeFactor + IntakeDay + (1|RespondentID),
      data = Male.Data, 
      weights = SampleWeight)
  </code></pre>
  
  <p>and I can successfully retrieve the random effects by <code>RespondentID</code> using <code>ranef(Male.lme1)</code>. I would also like to collect the result of the fixed effects by <code>RespondentID</code>. <code>coef(Male.lme1)</code> does not provide exactly what I need, as I show below.</p>
  
  <pre><code>&gt; summary(Male.lme1)
  Linear mixed model fit by REML 
  Formula: BoxCoxXY ~ AgeFactor + IntakeDay + (1 | RespondentID) 
     Data: Male.Data 
    AIC   BIC logLik deviance REMLdev
    9994 10039  -4990     9952    9980
  Random effects:
   Groups       Name        Variance Std.Dev.
   RespondentID (Intercept) 0.19408  0.44055 
   Residual                 0.37491  0.61230 
  Number of obs: 4498, groups: RespondentID, 2249
  
  Fixed effects:
                      Estimate Std. Error t value
  (Intercept)         13.98016    0.03405   410.6
  AgeFactor4to8        0.50572    0.04084    12.4
  AgeFactor9to13       0.94329    0.04159    22.7
  AgeFactor14to18      1.30654    0.04312    30.3
  IntakeDayDay2Intake -0.13871    0.01809    -7.7
  
  Correlation of Fixed Effects:
              (Intr) AgFc48 AgF913 AF1418
  AgeFactr4t8 -0.775                     
  AgeFctr9t13 -0.761  0.634              
  AgFctr14t18 -0.734  0.612  0.601       
  IntkDyDy2In -0.266  0.000  0.000  0.000
  </code></pre>
  
  <p>I have appended the fitted results to my data, <code>head(Male.Data)</code> shows</p>
  
  <pre><code>   NutrientID RespondentID Gender Age SampleWeight  IntakeDay IntakeAmt AgeFactor BoxCoxXY  lmefits
  2         267       100020      1  12    0.4952835 Day1Intake 12145.852     9to13 15.61196 15.22633
  7         267       100419      1  14    0.3632839 Day1Intake  9591.953    14to18 15.01444 15.31373
  8         267       100459      1  11    0.4952835 Day1Intake  7838.713     9to13 14.51458 15.00062
  12        267       101138      1  15    1.3258785 Day1Intake 11113.266    14to18 15.38541 15.75337
  14        267       101214      1   6    2.1198688 Day1Intake  7150.133      4to8 14.29022 14.32658
  18        267       101389      1   5    2.1198688 Day1Intake  5091.528      4to8 13.47928 14.58117
  </code></pre>
  
  <p>The first couple of lines from <code>coef(Male.lme1)</code> are:</p>
  
  <pre><code>$RespondentID
         (Intercept) AgeFactor4to8 AgeFactor9to13 AgeFactor14to18 IntakeDayDay2Intake
  100020    14.28304     0.5057221      0.9432941        1.306542          -0.1387098
  100419    14.00719     0.5057221      0.9432941        1.306542          -0.1387098
  100459    14.05732     0.5057221      0.9432941        1.306542          -0.1387098
  101138    14.44682     0.5057221      0.9432941        1.306542          -0.1387098
  101214    13.82086     0.5057221      0.9432941        1.306542          -0.1387098
  101389    14.07545     0.5057221      0.9432941        1.306542          -0.1387098
  </code></pre>
  
  <p>To demonstrate how the <code>coef</code> results relate to the fitted estimates in Male.Data (which were grabbed using <code>Male.Data$lmefits &lt;- fitted(Male.lme1)</code>, for the first RespondentID, who has the AgeFactor level 9-13:
  - the fitted value is <code>15.22633</code>, which equals - from the coeffs - <code>(Intercept) + (AgeFactor9-13) = 14.28304 + 0.9432941</code></p>
  
  <p>Is there a clever command for me to use that will do want I want automatically, which is to extract the fixed effect estimate for each subject, or am I faced with a series of <code>if</code> statements trying to apply the correct AgeFactor level to each subject to get the correct fixed effect estimate, after deducting the random effect contribution off the Intercept?</p>
  
  <p>Update, apologies, was trying to cut down on the output I was providing and forgot about str(). Output is:</p>
  
  <pre><code>&gt;str(Male.Data)
  'data.frame':   4498 obs. of  11 variables:
   $ NutrientID  : int  267 267 267 267 267 267 267 267 267 267 ...
   $ RespondentID: Factor w/ 2249 levels "100020","100419",..: 1 2 3 4 5 6 7 8 9 10 ...
   $ Gender      : int  1 1 1 1 1 1 1 1 1 1 ...
   $ Age         : int  12 14 11 15 6 5 10 2 2 9 ...
   $ BodyWeight  : num  51.6 46.3 46.1 63.2 28.4 18 38.2 14.4 14.6 32.1 ...
   $ SampleWeight: num  0.495 0.363 0.495 1.326 2.12 ...
   $ IntakeDay   : Factor w/ 2 levels "Day1Intake","Day2Intake": 1 1 1 1 1 1 1 1 1 1 ...
   $ IntakeAmt   : num  12146 9592 7839 11113 7150 ...
   $ AgeFactor   : Factor w/ 4 levels "1to3","4to8",..: 3 4 3 4 2 2 3 1 1 3 ...
   $ BoxCoxXY    : num  15.6 15 14.5 15.4 14.3 ...
   $ lmefits     : num  15.2 15.3 15 15.8 14.3 ...
  </code></pre>
  
  <p>The BodyWeight and Gender aren't being used (this is the males data, so all the Gender values are the same) and the NutrientID is similarly fixed for the data.</p>
  
  <p>I have been doing horrible ifelse statements sinced I posted, so will try out your suggestion immediately. :)</p>
  
  <p>Update2: this works perfectly with my current data and should be future-proof for new data, thanks to DWin for the extra help in the comment for this. :)</p>
  
  <pre><code>AgeLevels &lt;- length(unique(Male.Data$AgeFactor))
  Temp &lt;- as.data.frame(fixef(Male.lme1)['(Intercept)'] + 
  c(0,fixef(Male.lme1)[2:AgeLevels])[
        match(Male.Data$AgeFactor, c("1to3", "4to8", "9to13","14to18",  "19to30","31to50","51to70","71Plus") )] + 
  c(0,fixef(Male.lme1)[(AgeLevels+1)])[
        match(Male.Data$IntakeDay, c("Day1Intake","Day2Intake") )])
  names(Temp) &lt;- c("FxdEffct")
  </code></pre>