-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscen1_BinaryOutcome.m
275 lines (196 loc) · 9.78 KB
/
scen1_BinaryOutcome.m
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
function results = scen1_BinaryOutcome(seed,n,run_ID,isdisplay)
%% The source code is revised from the R code from
% Roy, J., Lum, K.J., Zeldow, B., Dworkin, J.D., Re III, V.L.
% and Daniels, M.J., 2017. Bayesian nonparametric generative models
% for causal inference with missing at random covariates. Biometrics.
% tic
%% Set random seed-------------------------------------------------------------
rng(seed);
inputfile = ['datasets/scen1/scen1_n' num2str(n) '_run_ID' num2str(run_ID) '.txt'];
tmpdata = dlmread(inputfile);
pt = 1;
p1 = 2;
p2 = 2;
T = tmpdata(:,pt);
X = tmpdata(:,pt+1:end-1);
Y = tmpdata(:,end);
% Standardize continuous covariates (important when choosing priors).
X(:,p1+1:p1+p2) = zscore(X(:,p1+1:p1+p2));
% Define design matrix.
X_matrix = [ones(size(T,1),1),X];
TX = [T,X];
TX_matrix = [ones(size(T,1),1),TX];
% End generation of dataset-----------------------------------------------
%% Define constants--------------------------------------------------------------
if n < 1000
% Define number of Markov Chain Monte Carlo (MCMC) draws in Gibbs Sampler.
% For small n (e.g. n = 250), need more samples.
GIBBS_TOTAL = 10000;
% Define number of MCMC draws to 'burn' in Gibbs Sampler (check convergence).
% For small n (e.g. n = 250).
BURN_IN = 2000;
else
% Define number of Markov Chain Monte Carlo (MCMC) draws in Gibbs Sampler.
% For big n (e.g. n = 1000), can use less samples (check posterior).
GIBBS_TOTAL = 10000;
% Define number of MCMC draws to 'burn' in Gibbs Sampler (check convergence).
BURN_IN = 2000;
end
% The value of auxiliary parameters for new clusters in Gibbs
% sampling.
NUM_AUX_PARAM = 5;
% Define number of Monte Carlo draws when using Monte Carlo integration to
% integrate out confounders.
NUM_MC = 10000;
% Define number of observations per draw when using Monte Carlo integration to
% integrate out confounders.
M = 1000;
% End Define constants --------------------------------------------------
%% Set initial values ------------------------------------------------------------
%% Hyperparameters-----------------------------------------------------------------
% For modeling binary covariates.
a0 = 1;
b0 = 1;
% For modeling continuous covariates.
nu0 = 2;
tau0 = 1;
c0 = 0.5;
mu0 = 0;
% For concentration parameters.
alpha_shape = 1;
alpha_rate = 1;
%% Initialize alpha parameters-----------------------------------------------------
alpha_theta = 1;
alpha_psi = 1;
alpha_omega = 1;
%% Parameters for cluster memberships------------------------------------------
% Set initial values for cluster membership,y,t,x
% Use k-means to find data clusters
z = init_cluster_kmeans(Y,TX,X,2,2,2);
% % Calculate number of clusters
[k,kjs,kljs] = computeclusnum(z);
% End Set initial values.
%% CHANGE OCCURS HERE------------------------------------------------------------
%% Make file_name where results will be stored for each dataset-------------------
resultsfile = ['results/scen1/cedp_scen1_n' num2str(n) '_run_ID' num2str(run_ID) '.mat'];
workplacefile = ['results/scen1_workplace/cedp_scen1_n' num2str(n) '_run_ID' num2str(run_ID) '.mat'];
%% Update values of parameters-----------------------------------------------------
% % For outcome regression coefficients parameters in outcome regression.
[beta_coeff0,diagbetacov0,beta_coeff] = init_beta_params_bern(Y,TX,TX_matrix,z,k);
%% Update values of parameters for treatment regression coefficients-----------------
[gamma_coeff0,diaggammacov0,gamma_coeff] = init_gamma_params(T,X,X_matrix,z,k,kjs);
%% Update covariate parameters------------------------------------------------------
% Within each cluster parameters are stored in a long vector nXClus x 1.
% Binary covariates have 1 parameter (x_pi_param).
% Continuous covariates have 2 parameters (x_mean_param, x_var_param).
x_pi_param = update_binarycovs_params(X,p1,z,k,kjs,kljs,a0,b0);
[x_mean_param,x_var_param] = update_continuouscovs_params(X,p1,p2,z,k,kjs,kljs,nu0,tau0,c0,mu0);
%% For MC integration------------------------------------------------------------
% Calculate f0y,f0t,f0x for use in calculating causal effect in Gibbs Sampler.
% Average covariate distribution over prior for each x_i
% variables of x.
beta_coeff_prior_mc = mvnrnd(beta_coeff0,diagbetacov0,NUM_MC);
gamma_coeff_prior_mc = mvnrnd(gamma_coeff0,diaggammacov0,NUM_MC);
% Make vectors to store draws from Gibbs Sampler.
psi_rr = NaN(GIBBS_TOTAL,1);
psi_rd = NaN(GIBBS_TOTAL,1);
%% Parameters for analysing----------------------------------------------------------
% Store the number of each y cluster.
yclusNum = zeros(GIBBS_TOTAL,1);
% Store the log likehood of data.
loglikeregData = zeros(GIBBS_TOTAL,1);
% Store the values of alpha.
alpha_theta_draws = zeros(GIBBS_TOTAL,1);
alpha_psi_draws = zeros(GIBBS_TOTAL,1);
alpha_omega_draws = zeros(GIBBS_TOTAL,1);
%% Start Gibbs Sampler -------------------------------------------------------
for gibbsreps = 1:GIBBS_TOTAL
% First draw each parameter for BNP model. Then calculate causal effect.
%% Update cluster membership-------------------------------------------------------
[z,beta_coeff,gamma_coeff,x_pi_param,x_mean_param,...
x_var_param] = update_cluster_binary(T,X,Y,p1,p2,z,beta_coeff,gamma_coeff,x_pi_param,x_mean_param,...
x_var_param,alpha_theta,alpha_psi,alpha_omega,NUM_AUX_PARAM,beta_coeff0, diagbetacov0,...
gamma_coeff0,diaggammacov0,c0, mu0, nu0, tau0, a0, b0);
if mod(gibbsreps,1000) == 0
fprintf([' sample ' num2str(gibbsreps),' k ', num2str( size(beta_coeff,1)) '\n']);
end
% End Update cluster membership--------------------------------------------------
%% Calculation of parameters of clusters------------------------------------
% Calculate number of clusters.
[k,kjs,kljs] = computeclusnum(z);
% Calculate numbers of data in each cluster.
[njs,nljs,nhljs] = computenuminclus(z,k,kjs,kljs);
%% Update parameters------------------------------------------------------------------------
% Update beta parameters.
beta_coeff = update_betas_bern(Y,TX_matrix,z,k,beta_coeff, beta_coeff0,diagbetacov0);
% Update parameters for T.
gamma_coeff = update_gammas(T,X_matrix,z,k,kjs,gamma_coeff,gamma_coeff0,diaggammacov0);
% Update parameters for X.
if p1 > 0
x_pi_param = update_binarycovs_params(X,p1,z,k,kjs,kljs,a0,b0);
end
% Update parameters for continuous covariates.
if p2 > 0
[x_mean_param,x_var_param] = update_continuouscovs_params(X,p1,p2,z,k,kjs,kljs,nu0,tau0,c0,mu0);
end
% End Update parameters --------------------------------------------
%% Store the number of each y cluster and calculate the log likehood of data---------------
yclusNum(gibbsreps) = k;
loglikeregData(gibbsreps) = computeloglike_binary(T,X,Y,TX_matrix,X_matrix,p1,p2,...
z,k,kjs,kljs,beta_coeff,gamma_coeff,x_pi_param,x_mean_param,x_var_param);
%% Update concentration parameters--------------------------------------------
alpha_theta = update_alpha_theta(alpha_shape,alpha_rate,n,k,alpha_theta);
alpha_psi = update_alpha_psi(alpha_shape,alpha_rate,z,alpha_psi);
alpha_omega = update_alpha_omega(alpha_shape,alpha_rate,z,alpha_omega);
alpha_theta_draws(gibbsreps,1) = alpha_theta;
alpha_psi_draws(gibbsreps,1) = alpha_psi;
alpha_omega_draws(gibbsreps,1) = alpha_omega;
% End Update concentration parameters-----------------------------------
% End Update of all parameters in BNP model--------------------------------
%% Calculate causal effect using Monte Carlo Integration to integrate out confounders.
% Don't need to do every iteration so compute every 100th draw after
% BURN_IN).
% Start----------------------------------------------------------------------
if gibbsreps >= BURN_IN && mod(gibbsreps,100) == 0
%% Calculation of causal effects
[psi_rr(gibbsreps,1),psi_rd(gibbsreps,1)] = compute_effect_binary(X,n,p1,p2,z,k,kjs,kljs,njs,nljs,nhljs,...
beta_coeff,gamma_coeff,x_pi_param,x_mean_param,x_var_param,...
alpha_theta,alpha_psi,alpha_omega,beta_coeff0,diagbetacov0,gamma_coeff0,diaggammacov0,...
c0, mu0, nu0, tau0, a0, b0,M,beta_coeff_prior_mc,gamma_coeff_prior_mc);
% Error handling
if isfinite(psi_rr(gibbsreps,1)) == false
return;
end
end
end % End Gibbs Sampler loop ---------------------------------------------
%% Calculate summary statistics from posterior distributions
median_alpha_theta = median(alpha_theta_draws(BURN_IN:GIBBS_TOTAL,1));
median_alpha_psi = median(alpha_psi_draws(BURN_IN:GIBBS_TOTAL,1));
median_alpha_omega = median(alpha_omega_draws(BURN_IN:GIBBS_TOTAL,1));
% True values of average relative risk (for this scenario)
psi_rr_true = 1.5;
[median_psi_rr,sd_psi_rr,cov_psi_rr,width_psi_rr] = compute_psi_rd_stats(psi_rr_true,psi_rr);
% True values of average risk difference (for this scenario)
psi_rd_true = 0.13;
[median_psi_rd,sd_psi_rd,cov_psi_rd,width_psi_rd] = compute_psi_rd_stats(psi_rd_true,psi_rd);
% Save results to a vector
results = [median_alpha_theta,median_alpha_psi,median_alpha_omega,median_psi_rr,sd_psi_rr,cov_psi_rr,width_psi_rr,median_psi_rd,sd_psi_rd,cov_psi_rd,width_psi_rd];
% Output results to a file
save(resultsfile,'results');
save(workplacefile);
%% Visualize trace plot for analysing
if isdisplay == 1
% Plot y clusters
plot_yclusNum_hist(yclusNum);
plot_yclusNum(yclusNum);
% Plot log likelihood of data
plot_loglike(loglikeregData);
% Plot concentration parameters
plot_alpha_theta(alpha_theta_draws);
plot_alpha_psi(alpha_psi_draws);
plot_alpha_omega(alpha_omega_draws);
% Plot relative causal risk and average causal effects
plot_psi_rr(psi_rr(~isnan(psi_rr)));
plot_psi_rd(psi_rd(~isnan(psi_rd)));
end
% toc