7

I am trying to fit a model which is similar to the

fm2orth.lm<-lme(distance~age,data=OrthoFem, random=~1|Subject)

and did

summary(fm2Orth.lm)

my output had these parts

  1. Aic/BIC
  2. Random Effects
  3. Fixed Effects table
  4. Correlation.

My question is I am interested in only the third output above and tried

fm2Orth.lm$coefficients 

and had coefficients of the individual subjects, which is not what I want, can someone show me how I could get only the fixed effects table with the p-values.

Steve
  • 71

2 Answers2

9

The answer is quite easy:

summary(fm2Orth.lm)$tTable

will give you the full table (not just the coefficients).

In general, use:

names(fm2Orth.lm)

and

names(summary(fm2Orth.lm))

to see what objects are contained in those lists. Then you can explore those objects and you will often find what you need.

EDIT: Since this was raised in the comments and may be useful to some: Extracting the variances of the random effects can be tricky from the objects in those lists. See here for a way of extracting those: https://stackoverflow.com/questions/9043565/extracting-random-effects-from-nlme-summary/9043924#9043924

Wolfgang
  • 16,997
  • 1
    +1 answer. Both names and str are very useful for poking into structures like that. Unless it's an S4 class (you use an 'foo@bar' instead of 'foo$bar' to access members), in which case you'll need slotNames. – Wayne Sep 22 '11 at 15:26
  • Yes, thanks for mentioning str and the difference with S4 classes. I think the output from str can be confusing for a novice (especially for complex structures like those created by the lme function), but once you get the hang of it, it is an indispensable tool. – Wolfgang Sep 23 '11 at 09:14
  • You're right: at first I never used str because of information overload, but now I find that it often saves an additional step (i.e. find a name, figure it might be of interest, then class or dim or print it to figure out what it really is). – Wayne Sep 23 '11 at 14:24
  • -1 Since this leads to a dead end. A method that works is given at stackoverflow using VarCorr but unfortunately this returns a character rather than a numeric. – nacnudus Aug 08 '14 at 10:27
  • What do you mean this leads to a dead end? The OP was asking about the table with the fixed effects and summary(fm2Orth.lm)$tTable gives just that. The link you gave shows how to get the random effects. – Wolfgang Aug 08 '14 at 13:55
  • 1
    Apologies, my bad, I misread the OP as looking for the random effects. However I did waste a lot of time using the "general" method to find an object for the random effects. I can remove the downvote if you make a trivial edit. – nacnudus Aug 23 '14 at 08:43
  • 1
    No problem. I added the link you mentioned to my answer, since this is indeed helpful. – Wolfgang Aug 23 '14 at 09:45
3

The class of the output of lme is, not surprisingly, lme. You can find this by running class(fm2orth.lm).

As a consequence, when you call summary on it, what is really called is summary.lme. This function is not exported from the nlme package (you can discover this when you type summary.lme at the prompt: you get a message that the object is not found), but you can still get to its source like this: getAnywhere("summary.lme").

You will see there some of the objects of the summary are built up and what name they are given (e.g. object$tTable <- ...) You will also see that the result of summary.lme is an object of class summary.lme (again, no surprises there).

Finally, you can check print.summary.lme, because print is what is automatically called by R to display the return value of the last statement, but like summary, it is dispatched to a version that is class-specific. Note, once again, this function is not exported from the package, so you'll have to use getAnywhere.

Finally, in this function's source, you'll see a call cat("Fixed effects: ") that obviously marks the start of printing the information you're interested in. There you can see that what is really printed (cat(deparse)), is based upon the summary's member call$fixed but may depend on the way the original model was built.

This should give you enough info to find out how to get to it.

Edit: I just noticed that the object returned from summary.lme is really the orginal fit (so the return value of lme itself, with some extra list items added to it. As such, this call member used in print.summary.lme is already part of the original fit (, since it is not added by summary.lme). Result: you probably don't really need the summary to get to the fixed effects.

Nick Sabbe
  • 12,819
  • 2
  • 37
  • 47
  • Hi Nick, I really enjoyed your question on this. Do you think it is possible to create a table with all the parameters that you estimate from a lmer, this is like saving in a table the fixed effects - beta values and the random effects too ? – Rosa Maria Mar 25 '22 at 13:43