# 1. Introduction

Moodle XML file for this exercise (Download and import it in Moodle)

In this exercise we will look at how we can check the students' results of a linear fit using their own data. Students have done a hypothetical measurement in the lab and have six data points (x and y-values). Students are expected to make a linear fit of their measurements and we ask for their data, slope, intercept, and correlation coefficient.

If you are not familiar with the CodeRunner question type for calculated questions, see the first example which explains the sections in more detail.

# 2. Setting up the question

## 2.1. Global extra

Figure 1 shows the whole question as the student sees it.

Figure 1. Student's view of the question.

First thing you'll notice is that it features a lot more empty fields than in the previous example where we only had two. This naturally leads us having a lot more input in Global extra. Figure 2 shows what is needed to produce the Answer section seen in Figure 1. Notice the alignment of the answer fields in the three bottom rows.

Figure 2. Global extra for asking the student's x- and y-values and main parameters of the linear fit.

## 2.2. Checking the answers

In the previous example we did not talk about the order of the answer cells and gapfiller fields. The gapfiller_fields[] in the Customisation section reads the fields defined in Global extra from left to right, row by row. We define the fields as shown in Figure 3. First two fields are the x- and y-values of the first data point of the student, respectively. This repeats until all the data points are considered, and the last three are the slope, intercept and correlation coefficient given by the student.

Figure 3. Gapfiller fields defined for the linear regression assignment.

Calculating the answers is naturally a bit more complicated than in the previous example. We start with importing the necessary tools, namely the math library for the `isclose`

checks and `linregress`

for calculating the parameters of the linear fit. First, at line 42, we define a "bad value" as `-1`

. This is the value the students type in if they wish a data point to be removed. It can be anything, but it should be a value that normally would not be in any student's answers. Next, at lines 43 and 44, we write the x- and y-values to their own lists, aptly named `listOfX `

and `listOfY`

. Lines 46 – 52 check if the student wishes for one of the data points to be removed. The loop checks if either list contains the bad value, and if so, it checks that both the x- and y-values of the same row are marked as `-1`

. If both values on the same row are `-1`

, they are removed. In any other case where there is a `-1`

as input, the student gets an error message. Finally at line 55 we use `linregress`

to calculate the necessary parameters which are stored in an object called `check`

. The name of the object can be freely chosen. It contains six attributes, and the ones we are interested in are the `slope`

, `intercept`

, and `rvalue`

. You can read the documentation of linregress here.

Now we have all the necessary values, we only need to check the answers. This is done exactly like in the simpler example. We have an `if`

-loop with the `isclose`

function for all three variables. The correct answers are stored in `check `

and we call them with `check.attribute`

, as is done at lines 62, 67, and 72 in Figure 4.

Figure 4. Checking the student's answers in the linear regression assignment.

## 2.3. Output check

Last thing we need to do is to make sure the expected output in the Test case corresponds with the lines we print for the correct answers. This time we check for three things, and the expected output is the printed lines of the correct answers: "`Slope of the linear fit is correct`

" , "`Intercept of the linear fit is correct`

" and "`Correlation coefficient of the linear fit is correct`

" , as shown in Figure 5.

Figure 5. Setting the correct expected output for the linear regression assignment.