Hello, For my functions I want to create output similar in appearance to that of what you get when you print a summary of lm model: Residuals: Min 1Q Median 3Q Max -0.209209 -0.043133 0.001793 0.044105 0.234750 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 0.981762 0.004089 240.103 < 2e-16 *** Factor 1 -0.009581 0.006381 -1.501 0.134296 Factor 2 -0.008993 0.009182 -0.979 0.328163 Factor 3 0.029960 0.009547 3.138 0.001866 ** Factor 4 -0.026575 0.007370 -3.606 0.000363 *** Factor 5 -0.004847 0.006382 -0.760 0.448138 Factor 6 0.005099 0.006483 0.786 0.432202 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 I want: 1) no $ before the list component names 2) component names that take values from outside variables (ex.: number <- 10 There are 'number' factors in the model:) There is not much information on how to create nice output in terms of lists, so I was looking for core to write the summary(lm) output, but could not find much. Obviously, I can type summary.lm, but it does not show how to create the name "Coefficients:" Could someone give me pointers on how to create nice lists? Thanks Sergey
Hi, I don't know if it will help you but to retrieve the slope of lm(y~x), I use : coefficients(lm(y~x))[2] ([1] for the intercept) I should not tell this but It took me a long time to find this obvious thing. Have a nice week-end, Ptit Bleu. -------------------------------------------------------- Sergey Goriatchev wrote:> > Hello, > > For my functions I want to create output similar in appearance to that > of what you get when you print a summary of lm model: > > Residuals: > Min 1Q Median 3Q Max > -0.209209 -0.043133 0.001793 0.044105 0.234750 > > Coefficients: > Estimate Std. Error t value Pr(>|t|) > (Intercept) 0.981762 0.004089 240.103 < 2e-16 *** > Factor 1 -0.009581 0.006381 -1.501 0.134296 > Factor 2 -0.008993 0.009182 -0.979 0.328163 > Factor 3 0.029960 0.009547 3.138 0.001866 ** > Factor 4 -0.026575 0.007370 -3.606 0.000363 *** > Factor 5 -0.004847 0.006382 -0.760 0.448138 > Factor 6 0.005099 0.006483 0.786 0.432202 > --- > Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > > I want: > 1) no $ before the list component names > 2) component names that take values from outside variables > (ex.: number <- 10 > There are 'number' factors in the model:) > > There is not much information on how to create nice output in terms of > lists, so I was looking for core to write the summary(lm) output, but > could not find much. Obviously, I can type summary.lm, but it does not > show how to create the name "Coefficients:" > > Could someone give me pointers on how to create nice lists? > > Thanks > Sergey > > ______________________________________________ > 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. > >-- View this message in context: http://www.nabble.com/Creating-nice-looking-lists%3A-how--tf4534730.html#a12942721 Sent from the R help mailing list archive at Nabble.com.
On Fri, 28 Sep 2007, Sergey Goriatchev wrote:> Hello, > > For my functions I want to create output similar in appearance to that > of what you get when you print a summary of lm model: > > Residuals: > Min 1Q Median 3Q Max > -0.209209 -0.043133 0.001793 0.044105 0.234750 > > Coefficients: > Estimate Std. Error t value Pr(>|t|) > (Intercept) 0.981762 0.004089 240.103 < 2e-16 *** > Factor 1 -0.009581 0.006381 -1.501 0.134296 > Factor 2 -0.008993 0.009182 -0.979 0.328163 > Factor 3 0.029960 0.009547 3.138 0.001866 ** > Factor 4 -0.026575 0.007370 -3.606 0.000363 *** > Factor 5 -0.004847 0.006382 -0.760 0.448138 > Factor 6 0.005099 0.006483 0.786 0.432202 > --- > Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > > I want: > 1) no $ before the list component names > 2) component names that take values from outside variables > (ex.: number <- 10 > There are 'number' factors in the model:) > > There is not much information on how to create nice output in terms of > lists, so I was looking for core to write the summary(lm) output, but > could not find much. Obviously, I can type summary.lm, but it does not > show how to create the name "Coefficients:"page( stats:::print.summary.lm, 'print' ) is what you want. It is called when auto-printing class "summary.lm" objects. Some print methods for summary methods do a lot of calculation, and I sometimes find it confusing that those calculations are not included in the summary nethod and spend a lot of time searching thru summary.whatnot till I wake up and remember to look at print.summary.whatnot. Chuck> > Could someone give me pointers on how to create nice lists? > > Thanks > Sergey > > ______________________________________________ > 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
The source code for print.summary.lm and summary.lm is in the file /src/library/stats/R/lm.R of the R source code, which you can download from CRAN if you don't have it already. The file lm.R is also at https://svn.r-project.org/R/trunk/src/library/stats/R/lm.R On Fri, 28 Sep 2007, Sergey Goriatchev wrote:> Hello, > > For my functions I want to create output similar in appearance to that > of what you get when you print a summary of lm model: > > Residuals: > Min 1Q Median 3Q Max > -0.209209 -0.043133 0.001793 0.044105 0.234750 > > Coefficients: > Estimate Std. Error t value Pr(>|t|) > (Intercept) 0.981762 0.004089 240.103 < 2e-16 *** > Factor 1 -0.009581 0.006381 -1.501 0.134296 > Factor 2 -0.008993 0.009182 -0.979 0.328163 > Factor 3 0.029960 0.009547 3.138 0.001866 ** > Factor 4 -0.026575 0.007370 -3.606 0.000363 *** > Factor 5 -0.004847 0.006382 -0.760 0.448138 > Factor 6 0.005099 0.006483 0.786 0.432202 > --- > Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > > I want: > 1) no $ before the list component names > 2) component names that take values from outside variables > (ex.: number <- 10 > There are 'number' factors in the model:) > > There is not much information on how to create nice output in terms of > lists, so I was looking for core to write the summary(lm) output, but > could not find much. Obviously, I can type summary.lm, but it does not > show how to create the name "Coefficients:" > > Could someone give me pointers on how to create nice lists? > > Thanks > Sergey > > ______________________________________________ > 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. >
Others have shown you where to find the actual printing code for print.summary.lm, but the short answer is: Use the cat function for the general text (the word "coefficients:" and the signif codes at the bottom) And put the actual coefficients in a matrix, use colnames and rownames to add column and row headers, then use print.matrix with quote=FALSE to print out the matrix of coefficients. For more detailed printing look at ?cat, ?format, and ?sprintf. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Sergey Goriatchev > Sent: Friday, September 28, 2007 8:03 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Creating nice looking lists: how? > > Hello, > > For my functions I want to create output similar in > appearance to that of what you get when you print a summary > of lm model: > > Residuals: > Min 1Q Median 3Q Max > -0.209209 -0.043133 0.001793 0.044105 0.234750 > > Coefficients: > Estimate Std. Error t value Pr(>|t|) > (Intercept) 0.981762 0.004089 240.103 < 2e-16 *** > Factor 1 -0.009581 0.006381 -1.501 0.134296 > Factor 2 -0.008993 0.009182 -0.979 0.328163 > Factor 3 0.029960 0.009547 3.138 0.001866 ** > Factor 4 -0.026575 0.007370 -3.606 0.000363 *** > Factor 5 -0.004847 0.006382 -0.760 0.448138 > Factor 6 0.005099 0.006483 0.786 0.432202 > --- > Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > > I want: > 1) no $ before the list component names > 2) component names that take values from outside variables > (ex.: number <- 10 > There are 'number' factors in the model:) > > There is not much information on how to create nice output in > terms of lists, so I was looking for core to write the > summary(lm) output, but could not find much. Obviously, I can > type summary.lm, but it does not show how to create the name > "Coefficients:" > > Could someone give me pointers on how to create nice lists? > > Thanks > Sergey > > ______________________________________________ > 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. >