<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/from-static-to-numeric-to-single-choice-exercises-in-rexams/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exams.org/tutorials/static_num_schoice/"> R/exams</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Idea</h2>
<p>Our colleagues over at the economics department became interested in using <a rel="nofollow" href="http://www.r-exams.org/" target="_blank">R/exams</a> for<br>
generating large-scale exams in their introductory economics courses. However, they face the challenge that<br>
so far they had been writing static exercises and modified them <em>by hand</em> if they wanted to reuse them in a<br>
different exam in another semester. To let R/exams do this job it is illustrated how a static arithmetic<br>
exercise can be turned into a <a rel="nofollow" href="http://www.r-exams.org/intro/dynamic/" target="_blank">dynamic exercise template</a> either in <code>num</code><br>
format with a numeric solution or into <code>schoice</code> format with a single-choice solution.<br>
The idea for the exercise is a very basic <em>price elasticity of demand</em> task:</p>
<table>
<tbody>
<tr>
<td>Consider the following inverse demand function:<br>

<p> for the price<br>

<p> given the demanded quantity<br>

<p>. What is the price elastiticy of demand at a price of<br>

<p>?



<p>The natural candidates for “parameterizing” this exercise are the<br>
price<br>

<p>and the parameters<br>

<p>and<br>

<p> of the inverse demand function.<br>
Based on these the solution is simply:</p>
<table>
<tbody>
<tr>
<td>First, we obtain the demand function by inverting the inverse demand function:<br>

<p>. <br> Then, at<br>

<p> the price elasticity of demand is<br>





