R script for example in paper