-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomework-1-KPDuBose.qmd
184 lines (137 loc) · 5.42 KB
/
homework-1-KPDuBose.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
---
title: "Assignment 1 - PHS 7045"
author: "Kline DuBose"
format: gfm
---
# Due date
Tuesday, February 7
# Design 1
```{r}
library(data.table)
# set.seed(1134)
design1.fun <- function(sampleSize, alpha, beta, effective){
delta <- 0.9912
numbDraws <- 100000
equalArm <- sampleSize / 4
subjects <- rbinom(sampleSize, 1, effective)
f <- 0:3
allocation <- data.table::as.data.table(split(subjects, f))
y_0 <- allocation$'0'
y_1 <- allocation$'1'
y_2 <- allocation$'2'
y_3 <- allocation$'3'
# Distribution of each thing
p_0 <- binom.sample(numbDraws, alpha, beta, y_0)
p_1 <- binom.sample(numbDraws, alpha, beta, y_1)
p_2 <- binom.sample(numbDraws, alpha, beta, y_2)
p_3 <- binom.sample(numbDraws, alpha, beta, y_3)
v_1 <- mean(p_1 > p_0)
v_2 <- mean(p_2 > p_0)
v_3 <- mean(p_3 > p_0)
v_0 <- min(sum((v_1)*((length(y_1) + 1)/(length(y_0) + 1)),(v_2)*((length(y_2) + 1)/(length(y_0) + 1)),(v_3)*((length(y_3) + 1)/(length(y_0) + 1))), max(v_1, v_2, v_3))
d1fv1 <<- sum(v_1, v_2, v_3)/sum(v_0, v_1, v_2, v_3)
d1fv2 <<- sum(v_2, v_3)/sum(v_0, v_1, v_2, v_3)
d1fv3 <<- sum(v_3)/sum(v_0, v_1, v_2, v_3)
if(max(v_1, v_2, v_3) > delta){
print("Successful trial")
print(max(v_1, v_2, v_3))
print("Number of patients per arm")
print(equalArm)
}
else{
print("Unsuccessful trial")
print(max(v_1, v_2, v_3))
print("Number of patients per arm")
print(equalArm)
}}
binom.sample <- function(draws, alpha, beta, vecInterest){
rbeta(draws, shape1 = alpha + sum(vecInterest), shape2 = beta + length(vecInterest) - sum(vecInterest))
}
```
# Design 2
```{r}
design2.fun <- function(popSize, alpha, beta, effective, interim){
numbDraws <- 100000
numbIter <- popSize %/% interim + 1
# Randomly assign subjects
subjects <- rbinom(popSize, 1, effective)
iter <- rep(1:6, each = 40, length.out = 228)
iter <- sample(iter)
subjects <- data.table::as.data.table(cbind(subjects, iter))
# Initial Distribution to interim
equal.dist(length(subjects[iter == 1, subjects]), alpha, beta, subjects[iter == 1, subjects], draws = numbDraws)
# Readjust "v"
update.v(v_1, v_2, v_3, length(y_0), length(y_1), length(y_2), length(y_3))
# Run the analysis for the remaining arms
for (i in 2:numbIter) {
# Run to get new numbers for the different arms
vari.dist(length(subjects[iter == i, subjects]), alpha, beta, subjects[iter == i, subjects], draws = numbDraws)
# recalculate v for the next round of information
update.v(v_1, v_2, v_3, length(y2_0), length(y2_1), length(y2_2), length(y2_3))
}
d2fv1 <<- sum(v_1, v_2, v_3)
d2fv2 <<- sum(v_2, v_3)
d2fv3 <<- v_3
print(paste("V_1:", fv_1))
print(paste("V_2:", fv_2))
print(paste("V_3:", fv_3))
print(paste0("Number of subjects in control ", length(y_0)))
print(paste0("Number of subjects in arm 1 ", length(y_1)))
print(paste0("Number of subjects in arm 2 ", length(y_2)))
print(paste0("Number of subjects in arm 3 ", length(y_3)))
}
update.v <- function(v1, v2, v3, n0, n1, n2, n3){
fv_1 <<- v1
fv_2 <<- v2
fv_3 <<- v3
v0 <- min(sum((v1)*((n1 + 1)/(n0 + 1)),(v2)*((n2 + 1)/(n0 + 1)),(v3)*((n3 + 1)/(n0 + 1))), max(v1, v2, v3))
v_0 <<- (v0)/sum(v0, v1, v2, v3)
v_1 <<- (v1)/sum(v0, v1, v2, v3)
v_2 <<- (v2)/sum(v0, v1, v2, v3)
v_3 <<- (v3)/sum(v0, v1, v2, v3)
}
equal.dist <- function(sampleSize, alpha, beta, subjectVector, draws){
equalArm <- sampleSize / 4
group <- rep_len(0:3, sampleSize)
# group <- sample(group, sampleSize)
allocation <- data.table::as.data.table(cbind(subjectVector, group))
y_0 <<- (allocation[group == 0, .(subjectVector)])$subjectVector
y_1 <<- (allocation[group == 1, .(subjectVector)])$subjectVector
y_2 <<- (allocation[group == 2, .(subjectVector)])$subjectVector
y_3 <<- (allocation[group == 3, .(subjectVector)])$subjectVector
# Distribution of each thing
p_0 <- binom.sample(draws, alpha, beta, y_0)
p_1 <- binom.sample(draws, alpha, beta, y_1)
p_2 <- binom.sample(draws, alpha, beta, y_2)
p_3 <- binom.sample(draws, alpha, beta, y_3)
v_1 <<- mean(p_1 > p_0)
v_2 <<- mean(p_2 > p_0)
v_3 <<- mean(p_3 > p_0)
}
vari.dist <- function(sampleSize, alpha, beta, subjectVector, draws){
group <- sample(0:3, sampleSize, replace = TRUE, prob = c(v_0, v_1, v_2, v_3))
allocation <- data.table::as.data.table(cbind(subjectVector, group))
y2_0 <<- as.vector(allocation[group == 0, .(subjectVector)])$subjectVector
y2_1 <<- as.vector(allocation[group == 1, .(subjectVector)])$subjectVector
y2_2 <<- as.vector(allocation[group == 2, .(subjectVector)])$subjectVector
y2_3 <<- as.vector(allocation[group == 3, .(subjectVector)])$subjectVector
# append the new values in each arm to the existing ones
y_0 <<- append(y_0, y2_0); y_1 <<- append(y_1, y2_1); y_2 <<- append(y_2, y2_2); y_3 <<- append(y_3, y2_3)
p_0 <- binom.sample(draws, alpha, beta, y2_0)
p_1 <- binom.sample(draws, alpha, beta, y2_1)
p_2 <- binom.sample(draws, alpha, beta, y2_2)
p_3 <- binom.sample(draws, alpha, beta, y2_3)
v_1 <<- mean(p_1 > p_0)
v_2 <<- mean(p_2 > p_0)
v_3 <<- mean(p_3 > p_0)
}
```
# Recreate Table 2
```{r}
design1.fun(228, 0.35, 0.65, 0.35)
design2.fun(228, 0.35, 0.65, 0.35, 40)
table2 <- matrix(c(d1fv1, d1fv2, d1fv3, d2fv1, d2fv2, d2fv3), ncol = 3, byrow = TRUE)
colnames(table2) <- c("Pr Arm1 or Greater", "Pr Arm2 or Greater", "Pr Arm3")
rownames(table2) <- c("F25", "RMatch")
table2
```