Keith Matthews Exam Question…

[This article was first published on Adventures in Data, 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.

Help? I set an exam question with 10 boxes to be filled from 13 possible answers. If randomly completed, what should be the average mark?
– @KeithRMatthews, 12:13 PM – 15 Dec 2016

<span class="n">library</span><span class="p">(</span><span class="n">ggplot2</span><span class="p">)</span><span class="w">
</span><span class="n">answers</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nb">letters</span><span class="p">[</span><span class="m">1</span><span class="o">:</span><span class="m">10</span><span class="p">]</span><span class="w">
</span><span class="n">options</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nb">letters</span><span class="p">[</span><span class="m">1</span><span class="o">:</span><span class="m">13</span><span class="p">]</span><span class="w">
</span>

functions to give random answers and mark them

<span class="n">giveAnswers</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">options</span><span class="p">)</span><span class="w"> </span><span class="n">sample</span><span class="p">(</span><span class="n">options</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">10</span><span class="p">,</span><span class="w"> </span><span class="n">replace</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">FALSE</span><span class="p">)</span><span class="w">
</span><span class="n">markAnswers</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">correct</span><span class="p">,</span><span class="w"> </span><span class="n">given</span><span class="p">)</span><span class="w"> </span><span class="nf">sum</span><span class="p">(</span><span class="n">given</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">correct</span><span class="p">)</span><span class="w">
</span>

collect scores

<span class="n">scores</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">numeric</span><span class="p">(</span><span class="m">1e5L</span><span class="p">)</span><span class="w">
</span><span class="k">for</span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="nf">seq_along</span><span class="p">(</span><span class="n">scores</span><span class="p">))</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">scores</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">markAnswers</span><span class="p">(</span><span class="n">answers</span><span class="p">,</span><span class="w"> </span><span class="n">giveAnswers</span><span class="p">(</span><span class="n">options</span><span class="p">))</span><span class="w">
</span><span class="p">}</span><span class="w">
</span>

averages

<span class="n">mean</span><span class="p">(</span><span class="n">scores</span><span class="p">)</span><span class="w">
</span>
## [1] 0.766
<span class="n">median</span><span class="p">(</span><span class="n">scores</span><span class="p">)</span><span class="w">
</span>
## [1] 1

plot

<span class="n">ggplot</span><span class="p">(</span><span class="n">data.frame</span><span class="p">(</span><span class="n">score</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">scores</span><span class="p">),</span><span class="w"> </span><span class="n">aes</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">scores</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_bar</span><span class="p">(</span><span class="n">stat</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"count"</span><span class="p">)</span><span class="w">
</span>

plot of chunk unnamed-chunk-5

To leave a comment for the author, please follow the link and comment on their blog: Adventures in Data.

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)