3

I´m writing my thesis in LaTeX (using Overleaf) and I analyzed some data in R. So I have a R script where I perform a bunch of tests and regressions. Now I want to e.g. include the t-statistic and p-value in the Text.

x <- rnorm(10, 180, 10)
y <- rnorm(10, 150, 10)

Our Hypothesis that x is taller than y was accepted (t =t.test(x , y)$statisic; p = t.test(x, y)$p.value).

S.t. In the final pdf it says:

Our Hypothesis that x is taller than y was accepted (t =-7.8841, p = 0.000).

What is the easiest way to do this? What is the workflow?

I`d be thankful for any advice using LaTeX. Even more so for advice for Overleaf.

  • I can think of 2 suggestions: datatool package if you are willing to save some of the results inside a file and read into your LaTeX code later. Another way is write18 which you might be able to run a shell command with that. However, if you are using overleaf as a online editor, only the first suggestion might worth trying. – Majid Abdolshah Mar 27 at 23:13
  • The sagetex package uses open source computer algebra system SAGE which includes R, see here. If you don't mind using Cocalc then you have access to SAGE, which should let you run R, and insert the results into your LaTeX document, similar to my answer here which used SAGE but not R in calculating descriptive statistics. Sagetex documentation here. – DJP Mar 27 at 23:33
  • First page of sagetex document says "Similarly, the R statistical computing environment includes Sweave, which lets you do the same thing with R code and LATEX". So maybe worth looking into Sweave. Maybe something like this? – DJP Mar 27 at 23:39
  • Welcome to TeX.SE. Use knitr like this: <tex.stackexchange.com/a/481061> – Ross Mar 27 at 23:48
  • thanks i made it work with knitr. How can i format what is printed in text. e.g. from scientific totation to normal with 3 decimals? – MoRitz Mar 28 at 12:07
3

A example code is worth a thousand words:

\documentclass{article}
\begin{document}
<<echo=F,results="hide">>=
set.seed(100)
options(scipen=0)
x <- rnorm(10, 155, 10)
y <- rnorm(10, 150, 10)
accept <- if (t.test(x,y)$p.value>0.05) {print("rejected")} else {print("accepted")}
@
Our hypothesis that $x$ is taller than $y$ was \Sexpr{accept}  (%
t=\Sexpr{round(t.test(x,y)$statistic,2)}; 
p=\Sexpr{signif(t.test(x,y)$p.value,2)}).
\end{document}

Save it with the extension .Rtex in Overleaf or .Rnw if you are using Rstudio. Of course, in this case you should have installed R and the knitr package to convert the .Rnw file to a true LaTeX file (.tex) and a LaTeX distribution to produce the .tex to .pdf conversion. In any case the result will be:

Our hypothesis that x is taller than y was rejected (t=0.76; p=0.46).

Or, replacing "155" by "180" (6th row):

Our hypothesis that x is taller than y was accepted (t=8.46; p=3.4 × 10⁷).

1

It works with the knitr package, in overleaf you dont have to load it.

Everything inside

<<echo= F>>=
#Code goes here
d <- read.csv("data.csv")

@

is R code. If you create objects in a chunk like this you can print and manipulate those in text with \Sexpr{head(d)}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.