Hello World! Here’s a Normal Distribution!

[This article was first published on Dan Garmat's Blog -- R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This code simulates Normal(0,1), and this visualization shows smaller samples can vary much more than large samples from the true distribution. Maybe it’s not a fascinating picture although there is a deep mystery or two in there. Can we know the truth? Isn’t everything we know based on a sample? Is everything we believe, like these three rnorm(), an incomplete story?

random simulations output

<span class="n">require</span><span class="p">(</span><span class="n">tidyverse</span><span class="p">)</span><span class="w">
</span><span class="n">require</span><span class="p">(</span><span class="n">tidyr</span><span class="p">)</span><span class="w">

</span><span class="n">random_simulations_1</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tibble</span><span class="p">(</span><span class="n">rnorm</span><span class="p">(</span><span class="m">100000</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">gather</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">rename</span><span class="p">(</span><span class="n">distribution</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">observed</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">value</span><span class="p">)</span><span class="w">

</span><span class="n">random_simulations_2</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tibble</span><span class="p">(</span><span class="n">rnorm</span><span class="p">(</span><span class="m">1000</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">gather</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">rename</span><span class="p">(</span><span class="n">distribution</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">observed</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">value</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">bind_rows</span><span class="p">(</span><span class="n">random_simulations_1</span><span class="p">)</span><span class="w">

</span><span class="n">random_simulations</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tibble</span><span class="p">(</span><span class="n">rnorm</span><span class="p">(</span><span class="m">10</span><span class="p">)</span><span class="c1">#, rnorm(100)  </span><span class="w">
                             </span><span class="c1">#runif(100)#,  </span><span class="w">
                             </span><span class="c1">#rhyper(100, 100, 50, 10), </span><span class="w">
                             </span><span class="c1">#rbinom(100, 10, .5)</span><span class="w">
                             </span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">gather</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">rename</span><span class="p">(</span><span class="n">distribution</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">observed</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">value</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">bind_rows</span><span class="p">(</span><span class="n">random_simulations_2</span><span class="p">)</span><span class="w">

</span><span class="c1"># note we've repeated three times, time for a function</span><span class="w">
</span><span class="c1"># also note there are other distributions to try this on</span><span class="w">
</span><span class="c1"># and really, it may ne nice to simulate a few pulls of </span><span class="w">
</span><span class="c1">#  the same size</span><span class="w">

</span><span class="n">ggplot</span><span class="p">(</span><span class="n">random_simulations</span><span class="p">,</span><span class="w"> </span><span class="n">aes</span><span class="p">(</span><span class="n">observed</span><span class="p">,</span><span class="w"> 
    </span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">as.factor</span><span class="p">(</span><span class="n">distribution</span><span class="p">)))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_density</span><span class="p">(</span><span class="n">alpha</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0.2</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> 
  </span><span class="n">labs</span><span class="p">(</span><span class="n">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Simulate N(0,1)"</span><span class="p">)</span><span class="w">

</span>

To leave a comment for the author, please follow the link and comment on their blog: Dan Garmat's Blog -- R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)