I am trying to answer the question in the following link: Link to the question
I have been trying to run the following code in R for the first part of the question: