<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/estimating-pi-2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://aghaynes.wordpress.com/2018/10/16/estimating-pi/"> R – Insights of a PhD</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I came across <a rel="nofollow" href="http://researchnotes.eric-a-booth.com/2018/03/estimating-pi-via-graphing-random.html" target="_blank">this post</a> which gives a method to estimate Pi by using a circle, it’s circumscribed square and (lots of) random points within said square. Booth used Stata to estimate Pi, but here’s some R code to do the same thing…</p>
<pre>x &lt;- 0.5 # center x
y &lt;- 0.5 # center y
n &lt;- 1000 # nr of pts
r &lt;- 0.5 # radius
pts &lt;- seq(0, 2 * pi, length.out = n)
plot(sin(pts), cos(pts), type = 'l', asp = 1) # test

require(sp)
xy &lt;- cbind(x + r * sin(pts), y + r * cos(pts))
sl &lt;- SpatialPolygons(list(Polygons(list(Polygon(xy)), &quot;polygon&quot;)))
plot(sl, add=FALSE, col = 'red', axes=T )


# the square
xy &lt;- cbind(c(0, 1, 1, 0), c(0, 0, 1, 1))
sq &lt;- SpatialPolygons(list(Polygons(list(Polygon(xy)), &quot;polygon&quot;)))

plot(sq, add = TRUE)

N &lt;- 1e6
x &lt;- runif(N, 0, 1)
y &lt;- runif(N, 0, 1)
sp &lt;- SpatialPoints(cbind(x, y))
plot(sp, add = TRUE, col = &quot;green&quot;)

require(rgeos)
(sim_pi &lt;- (sum(gIntersects(sp, sl, byid = TRUE))/N) *4)
sim_pi - pi</pre>
<p>Note the use of sp and rgeos packages to calculate the intersections.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://aghaynes.wordpress.com/2018/10/16/estimating-pi/"> R – Insights of a PhD</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/z-8l0jsMeL0" height="1"></a></div>