5.1 Spurious association

Code 5.1

Code 5.2

Code 5.3

Code 5.4

Code 5.5

Code 5.6

Code 5.7

Code 5.8

Code 5.9

Code 5.10

Code 5.11

Code 5.12

Code 5.13

Code 5.14

Code 5.15

Code 5.16

Code 5.17

Code 5.18

Code 5.19

Code 5.20

Code 5.21

Code 5.22

Code 5.23

Code 5.24

Code 5.25

Code 5.26

Code 5.27

5.2 Masked relationship

Code 5.28

Code 5.29

Code 5.30

Expected to fail

Code 5.31

Code 5.32

Code 5.33

Code 5.34

Code 5.35

Code 5.36

Code 5.37

Code 5.38

Code 5.40

Code 5.41

The code in the book corresponds to the bottom-right figure, which keeps N=0 (despite stated in the text).

Below is the code to produce the bottom-left figure (M=0).

Code 5.42

Code 5.43

Code 5.44

5.3 Categorical variables

Code 5.45

Code 5.46

Code 5.47

Code 5.48

Code 5.49

Code 5.50

Code 5.51

Code 5.52

Code 5.53

Code 5.54