---
title: "Homework 01 - R Essentials: loops, functions, and *apply functions"
format:
  html:
    embed-resources: true
---

# Due date

Tuesday, September 10


# Updates

1. Values for $\delta$ provided, though these can be found as a bonus.

2. FYI, you might get slightly different values than presented in Table 2. This could be because we have changed the problem slightly from using a prior for the log-odds. Also, there can be rounding error based upon the number of replicates used in the paper.

3. Removed the requirement to only use `base R` functions.

# Background

Response-adaptive randomization (RAR) has been used in precision medicine trials, such as the trials [I-SPY-2](https://www.nejm.org/doi/pdf/10.1056/NEJMoa1513750?articleTools=true), [BATTLE-1](https://aacrjournals.org/cancerdiscovery/article/1/1/44/2198?casa_token=pK1gZcX-FgkAAAAA:KmsD6qnoaOMxqHJlg0VGlmqr2nqIl49Xupuh0FX7nnJXNjtdBwVWsdmVtUIXKdEWQ_e5i9pG), and [BATTLE-2](https://cdn.amegroups.cn/journals/amepc/files/journals/12/articles/6846/public/6846-PB2-R2.pdf) to gather early evidence of treatment arms that work best for a given biomarker.  Throughout RAR, the treatment allocation adjusts depending on which treatment arm looks most promising.  RAR is criticized for the following reasons [(Korn)](https://watermark.silverchair.com/djx013.pdf?token=AQECAHi208BE49Ooan9kkhW_Ercy7Dm3ZL_9Cf3qfKAc485ysgAAAvswggL3BgkqhkiG9w0BBwagggLoMIIC5AIBADCCAt0GCSqGSIb3DQEHATAeBglghkgBZQMEAS4wEQQMUKqYjuX4J38OrUAJAgEQgIICrkuWgqA03alHxi4xfUz3ybPTepY21z2PaOZFRSmRgKhEf4ROr5tIP8MoeLwYxIexRpjXLTjKQ_Yn6s2oYBYUTJEG_gVqeevFGwvxyi8zas8R1GyiQWDyjnhBsjKcAsKCl6M63_K-LXEPLj7nyw4FZEefXgMSO91jz_bKseNLmhDEmZ7oPvO_xlBzWptEW4gE8I7mrv8h3NbFrM0BrbDIeOkIr36F3X-F3ip5a8lEDirZwrKyLCb598ivER1JVlr7iCzNV9OYHv6P79E6YxKGxJPDNk1WJ-ClcUb6aA87-Gae81SVipvqhu2npbLCFtWVikZKliWQ5lPE9CieOB9hnd93HLsp7Da9U3D0jodTMbk9RE28I7kzuoWHO8BEoPK8p1_1nTWw6pV5CbS02RFxtTr4-aCMm364vJAHrgP3rNmvi-KEGB4rxufA_hebdi2yS20nll2HdAVT81m_cm87x3YvVP7Ut9H-0zTCAVYACBJJUjKS7w7QKkIN-FqdQ7N7u_YJ83bl69sMF0fddIVTA6WFAJWpVmiij5ou34auXuzDtEKLN5nmQ-467HZtpMAkoyFxcyCdtI_ieuyCp1jlP3IC07JZAWvA_8A2W8hEFaRBimq0STpclvxEgt2OCDtE9kTXvS8v6ArS4U6ZkuM7Uw3PSG1ISoYAWPPvWzGMKWU4W4nCS0rT-TPWPJkPf8TGkUK9Q1cDemuYlWZBrYN-RUJ8X204vbqa-p9jsRKIfsQ0QDrW97qHYxf-659kKav288WZ8d5wmESsLKyHEc8JatY2dvmsIMIdeK3mKA68qDUqS3C1yO7X0T4BJZpJPGjjPzFrrqaLAmLdhdqITE5AcT_Eiy3upRuYw3TdqbznbDog3k2K2yIgQUZwu2HC9q_fJhqmxh0WkHQDyYeiXyqb): 

1. Possible bias: Time trends in participant enrollment (example: healthier participants earlier) may lead to a bias in estimating which treatment is superior
2. Possible inefficiency: Unequal allocation may lead to statistical inefficiencies compared to equal allocation
3. Possibly unethical: A moderately large sample size could be enrolled onto an arm worse than control

It's not always bad for a method to be criticized -- it means there are open questions to be addressed by new/improved methods.  For example, the manuscript [Comparison of methods for control allocation in multiple arm studies using response adaptive randomization](https://journals.sagepub.com/doi/pdf/10.1177/1740774519877836) develops and compares methods to improve RAR efficiency compared to equal allocation by maintaining a reasonable number of participants allocated to the control arm.

# Assignment

Replicate the results in Table 2 of [Comparison of methods for control allocation in multiple arm studies using response adaptive randomization](https://journals.sagepub.com/doi/pdf/10.1177/1740774519877836) for the designs RMatch and F40.  Use functions to avoid duplicating multiple lines of code (i.e., this is called modularizing your code).

In this assignment, you will practice writing functions, loops, and using the *apply functions.  

# Guidance

0. Although this paper draws upon Bayesian statistics, the purpose of this assignment is to develop the code to implement the method. We have talked about the algorithm in lab; however, please talk to us (in particular, Jonathan) if there are any questions about the mathematics/algorithm.

1. The manuscript uses a minimally informative, normal prior for the log-odds.  Rather than this prior, use a Beta(0.35, 0.65) prior on the response rate for each arm.  This is minimally informative favoring the null hypothesis and allows you to use the Beta-Binomial conjugacy to obtain a Beta posterior distribution.

2. A way to estimate $P_t$(Max) is to `cbind` K = [1000+] draws from the posterior distribution of each arm and to see how frequently (across the K draws from each arm) each arm is drawn to be the largest. Consider `cbind`ing a vector of draws from each arm and using the `apply` function to see how often treatment K is the max when comparing across rows.

3. The value of $\delta$ was found through simulation under the null scenario such that an efficacious trial was declared to be found only 2.5\% of the time.  The manuscript provides values for $\delta$ though, for a bonus, verify and modify as needed (there could be slight differences by using a different prior). Alternatively, use the values of $\delta$ provided (here)[https://uofuepibio.github.io/PHS7045-advanced-programming/04-debugging-and-profiling/lab.html].

4. The manuscript uses 100K replicates to determine $\delta$ and estimate the values in Table 2.  Start with a small number of replicates (1K or 10K) to make sure the code is running correctly and your results are in the ball park of being similar to the manuscript.  Ramp up the number of replicates as feasible (for this assignment feasible is a run time of no longer than 20 minutes).

5. Please email/talk to us if you are having difficulties with this assignment.  We realize it is in a space that may be new to you, and it is not the intent for this assignment to take more than 7-10 hours over the course of 2 weeks.
