Skip to content

Commit ac8e1f4

Browse files
committed
Merge pull request #709 from stan-dev/feature/ARM
Fixes #707. Feature/arm models
2 parents 03aebd6 + 876f973 commit ac8e1f4

File tree

429 files changed

+154915
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

429 files changed

+154915
-0
lines changed

ARM/Ch.10/10.3_Matching.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Computation of propensity score matches
2+
#FIXME: MISSING DATA
3+
4+
# starting point
5+
6+
# second model
7+
8+
# predicted values
9+
10+
# regression on matched data
11+
12+
## Propensity score as a one-number summary (Figure 10.7)
13+
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
library(rstan)
2+
library(ggplot2)
3+
4+
ideo2 <- read.table ("ideo2.dat")
5+
candidate.effects <- read.table ("candidate_effects.dat", row.names=1)
6+
7+
# Simple correction for incumbency advantage
8+
incadv <- function (years){
9+
ifelse (years<46, .02,
10+
ifelse (years<66, .02 + .08*(years-46)/(66-46), .10))
11+
}
12+
13+
# Regions of the country
14+
region <- c(3,4,4,3,4,4,1,1,3,3,4,4,2,2,2,2,3,3,1,1,1,2,2,3,2,4,2,4,1,1,4,1,3,2,2,3,4,1,1,3,2,3,3,4,1,3,4,1,2,4)
15+
south <- region==3
16+
17+
18+
# add stuff to ideo2
19+
20+
normvote <- rep (NA, nrow(ideo2))
21+
dum1 <- ideo2[,"dpvote"]
22+
dum2 <- ideo2[,"stgj"]
23+
dum3 <- (ideo2[,"year"]-44)/4
24+
for (i in 1:length(normvote)){
25+
normvote[i] <- dum1[i] - candidate.effects[dum2[i],dum3[i]]
26+
}
27+
dvfix <- ideo2[,"dv"] - incadv(ideo2[,"year"])*ideo2[,"i2"]
28+
dvpfix <- ideo2[,"dvp"] - incadv(ideo2[,"year"]-2)*ideo2[,"i1"]
29+
ideo2 <- cbind(ideo2[,1:19], normvote, dvfix, dvpfix)
30+
names(ideo2) <- c(names(ideo2)[1:19], "normvote", "dvfix", "dvpfix")
31+
32+
# impute normal vote from 2 years earlier: ideo2
33+
34+
for (year in seq(62,94,4)){
35+
yr.cond <- ideo2[, "year"] == year
36+
normvote <- rep (NA, sum(yr.cond))
37+
dpvote <- rep (NA, sum(yr.cond))
38+
stgj <- ideo2[yr.cond,"stgj"]
39+
cd <- ideo2[yr.cond,"cd"]
40+
indexes <- (1:nrow(ideo2))[yr.cond]
41+
yr2.cond <- ideo2[, "year"] == year-2
42+
data2 <- ideo2[yr2.cond,c("stgj","cd","normvote","dpvote")]
43+
for (i in 1:sum(yr.cond)){
44+
cond <- data2[,"stgj"]==stgj[i] & data2[,"cd"]==cd[i]
45+
if (sum(cond)==1){
46+
normvote[i] <- data2[cond,c("normvote")]
47+
dpvote[i] <- data2[cond,c("dpvote")]
48+
}
49+
}
50+
ideo2[yr.cond,c("normvote")] <- normvote
51+
ideo2[yr.cond,c("dpvote")] <- dpvote
52+
}
53+
54+
year <- 94
55+
yr.cond <- ideo2[, "year"] == year
56+
data <- ideo2[yr.cond, ]
57+
deminc.cond <- (data[, "dvp"] > 0.5) & (abs(data[,"i2"]) == 1)
58+
repinc.cond <- (data[, "dvp"] < 0.5) & (abs(data[,"i2"]) == 1)
59+
#
60+
# fudge for 1992, 1994
61+
if (year>=92) data[,"occup"] <- rep(0,nrow(data))
62+
#
63+
dum <- apply(is.na(data),1,sum)
64+
ok <- dum==0 & !south[data[,"stgj"]]
65+
attach(data)
66+
67+
# Plot figure 10.8
68+
frame1 = data.frame(x1=1-dvp[deminc.cond],y1=score1[deminc.cond])
69+
frame2 = data.frame(x2=1-dvp[repinc.cond],y2=score1[repinc.cond])
70+
p1 <- ggplot() +
71+
geom_point(data=frame1,aes(x=x1,y=y1),shape="x") +
72+
geom_point(data=frame2,aes(x=x2,y=y2),shape="o") +
73+
scale_y_continuous("(liberal) ideology score (conservative)") +
74+
scale_x_continuous("Republican's vote share") +
75+
theme_bw()
76+
print(p1)
77+
78+
# regression discontinuity analysis
79+
80+
x <- 1 - dvp
81+
party <- ifelse (dvp<.5, 1, 0)
82+
83+
## Regression in the area near the discontinuity (ideo_two_pred.stan)
84+
## lm (score1 ~ party + x, subset=overlap)
85+
86+
overlap <- (deminc.cond | repinc.cond) & dvp>.45 & dvp<.55 & !is.na(score1+party+x)
87+
sc1 <- score1[overlap]
88+
p1 <- party[overlap]
89+
x1 <- x[overlap]
90+
dataList.1 <- list(N=length(sc1), score1=sc1,party=p1,x=x1)
91+
ideo_two_pred.sf1 <- stan(file='ideo_two_pred.stan', data=dataList.1,
92+
iter=1000, chains=4)
93+
print(ideo_two_pred.sf1)
94+
95+
## Regression fit to all data (ideo_two_pred.stan)
96+
## lm (score1 ~ party + x, subset=incs)
97+
incs <- (deminc.cond | repinc.cond) & !is.na(score1+party+x)
98+
sc2 <- score1[incs]
99+
p2 <- party[incs]
100+
x2 <- x[incs]
101+
dataList.2 <- list(N=length(sc2), score1=sc2,party=p2,x=x2)
102+
ideo_two_pred.sf2 <- stan(file='ideo_two_pred.stan', data=dataList.2,
103+
iter=1000, chains=4)
104+
print(ideo_two_pred.sf2)
105+
106+
## Regression with interactions (ideo_interactions.stan)
107+
## lm (score1 ~ party + x + party:x, subset=incs)
108+
109+
ideo_interactions.sf1 <- stan(file='ideo_interactions.stan', data=dataList.2,
110+
iter=1000, chains=4)
111+
print(ideo_interactions.sf1)
112+
113+
## Reparametrized regression (ideo_reparam.stan)
114+
## lm (score1 ~ party + I(z*(party==0)) + I(z*(party==1)), subset=incs)
115+
116+
z <- x2 - 0.5
117+
z.1 <- I(z*(p2==0))
118+
z.2 <- I(z*(p2==1))
119+
dataList.3 <- list(N=length(sc2), score1=sc2,party=p2,z1=z.1,z2=z.2)
120+
ideo_reparam.sf1 <- stan(file='ideo_reparam.stan', data=dataList.3,
121+
iter=1000, chains=4)
122+
print(ideo_reparam.sf1)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
library(rstan)
2+
library(ggplot2)
3+
library(foreign)
4+
5+
sesame <- read.dta("sesame.dta")
6+
attach(sesame)
7+
8+
## Rename variables of interest
9+
watched <- regular
10+
encouraged <- encour
11+
y <- postlet
12+
13+
## Instrumental variables estimate (sesame_one_pred_a.stan)
14+
## lm (watched ~ encouraged)
15+
16+
dataList.1 <- list(N=length(watched), watched=watched,encouraged=encouraged)
17+
sesame_one_pred_a.sf1 <- stan(file='sesame_one_pred_a.stan', data=dataList.1,
18+
iter=1000, chains=4)
19+
print(sesame_one_pred_a.sf1)
20+
21+
beta.post <- extract(sesame_one_pred_a.sf1, "beta")$beta
22+
beta.mean1 <- colMeans(beta.post)
23+
24+
## (sesame_one_pred_b.stan)
25+
## lm (y ~ encouraged)
26+
27+
dataList.2 <- list(N=length(y), watched=y,encouraged=encouraged)
28+
sesame_one_pred_b.sf1 <- stan(file='sesame_one_pred_a.stan', data=dataList.2,
29+
iter=1000, chains=4)
30+
print(sesame_one_pred_b.sf1)
31+
32+
beta.post <- extract(sesame_one_pred_b.sf1, "beta")$beta
33+
beta.mean2 <- colMeans(beta.post)
34+
35+
36+
iv.est.1 <- beta.mean2[2] / beta.mean1[2]
37+
print(iv.est.1)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
library(rstan)
2+
library(ggplot2)
3+
4+
source("10.5_CasualEffectsUsingIV.R") # where data was cleaned
5+
6+
## Rename variables of interest
7+
8+
pretest <- prelet
9+
10+
## 2 stage least squares (sesame_one_pred_a.stan)
11+
## lm (watched ~ encouraged)
12+
13+
dataList.1 <- list(N=length(watched), watched=watched,encouraged=encouraged)
14+
sesame_one_pred_2a.sf1 <- stan(file='sesame_one_pred_a.stan', data=dataList.1,
15+
iter=1000, chains=4)
16+
print(sesame_one_pred_2a.sf1)
17+
beta.post <- extract(sesame_one_pred_2a.sf1, "beta")$beta
18+
beta.mean2a <- colMeans(beta.post)
19+
20+
watched.hat <- beta.mean2a[1] + beta.mean2a[2] * encouraged
21+
22+
## (sesame_one_pred_2b.stan)
23+
## lm (y ~ watched.hat)
24+
25+
dataList.2 <- list(N=length(y), watched=y,encouraged=watched.hat)
26+
sesame_one_pred_2b.sf1 <- stan(file='sesame_one_pred_a.stan', data=dataList.2,
27+
iter=1000, chains=4)
28+
print(sesame_one_pred_2b.sf1)
29+
30+
## Adjusting for covariates in a IV framework (sesame_multi_preds_3a.stan)
31+
## lm (watched ~ encouraged + pretest + as.factor(site) + setting)
32+
33+
dataList.3 <- list(N=length(watched), watched=watched,encouraged=encouraged,pretest=pretest, site=site,setting=setting)
34+
sesame_multi_pred_3a.sf1 <- stan(file='sesame_multi_preds_3a.stan',
35+
data=dataList.3,
36+
iter=1000, chains=4)
37+
print(sesame_multi_pred_3a.sf1)
38+
39+
beta.post <- extract(sesame_multi_pred_3a.sf1, "beta")$beta
40+
beta.mean3a <- colMeans(beta.post)
41+
42+
watched.hat <- beta.mean3a[1] + beta.mean3a[2] * encouraged + beta.mean3a[3] * pretest + beta.mean3a[4] * (site==2) + beta.mean3a[5] * (site==3) + beta.mean3a[6] * (site==4) + beta.mean3a[7] * (site==5) + beta.mean3a[8] * setting
43+
44+
## (sesame_multi_preds_3b.stan)
45+
## lm (y ~ watched.hat + pretest + as.factor(site) + setting)
46+
dataList.4 <- list(N=length(watched.hat), watched=y,encouraged=watched.hat,pretest=pretest, site=site,setting=setting)
47+
sesame_multi_pred_3b.sf1 <- stan(file='sesame_multi_preds_3b.stan',
48+
data=dataList.4,
49+
iter=1000, chains=4)
50+
print(sesame_multi_pred_3b.sf1)
51+
52+
## Se for IV estimates (FIXME)
53+
54+
## Performing 2sls automatically
55+
56+
# regression without pre-treatment variables
57+
58+
# regression controlling for pre-treatment variables

