R scripts for q4 and 5