简体   繁体   English

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

[英]Keep p-value format from the `emmeans::joint_tests()` output in a knitted pdf

This question might be related to knitr's kable is printing 2.29e-30 as "0"这个问题可能与knitr 的 kable 将 2.29e-30 打印为“0”有关

I am knitting my .Rmd to.pdf.我正在编织我的.Rmd到.pdf。 I'd like to print p.values as are from emmeans::joint_tests below, ie < 0.0001 .我想从下面的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 

But if I wrap kable around my table, the original format disappears.但是如果我将kable包裹在我的桌子上,原来的格式就会消失。 How do I tell kable to keep the original format from the joint_tests output?如何告诉kable保留joint_tests output 的原始格式?

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

Per @rawr suggestion, the code below solved the format issue but added a line to the table根据@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

Simplest brute-force solution,最简单的蛮力解决方案,

kable(joint_tests(model), digits = Inf)

although it prints too many digits...虽然它打印了太多数字......

OK.好的。 I just added an export argument to the print method for emmGrid objects.我刚刚在emmGrid对象的print方法中添加了一个export参数。 It returns a list with a summary and and annotations element.它返回一个带有summaryannotations元素的list Example:例子:

> 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"

You have to manually export each part of xprt$summary , eg via knitr::kable(xprt$summary[[1]]) or lapply(xprt$summary, knitr::kable)您必须手动导出xprt$summary的每个部分,例如通过knitr::kable(xprt$summary[[1]])lapply(xprt$summary, knitr::kable)

This will get pushed to GitHub soon.这将很快被推送到 GitHub。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM