[R] printCoefmat() for a data.frame with factors

Michael Friendly friendly at yorku.ca
Fri Nov 19 16:53:44 CET 2010


I want to use something like printCoefmat() in a print.summary method to 
print a more nicely formatted version
of the result from a summary method, but where the estimates may be 
cross-classified by one or more factors.

However, printCoefmat() assumes that the labels for the parameters are 
the rownames of the object, and
prints factors as integers. With one factor, I could just assign that as 
the rownames, but with >1,
I would be forced to paste() them together, giving a less readable 
display. Perhaps printCoefmat()
needs another argument for this case (factor.ind= indices of column 
numbers which are factors, or
stringsAsFactors=TRUE), or maybe there is some other method I could use.

Here is an example:

 > summary(lor.Emp)
EmploymentLength LayoffCause Log Odds Ratio Std. Error z value Pr(>|z|)
1 <1Mo:1-3Mo Closure -0.04082199 0.5143769 -0.07936203 0.468372334
2 1-3Mo:3-12Mo Closure 0.02353050 0.2782868 0.08455486 0.466307643
3 3-12Mo:1-2Yr Closure 0.04904020 0.2327390 0.21070895 0.416557193
4 1-2Yr:2-5Yr Closure -0.07555132 0.2468528 -0.30605823 0.379780157
5 2-5Yr:>5Yr Closure -0.26157903 0.2943756 -0.88858949 0.187111872
6 <1Mo:1-3Mo Replaced -0.19415601 0.3173846 -0.61173729 0.270355791
7 1-3Mo:3-12Mo Replaced -0.77994333 0.2546200 -3.06316597 0.001095043
8 3-12Mo:1-2Yr Replaced -0.18513757 0.3192303 -0.57994984 0.280974223
9 1-2Yr:2-5Yr Replaced 0.19521478 0.3422052 0.57046111 0.284182498
10 2-5Yr:>5Yr Replaced -0.24791884 0.3971645 -0.62422200 0.266240900

 > str(summary(lor.Emp))
'data.frame': 10 obs. of 6 variables:
$ EmploymentLength: Factor w/ 5 levels "<1Mo:1-3Mo","1-3Mo:3-12Mo",..: 1 
2 3 4 5 1 2 3 4 5
$ LayoffCause : Factor w/ 2 levels "Closure","Replaced": 1 1 1 1 1 2 2 2 2 2
$ Log Odds Ratio : num -0.0408 0.0235 0.049 -0.0756 -0.2616 ...
$ Std. Error : num 0.514 0.278 0.233 0.247 0.294 ...
$ z value : num -0.0794 0.0846 0.2107 -0.3061 -0.8886 ...
$ Pr(>|z|) : num 0.468 0.466 0.417 0.38 0.187 ...

Result from printCoefmat() is considerably worse:

 > printCoefmat(summary(lor.Emp))
EmploymentLength LayoffCause Log Odds Ratio Std. Error z value Pr(>|z|)
[1,] 1.000000 1.000000 -0.040822 0.514377 -0.0794 0.468372
[2,] 2.000000 1.000000 0.023530 0.278287 0.0846 0.466308
[3,] 3.000000 1.000000 0.049040 0.232739 0.2107 0.416557
[4,] 4.000000 1.000000 -0.075551 0.246853 -0.3061 0.379780
[5,] 5.000000 1.000000 -0.261579 0.294376 -0.8886 0.187112
[6,] 1.000000 2.000000 -0.194156 0.317385 -0.6117 0.270356
[7,] 2.000000 2.000000 -0.779943 0.254620 -3.0632 0.001095 **
[8,] 3.000000 2.000000 -0.185138 0.319230 -0.5799 0.280974
[9,] 4.000000 2.000000 0.195215 0.342205 0.5705 0.284182
[10,] 5.000000 2.000000 -0.247919 0.397165 -0.6242 0.266241
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
 >

-- 
Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept.
York University      Voice: 416 736-5115 x66249 Fax: 416 736-5814
4700 Keele Street    Web:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA



More information about the R-help mailing list