将 `emmeans::joint_tests()` 输出中的 p 值格式保留在针织 pdf 中

Posted

技术标签:

【中文标题】将 `emmeans::joint_tests()` 输出中的 p 值格式保留在针织 pdf 中【英文标题】:Keep p-value format from the `emmeans::joint_tests()` output in a knitted pdf 【发布时间】:2021-06-28 18:11:03 【问题描述】:

这个问题可能与knitr's kable is printing 2.29e-30 as "0"有关

我正在将我的 .Rmd 编织成 .pdf。 我想打印来自下面emmeans::joint_tests 的p.values,即< 0.0001

library(emmeans); library(kableExtra)

data("trees")

model <- lm( Volume ~ Girth*Height, data=trees ) 

joint_tests(model)


model term   df1 df2 F.ratio p.value

 Girth          1  27 438.245 <.0001 
 Height         1  27  36.959 <.0001 
 Girth:Height   1  27  30.512 <.0001 

但如果我将kable 包裹在我的桌子周围,原始格式就会消失。如何告诉kable 保留joint_tests 输出的原始格式?

kable(joint_tests(model))
model  term          df1    df2 F.ratio p.value
1      Girth           1    27  438.245 0.0e+00
3      Height          1    27  36.959  1.7e-06
2      Girth:Height    1    27  30.512  7.5e-06

根据@rawr 的建议,下面的代码解决了格式问题,但在表格中添加了一行

kable(read.table(text = paste(gsub('model', '', capture.output(joint_tests(model))), collapse = '\n')))

V1  V2  V3  V4  V5
term    df1 df2 F.ratio p.value
Girth   1   27  438.245 <.0001
Height  1   27  36.959  <.0001
Girth:Height    1   27  30.512  <.0001

【问题讨论】:

这些答案对我来说没有意义,听起来你想要&lt;0.001 而不是更多的数字。该模型使用emmeans:::print.summary_emm 打印,它格式化pvalues 但返回原始对象。 kable(read.table(text = paste(gsub('model', '', capture.output(joint_tests(model))), collapse = '\n'))) 呢? 谢谢。是的,您的代码解决了我的p.value 格式问题,但它在表格顶部添加了一行(V1 V2 V3 V4 V5)。 header = TRUE 添加到read.table 有一个xtable 方法可能会有所帮助 我会看看这个,看看print.summary_emm 方法中是否有一个合理的选项来导出格式化的表格。显然,我很注意合理地格式化结果,我讨厌看指数格式的 P 值。 【参考方案1】:

最简单的蛮力解决方案,

kable(joint_tests(model), digits = Inf)

虽然它打印了太多个数字...

【讨论】:

谢谢。我并不是要让 kable 打印太多数字,只是joint_tests 给我的数字,在这种情况下是“ 【参考方案2】:

好的。我刚刚为emmGrid 对象的print 方法添加了一个export 参数。它返回一个带有summaryannotations 元素的list。示例:

> noise.lm = lm(noise/10 ~ size * type * side, data = auto.noise)
> EMM = emmeans(noise.lm, ~ size * side | type)
> xprt = print(pairs(EMM), export = TRUE)

> xprt
$summary
$summary$`type = Std`
 contrast    estimate   SE      df   t.ratio   p.value  
 "S L - M L" "  -1.500" "0.312" "24" " -4.811" "0.0009 "
 "S L - L L" "   7.167" "0.312" "24" " 22.984" "<.0001 "
 "S L - S R" "   1.833" "0.312" "24" "  5.880" "0.0001 "
 "S L - M R" "  -0.667" "0.312" "24" " -2.138" "0.3023 "
 "S L - L R" "   4.833" "0.312" "24" " 15.501" "<.0001 "
 "M L - L L" "   8.667" "0.312" "24" " 27.795" "<.0001 "
 "M L - S R" "   3.333" "0.312" "24" " 10.690" "<.0001 "
 "M L - M R" "   0.833" "0.312" "24" "  2.673" "0.1182 "
 "M L - L R" "   6.333" "0.312" "24" " 20.312" "<.0001 "
 "L L - S R" "  -5.333" "0.312" "24" "-17.105" "<.0001 "
 "L L - M R" "  -7.833" "0.312" "24" "-25.123" "<.0001 "
 "L L - L R" "  -2.333" "0.312" "24" " -7.483" "<.0001 "
 "S R - M R" "  -2.500" "0.312" "24" " -8.018" "<.0001 "
 "S R - L R" "   3.000" "0.312" "24" "  9.621" "<.0001 "
 "M R - L R" "   5.500" "0.312" "24" " 17.639" "<.0001 "

$summary$`type = Octel`
 contrast    estimate   SE      df   t.ratio   p.value  
 "S L - M L" "   0.333" "0.312" "24" "  1.069" "0.8887 "
 "S L - L L" "   6.000" "0.312" "24" " 19.243" "<.0001 "
 "S L - S R" "   0.500" "0.312" "24" "  1.604" "0.6042 "
 "S L - M R" "   0.333" "0.312" "24" "  1.069" "0.8887 "
 "S L - L R" "   5.000" "0.312" "24" " 16.036" "<.0001 "
 "M L - L L" "   5.667" "0.312" "24" " 18.174" "<.0001 "
 "M L - S R" "   0.167" "0.312" "24" "  0.535" "0.9941 "
 "M L - M R" "   0.000" "0.312" "24" "  0.000" "1.0000 "
 "M L - L R" "   4.667" "0.312" "24" " 14.967" "<.0001 "
 "L L - S R" "  -5.500" "0.312" "24" "-17.639" "<.0001 "
 "L L - M R" "  -5.667" "0.312" "24" "-18.174" "<.0001 "
 "L L - L R" "  -1.000" "0.312" "24" " -3.207" "0.0389 "
 "S R - M R" "  -0.167" "0.312" "24" " -0.535" "0.9941 "
 "S R - L R" "   4.500" "0.312" "24" " 14.432" "<.0001 "
 "M R - L R" "   4.667" "0.312" "24" " 14.967" "<.0001 "

$annotations
[1] "P value adjustment: tukey method for comparing a family of 6 estimates"

您必须手动导出xprt$summary 的每个部分,例如通过knitr::kable(xprt$summary[[1]])lapply(xprt$summary, knitr::kable)

这将很快被推送到 GitHub。

【讨论】:

非常感谢您的宝贵时间。它还不适合我。 Rmarkdown 不编织。我试过knitr::kable(xprt$summary[[1]])kableExtra::kable(xprt$summary[[1]]),但是Rmardown 抛出这个错误! Package array Error: Empty preamble: l' used.``。我加载了emmeansknitrkableExtratinytex。我有 tinytex 作为错误的建议。 Error: LaTeX failed to compile testpdf.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See testpdf.log for more info. Execution halted 嗯。好吧,正如您在最近更新的“基础”小插图中看到的那样,我确实让它与 html 一起工作。 非常感谢您的宝贵时间。我现在对xtable 的建议很满意。也许随着其他相关包的更新,事情会落到他们的位置。

以上是关于将 `emmeans::joint_tests()` 输出中的 p 值格式保留在针织 pdf 中的主要内容,如果未能解决你的问题,请参考以下文章

将自己的博客园,打造成个人知乎

如何将thinkcmf导入eclipse

如何将Ios文件上传到

Javascript 将正则表达式 \\n 替换为 \n,将 \\t 替换为 \t,将 \\r 替换为 \r 等等

如何将视频文件转换格式

sh 一个将生成CA的脚本,将CA导入到钥匙串中,然后它将创建一个证书并与CA签名,然后将其导入到