# [R] nlme fixed effects specification

Douglas Bates bates at stat.wisc.edu
Fri May 4 17:26:36 CEST 2007

```On 5/3/07, ivo welch <ivowel at gmail.com> wrote:
> dear R experts:
>
> sorry, I have to ask this again.  I know that the answer is in section
> 7.2 of "S Programming," but I don't have the book (and I plan to buy
> the next edition---which I hope will be titled S/R programming ;-) ).

> I believe the following yields a standard fixed-effects estimation:

> fixed.effects = as.factor( as.integer( runif(100)*10 ) )
> y=rnorm(100); x=rnorm(100);
> print(summary(lm( Y ~ X + fe)))

Want to try that one again, Ivo?  You have defined objects called
"fixed.effects", "y" and "x" then used the formula Y ~ X + fe.

> I would like to know how to get the same coefficient on X using nlme.

The nlme function or the nlme package.  I'm guessing that you are
referring to the lme function in the nlme package but it would help if
you told us exactly what you mean.

Also, perhaps you could be a bit more explicit about what you mean by
"same coefficient".  If you fit a model with random effects for a
factor then you will get similar but not identical values for the
coefficients of the fixed effects term.

> (I cannot use this ordinary lm method in my real application, simply
> because I have 10,000 fixed effects.)  I tried a variety of arguments
> to the "fixed" nlme parameter (e.g., fixed=list(fmid)), but did not
> get the syntax right.  could someone please tell me the magic spell?

If you plan on fitting a linear mixed model then I would suggest

library(lme4)
lmer(Y ~ X + (1|fact))

where fact is the factor specifying the groups in the data.  For example

> set.seed(123454321)
> fact <- sample(10, 100, repl = TRUE)
> y <- rnorm(100); x <- rnorm(100)
> lmer(y ~ x + (1|fact))
Linear mixed-effects model fit by REML
Formula: y ~ x + (1 | fact)
AIC   BIC logLik MLdeviance REMLdeviance
294.5 302.3 -144.3      282.7        288.5
Random effects:
Groups   Name        Variance   Std.Dev.
fact     (Intercept) 5.0482e-10 2.2468e-05
Residual             1.0096e+00 1.0048e+00
number of obs: 100, groups: fact, 10

Fixed effects:
Estimate Std. Error t value
(Intercept)  0.08536    0.10062  0.8483
x            0.08725    0.08787  0.9929

Correlation of Fixed Effects:
(Intr)
x -0.052
Warning message:
Estimated variance for factor fact is effectively zero

> may I also suggest that such an example be added to the nlme examples