I try to get Latex and knitr to do a standardized \Sexpr{} output of statistics.
Example: I computed a correlation with
mycor<-cor.test(a,b)
Let's say the result would be:
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
How can I "teach" LaTeX to generate the output
r(54)=.37, p < .01
without having to write
$r(\Sexpr{mycor$parameter)=\Sexpr{mycor$estimate}$, $p < .05$
all the time?
I want to write a command which does this for every correlation I report. Please note that I look for a possibility to let it transform p = 0.004805 to p<.01 automatically.
BTW: I tried to do it with
\newcommand{\repcor}[1]{$r(\Sexpr{#1$parameter)=\Sexpr{#1$estimate}$}
but this doesn't work...
Thank you in advance! Benjamin
Why not just write a function to output what you want. For example, at the start of your document, have:
<<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)
}
@
Which allows \Sexpr{trans(mycor)}
to work as you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With