1

I have read the help file for format.pval and was wondering if this is a right tool to output numbers with proper format as far as significant digits are concerned

In this post:

R / Sweave formatting numbers with \Sexpr{} in scientific notation

a solution was proposed and I wonder if the function format.pval is already here to do this.

And I guess that we can just use it in Sweave as \Sexpr{format.pval(a value/variable here)} together with options for the number of significant digits.

Thanks a lot...

Community
  • 1
  • 1
yCalleecharan
  • 4,656
  • 11
  • 56
  • 86
  • 1
    `format.pval` uses `format` which is more general in purpose. So you should probably use `format`, `prettyNum` or `formatC` instead. – Andrie Dec 09 '11 at 07:30
  • @ Andrie Thanks. Can you consider converting your comment in a a solution here? 1 vote up. – yCalleecharan Dec 09 '11 at 07:36

4 Answers4

3

I have included that functionality in the knitr package, which makes knitr really smart now -- it automatically uses $a \times 10^b$ if your \Sexpr{} produces a number and it is either too big or too small (if you output HTML instead of TeX, it automatically uses a &times; 10<sup>b</sup>). You can probably start switching from Sweave to knitr now: http://yihui.github.com/knitr/

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
1

I think format.pval should probably not be used. We (statisticians) generally don't use conventional scientific notation for $p$-values because the leading 0s are also a reflection of precision. Thus we use total digits. You will find this in most scholarly publications.

For instance, reporting three digits means "trace" values are reported with "<0.001" as in the greatest precision afforded is to the thousands-th place. If I specify digits=3 and eps=0.001, a p-value of 0.0040523 is reported as 0.00405 suggesting my precision is to the hundred-thousandsth place when 0.004 would have been desired.

I use the following little wrapper:

format.p <- function(p, precision=0.001) {
  digits <- -log(precision, base=10)
  p <- formatC(p, format='f', digits=digits)
  p[p == formatC(0, format='f', digits=digits)] <- paste0('<', precision)
  p
}
AdamO
  • 4,283
  • 1
  • 27
  • 39
1

format.pval is specifically designed to format printed p.values, for example when viewing the output of lm.

The workhorse for format.pval is format which is more general in purpose. So you should probably use format or its cousins - prettyNum and formatC instead.

Footnote: for formatting dates (POSIXct or POSIXlt) you need the date formatting function strptime

Andrie
  • 176,377
  • 47
  • 447
  • 496
0

To be conservative, a p-value of 0.32312 should be rounded to 0.324, not 0.323. Here's my solution:

format.p = function(p, precision=0.001) {
  digits = -log(precision, base=10)
  p = formatC(ceiling(p/precision)*precision,format = 'f', digits=digits)
  p[p == formatC(0, format='f', digits=digits)] = paste0('<', precision)
  p
}