Re: [R] nlme fixed effects specification

From: Douglas Bates <bates_at_stat.wisc.edu>
Date: Fri, 04 May 2007 10:26:36 -0500

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
> documentation, too, please?

First we need to decide what the example is. :-)

Regards,
Doug



R-help_at_stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 04 May 2007 - 15:52:01 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 04 May 2007 - 21:32:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.