Stephen Thackeray | 2 May 09:55
Picon
Favicon
Gravatar

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

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

Christoph Meyer | 2 May 11:06
Picon
Favicon

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

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

Stephen Thackeray | 2 May 11:26
Picon
Favicon
Gravatar

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)

Griffith.Michael | 2 May 15:38
Picon

[R-sig-eco] segmented quantile regression and rqss


We have been investigating quantile regression for developing stressor -
response relationships from stream monitoring data.  I would like to add
a segmented model to our analyses to look for thresholds in these
relationships.  I have figured out how to fit a model composed of
multiple linear segments using rqss.  Koenker gives an example in the
vignette for Quantile Regression in R.  However, I would like to fit a 2
- segment model.  Can anyone give me pointers on how this might be done?

Michael

Michael B. Griffith, Ph.D.
Research Ecologist

USEPA, NCEA (MS A-110)
26 W. Martin Luther King Dr.
Cincinnati, OH  45268

telephone:  513 569-7034
e-mail:  griffith.michael@...
Stephen B. Cox | 2 May 16:33
Picon

Re: [R-sig-eco] segmented quantile regression and rqss

Not sure if this is what you are looking for, but just try something like:

m = nlrq(y~g0+g1*x*(x<=bp) + (g1 * bp + b1 * (x-bp))*(x>bp),
data=data, tau=.9,start=list(g0=1,g1=10,b1=-1,bp=5))

where 'bp' is the breakpoint between the two segments.  Obviously -
your starting values would change to fit your needs.

HTH

Stephen

--
Stephen B. Cox
Dept. Env. Tox
TTU

On Fri, May 2, 2008 at 8:38 AM,  <Griffith.Michael@...> wrote:
>
>  We have been investigating quantile regression for developing stressor -
>  response relationships from stream monitoring data.  I would like to add
>  a segmented model to our analyses to look for thresholds in these
>  relationships.  I have figured out how to fit a model composed of
>  multiple linear segments using rqss.  Koenker gives an example in the
>  vignette for Quantile Regression in R.  However, I would like to fit a 2
>  - segment model.  Can anyone give me pointers on how this might be done?
>
>  Michael
>
>  Michael B. Griffith, Ph.D.
(Continue reading)

Landis, R Matthew | 2 May 17:40
Favicon

Re: [R-sig-eco] segmented quantile regression and rqss

A simpler option might be to try the lsp function in Design.  It seems to get along with quantreg OK. For
example, try:

data(engel)
attach(engel)
fit <- rq(foodexp ~ lsp(income, 3000), tau = 0.5)
plot(income, foodexp)
lines(lowess(income, fitted(fit)))

Matt Landis

****************************************************
R. Matthew Landis, Ph.D.
Dept. Biology
Middlebury College
Middlebury, VT 05753

tel.: 802.443.3484
**************************************************


>-----Original Message-----
>From: r-sig-ecology-bounces@...
>[mailto:r-sig-ecology-bounces@...] On Behalf Of
>Stephen B. Cox
>Sent: Friday, May 02, 2008 10:33 AM
>To: Griffith.Michael@...
>Cc: r-sig-ecology@...
>Subject: Re: [R-sig-eco] segmented quantile regression and rqss
>
(Continue reading)

Ruben Roa Ureta | 2 May 17:29
Picon
Favicon

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

> 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

n <- length(ASTF)

> 2) the standard error of the slope

se.slope <- summary(lm1)[2,2]

> 3) the P value

p.v <- summary(lm1)[2,4]

> 4) the R-squared value

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

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)


Gmane