To follow-up on my last post, I would like to show the advantages of setting up exams with the R-exams package by showing an exercise example that I have prepared. This is an exercise to find the optimal level of pollution, which is a typical exercise to show up in an environmental economics exam.

As I mentioned last time, perhaps the biggest advantage of the R-exams package is to randomize numbers within an exam. This is quite useful to discourage cheating, which is a concern given our need to prepare take-home exams. We wanted to prepare numeric exercises similar to the ones we solved during class, but with slightly different functions, which would result in a different solution for each student.

The exercise I prepared was a simple optimal pollution exercise. Students had to optimize net benefits given the benefits and costs of pollution. Both benefits and costs are represented by a function which depends on the level of pollution. The level of pollution can represent pollution in tons, cubic meters, liters, etc; and it can represent all kinds of pollution, such as air, water, ground pollution, etc. In this example, I suppose that pollution is measured in tons of greenhouse gas (GHG) emissions.

The typical problem is to understand what the optimal level of pollution should be. This is a simple optimization exercise. Let us say we have a benefit function of the form:

And a cost function that looks like:

My goal is to randomize the parameters a, b, c and d for each exam. This will result in a different solution for each sample of parameters. For example, I would like “a” to be one number sampled from an interval from 100 to 500. In R, this would look like:

`a <- sample(seq(100, 500), 1)`

The same for all other parameters. “b” and “d” would be sampled from an ineterval between 10 and 20, while “c” would be sampled from an interval from 100 to 1000. If I were to do this manually, it could generate numbers like these:

```
> a <- sample(seq(100, 500), 1)
> a
[1] 387
> b <- sample(seq(10, 20), 1)
> b
[1] 15
> c <- sample(seq(100,1000), 1)
> c
[1] 749
> d <- sample(seq(10,20), 1)
> d
[1] 13
```

The benefits and costs function would hence look like:

And a cost function that looks like:

To write the benefits and cost functions in the exercise, I need to use the ‘r a’ terminology:

```
$B(GHG) = `r a`*GHG-`r b`*GHG^2$
$C(GHG) = `r c`*GHG+`r d`*GHG^2$
```

What would be the solution (optimal pollution level) of this exercise? I take the first derivative of the net benefits and equalize it to zero. The result is below as a function of the four randomly sampled parameters:

```
> Opt_GHG <- round((a-c) / (2*(b+d)))
> Opt_GHG
[1] -6
```

For the combination of 4 parameters shown before, the solution is “-6”. Perhaps some adjustments in the sampling procedure would have to be done to avoid negative numbers as the solution.

Let us suppose I want to create 12 different versions of this question. Each version will have a different combination of parameters. So I define *n* as the number of versions of the exam:

`n <- 12`

I will now add all of these components together to create the exercise in a .Rmd file. You can work on .Rmd files in the same way that you would work on an R script.

I can do the randomization in the .Rmd file, as such:

```
```{r data generation, echo = FALSE, results = "hide"}
## parameters
n <- 12 ### Number of students
a <- sample(seq(100, 500), 1)
b <- sample(seq(10, 20), 1)
c <- sample(seq(100,1000), 1)
d <- sample(seq(10,20), 1)
## solution
Opt_GHG <- round((a-c) / (2*(b+d)))
```
Question
========
$B(GHG) = `r a`*GHG-`r b`*GHG^2$
$C(GHG) = `r c`*GHG+`r d`*GHG^2$
What is the optimal level and price of emissions for this society?
Meta-information
============
extype: num
exsolution: `r Opt_GHG`
exname: optimal_question1
extol: 1
```

The exercise above consist of three parts: a randomization part, the question itself and meta-data about the exercise. The randomization part includes most of the components I have introduced before, while the question itself is what actually shows up in the exam/test. Finally, the meta-information part is a mandatory section of exercises that defines what kind of exercise this is (“extype: num” for numeric), what is the exercise’s solution (“exsolution: ‘r Opt_GHG’ “), the name of the question (“exname: optimal_question1”) and the error tolerance for students (“extol: 1”).

Finally, to compile the exercise into a pdf, I use the exams2pdf function:

```
library("exams")
setwd("C:/YOUR DIRECTORY")
exam <- c("Question_1.Rmd")
rexam <- exams2pdf(exam, n=12)
```

The command above creates 12 versions of the same exercise, by specifying “n=12”, and by sampling randomly the values of a, b, c, and d. For example, these are two of the versions of the exercises produced:

In each of these exercises, the solution will be different, which is something to take into account when grading.

I hope this is a useful introduction to whoever is teaching environmental economics and plans on using the R-exams package. This is just one example, and more exercises can be created using this same logic.