打印p值<0.001

时间:2014-04-11 16:48:05

标签: r latex knitr sweave rnw

我想知道如果p值小于<0.001以便在0.001中使用,如何设置Sweave符号。任何帮助将受到高度赞赏。感谢

ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
summary(lm.D9)$coef

           Estimate Std. Error  t value     Pr(>|t|)
(Intercept)   4.8465  0.1557174 31.12368 4.185248e-17
group1       -0.1855  0.1557174 -1.19126 2.490232e-01

所需输出

           Estimate Std. Error  t value     Pr(>|t|)
(Intercept)   4.8465  0.1557174 31.12368   <0.001
group1       -0.1855  0.1557174 -1.19126    0.249

1 个答案:

答案 0 :(得分:9)

我使用了两个主要功能format.pval,这是我从gforge中扯下并调整过来的。

ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
tmp <- data.frame(summary(lm.D9)$coef)
tmp <- setNames(tmp, colnames(summary(lm.D9)$coef))
tmp[ , 4] <- format.pval(tmp[ , 4], eps = .001, digits = 2)

tmp
#             Estimate Std. Error  t value Pr(>|t|)
# (Intercept)    5.032  0.2202177 22.85012   <0.001
# groupTrt      -0.371  0.3114349 -1.19126     0.25

我喜欢这个,因为它从pvalues中删除了精度&gt; .1(或者你喜欢的任何阈值,如果你想要不同的东西;也就是说,无论digits如何,如果值为&gt; .1),它只保留两个小数位,保持尾随零(见下面的例子),并添加&lt;就像你想要一定程度的精度(这里是0.001)。

pvalr <- function(pvals, sig.limit = .001, digits = 3, html = FALSE) {

  roundr <- function(x, digits = 1) {
    res <- sprintf(paste0('%.', digits, 'f'), x)
    zzz <- paste0('0.', paste(rep('0', digits), collapse = ''))
    res[res == paste0('-', zzz)] <- zzz
    res
  }

  sapply(pvals, function(x, sig.limit) {
    if (x < sig.limit)
      if (html)
        return(sprintf('&lt; %s', format(sig.limit))) else
          return(sprintf('< %s', format(sig.limit)))
    if (x > .1)
      return(roundr(x, digits = 2)) else
        return(roundr(x, digits = digits))
  }, sig.limit = sig.limit)
}

例子:

pvals <- c(.133213, .06023, .004233, .000000134234)
pvalr(pvals, digits = 3)
# [1] "0.13"    "0.060"   "0.004"   "< 0.001"