Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Often when writing a manuscript in using knitr and xtable I am flustered by my p-values. In simple summary tables, R conveniently rounds my p-values to be 0: a mathematically inappropriate task. A colleague recently commented on the poor reporting of my table (shown below using print.xtable with the type=”html” argument), inspiring a much needed change.

EstimateStd.errWaldPr(>|W|)
(Intercept)0.0017040.000005100409.7709560.000000
sizemedium0.0000460.00000590.5347050.000000
sizesmall0.0000030.0000050.2943310.587458
time-0.0000040.00000111.6149170.000654

The fix is actually fairly straight forward, and can be summarized in a simple function: “fixp”, with the code shown below:

fixp <- function(x, dig=3){
x <- as.data.frame(x)

if(substr(names(x)[ncol(x)],1,2) != "Pr")
warning("The name of the last column didn't start with Pr. This may indicate that p-values weren't in the last row, and thus, that this function is inappropriate.")
x[,ncol(x)] <- round(x[,ncol(x)], dig)
for(i in 1:nrow(x)){
if(x[i,ncol(x)] == 0)
x[i,ncol(x)] <- paste0("< .", paste0(rep(0,dig-1), collapse=""), "1")
}

x
}

All that’s going on: the function is pulling in the summary table (usually through a \$coef), trying to turn it into a dataframe (some already are, though some tables are numeric (e.g. lm)), throwing a warning if the last heading doesn’t begin with “Pr” (as it may not be the column that contains p-values), and editing any values that were rounded to 0 (at the user specified rounding point) to be < the smallest number that could be rounded to (e.g. <.01). Then we output the edited table, all ready for reporting! To mimic what was above, we set our digits to be equal to 6 (so go out 6 decimal places for the p-value), and re-run:

EstimateStd.errWaldPr(>|W|)
(Intercept)0.0017040.000005100409.770956< .000001
sizemedium0.0000460.00000590.534705< .000001
sizesmall0.0000030.0000050.2943310.587458
time-0.0000040.00000111.6149170.000654

Much better! Also, to report a two digit p-value (for some writing styles), we simply set dig = 2:

EstimateStd.errWaldPr(>|W|)
(Intercept)0.0017040.000005100409.770956< .01
sizemedium0.0000460.00000590.534705< .01
sizesmall0.0000030.0000050.2943310.59
time-0.0000040.00000111.614917< .01

By design, the p-values can be manipulated independent of the estimates. This allows reporting of the estimated coefficients in meaningful units (in the above example, very small units), while reporting the p-values on a scale that many writing styles request.

Want to try this yourself? Here’s an example that you can try with just a built in dataset in R:

#this gives a summary table with a small p-value. Trying to report this with xtable would results in an R rounding issue!
(mod <- coef(summary(lm(uptake ~ conc + Treatment + Type + Plant, data=CO2))))

#this fixes the p-value to 2 digits, correctly reporting p-values that would have been rounded to 0
fixp(mod,dig=2)

Here’s the final output via print.xtable (dig=2 for fixp and xtable):

EstimateStd. Errort valuePr(>|t|)
(Intercept)37.424.678.00< .01
conc0.020.007.96< .01
Treatmentchilled-12.505.10-2.450.02
TypeMississippi-23.336.01-3.88< .01
Plant.L21.5811.141.940.06
Plant.Q-4.622.27-2.030.05
Plant.C1.465.100.290.78
Plant^42.342.271.030.31
Plant^5-0.485.77-0.080.93
Plant^6-0.042.27-0.020.99
Plant^7-1.913.64-0.530.6
Plant^8-3.282.27-1.440.15
Plant^100.552.270.240.81

Limitations (ish):

1. Again, this assumes that the last column is the one to be transformed. This is by design, though may be inconvenient in some situations. If needed, the change is easily made through the definition of the function.
2. When the last column is manipulated, it becomes a character column in the dataframe. Alternatively, when it is rounded but no entry rounds to 0, it is numeric.
3. This assumes a dataframe-style format of your table. Thus, this method will NOT be effective at correcting reported p-values for an individual test: say a t-test, where only the statistic is reported (and not a table). Personally this is not a concern, as I deal with these situations in other ways, but for some users seeking an overall “p-value fixing” method this may not be the answer.

As with other functions I write posts on, this function is available in my package (creatively named “myStuff”)  via Github. If you’d like to play with the most current version of the function, I’d encourage you to check it out here. Alternatively, to have access to other fun functions, install the package directly from GitHub with the code below (requires devtools):

devtools:install_github("flor3652/myStuff")