This is an R Markdown document that you can easily export to HTML, PDF, and MS Word formats. For more information on R Markdown, see <http://rmarkdown.rstudio.com>.
# Buffon's Needle
When you click on the button **Knit**, the document will be compiled in order to re-execute the R code and to include the results into the final document. As we have shown in the video, R code is inserted as follows:
Applying the method of [Buffon's needle](https://en.wikipedia.org/wiki/Buffon%27s_needle_problem), we get the **approximation**
```{r cars}
```{r}
summary(cars)
set.seed(42)
N = 100000
x = runif(N)
theta = pi/2*runif(N)
2/(mean(x+sin(theta)>1))
```
```
It is also straightforward to include figures. For example:
# Using a surface fraction argument
```{r pressure, echo=FALSE}
plot(pressure)
```
Note the parameter `echo = FALSE` that indicates that the code will not appear in the final version of the document. We recommend not to use this parameter in the context of this MOOC, because we want your data analyses to be perfectly transparent and reproducible.
A method that is easier to understand and does not make use of the $sin$ function is based on the fact that if $X ~ U(0,1)$ and $Y ~ U(0,1)$, then $P[X^2 + Y^2 <= 1] = π/4$ (see ["Monte Carlo method" on Wikipedia](https://en.wikipedia.org/wiki/Monte_Carlo_method)). The following code uses this approach:
Since the results are not stored in Rmd files, you should generate an HTML or PDF version of your exercises and commit them. Otherwise reading and checking your analysis will be difficult for anyone else but you.
<p>This is an R Markdown document that you can easily export to HTML,
<p>My computer tells me that π is <em>approximatively</em></p>
PDF, and MS Word formats. For more information on R Markdown, see <ahref="http://rmarkdown.rstudio.com"class="uri">http://rmarkdown.rstudio.com</a>.</p>
<preclass="r"><code>pi</code></pre>
<p>When you click on the button <strong>Knit</strong>, the document will
<pre><code>## [1] 3.141593</code></pre>
be compiled in order to re-execute the R code and to include the results
</div>
into the final document. As we have shown in the video, R code is
<divid="buffons-needle"class="section level1">
inserted as follows:</p>
<h1>Buffon’s Needle</h1>
<preclass="r"><code>summary(cars)</code></pre>
<p>Applying the method of <ahref="https://en.wikipedia.org/wiki/Buffon%27s_needle_problem">Buffon’s
<pre><code>## speed dist
needle</a>, we get the <strong>approximation</strong></p>
## Min. : 4.0 Min. : 2.00
<preclass="r"><code>set.seed(42)
## 1st Qu.:12.0 1st Qu.: 26.00
N = 100000
## Median :15.0 Median : 36.00
x = runif(N)
## Mean :15.4 Mean : 42.98
theta = pi/2*runif(N)
## 3rd Qu.:19.0 3rd Qu.: 56.00
2/(mean(x+sin(theta)>1))</code></pre>
## Max. :25.0 Max. :120.00</code></pre>
<pre><code>## [1] 3.14327</code></pre>
<p>It is also straightforward to include figures. For example:</p>
<p>Note the parameter <code>echo = FALSE</code> that indicates that the
<h1>Using a surface fraction argument</h1>
code will not appear in the final version of the document. We recommend
<p>A method that is easier to understand and does not make use of the
not to use this parameter in the context of this MOOC, because we want
<spanclass="math inline">\(sin\)</span> function is based on the fact
your data analyses to be perfectly transparent and reproducible.</p>
that if <spanclass="math inline">\(X ~ U(0,1)\)</span> and <spanclass="math inline">\(Y ~ U(0,1)\)</span>, then <spanclass="math inline">\(P[X^2 + Y^2 <= 1] = π/4\)</span> (see <ahref="https://en.wikipedia.org/wiki/Monte_Carlo_method">“Monte Carlo
<p>Since the results are not stored in Rmd files, you should generate an
method” on Wikipedia</a>). The following code uses this approach:</p>
HTML or PDF version of your exercises and commit them. Otherwise reading
<preclass="r"><code>set.seed(42)
and checking your analysis will be difficult for anyone else but
N = 1000
you.</p>
df = data.frame(X = runif(N), Y = runif(N))
<p>Now it’s your turn! You can delete all this information and replace