使用\ Sexpr进行测试统计的标准化输出

use*_*485 5 automation latex r knitr output

我试图让Latex和knitr做一个标准化的\ Sexpr {}输出统计数据.

示例:我计算了相关性

mycor<-cor.test(a,b)
Run Code Online (Sandbox Code Playgroud)

假设结果如下:

Pearson's product-moment correlation

data:  a and b
t = 2.9413, df = 54, p-value = 0.004805
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.1204668 0.5780293
sample estimates:
cor
0.3715985
Run Code Online (Sandbox Code Playgroud)

如何"教"LaTeX生成输出

r(54)=.37,p <.01

无需写

$r(\Sexpr{mycor$parameter)=\Sexpr{mycor$estimate}$, $p < .05$
Run Code Online (Sandbox Code Playgroud)

每时每刻?

我想写一个命令,它为我报告的每个相关性执行此操作.请注意,我希望能够自动将p = 0.004805转换为p <.01.

顺便说一句:我试过这样做

\newcommand{\repcor}[1]{$r(\Sexpr{#1$parameter)=\Sexpr{#1$estimate}$}
Run Code Online (Sandbox Code Playgroud)

但这不起作用......

先感谢您!本杰明

csg*_*pie 5

为什么不写一个函数来输出你想要的东西.例如,在文档的开头,有:

<<echo=FALSE, tidy=FALSE>>=
trans = function(mycor) {

  out1 = paste0("$r(", mycor$parameter, ") = ", signif(mycor$estimate, 2), "$,")

  p = mycor$p.value
  cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1) 
  round_p = cut(p, cutpoints, labels=cutpoints[-1])
  p_value = paste0("$p < ", round_p, "$"  )

  paste(out1, p_value)
}
@
Run Code Online (Sandbox Code Playgroud)

这允许\Sexpr{trans(mycor)}你想要的工作.