<h2>Overview</h2>
<p>Below various incarnations of this exercise are provided in both R/Markdown <code>Rmd</code> and R/LaTeX <code>Rnw</code> format.<br>
The following table gives a brief overview of all available versions along with a short description of the idea behind it.<br>
More detailed explanations are provided in the subsequent sections.</p>
<table>
<thead>
<tr>
<th>#</th>
<th>Exercise templates</th>
<th>Dynamic?</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><a rel="nofollow" href="http://www.r-exams.org/assets/posts/2017-10-07-static_num_schoice/elasticity1.Rmd" target="_blank">elasticity1.Rmd</a> <br> <a rel="nofollow" href="http://www.r-exams.org/assets/posts/2017-10-07-static_num_schoice/elasticity1.Rnw" target="_blank">elasticity1.Rnw</a></td>
<td>No</td>
<td><code>num</code></td>
<td>Fixed parameters and numeric solution.</td>
</tr>
<tr>
<td>2</td>
<td><a rel="nofollow" href="http://www.r-exams.org/assets/posts/2017-10-07-static_num_schoice/elasticity2.Rmd" target="_blank">elasticity2.Rmd</a> <br> <a rel="nofollow" href="http://www.r-exams.org/assets/posts/2017-10-07-static_num_schoice/elasticity2.Rnw" target="_blank">elasticity2.Rnw</a></td>
<td>No</td>
<td><code>schoice</code></td>
<td>As in #1 but with single-choice solution (five answer alternatives).</td>
</tr>
<tr>
<td>3</td>
<td><a rel="nofollow" href="http://www.r-exams.org/assets/posts/2017-10-07-static_num_schoice/elasticity3.Rmd" target="_blank">elasticity3.Rmd</a> <br> <a rel="nofollow" href="http://www.r-exams.org/assets/posts/2017-10-07-static_num_schoice/elasticity3.Rnw" target="_blank">elasticity3.Rnw</a></td>
<td>Yes</td>
<td><code>num</code></td>
<td>Randomly drawn parameters with dynamic computation of correct solution, based on #1.</td>
</tr>
<tr>
<td>4</td>
<td><a rel="nofollow" href="http://www.r-exams.org/assets/posts/2017-10-07-static_num_schoice/elasticity4.Rmd" target="_blank">elasticity4.Rmd</a> <br> <a rel="nofollow" href="http://www.r-exams.org/assets/posts/2017-10-07-static_num_schoice/elasticity4.Rnw" target="_blank">elasticity4.Rnw</a></td>
<td>Yes</td>
<td><code>schoice</code></td>
<td>Randomly drawn parameters (as in #3) with dynamically-generated single-choice solution (as in #2), computed by <code>num_to_schoice()</code>.</td>
</tr>
<tr>
<td>5</td>
<td><a rel="nofollow" href="http://www.r-exams.org/assets/posts/2017-10-07-static_num_schoice/elasticity5.Rmd" target="_blank">elasticity5.Rmd</a> <br> <a rel="nofollow" href="http://www.r-exams.org/assets/posts/2017-10-07-static_num_schoice/elasticity5.Rnw" target="_blank">elasticity5.Rnw</a></td>
<td>Yes</td>
<td><code>schoice</code></td>
<td>As in #4 but with the last alternative: <em>None of the above.</em></td>
</tr>
</tbody>
</table>
<h2>Static numeric</h2>
<p>The starting point is a completely static exercise as it had been used in a previous<br>
introductory economics exam. The parameters had been set<br>
to<br>

<p>,<br>

<p>,<br>
and<br>

<p>.<br>
This implies that<br>

<p>,<br>
leading to an elasticity of<br>

<p>.</p>
<p>The corresponding R/exams templates simply hard-code these numbers into the question/solution<br>
and wrap everything into R/Markdown (<a rel="nofollow" href="http://www.r-exams.org/assets/posts/2017-10-07-static_num_schoice/elasticity1.Rmd" target="_blank">elasticity1.Rmd</a>)<br>
or R/LaTeX (<a rel="nofollow" href="http://www.r-exams.org/assets/posts/2017-10-07-static_num_schoice/elasticity1.Rnw" target="_blank">elasticity1.Rnw</a>).<br>
Note that LaTeX is used in either case for the mathematical notation. In case you are unfamiliar<br>
with the R/exams format, please check out the <a rel="nofollow" href="http://www.r-exams.org/tutorials/first_steps/" target="_blank">First Steps</a> tutorial.</p>
<p>The meta-information simply sets <code>extype</code> to <code>num</code>, supplies the <code>exsolution</code> with a precision of three digits,<br>
and allows an <code>extol</code> tolerance of 0.01. To see what the result looks like download the files linked above<br>
and run <code>exams2html()</code> and/or <code>exams2pdf()</code>. (The examples below always use the R/Markdown version but the<br>
R/LaTeX version can be used in exactly the same way.)</p>
<pre>
library(&quot;exams&quot;)
exams2html(&quot;elasticity1.Rmd&quot;)
exams2pdf(&quot;elasticity1.Rmd&quot;)
</pre>
<h2>Static single-choice</h2>
<p>Single-choice versions of exercises are often desired for use in <a rel="nofollow" href="http://www.r-exams.org/intro/written/" target="_blank">written exams</a><br>
because they can be conveniently scanned and automatically evaluated. Thus, we need to come up<br>
with a number of incorrect alternative solutions (or “distractors”). If desired, these could include<br>
typical wrong solutions or a <em>None of the others</em> alternative.</p>
<p>The R/exams templates<br>
<a rel="nofollow" href="http://www.r-exams.org/assets/posts/2017-10-07-static_num_schoice/elasticity2.Rmd" target="_blank">elasticity2.Rmd</a> and<br>
<a rel="nofollow" href="http://www.r-exams.org/assets/posts/2017-10-07-static_num_schoice/elasticity2.Rnw" target="_blank">elasticity2.Rnw</a> are<br>
essentially copies of the static numeric exercise above but:</p>
<ol>
<li>Question/solution now contain an answerlist (with five alternatives).</li>
<li>The <code>extype</code> has been changed to <code>schoice</code>.</li>
<li>The <code>exsolution</code> now contains a binary coding of the correct solution.</li>
<li>Furthermore, to obtain some basic randomization we have turned on shuffling by setting <code>exshuffle</code><br>
to <code>TRUE</code>. (Subsampling more than five alternatives would also be possible and would add some further randomization.)</li>
</ol>
<p>As above <code>exams2html()</code> and/or <code>exams2pdf()</code> can be used to display the exercise interactively in R/exams.</p>
<h2>Dynamic numeric</h2>
<p>Next, the static exercise from above is made dynamic by drawing the parameters from a suitable<br>
data-generating process. In this case, the following works well:</p>
<pre>
## p = a - b * x
p &lt;- sample(5:15, 1)
fctr &lt;- sample(c(2, 4, 5, 10), 1)
x &lt;- sample(5:15, 1) * fctr
b &lt;- sample(1:5, 1) / fctr
a &lt;- p + b * x

## elasticity
sol &lt;- -1/b * p/x
</pre>
<p>Note that in order to obtain “nice” numbers a common scaling factor <code>fctr</code> is used for both <code>x</code> and <code>b</code>.<br>
Also, while the examinees are presented with parameters <code>a</code> and <code>b</code> and have to compute <code>x</code>,<br>
the data-generating process actually draws <code>x</code> and <code>b</code> and computes <code>a</code> from that. Again, this makes it<br>
easier to obtain “nice” numbers.</p>
<p>The R/exams templates<br>
<a rel="nofollow" href="http://www.r-exams.org/assets/posts/2017-10-07-static_num_schoice/elasticity3.Rmd" target="_blank">elasticity3.Rmd</a> and<br>
<a rel="nofollow" href="http://www.r-exams.org/assets/posts/2017-10-07-static_num_schoice/elasticity3.Rnw" target="_blank">elasticity3.Rnw</a> include<br>
the data-generating process above as a code chunk either in R/Markdown or R/LaTeX format.<br>
The parameters are then inserted into question/solution/metainformation using <code>`r a`</code> (R/Markdown)<br>
or <code>\Sexpr{a}</code> (R/LaTeX). Sometimes the <code>fmt()</code> function is used for formatting the parameters<br>
with a desired number of digits. (See <code>?fmt</code> for more details.)</p>
<p>As before <code>exams2html()</code> and/or <code>exams2pdf()</code> can be used to display the <em>random draws</em> from<br>
the exercise templates. For checking that the meta-information is included correctly, it is often<br>
helpful to run</p>
<pre>
exams_metainfo(exams2html(&quot;elasticity3.Rmd&quot;))
</pre>
<p>Furthermore, some tweaking is usually required when calibrating the parameter ranges in the<br>
data-generating process. The <code>stresstest_exercise()</code> function draws a large number of random replications<br>
and thus can help to spot errors that occur or to find solutions that are “extreme” (e.g., much larger or smaller than usual). To clean up your<br>
global environment and run the function use something like this:</p>
<pre>
rm(list = ls())
s &lt;- stresstest_exercise(&quot;elasticity3.Rmd&quot;, n = 200)
plot(s)
plot(s, type = &quot;solution&quot;)
</pre>
<p>The latter command plots the correct solution against the (scalar) parameters that were<br>
generated in the exercise. This might show patterns for which parameters the solution becomes<br>
too large or too small etc. See <code>?stresstest_exercise</code> for further features/details.</p>
<h2>Dynamic single-choice</h2>
<p>To go from the dynamic numeric exercise to a dynamic single-choice exercise we need to<br>
extend the data-generating process to also produce a number of wrong alternatives.<br>
The function <code>num_to_schoice()</code> helps with this by providing different sampling<br>
mechanisms. It allows to set a range in which the alternatives have to be, a minimum<br>
distance between all alternatives, possibly include typical wrong solutions, etc.<br>
It also shuffles the resulting alternatives and tries to make sure that the correct<br>
solution is <em>not</em> a certain order statistic (e.g., almost always the largest or<br>
smallest alternative).</p>
<p>The R/exams templates<br>
<a rel="nofollow" href="http://www.r-exams.org/assets/posts/2017-10-07-static_num_schoice/elasticity4.Rmd" target="_blank">elasticity4.Rmd</a> and<br>
<a rel="nofollow" href="http://www.r-exams.org/assets/posts/2017-10-07-static_num_schoice/elasticity4.Rnw" target="_blank">elasticity4.Rnw</a> first<br>
wrap the data-generating process into a while loop with <code>while(sol > -0.11)</code>. This makes sure<br>
that there is enough “space” for four wrong alternatives between -0.11 and 0, using a minimum<br>
distance of 0.017. Subsequently, the four wrong alternatives are generated by:</p>
<pre>
## single-choice incl. typical errors
err &lt;- c(1/sol, sol/p, p/sol)
err &lt;- err[(err > -5) &amp; (err &lt; -0.2) &amp; abs(err - sol) > 0.01]
rng &lt;- c(min(1.5 * sol, -1), -0.01)
sc &lt;- num_to_schoice(sol, wrong = err, range = rng,
  delta = 0.017, method = &quot;delta&quot;, digits = 3)
</pre>
<p>This suggests a number of typical wrong solutions <code>err</code> (provided that they are not too small<br>
or too large) and makes sure that the range <code>rng</code> is large enough. With these arguments<br>
<code>num_to_schoice()</code> is run, see <code>?num_to_schoice</code> for the details. The resulting <code>sc</code> list<br>
contains suitable <code>$questions</code> and <code>$solutions</code> that can be easily embedded in an <code>answerlist()</code><br>
and in the meta-information. Sometimes it is useful to wrap <code>num_to_schoice()</code> into another<br>
<code>while()</code> loop to make sure a valid result is found. See the <a rel="nofollow" href="http://www.r-exams.org/templates/deriv2/" target="_blank">deriv2</a><br>
template for illustraion.</p>
<p>As before <code>exams2html()</code> and/or <code>exams2pdf()</code> can be used to display the <em>random draws</em> from<br>
this exercise template. And some stress-testing could be carried out by:</p>
<pre>
rm(list = ls())
s &lt;- stresstest_exercise(&quot;elasticity4.Rmd&quot;, n = 200)
plot(s)
plot(s, type = &quot;ordering&quot;)
</pre>
<p>As a final variation we could include <em>None of the above</em> as the last alternative. This is<br>
very easy because we can simply replace the fifth element of the question list with the corresponding<br>
string:</p>
<pre>
sc$questions[5] &lt;- &quot;None of the above.&quot;
</pre>
<p>No matter whether this was actually the correct alternative or one of the incorrect alternatives, the<br>
corresponding solution will stay the same. The R/exams templates<br>
<a rel="nofollow" href="http://www.r-exams.org/assets/posts/2017-10-07-static_num_schoice/elasticity5.Rmd" target="_blank">elasticity5.Rmd</a> and<br>
<a rel="nofollow" href="http://www.r-exams.org/assets/posts/2017-10-07-static_num_schoice/elasticity5.Rnw" target="_blank">elasticity5.Rnw</a> incorporate<br>
this additional line of code in the data-generating process.</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="http://www.r-exams.org/tutorials/static_num_schoice/"> R/exams</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/9EnV037uFPk" height="1"></a></div></td></tr></tbody></table></td></tr></tbody></table>