Hello R-Experts, I am facing the problem that I have to estimate several parameters for a lot of different dependent variables. One single regression looks something like this: y = beta0 + beta1 * x1 + beta2 * x2 + beta3 * x1 * x2 + beta4 * x4 + beta5 * lag(x4,-1) where y is the dependent variable and xi are the independent ones. Important to me are the different estimates of betai and their respective p-values only. Now I have aprx. 50 different data sets of y and x1 to x4. So for each data set I need the respective estimators and their p-values. Do I really have to type in each single regression for each data set and copy the output into a table manually? Isn't it possible to get an efficient output like the following? beta0(data set 1) | P-Value(beta0, data set 1) | beta1(data set 1) | P-Value(beta1, data set 1)| .... | P-Value(beta5, data set n) Or at least getting a vector with all the estimates of beta0 for each data set, another vector with all estimates of beta1 for each data set, and a matrix with P-values for each betai of each data set? The file containing the observations for xi and y for each set of data can be adjusted to any kind of format. Thank you very much in advance. With kind regards Christian -- View this message in context: http://r.789695.n4.nabble.com/Customized-R-Regression-Output-tp4631497.html Sent from the R help mailing list archive at Nabble.com.
Dear Christian, An illustrative and simple example follows:> y<-rnorm(100,0,1) #response variable > x1<-rnorm(100,0,1) #1st covariate > x2<-rnorm(100,0,1) #2nd covariate > > lm1<-lm(y~x1+x2) #fitting a linear model > cbind(summary(lm1)$coef[,1],summary(lm1)$coef[,1]) # obtaining the > estimates and related p-values[,1] [,2] (Intercept) -0.02997559 -0.02997559 x1 -0.06170968 -0.06170968 x2 -0.12740465 -0.12740465 Here, the 1st column corresponds t? the estimates and 2nd one correspnds to the related p-values. I think you can easily adapt this to your datasets and combine muliple results of multiple datasets. Best Ozgur ----- ************************************ Ozgur ASAR Research Assistant Middle East Technical University Department of Statistics 06531, Ankara Turkey Ph: 90-312-2105309 http://www.stat.metu.edu.tr/people/assistants/ozgur/ -- View this message in context: http://r.789695.n4.nabble.com/Customized-R-Regression-Output-tp4631497p4631499.html Sent from the R help mailing list archive at Nabble.com.
Sorry for my typo, Last line of the R script should be>cbind(summary(lm1)$coef[,1],summary(lm1)$coef[,4]) # obtaining theestimates and related p-values Best Ozgur ----- ************************************ Ozgur ASAR Research Assistant Middle East Technical University Department of Statistics 06531, Ankara Turkey Ph: 90-312-2105309 http://www.stat.metu.edu.tr/people/assistants/ozgur/ -- View this message in context: http://r.789695.n4.nabble.com/Customized-R-Regression-Output-tp4631497p4631501.html Sent from the R help mailing list archive at Nabble.com.
Hello,> > Do I really have to type in each single regression for each data set and > copy the output into a table manually? >No, using paste/as.formula you can do it in a loop. Example: x1 <- 1:100 x2 <- log(x1^2) y1 <- x1 + x2 + rnorm(100) y2 <- x1*runif(100, 0.5, 1.0) + x2 + rnorm(100) predictors <- c("x1", "x2") responses <- c("y1", "y2") fmla <- paste(responses, paste(predictors, collapse="+"), sep="~") model <- vector("list", length(fmla)) for(i in seq_along(fmla)) model[[i]] <- lm(as.formula(fmla[i])) summary(model[[1]]) As for making a table of results, something like estimate <- coef(summary(model[[2]]))[, 1] p.value <- coef(summary(model[[2]]))[, 4] cbind(estimate, p.value) Hope this helps, Rui Barradas Chris87 wrote> > Hello R-Experts, > > I am facing the problem that I have to estimate several parameters for a > lot of different dependent variables. > > One single regression looks something like this: > > y = beta0 + beta1 * x1 + beta2 * x2 + beta3 * x1 * x2 + beta4 * x4 + beta5 > * lag(x4,-1) > > where y is the dependent variable and xi are the independent ones. > Important to me are the different estimates of betai and their respective > p-values only. Now I have aprx. 50 different data sets of y and x1 to x4. > So for each data set I need the respective estimators and their p-values. > Do I really have to type in each single regression for each data set and > copy the output into a table manually? > > Isn't it possible to get an efficient output like the following? > > beta0(data set 1) | P-Value(beta0, data set 1) | beta1(data set 1) | > P-Value(beta1, data set 1)| .... | P-Value(beta5, data set n) > > > Or at least getting a vector with all the estimates of beta0 for each data > set, another vector with all estimates of beta1 for each data set, and a > matrix with P-values for each betai of each data set? > > The file containing the observations for xi and y for each set of data can > be adjusted to any kind of format. > > Thank you very much in advance. > > With kind regards > Christian >-- View this message in context: http://r.789695.n4.nabble.com/Customized-R-Regression-Output-tp4631497p4631503.html Sent from the R help mailing list archive at Nabble.com.
Dear Rui and Ozgur Thanks to both of you for your swift and helpful responses! The idea with the loop is great and the cbind() function is also what I am looking for. However, is it also possible to summarize the estimators and p-values for each data set y in one table only? For example, something like estimate p.value (Intercept) -0.8698107 1.445023e-01 x1 0.9991003 1.539969e-108 x2 1.1200786 3.691776e-14 estimate p.value (Intercept) -1.6826360 7.523808e-01 x1 0.6564982 1.632763e-14 x2 1.7081632 1.358624e-01 Is it even possible to call the R^2 of each regression? In the end I need to provide a table like this (for ~50 data sets): Intercept P-Value Beta 1 P-Value R2 data set1 ... ... ... ... ... data set2 ... ... ... ... ... Is an output like this even possible? Thank you once more for your kind help. Best, Chris -- View this message in context: http://r.789695.n4.nabble.com/Customized-R-Regression-Output-tp4631497p4631505.html Sent from the R help mailing list archive at Nabble.com.
Your welcome, To extract R squared values, you can use summary(lm1)$r.squared and include it in the cbind as result1<-cbind(summary(lm1)$coef[,1],summary(lm1)$coef[,1],summary(lm1)$r.squared) To add the results of multiple outputs you can use rbind such as rbind(result1,result2) where result2 is the output of the second model. Best Ozgur ----- ************************************ Ozgur ASAR Research Assistant Middle East Technical University Department of Statistics 06531, Ankara Turkey Ph: 90-312-2105309 http://www.stat.metu.edu.tr/people/assistants/ozgur/ -- View this message in context: http://r.789695.n4.nabble.com/Customized-R-Regression-Output-tp4631497p4631507.html Sent from the R help mailing list archive at Nabble.com.
No. Your advice is incorrect. See below. On Sun, May 27, 2012 at 7:42 AM, ?zg?r Asar <oasar at metu.edu.tr> wrote:> Your welcome, > > To extract R squared values, you can use > > summary(lm1)$r.squared > > and include it in the cbind as > > result1<-cbind(summary(lm1)$coef[,1],summary(lm1)$coef[,1],summary(lm1)$r.squared)use ?c to concatenate. cbind is for matrix-like structures. c(summary(lm1)$coef[,1],summary(lm1)$coef[,1],summary(lm1)$r.squared)> > To add the results of multiple outputs you can use rbind such as > > rbind(result1,result2)No. This will most likely fail. rbind is for matrix-like structures. Use ?list to combine possibly different structures into a single entity. I suggest that both the OP and the responder spend some time with "An Introduction to R" or other basic R tutorial of their choice where matters like this are discussed before posting further to this list Cheers, Bert> > where result2 is the output of the second model. > > Best > Ozgur > > > ----- > ************************************ > Ozgur ASAR > > Research Assistant > Middle East Technical University > Department of Statistics > 06531, Ankara Turkey > Ph: 90-312-2105309 > http://www.stat.metu.edu.tr/people/assistants/ozgur/ > -- > View this message in context: http://r.789695.n4.nabble.com/Customized-R-Regression-Output-tp4631497p4631507.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
Are the x variables all the same? if so, you can give lm a matrix as the y variable and it will compute all the different regressions for each column in the y matrix. The summary function will then return a list with the summary information for each of the regressions. If the x variables are not identical between regressions then you could stack all your data sets in a data frame and use the lmList function from the nlme package to do all the regressions in one step and get a list of returns. In either case you can then use the sapply function to extract the information you want from each summary and it will combine them into a matrix for you (or use the plyr package for a little more control). On Sun, May 27, 2012 at 6:20 AM, Chris87 <c.satzky at gmail.com> wrote:> Hello R-Experts, > > I am facing the problem that I have to estimate several parameters for a lot > of different dependent variables. > > One single regression looks something like this: > > y = beta0 + beta1 * x1 + beta2 * x2 + beta3 * x1 * x2 + beta4 * x4 + beta5 * > lag(x4,-1) > > where y is the dependent variable and xi are the independent ones. Important > to me are the different estimates of betai and their respective p-values > only. ?Now I have aprx. 50 different data sets of y and x1 to x4. So for > each data set I need the respective estimators and their p-values. Do I > really have to type in each single regression for each data set and copy the > output into a table manually? > > Isn't it possible to get an efficient output like the following? > > beta0(data set 1) | P-Value(beta0, data set 1) | beta1(data set 1) | > P-Value(beta1, data set 1)| .... | P-Value(beta5, data set n) > > > Or at least getting a vector with all the estimates of beta0 for each data > set, another vector with all estimates of beta1 for each data set, and a > matrix with P-values for each betai of each data set? > > The file containing the observations for xi and y for each set of data can > be adjusted to any kind of format. > > Thank you very much in advance. > > With kind regards > Christian > > -- > View this message in context: http://r.789695.n4.nabble.com/Customized-R-Regression-Output-tp4631497.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com