<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/descriptive-statistics-final-project-with-python-r/"></div></div><div><a data-url="https://www.r-bloggers.com/descriptive-statistics-final-project-with-python-r/" data-counturl="https://www.r-bloggers.com/descriptive-statistics-final-project-with-python-r/" data-count="horizontal" data-text="Descriptive Statistics Final Project with Python &amp; R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.networkx.nl/data-science/descriptive_statistics_final_project/"> R – Networkx</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img sizes="(max-width: 640px) 100vw, 640px" alt="Standard_deviation_diagram" class="wp-image-1585" src="https://i0.wp.com/www.networkx.nl/wp-content/uploads/2016/08/Standard_deviation_diagram-1024x512.png?resize=456%2C228" data-recalc-dims="1"></p>
<div>
<div>
<ul>
<li><a target="_blank" data-toggle="tab" href="http://www.networkx.nl/data-science/descriptive_statistics_final_project/#ert_pane1-0">Python</a></li>
<li><a target="_blank" data-toggle="tab" href="http://www.networkx.nl/data-science/descriptive_statistics_final_project/#ert_pane1-1">R</a></li>
</ul>
</div>
<div>
<ul>
<li>
<p></p>
<p></p>
<p><iframe height="150" width="300" src="http://www.networkx.nl/notebooks/Descriptive_Statistics_Final_Project.html"><br>
</iframe></p>
<p></p>
</li>
<li>
<p><a target="_blank" href="https://docs.google.com/document/d/1059JMJ9C5dn7vKUrmfWYle57Ai3Uk9PzxPQBGj5drjE/pub">Source</a></p>
<h4>Overview</h4>
<p>Welcome to the Descriptive Statistics Final Project! In this project, you will demonstrate what you have learned in this course by conducting an experiment dealing with drawing from a deck of playing cards and creating a writeup containing your findings. Be sure to check through the project rubric to self-assess and share with others who will give you feedback.</p>
<h4>Questions for Investigation</h4>
<p>This experiment will require the use of a standard deck of playing cards. This is a deck of fifty-two cards divided into four suits (spades (♠), hearts (<span>♥</span>), diamonds (<span>♦</span>), and clubs (♣)), each suit containing thirteen cards (Ace, numbers 2-10, and face cards Jack, Queen, and King). You can use either a physical deck of cards for this experiment or you may use a virtual deck of cards such as that found on random.org (<a target="_blank" href="http://www.random.org/playing-cards/">http://www.random.org/playing-cards/</a>). For the purposes of this task, assign each card a value: The Ace takes a value of 1, numbered cards take the value printed on the card, and the Jack, Queen, and King each take a value of 10.</p>
<ol>
<li>First, create a histogram depicting the relative frequencies of the card values.</li>
<li>Now, we will get samples for a new distribution. To obtain a single sample, shuffle your deck of cards and draw three cards from it. (You will be sampling from the deck without replacement.) Record the cards that you have drawn and the sum of the three cards’ values. Repeat this sampling procedure a total of at least thirty times.</li>
<li>Let’s take a look at the distribution of the card sums. Report descriptive statistics for the samples you have drawn. Include at least two measures of central tendency and two measures of variability.</li>
<li>Create a histogram of the sampled card sums you have recorded. Compare its shape to that of the original distribution. How are they different, and can you explain why this is the case?</li>
<li>Make some estimates about values you will get on future draws. Within what range will you expect approximately 90% of your draw values to fall? What is the approximate probability that you will get a draw value of at least 20? Make sure you justify how you obtained your values.</li>
</ol>
<h3>Load the libraries</h3>
<pre>library(ggplot2)</pre>
<h3>Create card deck</h3>
<pre>suits &lt;- c(&quot;D&quot;, &quot;C&quot;, &quot;H&quot;, &quot;S&quot;)
cards &lt;- c(&quot;A&quot;, as.character(seq(2,10)), &quot;J&quot;, &quot;Q&quot;, &quot;K&quot;)
values &lt;- c(1, 2:9, rep(10, 4))

# Build deck
deck &lt;- expand.grid(cards=cards, suits=suits)
deck$value &lt;- values
deck</pre>
<pre>##    cards suits value
## 1      A     D     1
## 2      2     D     2
## 3      3     D     3
## 4      4     D     4
## 5      5     D     5
## 6      6     D     6
## 7      7     D     7
## 8      8     D     8
## 9      9     D     9
## 10    10     D    10
## 11     J     D    10
## 12     Q     D    10
## 13     K     D    10
## 14     A     C     1
## 15     2     C     2
## 16     3     C     3
## 17     4     C     4
## 18     5     C     5
## 19     6     C     6
## 20     7     C     7
## 21     8     C     8
## 22     9     C     9
## 23    10     C    10
## 24     J     C    10
## 25     Q     C    10
## 26     K     C    10
## 27     A     H     1
## 28     2     H     2
## 29     3     H     3
## 30     4     H     4
## 31     5     H     5
## 32     6     H     6
## 33     7     H     7
## 34     8     H     8
## 35     9     H     9
## 36    10     H    10
## 37     J     H    10
## 38     Q     H    10
## 39     K     H    10
## 40     A     S     1
## 41     2     S     2
## 42     3     S     3
## 43     4     S     4
## 44     5     S     5
## 45     6     S     6
## 46     7     S     7
## 47     8     S     8
## 48     9     S     9
## 49    10     S    10
## 50     J     S    10
## 51     Q     S    10
## 52     K     S    10</pre>
<h3>1. Create a histogram depicting the relative frequencies of the card values</h3>
<p>First, create a histogram depicting the relative frequencies of the card values.</p>
<pre>ggplot(deck, aes(x=values)) + 
    geom_histogram(binwidth=1, origin=-0.5, col=&quot;white&quot;, fill=&quot;royalblue&quot;, alpha=0.5) + 
    labs(x=&quot;Value&quot;, y=&quot;Count&quot;, title=&quot;Card Value Histogram&quot;) +
    scale_x_continuous(breaks = seq(1,10)) +
    theme_bw()</pre>
<p>
<img alt="plot of chunk unnamed-chunk-3" src="https://i1.wp.com/www.networkx.nl/wp-content/uploads/2016/08/unnamed-chunk-3-1.png?w=456" data-recalc-dims="1"></p>
<h3>2. Get samples for a new distribution</h3>
<p>Now, we will get samples for a new distribution. To obtain a single sample, shuffle your deck of cards and draw three cards from it. (You will be sampling from the deck without replacement.) Record the cards that you have drawn and the sum of the three cards’ values. Repeat this sampling procedure a total of at least thirty times.</p>
<pre>df &lt;- c()
for (i in 1:5000) {
    df$value[i] &lt;- sum(sample(deck$value,3,replace = FALSE))
}
samples &lt;- data.frame(df)</pre>
<p><h3>3. Report descriptive statistics for the samples</h3>
<p>Let’s take a look at the distribution of the card sums. Report descriptive statistics for the samples you have drawn. Include at least two measures of central tendency and two measures of variability.</p>
<pre>summary(samples)</pre>

<pre>##      value      
##  Min.   : 3.00  
##  1st Qu.:16.00  
##  Median :20.00  
##  Mean   :19.55  
##  3rd Qu.:23.00  
##  Max.   :30.00</pre>
<p><h3>4. Create a histogram of the sampled card sums you have recorded</h3>
<p>Create a histogram of the sampled card sums you have recorded. Compare its shape to that of the original distribution. How are they different, and can you explain why this is the case?</p>
<pre>ggplot(samples, aes(x=value)) + 
    geom_histogram(binwidth=1, origin=-0.5, col=&quot;white&quot;, fill=&quot;royalblue&quot;, alpha=0.5) + 
    labs(x=&quot;Value&quot;, y=&quot;Count&quot;, title=&quot;3 Card Sum Histogram (n=5000)&quot;) +
    theme_bw()</pre>
<p>
<img alt="plot of chunk unnamed-chunk-6" src="https://i0.wp.com/www.networkx.nl/wp-content/uploads/2016/08/unnamed-chunk-6-1.png?w=456" data-recalc-dims="1"></p>
<h3>5. Make some estimates about values you will get on future draws</h3>
<p>Make some estimates about values you will get on future draws. Within what range will you expect approximately 90% of your draw values to fall? What is the approximate probability that you will get a draw value of at least 20? Make sure you justify how you obtained your values.</p>
<h4>a. Within what range will you expect approximately 90% of your draw values to fall?</h4>
<pre>quantile(samples$value, probs = seq(.05, .95, 0.9))</pre>
<pre>##  5% 95% 
##  11  28</pre>
<h4>b. Approximate probability that you will get a draw value of at least 20?</h4>
<p>Calculate the z score</p>
<p><img title="Rendered by QuickLaTeX.com" alt="$Z=\frac{X-\mu}{\sigma}$" src="https://i1.wp.com/www.networkx.nl/wp-content/ql-cache/quicklatex.com-0d71844aa865cb9d30becf8bb8ceb76e_l3.png?resize=65%2C19" data-recalc-dims="1"></p>
<pre>atleast &lt;- 20
mean &lt;- mean(samples$value)
sd &lt;- sd(samples$value)
z &lt;- (atleast-mean)/sd
z</pre>

<pre>## [1] 0.08467613</pre>
<p>
We could lookup the value in the <a target="_blank" href="https://s3.amazonaws.com/udacity-hosted-downloads/ZTable.jpg">Z score table</a>, but I want to calculate it.</p>
<p>Convert the Z score to a p-value with the Cumulative Distribution Function. We want to find the probability is larger than the given number so we use the <code>lower.tail=FALSE</code>.</p>
<p><img title="Rendered by QuickLaTeX.com" alt="$F(x) = 1 - P(X &lt;= x)$" src="https://i1.wp.com/www.networkx.nl/wp-content/ql-cache/quicklatex.com-7daa1f995b5e1ec471d407a0a9d0ab54_l3.png?resize=165%2C18" data-recalc-dims="1"></p>
<pre>cdf &lt;- pnorm(z, lower.tail=FALSE)
cdf</pre>

<pre>## [1] 0.4662594</pre>
<p>
As we can see the probability that we will get a draw value of at least 20 is <strong>0.4662594</strong>.</p>
</li>
</ul>
</div>
</div>
<p>The post <a target="_blank" rel="nofollow" href="http://www.networkx.nl/data-science/descriptive_statistics_final_project/">Descriptive Statistics Final Project with Python &amp; R</a> appeared first on <a target="_blank" rel="nofollow" href="http://www.networkx.nl/">Networkx</a>.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/descriptive-statistics-final-project-with-python-r/"></div></div><div><a data-url="https://www.r-bloggers.com/descriptive-statistics-final-project-with-python-r/" data-counturl="https://www.r-bloggers.com/descriptive-statistics-final-project-with-python-r/" data-count="vertical" data-text="Descriptive Statistics Final Project with Python &amp; R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.networkx.nl/data-science/descriptive_statistics_final_project/"> R – Networkx</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/TzKyJiEpAlg"></a></div>