R code for my BAEA research