3

I have a NLME object with a correlation. I was wondering how I could extract the range of the correlation from the model. I'm running simulations, so I can't just read the summary, and get it manually.

So my model looks something like this:

library(MASS)
library(nlme)
lme(fixed=temp ~ time, random=~1|day,correlation=corExp(form=~time),data=beav1)

So I'd like to get the parameter of the correlation here.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Galadude
  • 253
  • 1
  • 3
  • 11
  • Unless you have data too, I can't run that model to see what pops out. Can you use a built in data set? Can you show the summary to indicate which part you are trying to extract? – MrFlick May 30 '14 at 22:53
  • I've now changed it to a standard data set. – Galadude May 30 '14 at 23:37
  • 1
    @shujaa beat me to it, but you have an answer now. Things are so much easier with a simple example. – MrFlick May 31 '14 at 00:12
  • @MrFlick Hope I wasn't poaching... though I'm pretty sure a week or so ago I had more reputation than you--by now you're about 50% ahead of me! :) – Gregor Thomas May 31 '14 at 00:14
  • 1
    No worries @shujaa. I just like it when questions get answered. It doesn't have to be by me. And I admit I have gotten somewhat addicted to answering R questions on this site recently. Hopefully my interest will subside to a more reasonable level soon. – MrFlick May 31 '14 at 00:17

1 Answers1

4

That took a lot of digging to find! I had to look at the code for nlme:::print.summary.lme to find $modelStruct$corStruct, and then at nlme:::print.summary.corStruct to get there.

This should work

library(datasets)
library(nlme)
mod <- lme(fixed = temp ~ time, random = ~1|day,
           correlation = corExp(form=~time), data=beaver1)

store_range <- coef(mod$modelStruct$corStruct, unconstrained = F)

Yielding

> store_range
   range 
58.82908 
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • I was doing the same thing. I was just looking to see how the class change in the `print.summary.corStruct` changed the behavior of `coef` . Very interesting use of generic functions. – MrFlick May 31 '14 at 00:14
  • Yeah, I'm still confused by the necessity of that class change. Definitely gained an appreciation for the move to S4 classes in `lme4`. – Gregor Thomas May 31 '14 at 00:15
  • 1
    You can shorten this a little bit -- you don't need `summary()`; `coef(mod$modelStruct$corStruct,unconstrained=FALSE)` – Ben Bolker Jun 01 '14 at 15:06
  • @BenBolker Thanks! Incorporated your suggestion. Thanks for maintaining `lme4` too, answering this question made me even more grateful for it. – Gregor Thomas Jun 01 '14 at 17:37