formatters: Formatter Functions

formattersR Documentation

Formatter Functions

Description

Functions to format various types of outputs. \loadmathjax

Usage

fmtp(p, digits=4, pname="", equal=FALSE, sep=FALSE, add0=FALSE, quote=FALSE)
fmtp2(p, cutoff=c(0.001,0.06), pname="p", sep=TRUE, add0=FALSE, quote=FALSE)
fmtx(x, digits=4, flag="", quote=FALSE, ...)
fmtt(val, tname, df, df1, df2, pval, digits=4,
     pname="p-val", format=1, sep=TRUE, quote=FALSE, call=FALSE, ...)

Arguments

Arguments for fmtp and fmtp2:

p

vector of p-values to be formatted.

digits

integer to specify the number of decimal places to which the values should be rounded. For fmmt, can be a vector of length 2, to specify the number of digits for the test statistic and the p-value, respectively.

pname

string to add as a prefix to the p-value (e.g., something like "p-val" or "p").

equal

logical to specify whether an equal symbol should be shown before the p-value (when it is larger than the rounding cutoff).

sep

logical to specify whether a space should be added between pname, the equal/lesser symbol, and the p-value.

add0

logical to specify whether a 0 should be shown before the decimal point (for fmtp, this only applies when the p-value is below the rounding cutoff).

quote

logical to specify whether formatted strings should be quoted when printed.

cutoff

numeric vector giving the cutoff values.

Arguments specific for fmtx:

x

vector of numeric values to be formatted.

flag

a character string giving a format modifier as defined for formatC.

Arguments specific for fmtt:

val

test statistic value to be formatted.

tname

character string for the name of the test statistic.

df

optional value for the degrees of freedom of the test statistic.

df1

optional value for the numerator degrees of freedom of the test statistic.

df2

optional value for the denominator degrees of freedom of the test statistic.

pval

the p-value corresponding to the test statistic.

format

either 1 or 2 to denote whether the degrees of freedom should be given before the test statistic (in parentheses) or after the test statistic.

call

logical to specify whether the formatted test result should be returned as a call or not.

...

other arguments.

Details

The fmtp function takes one or multiple p-values as input and rounds them to the chosen number of digits. For p-values that are smaller than 10^(-digits) (e.g., 0.0001 for digits=4), the value is shown to fall below this bound (e.g., <.0001). One can further customize the way the output of the values is formatted via the pname, equal, sep, add0, and quote arguments.

The fmtp2 function is an alternative function to format p-values, which yields output that essentially matches APA style guidelines. Values that fall below the first cutoff are printed as such (e.g., a p-value of .00002 would be printed as p < .001), values that fall in between the first and second cutoff are printed as exact p-values with the number of digits determined by the first cutoff (e.g., a p-value of .01723 would be printed as p = .017), and values falling above the second cutoff are printed as exact p-values with the number of digits determined by the second cutoff (e.g., a p-value of .08432 would be printed as p = .08). Note that the second cutoff is by default .06 to show that p-values in the range of .051 and .054 are above .05.

The fmtx function takes one or multiple numeric values as input and rounds them to the chosen number of digits, without using scientific notation and without dropping trailing zeros (using formatC).

The fmtt function takes a single test statistic value as input (and, if applicable, its degrees of freedom via argument df or its numerator and denominator degrees of freedom via arguments df1 and df2) and the corresponding p-value and formats it for printing. Two different formats are available (chosen via the format argument), one giving the degrees of freedom before the test statistic (in parentheses) and one after the test statistic.

Value

A character vector with the formatted values. By default (i.e., when quote=FALSE), formatted strings are not quoted when printed.

Note

The option in fmtt to return the formatted test result as a call can be useful when adding the output to a plot with text and one would like to use plotmath formatting for tname.

Author(s)

Wolfgang Viechtbauer (wvb@metafor-project.org, https://www.metafor-project.org).

References

Viechtbauer, W. (2010). Conducting meta-analyses in R with the metafor package. Journal of Statistical Software, 36(3), 1–48. ⁠https://doi.org/10.18637/jss.v036.i03⁠

Examples

# examples for fmtp()
fmtp(c(.0002, .00008), quote=TRUE, digits=4)
fmtp(c(.0002, .00008), quote=TRUE, digits=4, equal=TRUE)
fmtp(c(.0002, .00008), quote=TRUE, digits=4, equal=TRUE, sep=TRUE)
fmtp(c(.0002, .00008), quote=TRUE, digits=4, equal=TRUE, sep=TRUE, add0=TRUE)

# example for fmtp2()
fmtp2(c(.0005, .001, .002, .0423, .0543, .0578, .0623, .5329), quote=TRUE)

# examples for fmtx()
fmtx(c(1.0002, 2.00008, 3.00004), digits=4)
fmtx(c(-1, 1), digits=4)
fmtx(c(-1, 1), digits=4, flag=" ")

# examples for fmtt()
fmtt(2.45, "z", pval=0.01429, digits=2)
fmtt(3.45, "z", pval=0.00056, digits=2)
fmtt(2.45, "t", df=23, pval=0.02232, digits=2)
fmtt(3.45, "t", df=23, pval=0.00218, digits=2)
fmtt(3.45, "t", df=23, pval=0.00218, digits=2, format=2)
fmtt(46.23, "Q", df=29, pval=0.0226, digits=2)
fmtt(46.23, "Q", df=29, pval=0.0226, digits=2, format=2)
fmtt(8.75, "F", df1=2, df2=35, pval=0.00083, digits=c(2,3))
fmtt(8.75, "F", df1=2, df2=35, pval=0.00083, digits=c(2,3), format=2, pname="p")
fmtt(8.75, "F", df1=2, df2=35, pval=0.00083, digits=c(2,3), format=2, pname="p", sep=FALSE)

wviechtb/metafor documentation built on Dec. 4, 2024, 1:03 a.m.