Péter Sólymos | 2 May 19:43
Picon

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

Hi Stephen and all,

your question is a good point to highlight (or recall) come of the
most basic and powerful features of R: it is an object oriented
language.
This means, that it might be useful for users to look into different
object classes, and their summary() and print() methods.

Here I paste some code that I hope might help to understand what I
mean. But for this particular problem, when you have a time series (if
I am right), the use of LM is not necessarily the best choice, because
of the non-independence of the data. You might consider to use the
nlme or lme4 packages and explicitly define autoregressive correlation
structure.

Anyway, here the code goes!

Cheers,

Peter

--

-- 
Peter Solymos, PhD
Institute for Biology
Faculty of Veterinary Science
Szent Istvan University, Hungary
http://www.univet.hu/users/psolymos/personal/

mefa R package
http://mefa.r-forge.r-project.org/
(Continue reading)

Stephen Thackeray | 2 May 11:26
Picon
Favicon

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@...
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@...> 10:06 02/05/2008 >>>
(Continue reading)

Glen A Sargeant | 2 May 18:33
Picon
Favicon

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

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
(Continue reading)

Mike Dunbar | 2 May 11:00
Picon
Favicon

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

Hi Stephen

You should be able to find what you need either using str(lm1) or str(summary(lm1)). With the possible
exception of the overall P value, which you can get from the F statistic and the degrees of freedom using the
pf function. For the coefficients, its considered slightly better form to use coef(lm1)[x] rather than
lm1$coefficients[x], simply because there is the coef extractor function provided.

cheers

Mike Dunbar

>>> "Stephen Thackeray" <sjtr@...> 02/05/2008 08:55:10 >>>
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
(Continue reading)


Gmane