Lab 02 (I): The Carbon Bookkeeping Model

Submit the lab before May 16th 2024, 20:00

Introduction

This is the first half of the assignment associated with the carbon bookkeeping model. The assignment is subdivided into three parts: (1) first, we introduce the general structure and system of a CBKM, and get familiar with the way it is implemented in R. (2) Second, we will ask you to run some basic calculations and preparation of graphs. (3) Last, we will ask you to take the code provided and convert it into a function in R. The reason for that is that in the second part of the lab (i.e., next week) we will ask you to run the same code for multiple land-use changes and using different assumptions of future land-use change - and by writing your own function your code will be much less prone to errors and easier to read. In section Exercise IV: Converting the code into your own R-Function we will explain a bit more in depth how to write a function in R.

Important

This week, we will ask you to submit a .html file into moodle, which you generate by applying the knitr function in R-Studio.

Exercise I: Getting to know the model and model fluxes from woodland-to-cropland conversions (F_C).

When you open the file, you’ll notice some cells for basic definitions, such as the land-cover variables, the reporting period or the instantiation of carbon stocks in the model. Carefully examine the code and variables that are being generated in R when executing the cells. In addition, when you look at the paper Baumann et al. (2017) you’ll notice that the Chaco has three sub-regions, but here we only consider values of one region. Quickly scan the paper and select from the options below the ecoregion that we are looking at in this example..

Question I:

Which ecoregion do the numbers represent?

◻️ Dry Chaco

◻️ Humid Chaco

◻️ Very Dry Chaco

Now, continue going through the R-script and focus on the parameterization of the model. One set of parameters considers information of what happens to the AGB after deforestation. In the Chaco, we generally divide between (a) AGB that is directly burned, (b) AGB that is left on the site, (c) AGB that is extracted to create wood products, and (d) AGB that mineralizes after burning. Those are what we know from Vensim as flows (e.g., in-flows and out-flows); and in case of emissions, this is what we are after. For now, keep the values as they are – yet, after carefully reading the paper by Baumann et al. (2017) you will understand better why setting these fractions is important, and why often a sensitive issue and point of critique.

Question II:

Why are these fractions so influential on the outcome, and why can they be also used to manipulate results (or at least result in false interpretation? (3-4 sentences in moodle)

Now that everything is set, you can run the model. Do so and examine the output table to see whether you recognize structures in the data. In addition, it is always good to create a suitable visualization of the data. Use the code chunk we have prepared to create a suitable visualization of your results. After that, take the output table and calculate the net emissions until 2050.

Question III:

Create a suitable visualization inside the code chunk in the .Rmd-File that contains all different flows (i.e., also the gains)

Question IV:

Calculate the total net emissions by 2050 and upload the number into moodle.

Exercise II: Modelling soil organic carbon (SOC) emissions

As you know from the lecture and the discussion, soil organic carbon (SOC) is an important aspect when estimating carbon emissions from land-use change. The cool thing in the context of the CBKM is that we work in the same logic as in case of the land-cover change (e.g., forest to cropland conversions) and only adjust the different coefficients and parameters. In the context off SOC emissions, we do not have stocks like we have in case of AGB, but we assume constant annual emissions over a certain period of time. This is reflected in the .Rmd-File where you can find all coefficients and the calculation for calculating SOC emissions. Do so, and then answer the following questions:

Question V:

Calculate the total emissions from aboveground biomass and soil organic carbon (i.e., the sum of the two) by 2050 and upload the number into moodle.

Question VI:

Create a suitable visualization inside the code chunk in the .Rmd-File. The graph should contain (a) Total gross AGB emissions, (2) Total SOC emissions, (3) Carbon gains from AGB, (4) Total Net emissions.

Exercise III: Diversity of land-use change

Contrary to many perceptions, the most dominant conversions in the Chaco are not forest-to-cropland but forest-to-pastures. As such, these land-cover changes need to be considered differently when calculating emissions. Now that you know how to calculate carbon emissions for forest-to-cropland conversions, it is easy to calculate the corresponding emissions for forest-to-pasture conversions as the only thing you need to do is adjust the stocks and emission factors. In Baumann et al. (2017) you find a table with all necessary parameters to build a similar model to the one for cropland. Build the same simulation for forest-to-pasture conversions. Thereby, assume that we have pastures without trees (i.e., where most/all AGB is removed after conversion), as opposed to silvopastures, where trees remain at the site and hence more AGB. Carefully read through the paper and identify the suitable parameters. Once you have done that, create a suitable visualization that shows (1) AGB emissions from forest-to-cropland conversions, (2) AGB emissions from forest-to-pasture conversions, (3) combined SOC emissions.

Exercise IV: Converting the code into your own R-Function

You probably noticed that the idea is rather simple, and that the only difference between exercise I and III was to change only some parameters while the overall calculation remained the same. We also suggested to copy the code between the respective code chunks, and probably you did well and paid attention to renaming the different variables and re-defining them. Now, imagine you have to do that not only for two land-cover changes and for one ecoregion (i.e., the dry Chaco) but for a larger combination. Likewise, maybe you want to make assumption about future land-cover changes and calculate scenarios of carbon emissions from future land-use change. If you follow the approach that we took until now, then you will have to copy the code several times, always adjusting parameters and variable names. This is not only a substantial source for errors, but it also makes you code lengthy and hard to oversee. One way out is to code your own function in R - a function that is designed to the different land-cover changes and parameters. Your task is exactly that: take the code chunks from above and convert them into functions that you can call using different parameters. Below you find some basic introduction into how functions can be defined in R, followed by some extra sources of information. Discuss with your fellow students a suitable definition of such a function. For example, what should be the arguments for the function: all parameters individually, or a list/tupel of arguments? What are the return values and how can they be accessed?

Note

Take the code chunks you have applied so far and convert them into two functions: (1) a function for the emissions of AGB, and (2) a function for SOC emissions. Provide proof that your function results in the same numbers (i.e., the same emissions) as in the previous exercises. Write each function into a different chunk.

A function in R is always build in the same way: (1) a variable under which the function is stored; (2) the keyword function indicating that the variable is a function, followed by the function parameters in parentheses (); (3) {} between which the operation of the function is described; and (4) a return argument with the values that the function should return. Complicated? Not really - have a look at the example below:

myFunc <- function(a, b, c){
  sum <- a + b + c
  return(sum)
}

This function needs three arguments a, b, c, calculates the sum, and returns it. We can call the function to get our results, here as example for the numbers 10, 20, 30.

callMyFunc <- myFunc(10, 20, 30)
callMyFunc
[1] 60

In case of this very simplistic function, the value of it may not be immediately visible, but when you consider the for-loops in the code, the different stocks, etc. then it might be handy to program such a function for your emission calculations. Try it out! Again: you don’t have to do it alone, but form a group with 2-3 fellow students and solve this together! One last tip: if you want to return more than one value (i.e., not only the sum), you can do this as well. Have a look at the function below:

mySecondFunc <- function(a, b, c){
  sum <- a + b + c
  product <- a * b * c
  return(list("s"=sum, "p"=product))
}
callMySecondFunc <- mySecondFunc(10, 20, 30)
callMySecondFunc
$s
[1] 60

$p
[1] 6000
# Access the individual elements of the output
callMySecondFunc$s
[1] 60

In the context of your function, think about the different elements ($s and $p) as for example as the different emission components. You are free to write the function in the way you want - the only requirement is that you can calculate the same numbers as in exercises I-III. Good luck!

More sources on functions in R

  1. https://www.programiz.com/r/function
  2. https://www.dataquest.io/blog/r-functions-tutorial/