Skip to content

Commit 4ffcfdf

Browse files
committed
updated stan code
1 parent edf8507 commit 4ffcfdf

8 files changed

+1208
-535
lines changed

.DS_Store

0 Bytes
Binary file not shown.

education/.DS_Store

2 KB
Binary file not shown.

education/causal_iv_one-sided/Model_01_CACE_with_Exclusion_Restriction.stan

-54
This file was deleted.

education/causal_iv_one-sided/Model_02_CACE_without_Exclusion_Restriction.stan

-57
This file was deleted.

education/causal_iv_one-sided/case_study_02_IV_one-sided.html

+984-337
Large diffs are not rendered by default.

education/causal_iv_one-sided/case_study_02_IV_one-sided.qmd

+104-87
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
data {
2+
int<lower=1> N; // Sample size N
3+
int<lower=0, upper=1> Z[N]; // Treatment assigned Z
4+
int<lower=0, upper=1> W[N]; // Treatment received W
5+
int<lower=0, upper=1> Y[N]; // Outcome Y
6+
}
7+
8+
parameters {
9+
// Population probability of being a complier
10+
real<lower=0, upper=1> pi_c;
11+
12+
// Probabilities for the binomial outcome distributions
13+
real<lower=0, upper=1> eta_c0;
14+
real<lower=0, upper=1> eta_c1;
15+
real<lower=0, upper=1> eta_n;
16+
}
17+
18+
transformed parameters {
19+
// Superpopulation complier average causal effect (CACE)
20+
// in per-1000 units
21+
real CACE = (eta_c1 - eta_c0) * 10^3;
22+
}
23+
24+
model {
25+
// Define local variables for efficiency
26+
real log_pi_c = log(pi_c);
27+
real log1m_pi_c = log1m(pi_c);
28+
29+
// Prior for Complier probability
30+
// implicit prior: pi_c ~ Unif(0, 1)
31+
32+
// Priors for outcome model parameters
33+
eta_c0 ~ beta(2, 2);
34+
eta_c1 ~ beta(2, 2);
35+
eta_n ~ beta(2, 2);
36+
37+
// Likelihood
38+
for(n in 1:N){
39+
40+
// Complier (assigned to treatment)
41+
if (Z[n] == 1 && W[n] == 1){
42+
target += log_pi_c + bernoulli_lpmf(Y[n] | eta_c1) ;
43+
}
44+
45+
// Never-taker (assigned to treatment)
46+
else if (Z[n] == 1 && W[n] == 0){
47+
target += log1m_pi_c + bernoulli_lpmf(Y[n] | eta_n);
48+
}
49+
50+
// Complier or Never-taker (assigned to control)
51+
else if (Z[n] == 0 && W[n] == 0){
52+
target += log_mix(
53+
pi_c, // Complier probability
54+
bernoulli_lpmf(Y[n] | eta_c0), // Complier
55+
bernoulli_lpmf(Y[n] | eta_n) // Never-taker
56+
);
57+
}
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
data {
2+
int<lower=1> N; // Sample size N
3+
int<lower=0, upper=1> Z[N]; // Treatment assigned Z
4+
int<lower=0, upper=1> W[N]; // Treatment received W
5+
int<lower=0, upper=1> Y[N]; // Outcome Y
6+
}
7+
8+
parameters {
9+
// Population probability of being a complier
10+
real<lower=0, upper=1> pi_c;
11+
12+
// Probabilities for the binomial outcome distributions
13+
real<lower=0, upper=1> eta_c0;
14+
real<lower=0, upper=1> eta_c1;
15+
real<lower=0, upper=1> eta_n0;
16+
real<lower=0, upper=1> eta_n1;
17+
}
18+
19+
transformed parameters {
20+
// Super-population average causal effects
21+
real CACE = (eta_c1 - eta_c0) * 10^3;
22+
real NACE = (eta_n1 - eta_n0) * 10^3;
23+
}
24+
25+
model {
26+
// Define local variables for efficiency
27+
real log_pi_c = log(pi_c);
28+
real log1m_pi_c = log1m(pi_c);
29+
30+
// Prior for Complier probability
31+
// implicit prior: pi_c ~ Unif(0, 1)
32+
33+
// Priors for outcome model parameters
34+
eta_c0 ~ beta(2, 2);
35+
eta_c1 ~ beta(2, 2);
36+
eta_n0 ~ beta(2, 2);
37+
eta_n1 ~ beta(2, 2);
38+
39+
// Likelihood
40+
for(n in 1:N){
41+
42+
// Complier (assigned to treatment)
43+
if (Z[n] == 1 && W[n] == 1){
44+
target += log_pi_c + bernoulli_lpmf(Y[n] | eta_c1) ;
45+
}
46+
47+
// Never-taker (assigned to treatment)
48+
else if (Z[n] == 1 && W[n] == 0){
49+
target += log1m_pi_c + bernoulli_lpmf(Y[n] | eta_n1);
50+
}
51+
52+
// Complier or Never-taker (assigned to control)
53+
else if (Z[n] == 0 && W[n] == 0){
54+
target += log_mix(
55+
pi_c, // Complier probability
56+
bernoulli_lpmf(Y[n] | eta_c0), // Complier
57+
bernoulli_lpmf(Y[n] | eta_n0) // Never-taker
58+
);
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)