ARM/Ch.10/README

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Data
2+
====
3+
4+
1. ideo_incs.data.R
5+
- N : number of observations
6+
- party : party association? 1: Republican, 0: Democrat
7+
- score1: effect of election results
8+
- x : pretreatment variable
9+
10+
2. ideo_overlap.data.R
11+
- N : number of observations
12+
- party : party association? 1: Republican, 0: Democrat
13+
- score1: effect of election results
14+
- x : pretreatment variable
15+
16+
3. ideo_reparam.data.R
17+
- N : number of observations
18+
- party : party association? 1: Republican, 0: Democrat
19+
- score1: effect of election results
20+
- z1 : reparameterized term 1
21+
- z2 : reparameterized term 2
22+
23+
4. sesame.data.R
24+
- N : number of observations
25+
- encouraged : encouraged to watch? 1: Yes, 0: No
26+
- watched : watched Sesame Street? 1: Yes, 0: No
27+
- watched_hat: estimated casual effect of watching sesame street
28+
- y : post test scores
29+
30+
5. sesame_one_pred_2b.data.R
31+
- N : number of observations
32+
- encouraged : encouraged to watch? 1: Yes, 0: No
33+
- watched : watched Sesame Street? 1: Yes, 0: No
34+
- watched_hat: estimated casual effect of watching sesame street
35+
- y : post test scores
36+
37+
6. sesame_multi_preds_3b.data.R
38+
- N : number of observations
39+
- encouraged : encouraged to watch? 1: Yes, 0: No
40+
- pretest : pre test scores
41+
- setting : setting category
42+
- site : site category
43+
- watched : watched Sesame Street? 1: Yes, 0: No
44+
- watched_hat: estimated casual effect of watching sesame street
45+
- y : post test scores
46+
47+
Models
48+
======
49+
50+
1. One predictor
51+
sesame_one_pred_2b.stan: lm(y ~ watched_hat)
52+
sesame_one_pred_a.stan : lm(watched ~ encouraged)
53+
sesame_one_pred_b.stan : lm(y ~ encouraged)
54+
55+
3. Multiple predictors without interaction
56+
ideo_reparam.stan : lm(score1 ~ party + z1 + z2)
57+
ideo_two_pred.stan : lm(score1 ~ party + x)
58+
sesame_mult_preds_3a.stan: lm(y ~ encouraged + pretest + factor(site) + setting)
59+
sesame_mult_preds_3b.stan: lm(y ~ watched_hat + pretest + factor(site) + setting)
60+
61+
3. Multiple predictors with interaction
62+
ideo_interactions.stan: lm(score1 ~ party + x + party:x)

ARM/Ch.10/candidate_effects.dat

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
AL -0.0074 0.0128 -0.0042 -0.0309 -0.1137 -0.0116 -0.0107 0.0601 0.0708 0.0125 0.0033 0.0595
2+
AK NA NA NA -0.011 0.0073 -0.0007 -0.0069 -0.0331 -0.0463 -0.0144 -0.0144 -0.0085
3+
AZ -0.006 -0.0035 -0.0029 0.0032 -0.0363 -0.0116 -0.0149 -0.0404 -0.0441 -0.0144 -0.0127 -0.0085
4+
AR 0.0074 0.0076 0.0068 -0.0319 -0.0233 -0.0046 -0.0092 0.0604 0.0794 0.0144 0.0035 0.094
5+
CA -0.0242 -0.0217 -0.0211 -0.0315 0.0053 -0.0368 -0.041 0.0057 -0.0277 -0.033 0.0022 0.0055
6+
CO -0.0074 -0.007 -0.0064 -0.0127 -0.0033 -0.0066 -0.012 -0.0364 -0.0396 -0.0093 -0.0073 -0.0006
7+
CT -0.006 -0.0035 -0.0028 0.0737 0.0399 0.0038 -0.0018 0.0058 0.0006 0.0015 0.001 0.003
8+
DE -0.0016 0.0009 0.0015 -0.014 0.0072 -0.0029 -0.0091 0.0025 0.0069 0.0094 0.0045 -0.0007
9+
FL -0.0001 0.0024 0.0031 -0.0223 -0.0363 -0.0116 -0.0149 0.0582 0.0665 0.0012 -0.0058 0.0565
10+
GA 0.0074 0.0076 0.0068 -0.0338 -0.1137 -0.0062 -0.0149 0.0898 0.1109 0.0144 0.0089 0.0596
11+
HI NA NA NA 0.0274 0.0159 0.0092 0.0105 0.0068 0.0041 0.011 0.0106 0.0085
12+
ID -0.0074 -0.0076 -0.0068 -0.0387 -0.0026 -0.0116 -0.0149 -0.0404 -0.0481 -0.0144 -0.0152 -0.0085
13+
IL -0.0043 0.0297 0.0303 0.012 0.0036 -0.0048 0.0222 0.0036 0.0026 0.0038 0.0069 0.0074
14+
IN -0.0074 -0.0062 -0.0056 -0.0222 -0.0039 -0.0108 -0.0149 -0.0056 -0.0042 -0.0037 -0.0202 -0.0241
15+
IA -0.0074 -0.0076 -0.0068 -0.0188 -0.0054 -0.0076 0.0181 0.0016 0.0035 0.0013 0.0086 0.0085
16+
KS -0.0074 -0.0076 -0.0068 -0.0256 -0.0144 -0.0116 0.0167 -0.0245 -0.0056 -0.0076 -0.0101 -0.0053
17+
KY 0.0224 0.0072 0.0068 -0.0191 0.0014 -0.0038 -0.0098 -0.0022 0.0062 0.0105 0.0043 -0.0005
18+
LA 0.0074 0.0076 0.0068 0.0287 -0.1137 -0.0074 -0.0089 0.0624 0.068 0.0079 0.002 0.0647
19+
ME -0.0074 -0.0076 -0.0068 0.0357 0.0212 0.0148 0.0021 0.0057 0.0024 0.0067 0.0028 -0.0008
20+
MD -0.0058 -0.0033 -0.0027 -0.006 0.0082 -0.0157 -0.0222 0.0024 0.0065 0.0126 0.0143 0.0085
21+
MA -0.001 0.0015 0.0021 0.1039 0.0495 0.0116 0.0324 0.0068 0.0144 0.0141 0.0464 0.0085
22+
MI -0.0058 -0.0033 -0.0027 0.0074 0.007 0.0012 0.0304 -0.0247 0.0013 0.0036 0.0037 0.0022
23+
MN -0.0009 0.0016 0.0022 0.0127 0.0254 0.0303 0.0323 0.0245 0.0321 0.0459 0.0152 0.0085
24+
MS 0.0074 0.0076 0.0068 -0.031 -0.1137 -0.0116 -0.0113 0.0582 0.0609 0.0089 0.002 0.058
25+
MO 0.0356 0.0066 0.0068 -0.0037 0.0092 -0.0014 -0.0067 0.0014 0.0049 0.0064 0.0032 0.0042
26+
MT -0.0072 -0.0047 -0.0041 -0.0024 0.0031 -0.0059 -0.0129 -0.0329 -0.0351 -0.0077 -0.0036 0.0023
27+
NE -0.0074 -0.0076 -0.0068 -0.0159 -0.0159 -0.0116 0.0167 -0.0068 -0.0144 -0.0144 -0.0152 -0.0085
28+
NV -0.0074 -0.0076 -0.0068 -0.0134 0.0063 -0.0046 -0.0129 -0.0356 -0.0346 -0.0133 -0.0138 -0.0085
29+
NH -0.0074 -0.0076 -0.0068 0.0555 0.0288 -0.0042 -0.0106 -0.0026 -0.0058 -0.0125 -0.0142 -0.0085
30+
NJ -0.0074 -0.0059 -0.0053 0.0281 0.0008 0.0001 -0.0065 0.0009 0.001 0.0006 0.0006 -0.0018
31+
NM -0.002 0.0005 0.0012 0.0333 0.0048 -0.0047 -0.0146 -0.0356 -0.0328 -0.0022 -0.0005 0.0034
32+
NY -0.0374 -0.0034 -0.0028 0.0205 -0.0115 0.0036 -0.0013 0.0065 0.007 0.0266 0.0112 0.0085
33+
NC 0.0074 0.0076 0.0068 -0.0351 -0.027 -0.0059 -0.0149 0.0582 0.0709 0.0114 0.0022 0.0609
34+
ND -0.0074 -0.0076 -0.0068 -0.0009 -0.0051 -0.0096 0.0167 -0.0041 -0.0354 -0.0144 -0.0125 -0.0044
35+
OH -0.0064 -0.0039 -0.0033 -0.0072 -0.0018 -0.0046 -0.0077 0.0022 0.0033 0.0031 0.0027 0.0005
36+
OK 0.0021 0.0046 0.0052 -0.0331 -0.0063 -0.0116 -0.0149 -0.0068 -0.0041 -0.0054 -0.0124 -0.0085
37+
OR -0.0074 -0.0076 -0.0068 -0.0216 0.0023 -0.0034 -0.0094 0.0063 0.0056 0.0031 0.0067 0.0085
38+
PA -0.0033 -0.0008 -0.0002 0.02 0.0067 0.0001 -0.0036 0.0047 0.0052 0.0058 0.0103 0.0085
39+
RI 0.0038 0.0063 0.0068 0.1113 0.0495 0.0116 0.0149 0.0068 0.0139 0.0144 0.0152 0.0085
40+
SC 0.0074 0.0076 0.0068 -0.0341 -0.1137 -0.0116 -0.0149 0.0582 0.0716 0.0125 -0 0.0565
41+
SD -0.0074 -0.0076 -0.0068 -0.0138 -0.0069 -0.0116 0.0482 0.0058 0.0052 -0.0078 -0.0069 0.0009
42+
TN 0.0042 0.0067 0.0245 -0.0326 -0.0354 -0.0103 -0.0149 0.0582 0.0727 0.013 0.0071 0.0807
43+
TX 0.0074 -0.0239 -0.0247 0.0148 -0.0005 -0.0013 -0.0054 0.0633 0.05 -0.0152 -0.0169 0.0305
44+
UT -0.0067 -0.0042 -0.0035 -0.042 -0.0058 -0.0116 -0.0149 -0.0404 -0.0481 -0.0144 -0.0152 -0.0085
45+
VT -0.0074 -0.0076 -0.0068 0.0459 0.0184 -0.0058 -0.0102 -0.0016 -0.0051 0.0016 0.0047 0.0053
46+
VA -0.0043 -0.0018 -0.0011 -0.0356 -0.039 -0.011 -0.0149 0.0582 0.0636 0.0014 -0.0016 0.058
47+
WA -0.0019 0.0006 0.0013 -0.0161 0.0048 -0.0036 -0.0055 0.0048 0.0012 0.0003 0.0053 0.0085
48+
WV 0.0067 0.0076 0.0068 -0.0268 0.0103 0.0032 0.0004 0.0031 0.0122 0.0144 0.0118 0.0085
49+
WI -0.0074 -0.0076 -0.0068 0.0144 -0.0003 -0.0041 0.0228 0.0068 0.0069 0.0068 0.0099 0.0085
50+
WY -0.0074 -0.0076 -0.0068 -0.022 -0.0036 -0.0103 -0.0149 -0.0404 -0.0467 -0.0144 -0.0152 -0.0085

0 commit comments

Comments
 (0)