UPDATE: Based on the comment from ‘linuxizer’, I’ve updated this to stay inline with the S3 classes, something I didn’t have my head around at the time, still don’t know it inside out.
Brief post.
One thing I do often in my analysis is use things like ‘summary(mod)$coefficients’ and variance derivatives of that to pull the results from models into table so I can write my results nicely to csv, which can then be circulated to colleagues. I take the time to do this, so that when I go back and subset the data someone, I can instantly generate the same, nice, tailored results table. As it is often the case that you will run analysis multiple times, perhaps subsetting on years, or ages or subjects etc etc.
With lme, I found it frustrating that I couldn’t easily access coefficients and CIs. So wrote a very simple function to do this, so I can then use this output to populate my results tables.
Code is available hereĀ http://pastebin.com/TctYeggd
Example:
# lme summary library(nlme) ?lme fm1 <- lme(distance ~ age, data = Orthodont) # random is ~ age summary(fm1) coef.lme <- function(mod){ res <- data.frame( "Beta.CI" = paste(round(summary(mod)$coefficients$fixed, 3), " (",round(summary(mod)$coefficients$fixed-1.96*sqrt(diag(mod$varFix)), 2), ",", round(summary(mod)$coefficients$fixed+1.96*sqrt(diag(mod$varFix)), 2),")", sep=""), "P.value" = round(2 * pt(-abs(summary(mod)$coefficients$fixed/sqrt(diag(mod$varFix))), summary(mod)$fixDF[[1]]), 3) ) return(res) } coef(fm1) # since this is an S3 class it will know to use the '.lme' function ####### Results in > coef(fm1) Beta.CI P.value (Intercept) 16.761 (15.24,18.28) 0 age 0.66 (0.52,0.8) 0
Nice function, but why are you breaking the R convention? Using S3 classes, you should be able to write this so that coef(fm1) and coef(summary(fm1)) work properly. Just name your function coef.lme and coef.sum.lme (or coef.summary.lme, I can’t remember) and R’s object oriented system will take care of the rest.
Thanks linuxizer, definitely made an improvement to the function. Cheers.
Nicely done! Next step is to send it to the package maintainer to include in their next release :-). http://cran.r-project.org/web/packages/nlme/index.html