QBA workshop - Exercises - Bayesian part
2022-02-23
Chapitre 1 About this document
In this document you will find indications on how to use various R
libraries and functions for conducting quantitative bias adjustment in a Bayesian framework, along with various exercises. We suppose that you do have some basic R
skills. If you have not worked with R
before or feel a bit rusty, here are some resources to help you to prepare:
Chapters 1, 2, and 4 of the CodeCademy “Learn R” course will provide a good overview of the basic concepts required for this workshop.
If you are familiar with
R
and want to do some further reading, Hadley Wickham’s “R for Data Science” is a great resource.
Remember, there are often many different ways to conduct a given piece of work in R
. Throughout this document, we tried to stick with the simpler approaches (e.g., the shortest code, the minimal number of R
libraries).
1.1 Some notation
Throughout the document, you will find examples of R
code along with comments. The R
code used always appear in grey boxes (see the following example). This is the code that you will be able to use for your own analyses. Lines that are preceded by a # sign are comments, they are skipped when R
reads them. Following each grey box with R
code, a white box with results from the analysis is presented.
For instance, this is a R
code where I am simply asking to show main descriptive statistics for the speed variable of the cars dataset (note that this dataset is already uploaded in R
).
#This is a comment. R will ignore this line
#The summary() function can be use to ask for a summary of various R objects. For a variable (here the variable speed), it shows descriptive statistics.
summary(cars$speed)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 4.0 12.0 15.0 15.4 19.0 25.0
Throughout the document, in the text I will use:
- Italics for datasets or variables. For instance, the speed variable of the dataset cars.
- Shaded boxes for R
libraries (e.g., episensr
) and functions (e.g., summary()
).
In R
we can first call a given library and then use the functions related to each library or we can name the library followed by ::
and then the function. For instance the two following pieces of code are equivalent:
library(ggplot2)
ggplot(data=cars, mapping=(aes(x=speed)))+
geom_histogram()
##OR##
::ggplot(data=cars, mapping=(aes(x=speed)))+
ggplot2geom_histogram()
The latter may improve reproducibility, but to the expense of longer codes. Throughout the document, we will always first call the library and then run the functions to keep codes shorter.
One last thing, when using a given function, it is not mandatory to name all the arguments, as long as they are presented in the sequence expected by this function. For instance, the ggplot()
function that we used in the previous chunk of code is expecting to see first a dataset (data=
) and then a mapping attribute (mapping=
) and, within that mapping attribute a x variable (x=
). We could shorten the code by omitting all of these. The two following pieces of code are, therefore, equivalent:
library(ggplot2)
ggplot(data=cars, mapping=(aes(x=speed)))+
geom_histogram()
##OR##
library(ggplot2)
ggplot(cars, (aes(speed)))+
geom_histogram()
Throughout the document, however, we will use the longer code with all the arguments being named. Since you are learning these new functions, it would be quite a challenge to use the shorter code right from the start. But you could certainly adopt the shorter codes later on.
LET’S GET STARTED!