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: [hidden email] 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. -- This message (and any attachments) is for the recipient ...{{dropped:6}} _______________________________________________ R-sig-ecology mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-ecology |
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: [hidden email] > 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: [hidden email] http://www.uni-ulm.de/nawi/nawi-bio3.html _______________________________________________ R-sig-ecology mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-ecology |
In reply to this post by Stephen Thackeray
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: [hidden email] 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 <[hidden email]> 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: [hidden email] > 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: [hidden email] http://www.uni-ulm.de/nawi/nawi-bio3.html *************************************************************** -- This message (and any attachments) is for the recipient ...{{dropped:6}} _______________________________________________ R-sig-ecology mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-ecology |
In reply to this post by Stephen Thackeray
> 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 r.squ <- summary(lm1)$r.squared HTH Ruben _______________________________________________ R-sig-ecology mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-ecology |
In reply to this post by Stephen Thackeray
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: [hidden email] FAX: (701) 253-5553 ************************************************* "Stephen Thackeray" <[hidden email]> Sent by: [hidden email] 05/02/2008 04:26 AM To <[hidden email]>, <[hidden email]> cc [hidden email] 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: [hidden email] 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 <[hidden email]> 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: [hidden email] > 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: [hidden email] http://www.uni-ulm.de/nawi/nawi-bio3.html *************************************************************** -- This message (and any attachments) is for the recipient ...{{dropped:8}} _______________________________________________ R-sig-ecology mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
Glen Sargeant
Research Wildlife Biologist |
In reply to this post by Stephen Thackeray
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/ # create an empty list mod <- list() # start a loop for create 5 objects of class 'lm' for (i in 1:5) { x <- rnorm(i*10) y <- rnorm(i*10) mod[[paste("run",i,sep="")]] <- lm(y ~ x) } # let's have a look at the models mod # summary for one element summary(mod$run1) # see description of value in help: ?summary.lm # and also how its display works: getAnywhere(print.summary.lm) # your requests # 1) the number of data points in the analysis, n length(mod$run1$model$y) # 2) the standard error of the slope summary(mod$run1)$coefficients[2,2] # 3) the P value (for coefficients and for the F statistic, respectively) summary(mod$run1)$coefficients[,4] pf(summary(mod$run1)$fstatistic[1], summary(mod$run1)$fstatistic[2], summary(mod$run1)$fstatistic[3], lower.tail = FALSE) # 4) the R-squared value summary(mod$run1)$r.squared # to extract these from a list of lm models, better to write a function # (you can modify this accordingly): myFun <- function(lm) { out <- c(lm$coefficients[1], lm$coefficients[2], length(lm$model$y), summary(lm)$coefficients[2,2], pf(summary(lm)$fstatistic[1], summary(lm)$fstatistic[2], summary(lm)$fstatistic[3], lower.tail = FALSE), summary(lm)$r.squared) names(out) <- c("intercept","slope","n","slope.SE","p.value","r.squared") return(out)} # now let's see myFun(mod$run1) # and for a list of lm models: results <- list() for (i in 1:length(mod)) results[[names(mod)[i]]] <- myFun(mod[[i]]) as.data.frame(results) # for a more generic solution, you should have look at the stamp function in the reshape package: library(reshape) ?stamp # example from the stamp help: french_fries$time <- as.numeric(as.character(french_fries$time)) stamp(french_fries, subject ~ ., function(df) coef(lm(painty ~ time, df))[2]) stamp(french_fries, subject ~ treatment, function(df) coef(lm(painty ~ time, df))[2]) models <- stamp(french_fries, subject ~ ., function(df) lm(painty ~ time, df)) dim(models) anova(models[[3,1]]) On Fri, May 2, 2008 at 12:00 PM, <[hidden email]> wrote: > Message: 1 > Date: Fri, 02 May 2008 08:55:10 +0100 > From: "Stephen Thackeray" <[hidden email]> > Subject: [R-sig-eco] Extracting information from lm results > To: <[hidden email]> > Message-ID: <[hidden email]> > Content-Type: text/plain; charset=US-ASCII > > 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: [hidden email] > 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. > > _______________________________________________ R-sig-ecology mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-ecology |
Free forum by Nabble | Edit this page |