Toby Marthews
2008-Jun-04 14:01 UTC
[R] Constructing groupedData objects in nlme - a little problem
Dear R-help,
I am trying to create groupedData objects using the nlme library. I'm
missing something basic, I know:
Here is the first example in ch.1 of Pinheiro & Bates (2000):
library(nlme)
x2=Rail$travel;x1=Rail$Rail;eg1=data.frame(x1,x2);eg1gd=Rail
print(eg1gd)
x11();print(plot(eg1gd))
femodel=lm(x2~x1-1,data=eg1gd)
print(femodel$coefficients)
Result:
x12 x15 x11 x16 x13 x14
31.66667 50.00000 54.00000 82.66667 84.66667 96.00000
...which works fine. This uses a built-in groupedData object called
"Rail"
that is part of the nlme library.
I am trying to 'recreate' this groupedData object. Here's what
I've done:
x1=c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6);x2=c(55,53,54,26,37,32,78,91,85,92,100,96,49,51,50,80,85,83)
eg1=data.frame(x1,x2);colnames(eg1)=c("Rail","travel");eg1gd=groupedData(travel~1|Rail,data=eg1)
print(eg1gd)
x11();print(plot(eg1gd))
femodel=lm(x2~x1-1,data=eg1gd)
print(femodel$coefficients)
Result:
x1
16.49817
...but, as you can see, the coefficients I get at the end this time are
completely different and I don't know why. Somehow, I am not creating the
structure properly even though the formula and data values are all
correct.
Can anyone help? I've looked at the ?groupedData man page, but it has no
solution to this.
Thanks very much for any advice,
Toby
Pinheiro JC & Bates DM (2000). Mixed-Effects Models in S and S-PLUS (1st
ed.). Springer, New York.
Hank Stevens
2008-Jun-04 16:38 UTC
[R] Constructing groupedData objects in nlme - a little problem
HI Toby,
I think that the problem is that you have not specified using Rail and
travel in the model statement, but rather are still using x1 and x2.
It also does not realize that you want x1 to be a factor. Try
x1=factor(c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6))
x2=c(55,53,54,26,37,32,78,91,85,92,100,96,49,51,50,80,85,83)
eg1=data.frame(x1,x2);
colnames(eg1)=c("Rail","travel");
eg1gd=groupedData(travel~1|Rail,data=eg1)
print(eg1gd)
x11();
print(plot(eg1gd))
femodel=lm(x2~x1-1,data=eg1gd)
femodel=lm(travel~Rail-1,data=eg1gd)
summary(femodel)
Cheers,
Hank
On Jun 4, 2008, at 10:01 AM, Toby Marthews wrote:
> Dear R-help,
>
> I am trying to create groupedData objects using the nlme library. I'm
> missing something basic, I know:
>
> Here is the first example in ch.1 of Pinheiro & Bates (2000):
>
> library(nlme)
> x2=Rail$travel;x1=Rail$Rail;eg1=data.frame(x1,x2);eg1gd=Rail
> print(eg1gd)
> x11();print(plot(eg1gd))
> femodel=lm(x2~x1-1,data=eg1gd)
> print(femodel$coefficients)
> Result:
> x12 x15 x11 x16 x13 x14
> 31.66667 50.00000 54.00000 82.66667 84.66667 96.00000
>
> ...which works fine. This uses a built-in groupedData object called
> "Rail"
> that is part of the nlme library.
> I am trying to 'recreate' this groupedData object. Here's what
I've
> done:
>
> x1=c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6);
> x2=c(55,53,54,26,37,32,78,91,85,92,100,96,49,51,50,80,85,83)
> eg1=data.frame(x1,x2);
> colnames(eg1)=c("Rail","travel");
> eg1gd=groupedData(travel~1|Rail,data=eg1)
> print(eg1gd)
> x11();
> print(plot(eg1gd))
> femodel=lm(x2~x1-1,data=eg1gd)
> print(femodel$coefficients)
> Result:
> x1
> 16.49817
>
> ...but, as you can see, the coefficients I get at the end this time
> are
> completely different and I don't know why. Somehow, I am not
> creating the
> structure properly even though the formula and data values are all
> correct.
>
> Can anyone help? I've looked at the ?groupedData man page, but it
> has no
> solution to this.
>
> Thanks very much for any advice,
> Toby
>
> Pinheiro JC & Bates DM (2000). Mixed-Effects Models in S and S-PLUS
> (1st
> ed.). Springer, New York.
>
> ______________________________________________
> R-help at r-project.org 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.
Dr. Hank Stevens, Associate Professor
338 Pearson Hall
Botany Department
Miami University
Oxford, OH 45056
Office: (513) 529-4206
Lab: (513) 529-4262
FAX: (513) 529-4243
http://www.cas.muohio.edu/~stevenmh/
http://www.cas.muohio.edu/ecology
http://www.muohio.edu/botany/
"If the stars should appear one night in a thousand years, how would men
believe and adore." -Ralph Waldo Emerson, writer and philosopher
(1803-1882)
Dieter Menne
2008-Jun-04 17:15 UTC
[R] Constructing groupedData objects in nlme - a little problem
Toby Marthews <Toby.Marthews <at> lsce.ipsl.fr> writes:> > Dear R-help, > > I am trying to create groupedData objects using the nlme library. I'm > missing something basic, I know: > > Here is the first example in ch.1 of Pinheiro & Bates (2000):...> > ...but, as you can see, the coefficients I get at the end this time are > completely different and I don't know why. Somehow, I am not creating the > structure properly even though the formula and data values are all > correct. >Hank is right. On the other hand your well-defined query points to a problem using groupedData. I do a lot of my work with nlme and love it, but I admit that I only started to understand it after I totally stopped using groupedData. Which makes reading Pinheiro/Bates sometimes difficult, because many examples give the impression that these must be done with groupedData. Everything can be done by explicitly saying what should be analyzed in lme and friends, there is no need to rely on groupedData. Note that in lme4 that partially re-designs nlme, there is no such thing as groupedData. Dieter