-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simulation function_notuniform_start.R
139 lines (95 loc) · 4.03 KB
/
Simulation function_notuniform_start.R
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
library(survival)
library(ggplot2)
library(ggprism)
simulation = function(nsim=100 ,n=100, maxt=200, beta1=0.2, beta2=-0.1,width=28){
n = floor(n/2)*2 #make sure it's an even number
library(dplyr)
#use a 4-p log-logistic
t <- seq(0, maxt, by = 1)
a1 = 0.014
p1 = 1.6
l1=0.026
h1 = (p1*(a1^p1)*t^(p1-1))/(1+(l1*t)^p1)
ch1 = ((a1/l1)^p1)*log(1+(l1*t)^p1)
#max point of peak (28 days = 4 weeks)
((p1-1)/(l1^p1))^(1/p1)
a2 = 0.04
p2 = 3
l2 = 0.05
h2 = ifelse(t<28,0,(p2*(a2^p2)*(t-28)^(p2-1))/(1+(l2*(t-28))^p2))
ch2 = c(rep(0,29),((a2/l2)^p2)*log(1+(l2*(t[-(1:29)]-28))^p2))
#max point
((p2-1)/(l2^p2))^(1/p2) + 28
newd = expand.grid(t=t,x=c(0,1))
newd$h1=h1
newd$h2=h2
newd$s=exp(-ch1-ch2)
newd$haz1 = h1*exp(beta1*newd$x)
newd$haz2 = h2*exp(beta2*newd$x)
newd$cumhaz1 = ch1*exp(beta1*newd$x)
newd$cumhaz2 = ch2*exp(beta2*newd$x)
newd$surv = exp(-newd$cumhaz1 - newd$cumhaz2)
newd = newd %>% group_by(x) %>% mutate(cif1=cumsum(surv*haz1),cif2=cumsum(surv*haz2))
simplotdata <<- newd
library(ggplot2)
library(ggpubr)
ggplot(data=newd, aes(x = t))+
geom_line(aes(y = h1, col="Event 1"),size=1.2)+
geom_line(aes(y=h2, col = "Event 2"),size=1.2)+
theme_prism()+
theme(legend.position = "top")+
xlab("Time")+
ylab("Hazard")+
scale_color_viridis_d(begin=0.20, end=0.75)
newd = newd %>% group_by(x) %>% mutate(surv_prob=lag(surv,default = NA)-surv)
newd = newd[newd$t>0,]
newd0 = newd[newd$x==0,]
newd1 = newd[newd$x==1,]
simdat = list()
for (i in 1:nsim){
simdat[[i]]=data.frame(id=1:n)
simdat[[i]]$x = rep(c(0,1),each=n/2)
simdat[[i]]$L0time = 0
fixed = seq(0,728+5*width,width)
visits=list()
for(j in 1:n){
set.seed(paste0(i,j,1))
visits[[j]] = round(fixed + c(0,rnorm(length(fixed),0,3)[-1]))
simdat[[i]]$R0time[j] = visits[[j]][2]
}
for (j in 1:n){
mu = simdat[[i]]$R0time[j]-10
mu = ifelse(mu<=1,1,mu)
set.seed(paste0(j,420,i))
xx=mu+abs(rnorm(10, 0,4))
simdat[[i]]$W[j] = xx[which(xx<simdat[[i]]$R0time[j])[1]]
}
#using method by Jan Beyersmann 2009 for the times and events simulation
set.seed(paste0(i,1))
simdat[[i]]$t[1:(n/2)] = sample(c(newd0$t,maxt+1), size = n/2,replace=TRUE, prob=c(newd0$surv_prob,1-sum(newd0$surv_prob)))
set.seed(paste0(i,2))
simdat[[i]]$t[(n/2+1):n] = sample(c(newd1$t,maxt+1), size = n/2,replace=TRUE, prob=c(newd1$surv_prob,1-sum(newd1$surv_prob)))
simdat[[i]]$h1 = ((p1*a1^p1*(simdat[[i]]$t^(p1-1)))/(1+(l1*simdat[[i]]$t)^p1))*exp(simdat[[i]]$x*beta1)
simdat[[i]]$h2 = ifelse(simdat[[i]]$t<28,0,((p2*a2^p2*((simdat[[i]]$t-28)^(p2-1)))/(1+(l2*(simdat[[i]]$t-28))^p2))*exp(simdat[[i]]$x*beta2))
simdat[[i]]$pr=simdat[[i]]$h1/(simdat[[i]]$h1+simdat[[i]]$h2)
set.seed(paste0(i,3))
simdat[[i]]$event = 2-rbinom(n,1,simdat[[i]]$pr)
simdat[[i]] = simdat[[i]] %>% mutate(status= ifelse(t>maxt,0,1),
t = ifelse(t > maxt, maxt, t),
event=ifelse(status==0,0,event))
simdat[[i]]$U=simdat[[i]]$W+simdat[[i]]$t
for(j in 1:n){
val3 = last (which(visits[[j]]<simdat[[i]]$U[j]))
val4 = first(which(visits[[j]]>=simdat[[i]]$U[j]))
simdat[[i]]$L1time[j]=visits[[j]][val3]
simdat[[i]]$R1time[j]=visits[[j]][val4]
}
simdat[[i]] = simdat[[i]] %>% mutate(overlap=ifelse(R1time==R0time & L0time==L1time,1,0),
gam_event=as.numeric(I(event==1)),
res_event=as.numeric(I(event==2)),
R1time=ifelse(status==0,L1time,R1time),
state=ifelse(status==0,1,ifelse(gam_event==1,2,3)))
print(i)
}
return(simdat)
}