FitterFormat()
A fitter (better) general formatter. Converts numeric X objects, including matrices, to character objects with elements having the same reasonable number of decimal places. P-values are handled appropriately, too, e.g. 0.0001234 and 0.99912 are by default transformed to <0.001 and >0.999, respectively. Values may also be re-expressed using scientific notation with the form "1.2 x 10^-8" rather than R's customary "1.23456e-08". See the many examples.
FitterFormat() is included within these Rfuncs: HDquantile(), Toothpics(), WMWodds()
A fitter (better) general formatter. Converts numeric X objects, including matrices, to character objects with elements having the same reasonable number of decimal places. P-values are handled appropriately, too, e.g. 0.0001234 and 0.99912 are by default transformed to <0.001 and >0.999, respectively. Values may also be re-expressed using scientific notation with the form "1.2 x 10^-8" rather than R's customary "1.23456e-08". See the many examples.
- Current version released 5 May 2018, to change one line of documentation.
- Download FitterFormat.Rfunc180505.R
FitterFormat() is included within these Rfuncs: HDquantile(), Toothpics(), WMWodds()
Arguments
X
X
- numeric object to be converted to character object. May have multiple elements, including being a matrix.
- number of digits to right of decimal point. D=NA auto-determines D to give 3 significant digits for X < 1000. 123.123 becomes "123"; 12.123 becomes "12.1"; 0.00012345 becomes "0.000123"; 1234.123 becomes "1234" (not "1230").
- = minimum number of spaces to use. Any non-NA value causes all elements have the same number of spaces.
- Center=FALSE will shift to right: "___12.3" (Spaces=7). Center=TRUE will center: "__12.3_" (Spaces=7).
- For D=NA, converts values <0.001 and >0.999 to "<0.001" and ">0.999". If D=4, then converts values <0.0001 and >0.9999 to "<0.0001" and ">0.9999".
- converts to scientific notation. R would normally print 0.000012345 as "1.2345e-08". "1.2345" is the mantissa. "e-07" means "multiply by 10^-8". FitterFormat(0.000012345) returns "1.2 x 10^-8". D= refers to the mantissa, with D=1 as the default. If D=2, then 1.2345e-08 becomes "1.23 x 10^-8", which, in effect, is 3 significant digits.
Object Returned
- A character object (unnamed) isomorphic to X but with elements reformatted. See examples.
Simple Examples
Basic Functionality
Basic Functionality
> a1 <- 1.23456789; a2 <- 123.456789; a3 <- 123456.789
> FitterFormat(a1)
[1] "1.23"
> cat(FitterFormat(a1))
1.23
> print(FitterFormat(a1),quote=F)
[1] 1.23
> cat(FitterFormat(a2))
123
> cat(FitterFormat(a3))
123457
> cat(FitterFormat(c(a1,a2,a3)))
1.23 123.46 123456.79
> cat(FitterFormat(c(a2,a3)))
123 123457
> cat(FitterFormat(c(a1,a2,a3),D=1))
1.2 123.5 123456.8
> FitterFormat(c(a1,a2,a3),D=1,Spaces=10)
[1] " 1.2" " 123.5" " 123456.8"
> FitterFormat(c(a1,a2,a3),D=1,Spaces=10,Center=T)
[1] " 1.2 " " 123.5 " " 123456.8 "
> cat(FitterFormat(c(a1,a2,a3),D=1,Spaces=10,Center=T))
1.2 123.5 123456.8
P-values
> p <- c(0.12345, 0.000123, 0.000012345, 0.9991234, 0.99992345)
> cat(FitterFormat(p))
0.1234500 0.0001230 0.0000123 0.9991234 0.9999235
> cat(FitterFormat(p, Pval=T))
0.123 <0.001 <0.001 >0.999 >0.999
> cat(FitterFormat(p, Pval=T,D=4))
0.1235 0.0001 <0.0001 0.9991 >0.9999
Scientific Notation
> y <- c(123456789.1234, -123456789.1234, 0.000000012345, -0.000000012345)
> print(FitterFormat(y,SciNotation=TRUE))
[1] "1.2 x 10^8" "-1.2 x 10^8" "1.2 x 10^-8" "-1.2 x 10^-8"
> print(FitterFormat(y,SciNotation=TRUE,D=2), quote=FALSE)
[1] 1.23 x 10^8 -1.23 x 10^8 1.23 x 10^-8 -1.23 x 10^-8
Matrices
> m <- matrix(c(123.45, 2231.43, 123, 97.4),nrow=2)
> rownames(m) <- c("Alpha","Beta"); colnames(m) <- c(" Green"," Red")
> print(FitterFormat(m,Spaces=7), quote=F, justify="right")
Green Red
Alpha 123.5 123.0
Beta 2231.4 97.4
> print(FitterFormat(m,Spaces=7,D=0), quote=F, justify="right")
Green Red
Alpha 123 123
Beta 2231 97
Substantive Example
FitterFormat() helps produce more useful and lucid output. Often, values from core R functions need to be tranformed before printing.
For example, in logistic regression, the beta coefficients are often exponentiated so that they become odds ratios. Suppose the X values are -2, -1, 0, 1, and 2, and odds(x) refers to the odds of Y=1 versus Y=0 when X=x. The model is log(odds(x)) = logit(x) = beta.0 + beta.1*x. If beta.0 = log(0.60) and beta.1 = log(1.5), then logit(0) = log(0.60), or odds(0) = exp(log(0.60)) = 0.60. It is easy to show that the odds ratio is odds(x)/odds(x-1) = exp(log(1.5)) = 1.5.
> x <- rep(-2:2,times=c(1,2,3,2,2)*10)
> logit.y.eq.1 <- log(.60) + log(1.5)*x # true odds ratio (per unit x) = 1.5
> prob.y.eq.1 <- 1/(1 + exp(-logit.y.eq.1))
> set.seed(1233)
> y <- rbinom(length(x),1, prob.y.eq.1)
> (glmfit <- glm(y~x, family=binomial))
<snip>
Coefficients:
(Intercept) x
-0.6384 0.5066
<snip>
> CI95 <- confint(glmfit)[2,]
Waiting for profiling to be done...
> {
+ cat("\nEstimated Odds Ratio Per Unit X:\n",
+ FitterFormat(exp(glmfit$coefficients[2])))
+ cat("\n\n95% CI:\n")
+ print(FitterFormat(exp(CI95)),quote=F)
+ }
Estimated Odds Ratio Per Unit X:
1.66
95% CI:
2.5 % 97.5 %
1.18 2.41