-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid_search_para.R
223 lines (196 loc) · 8.21 KB
/
grid_search_para.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#######################################################################################
#
# Filename : Table 2 and Table 5.R
#
# Project : Article "Estimating the correlation between semi-competing risk survival endpoints"
# Authors : L Sorrell, Y Wei, M Wojtys and P Rowe
# Date : 01/06/2021
#
# R Version : R-3.6.1
#
# Required R packages : copula, mvtnorm, mstate
#
########################################################################################
library(copula)
library(mvtnorm)
library(mstate) #ACS data
source("Functions.R") #Likelihood functions
library(foreach)
data(aidssi2) #Load ACS data set
X <- aidssi2$si.time-aidssi2$entry.time #time to non-terminal event (SI switch)
d1 <- aidssi2$si.stat #indicator for SI switch
Y <- aidssi2$death.time-aidssi2$entry.time #time to terminal event (death from AIDS)
d2 <- aidssi2$death.stat #indicator for death from AIDS
df <- data.frame(X,Y,d1,d2)
########################
## Recreating Table 2 ##
########################
table2 <- addmargins(table(d2,d1))
########################
## Recreating Table 5 ##
########################
#prepare table as data frame:
lambda1 <- rep(NA, 4) #lambda_1
lambda1_lwci <- rep(NA, 4) #lower confidence interval for lambda_1
lambda1_upci <- rep(NA,4) #upper confidence interval for lambda_1
lambda2 <- rep(NA, 4) #lambda_2
lambda2_lwci <- rep(NA, 4) #lower confidence interval for lambda_2
lambda2_upci <- rep(NA,4) #upper confidence interval for lambda_2
rho <- rep(NA, 4) #rho
rho_lwci <- rep(NA, 4) #lower confidence interval for rho
rho_upci <- rep(NA,4) #upper confidence interval for rho
AIC <- rep(NA,4) #AIC
copula <- c("Normal", "Clayton", "Frank", "Gumbel")
get_initial_value<- function(para_list, likelihood_func, X, Y, d1, d2 ){
combin_para= expand.grid(para_list)
best_combin = 0
temp_likelihood = -10000000
for (variable in 1:nrow(combin_para)){
# foreach(variable = 1:nrow(combin_para)) %dopar% {
par = combin_para[variable,]
likelihood = likelihood_func(par,X, Y, d1, d2)
if ((!is.nan(likelihood))&&(is.finite(likelihood))){
cat("likelihood:", likelihood, "\n")
if(!is.na(temp_likelihood < likelihood)){
if(temp_likelihood < likelihood){
temp_likelihood <- likelihood
best_combin = variable
cat("best_combin:", best_combin, "\n")
cat("temp_likelihood:", temp_likelihood, "\n")
}
}
}
}
cat("last_best_combin:", best_combin, "\n")
cat("last_temp_likelihood:", temp_likelihood, "\n")
par_best = combin_para[best_combin,]
return (par_best)
}
get_initial_value_parallel<- function(para_list, likelihood_func, X, Y, d1, d2 ){
combin_para= expand.grid(para_list)
# iterd <-iterators::iter(combin_para, by='row')
a <- foreach(para=iterators::iter(combin_para, by='row'), .combine='rbind') %dopar% {
likelihood_func(para,X, Y, d1, d2)
}
value = max(a[is.finite(a)])
a[!is.finite(a)] = -10000000000000
value_index = which(a==max(a), arr.ind=TRUE)
return (value_index)
}
get_initial_value_parallel1<- function(para_list, likelihood_func, X, Y, d1, d2 ){
combin_para= expand.grid(para_list)
# iterd <-iterators::iter(combin_para, by='row')
a <- foreach(para=iterators::iter(combin_para, by='row'), .combine='rbind') %dopar% {
# likelihood_func(para,X, Y, d1, d2)
l1 <- para[1]
l2 <- para[2]
theta <- para[3]
C <- exp(-((-log(exp(-l1*X)))^(theta)+(-log(exp(-l2*Y)))^(theta))^(1/theta))
C <- as.numeric(C)
part1 <- ifelse(d1*d2==1,(log(C)+(theta-1)*log(-log(exp(-l1*X)))+(theta-1)*log(-log(exp(-l2*Y)))+log(theta-1+((-log(exp(-l1*X)))^theta+(-log(exp(-l2*Y)))^theta)^(1/theta))-log(exp(-l1*X))-log(exp(-l2*Y))-(2*theta-1)*log(-log(C))+log(l1)-l1*X+log(l2)-l2*Y),0)
part2 <- ifelse(d1*(1-d2)==1,(log(C)+(theta-1)*log(-log(exp(-l1*X)))-log(exp(-l1*X))-(theta-1)*log(-log(C))+log(l1)-l1*X),0)
part3 <- ifelse(((1-d1)*(d2))==1,(log(C)+(theta-1)*log(-log(exp(-l2*Y)))-log(exp(-l2*Y))-(theta-1)*log(-log(C))+log(l2)-l2*Y),0)
part4 <- ifelse(((1-d1)*(1-d2))==1,log(C),0)
loglik <- sum(as.numeric(part1)+as.numeric(part2)+as.numeric(part3)+as.numeric(part4) )#sum(part1+part2+part3+part4)
}
value = max(a[is.finite(a)])
a[!is.finite(a)] = -10000000000000
value_index = which(a==max(a), arr.ind=TRUE)
return (value_index)
}
par2<-par1 <- seq(-10 ,10,0.5) # 0.01,10,1
par3<- seq(1,10,0.5) #. 0.1,1,0.1
para_list_gumbel= list(par1, par2, par3)
index_ini_gumbel = get_initial_value_parallel1(para_list_gumbel, gumbel_loglik, X, Y, d1, d2 )
# get_initial_value<- function(para_list, likelihood_func, X, Y, d1, d2 ){
#
# combin_para= expand.grid(para_list)
# best_combin = 0
# par_best=0
# temp_likelihood = -10000000000
# for (variable in 1:nrow(combin_para)) {
# para = combin_para[variable,]
# l1 <- para[1]
# l2 <- para[2]
# theta <- para[3]
#
# C <- -1/theta * log(((1-exp(-theta)-(1-exp(-theta*exp(-l1*X)))*(1-exp(-theta*exp(-l2*Y)))))/(1-exp(-theta)))
#
# part1 <- ifelse(d1*d2==1,(log(theta)+theta*C+log(exp(theta*C)-1)-log(exp(theta*exp(-l1*X))-1)-log(exp(theta*exp(-l2*Y))-1)+log(l1)-l1*X+log(l2)-l2*Y),0)
# part2 <- ifelse(d1*(1-d2)==1,(log((1-exp(theta*C))/(1-exp(theta*exp(-l1*X))))+log(l1)-l1*X),0)
# part3 <- ifelse(((1-d1)*(d2))==1,(log((1-exp(theta*C))/(1-exp(theta*exp(-l2*Y))))+log(l2)-l2*Y),0)
# part4 <- ifelse(((1-d1)*(1-d2))==1,log(C),0)
#
# likelihood <- sum(as.numeric(part1)+as.numeric(part2)+as.numeric(part3)+as.numeric(part4) )#sum(part1+part2+part3+part4)
# print(likelihood)
# # likelihood = likelihood_func(par,X, Y, d1, d2)
# if (!is.nan(likelihood))
# {
# is_samll = as.numeric(temp_likelihood) < as.numeric(likelihood)
# # cat("is_samll:", is_samll, "\n")
# if(is_samll){
#
# temp_likelihood <- likelihood
# best_combin <- variable
# print(best_combin)
# cat("temp_likelihood:", temp_likelihood, "\n")
#
# }
# }
# }
# print(best_combin)
# print(temp_likelihood)
#
# par_best = combin_para[best_combin,]
# return (par_best)
# }
# par2<-par1 <- seq(0.1,5,0.2) # 0.01,10,1
# par3<- seq(0.1,10,0.2) #. 0.1,1,0.1
# par2<-par1 <- seq(-10 ,10,0.5) # 0.01,10,1
# par3<- seq(-10,10,0.5) #. 0.1,1,0.1
# para_list= list(par1, par2, par3)
# # para_list= list(par1, par2, par3, par1, par2)
# combin_para= expand.grid(para_list)
#
#
# best_par1 = get_initial_value_parallel(para_list, frank_loglik, X, Y, d1, d2 ) # clayton_loglik get_initial_value
#
# iterd <-iterators::iter(combin_para, by='row')
# a <- foreach(para=iterators::iter(combin_para, by='row'), .combine='rbind') %dopar% {
# frank_loglik(para,X, Y, d1, d2)
# }
# value = max(a[is.finite(a)])
# a[!is.finite(a)] = -10000000000000
# value_index = which(a==max(a),arr.ind=TRUE)
#
#
#
# best_combin = 0
#
# par = combin_para[23,]
# parxx= par[1]
# temp_likelihood = -100000
# likelihood = frank_loglik(par,X, Y, d1, d2)
# l1 <- par[1] #hazard rate for non-terminal event
# l2 <- par[2] #hazard rate for terminal event
# theta <- par[3] #association parameter
#
# C <- (exp(-l1*X)^(-theta)+exp(-l2*Y)^(-theta)-1)^(-1/theta) #copula
#
# part1 <- ifelse(d1*d2==1,(log(1+theta)+(1+2*theta)*log(C)-(theta+1)*log(exp(-l1*X))-(theta+1)*log(exp(-l2*Y))+log(l1)-l1*X+log(l2)-l2*Y),0) #both events
# part2 <- ifelse(d1*(1-d2)==1,((theta+1)*log(C)-(theta+1)*log(exp(-l1*X))+log(l1)-l1*X),0) #non-terminal event only
# part3 <- ifelse(((1-d1)*(d2))==1,((theta+1)*log(C)-(theta+1)*log(exp(-l2*Y))+log(l2)-l2*Y),0) #terminal event only
# part4 <- ifelse(((1-d1)*(1-d2))==1,log(C),0) #both events censored
#
# likelihood <- sum(as.numeric(part1)+as.numeric(part2)+as.numeric(part3)+as.numeric(part4) )
# if(temp_likelihood < likelihood)
# {
#
# temp_likelihood = likelihood
# best_combin = variable
# print(best_combin)
# print(temp_likelihood)
# }
#
#
# par_best = combin_para[best_combin,]