[Rd] printing coefficients with text

Ross Boylan ross at biostat.ucsf.edu
Fri Dec 1 20:40:27 CET 2006


On Fri, Dec 01, 2006 at 10:34:45AM +0100, Martin Maechler wrote:
> >>>>> "RossB" == Ross Boylan <ross at biostat.ucsf.edu>
> >>>>>     on Thu, 30 Nov 2006 12:17:55 -0800 writes:
> 
>     RossB> I want to print the coefficient estimates of a model
>     RossB> in a way as consistent with other output in R as
>     RossB> possible. stats provides the printCoefmat function
>     RossB> for doing this, but there is one problem.  I have an
>     RossB> additional piece of textual information I want to put
>     RossB> on the line with the other info on each coefficient.
> 
> that's not a real problem, see below
> 
>     RossB> The documentation for printCoefmat says the first
>     RossB> argument must be numeric, which seems to rule this out.
> 
> it does say that (it says "x: a numeric matrix like object"
> 		  which includes data frames with factors)
> but you are right that it does preclude a column of "character".

Having gone through the code, it's clear the code itself requires all
numerics.

> 
>     RossB> I just realized I might be able to cheat by inserting
>     RossB> the text into the name of the variable (fortunately
>     RossB> there is just one item of text).  I think that's in
>     RossB> the names of the matrix given as the first argument
>     RossB> to the function.
> 
> yes; it's the rownames();
> i.e., you'd do something like
>       rownames(myx) <- paste(rownames(myx), format(mytext_var)))
> 
> which seems simple enough to me,
> but it only works when the "text" is the first column

This actually worked out great for me.

> 
>     RossB> Are there any better solutions?  Obviously I could
>     RossB> just copy the method and modify it, but that creates
>     RossB> duplicate code and loses the ability to track future
>     RossB> changes to printCoefmat.
> 
> As original author of printCoefmat(), I'm quite willing to
> accept and incorporate a patch to the current function
> definition (in https://svn.r-project.org/R/trunk/src/library/R/anova.R),
> if it's well written.
> 
> As a matter of fact, I think already see how to generalize printCoefmat()
> to work for the case of data frame with character columns

Yes, that seems as if it would be a good generalization.  However,
there is code that makes inferences based on the number of columns of
data, and I'm not sure how that should work.  Probably it should
ignore the non-numeric data.

> [I would not want a character matrix however; since that would mean
>  going numeric -> character -> numeric -> formatting (i.e character)
>  for the 'coefficients' themselves].
> 
> Can you send me a reproducible example?
You mean of an input data frame?  Or something else?  The input isn't
currently a data frame, but I could certainly make one.

Do you think generalizing to other types (factor, logical) would make
sense too?

> or at least an *.Rda file of a save()d such data frame?
> 
>     RossB> Thanks.  Ross Boylan
> 
> You're welcome,
> Martin Maechler, ETH Zurich



More information about the R-devel mailing list