[R-sig-eco] Extracting information from lm results

Glen A Sargeant gsargeant at usgs.gov
Fri May 2 18:33:50 CEST 2008


Steve,

This goes a bit beyond your question, but you may have trouble 
shoe-horning quantities of interest into data frames (coefficient vectors, 
for example, may not be of equal length).  Perhaps you should consider 
using lists both to facilitate efficient coding and as more suitable 
structures for holding results.

Pseudo-code below helps illustrate the general idea.

Glen

#Function to extract a couple of quantities from a model
foo <- function(lm){
  coef <- coef(lm)
  sum. <- summary(lm)
  r.2 <- sum.$r.squared
  output <- data.frame(coef, r.2)
  output
}

###

#List of 3 fitted models:
model.list <- list(lm1, lm2, lm3)

#Use lapply to produce a list; each component
#of the list will itself be a list with 2
#components, coef and r.2; number of coef
#can vary among models

results <- lapply(model.list, foo)

###

#You can extract values from results

results$lm1$coef

###

#You also can use lapply on your resutlts; 
#the following example creates a list with 
#1 component per model; each component contains 
#only model coefficients; numbers of coefficients 
#may vary among models

#First you need an extraction function
fee <- function(lst){
lst$coef}

#Now use lapply
list.of.coefficients <- lapply(results, fee)

#If you create a list with compatible components, 
#you can use this utility to combine them in a
#data frame:

list.to.frame <- function (df.list) 
{
    df = df.list[[1]]
    for (i in 2:length(df.list)) {
        df = rbind(df, df.list[[i]])
    }
    df
}


*************************************************
Glen A. Sargeant, Ph.D.
Research Wildlife Biologist/Statistician
Northern Prairie Wildlife Research Center
8711 37th Street SE
Jamestown, ND  58401

Phone: (701) 253-5528
E-mail:  glen_sargeant at usgs.gov
FAX:     (701) 253-5553
*************************************************



"Stephen Thackeray" <sjtr at ceh.ac.uk> 
Sent by: r-sig-ecology-bounces at r-project.org
05/02/2008 04:26 AM

To
<sjtr at ceh.ac.uk>, <christoph.meyer at uni-ulm.de>
cc
r-sig-ecology at r-project.org
Subject
Re: [R-sig-eco] Extracting information from lm results






Dear all, 

Thank you very much for all of your suggestions! You've all been very 
helpful.

I'll go and try some of your code out!

all the best

Steve



Dr Stephen Thackeray 
Lake Ecosystem Group
CEH Lancaster
Lancaster Environment Centre
Library Avenue
Bailrigg
Lancaster
LA1 4AP

Email: sjtr at ceh.ac.uk
Tel: +44 (0) 1524 595852
Fax: +44 (0) 1524 61536

Disclaimer: Any views or opinions expressed in this mail or any files 
transmitted with it are those of the author and do not represent the views 
of NERC unless otherwise explicitly stated. The information contained in 
this email may be subject to public disclosure under the Freedom of 
Information Act 2000. Unless the information is legally exempt from 
disclosure, the confidentiality of this e-mail and your reply cannot be 
guaranteed.

>>> Christoph Meyer <christoph.meyer at uni-ulm.de> 10:06 02/05/2008 >>>
Hi Steve,

you can extract this information from the summary of your linear
regressions, i.e. summary(lm1).

e.g.

sum.lm1=summary(lm1)
sum.lm1$coef[2,2]    #this gives you the SE of the slope
sum.lm1$r.squared    #this gives you the R2
and so on...
This should be clear from a look at str(sum.lm1).

Hope that helps,

Christoph




Friday, May 2, 2008, 9:55:10 AM, you wrote:

> Dear all,

> I suspect that this might be a bit basic, but here goes anyway...

> I am soon to run a large number of linear regressions and I would
> like to extract a number of details from the models, and then
> collate them in a dataframe as a summary of the overall block of
> analyses. I can successfully extract the intercept and slope by using, 
for example:

> lm1<-lm(ASTF~Year,na.action=na.omit,subset=yr10==T)
> a1<-lm1$coefficients[1]
> b1<-lm1$coefficients[2]
> out1<-cbind("ASTF","1996-2005",lm1$coefficients[1],lm1$coefficients[2])

> However, I also would like to extract the following too:

> 1) the number of data points in the analysis, n
> 2) the standard error of the slope
> 3) the P value
> 4) the R-squared value

> Is it possible to extract these parameters in the same way as the
> slope and intercept, to save a lot of typing?

> Any help much appreciated!

> Steve Thackeray



> Dr Stephen Thackeray 
> Lake Ecosystem Group
> CEH Lancaster
> Lancaster Environment Centre
> Library Avenue
> Bailrigg
> Lancaster
> LA1 4AP

> Email: sjtr at ceh.ac.uk 
> Tel: +44 (0) 1524 595852
> Fax: +44 (0) 1524 61536

> Disclaimer: Any views or opinions expressed in this mail or any
> files transmitted with it are those of the author and do not
> represent the views of NERC unless otherwise explicitly stated. The
> information contained in this email may be subject to public
> disclosure under the Freedom of Information Act 2000. Unless the
> information is legally exempt from disclosure, the confidentiality
> of this e-mail and your reply cannot be guaranteed.



***************************************************************
Dr. Christoph Meyer
Institute of Experimental Ecology
University of Ulm
Albert-Einstein-Allee 11
D-89069 Ulm
Germany
Phone:  ++49-(0)731-502-2675
Fax:    ++49-(0)731-502-2683
Mobile: ++49-(0)1577-156-7049
E-mail: christoph.meyer at uni-ulm.de 
http://www.uni-ulm.de/nawi/nawi-bio3.html 
***************************************************************



-- 
This message (and any attachments) is for the recipient ...{{dropped:8}}



More information about the R-sig-ecology mailing list