-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions_GLM_real_data.R
More file actions
278 lines (214 loc) · 8.86 KB
/
Functions_GLM_real_data.R
File metadata and controls
278 lines (214 loc) · 8.86 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#######################################
# Functions for estimation #
#######################################
expit <- function(x){
1/(exp(-x)+1)
}
compare.metric<-function(theta, Test){
x <- Test$x
y <- Test$y
pred <- expit(theta[1] + x%*%theta[-1])
return(list(pred = as.numeric(pred), y = y))
}
local_es<-function(Target,family = c("binomial")){
y.t <- Target$y
x.t <- Target$x
fit0 <- cv.glmnet(x.t, y.t, family = family)
beta <- as.vector(coef(fit0, s = "lambda.min"))
return(beta)
}
pool_TL <- function(Target,
Source,
family = c("binomial"),
support){
y.t <- Target$y
x.t <- Target$x
p <- ncol(x.t) + 1
M <- length(Source)
y.s <- NULL
x.s <- NULL
offset.all <- rep(0, length(y.t))
for (m in 1:M) {
source.m <- Source[[m]]
x.sm <- source.m$x
y.sm <- source.m$y
y.s <- c(y.s, y.sm)
x.s <- rbind(x.s, x.sm)
fit0 <- cv.glmnet(x.sm, y.sm, family = family)
beta <- as.vector(coef(fit0, s = "lambda.min"))
beta.S <- beta
beta.Sc <- beta
beta.S[-c(support + 1)] <- 0
beta.Sc[c(support + 1)] <- 0
offset <- x.t %*% beta.S[-1]
fit_cv <- cv.glmnet(x.t, y.t, family = family, offset = offset)
delta.m <- as.vector(coef(fit_cv, s = "lambda.min")) - beta.Sc
delta.m <- -delta.m
offset.all <- c(offset.all, delta.m[1] + x.sm %*% delta.m[-1])
}
x.all <- rbind(x.t, x.s)
y.all <- c(y.t, y.s)
fit_cv <- cv.glmnet(x.all, y.all, family = family, offset = offset.all)
beta.min <- as.vector(coef(fit_cv, s = "lambda.min"))
return(list(beta.min = beta.min))
}
point_est_AUC <- function(outcome_list, covariate_list, random_seed){
set.seed(random_seed)
site_size <- rep(0, 4)
for(i in 1:4){
site_size[i] <- length(outcome_list[[i]])
}
# the third site is the target
Source <- as.list(rep(1, 3))
Source[[1]] <- list(y = outcome_list[[1]], x = as.matrix(covariate_list[[1]]))
Source[[2]] <- list(y = outcome_list[[2]], x = as.matrix(covariate_list[[2]]))
Source[[3]] <- list(y = outcome_list[[4]], x = as.matrix(covariate_list[[4]]))
Target <- list(y=NULL,x=NULL)
Test <- list(y=NULL,x=NULL)
target_size <- site_size[3]
testing_index <- sample(1:target_size, size = ceiling(0.4 * target_size))
Target$x <- as.matrix(covariate_list[[3]][-testing_index, ])
Target$y <- outcome_list[[3]][-testing_index]
Test$x <- as.matrix(covariate_list[[3]][testing_index, ])
Test$y <- outcome_list[[3]][testing_index]
p <- ncol(covariate_list[[3]])
####local lasso using target only
beta.local <- local_es(Target, family = c("binomial"))
re.local<-compare.metric(beta.local,Test)
local_pred <- re.local$pred
y_truth <- re.local$y
support <- which(beta.local[-1]!=0)
####Per Trans
fit <- pool_TL(Target, Source, family=c("binomial"), support)
beta.pool.min <- fit$beta.min
beta.pool.min1 <- beta.pool.min
re.pool.min <- compare.metric(beta.pool.min, Test)
per_pred <- re.pool.min$pred
####Trans all
fit<-pool_TL(Target, Source, family=c("binomial"), support = 1:(p-1))
beta.pool.all <- fit$beta.min
re.pool.all <- compare.metric(beta.pool.all, Test)
per_all_pred <- re.pool.all$pred
#### Ah-Trans
fit <- glmtrans(Target, Source, family = "binomial", transfer.source.id = "auto")
beta.auto <- fit$beta
re.auto <- compare.metric(beta.auto,Test)
feng_pred <- re.auto$pred
error_local <- mean(I(local_pred > 0.5) * I(y_truth == 0) + I(local_pred <= 0.5) * I(y_truth == 1))
auc_local <- auc(roc(response = y_truth, predictor = local_pred))
error_per <- mean(I(per_pred > 0.5) * I(y_truth == 0) + I(per_pred <= 0.5) * I(y_truth == 1))
auc_per <- auc(roc(response = y_truth, predictor = per_pred))
error_per_all <- mean(I(per_all_pred > 0.5) * I(y_truth == 0) + I(per_all_pred <= 0.5) * I(y_truth == 1))
auc_per_all <- auc(roc(response = y_truth, predictor = per_all_pred))
error_feng <- mean(I(feng_pred > 0.5) * I(y_truth == 0) + I(feng_pred <= 0.5) * I(y_truth == 1))
auc_feng <- auc(roc(response = y_truth, predictor = feng_pred))
AUC_res <- c(auc_local, auc_per, auc_per_all, auc_feng)
err_res <- c(error_local, error_per, error_per_all, error_feng)
return(list(AUC = AUC_res, error = err_res))
}
bootstrap_fun <- function(boot, covariate_list = c(), outcome_list = c(), random_seed = c()){
####prepare for sampling
original_covariates <- as.matrix(covariate_list[[3]])
original_outcome <- outcome_list[[3]]
p <- ncol(original_covariates)
target_size <- length(original_outcome)
# the third site is the target
Source <- as.list(rep(1, 3))
Source[[1]] <- list(y = outcome_list[[1]], x = as.matrix(covariate_list[[1]]))
Source[[2]] <- list(y = outcome_list[[2]], x = as.matrix(covariate_list[[2]]))
Source[[3]] <- list(y = outcome_list[[4]], x = as.matrix(covariate_list[[4]]))
set.seed(random_seed)
testing_index <- sample(1:target_size, size = ceiling(0.4 * target_size))
set.seed(random_seed*1000 + boot)
target_idx <- sample(1:target_size, size = target_size, replace = TRUE)
target_covariates <- original_covariates[target_idx, ]
target_outcome <- original_outcome[target_idx]
Target <- list(y=NULL,x=NULL)
Test <- list(y=NULL,x=NULL)
Target$x <- target_covariates[-testing_index, ]
Target$y <- target_outcome[-testing_index]
Test$x <- target_covariates[testing_index, ]
Test$y <- target_outcome[testing_index]
####local lasso using target only
beta.local <- local_es(Target, family = c("binomial"))
re.local<-compare.metric(beta.local,Test)
local_pred <- re.local$pred
y_truth <- re.local$y
support <- which(beta.local[-1]!=0)
# as.numeric(support_true)
if(length(support) > 1){
####Per Trans
fit <- pool_TL(Target, Source, family=c("binomial"), support)
beta.pool.min <- fit$beta.min
re.pool.min <- compare.metric(beta.pool.min, Test)
per_pred <- re.pool.min$pred
####Trans all
fit<-pool_TL(Target, Source, family=c("binomial"), support = 1:(p-1))
beta.pool.all <- fit$beta.min
re.pool.all <- compare.metric(beta.pool.all, Test)
per_all_pred <- re.pool.all$pred
#### Ah-Trans
fit <- glmtrans(Target, Source, family = "binomial", transfer.source.id = "auto")
beta.auto <- fit$beta
re.auto <- compare.metric(beta.auto,Test)
feng_pred <- re.auto$pred
error_local <- mean(I(local_pred > 0.5) * I(y_truth == 0) + I(local_pred <= 0.5) * I(y_truth == 1))
auc_local <- auc(roc(response = y_truth, predictor = local_pred))
error_per <- mean(I(per_pred > 0.5) * I(y_truth == 0) + I(per_pred <= 0.5) * I(y_truth == 1))
auc_per <- auc(roc(response = y_truth, predictor = per_pred))
error_per_all <- mean(I(per_all_pred > 0.5) * I(y_truth == 0) + I(per_all_pred <= 0.5) * I(y_truth == 1))
auc_per_all <- auc(roc(response = y_truth, predictor = per_all_pred))
error_feng <- mean(I(feng_pred > 0.5) * I(y_truth == 0) + I(feng_pred <= 0.5) * I(y_truth == 1))
auc_feng <- auc(roc(response = y_truth, predictor = feng_pred))
AUC_res <- c(auc_local, auc_per, auc_per_all, auc_feng)
err_res <- c(error_local, error_per, error_per_all, error_feng)
total_res <- list(boot, AUC_res, err_res)
}else{
total_res <- boot
}
return(total_res)
}
# ---- helper: 3x4 matrix -> df ----
mat_to_forest_df <- function(M,
methods = c("Local", "Targeted-IFS", "Targeted-IFS-all", "Ah-Trans")) {
stopifnot(is.matrix(M), nrow(M) == 3, ncol(M) == 4)
colnames(M) <- methods
data.frame(
Method = factor(methods, levels = methods),
lower = as.numeric(M[1, ]),
point = as.numeric(M[2, ]),
upper = as.numeric(M[3, ])
)
}
# ---- helper: forest plot ----
plot_forest_refined <- function(df, xlab, x_breaks = waiver(), x_limits = NULL) {
stopifnot(all(c("Method", "lower", "point", "upper") %in% names(df)))
df$Method <- factor(df$Method, levels = my_order)
ggplot(df, aes(y = Method, x = point, color = Method)) +
geom_errorbarh(
aes(xmin = lower, xmax = upper),
height = 0.22,
linewidth = 0.9
) +
geom_point(size = 2.8) +
scale_color_manual(values = method_cols, drop = FALSE) +
# avoid x-axis text being too close to boundary (add left/right padding)
scale_x_continuous(
breaks = x_breaks,
limits = x_limits,
expand = expansion(mult = c(0.04, 0.08))
) +
labs(x = xlab, y = NULL) +
coord_cartesian(clip = "off") +
theme_bw(base_size = 12) +
theme(
legend.position = "none",
panel.grid.major.y = element_blank(),
panel.grid.minor = element_blank(),
axis.title.x = element_text(size = 13),
axis.text.x = element_text(size = 13),
axis.text.y = element_text(size = 13, angle = 30, hjust = 1),
# extra margins to prevent clipping in Viewer / saving
plot.margin = margin(t = 8, r = 18, b = 10, l = 10)
)
}