add subject and visit index to the function