Updated R script for affinity model