R functions for PGS