-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.R
229 lines (188 loc) · 7.32 KB
/
2.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
223
224
225
226
227
228
#!/usr/bin/env Rscript
# clear workspace
rm(list=ls())
# Function to load packages
loadPkg=function(toLoad){
for(lib in toLoad){
if(! lib %in% installed.packages()[,1])
{ install.packages(lib, repos='http://cran.rstudio.com/') }
suppressMessages( library(lib, character.only=TRUE) ) }
}
# load libraries
packs=c('tidyverse', 'haven', 'MatchIt', 'broom',
'janitor', 'ggrepel', 'hrbrthemes', 'stargazer') #
loadPkg(packs)
# set up data -------------------------------------------------------------
# load
ceo_850 = as.data.frame(read_spss('Data/CEO_850.sav'))
ceo_857 = as.data.frame(read_spss('Data/CEO_857.sav'))
ceo_863 = as.data.frame(read_spss('Data/CEO_863_withdates.sav'))
# drop observations after the declaration
ceo_863 = ceo_863[ceo_863$DIA < 27,]
# id treatment
ceo_850$treat = 0
ceo_857$treat = 0
ceo_863$treat = 1
# stack
df = bind_rows(list(ceo_850, ceo_857, ceo_863))
# subset
keep = c('P31', 'treat', 'C705', 'Edat_CEO', 'C500', 'C800',
'C401', 'Edat_CEO', 'SEXE', 'P21A', 'P21D', 'P21F',
'P21H', 'P21I', 'P21J', 'P21L', 'P21M', 'P21R', 'C110',
'C120', 'ANY')
df = df[keep]
# recode preferences for indep 2 -> 0
df$P31 = car::recode(df$P31, "2=0")
# recode employment
df$C401 = car::recode(df$C401, "2=0; 1=2; 3=1")
# replace all 98 and 99
df[df == 98] = NaN
df[df == 99] = NaN
# create num_p_cata
df = df[complete.cases(df$'C110'),]
df = df[complete.cases(df$'C120'),]
df$num_p_cata = 0
for (ind in c(1:nrow(df))){
count = 0
if( df[ind, 'C110'] == 1){ count = count + 1 }
if( df[ind, 'C120'] == 1){ count = count + 1 }
df[ind, 'num_p_cata'] = count
}
# just data from 2017 with period after election marked under treat
df_2017 = data.frame(df[df$ANY == 2017,])
df_c_2017 = df_2017[c('P31', 'treat', 'C705', 'num_p_cata', 'C500', 'C800',
'C401', 'Edat_CEO', 'SEXE')]
# only complete cases
df_c_2017 = df_c_2017[complete.cases(df_c_2017),]
# pull out pre- and post- stat tables
labs = c('First Language', 'Num. of Catalan Parents',
'Education Level', 'Economic Class', 'Work Status',
'Age', 'Sex')
# sample statistics
samp = df_c_2017 %>%
select(C705:SEXE) %>%
data.table::setnames(., old = names(.), new = labs) %>%
filter(`Education Level` < 60) %>%
summarise(`First Language (most common)` = names(sort(-table(`First Language`)))[1],
`Num. of Catalan Parents (average)` = mean(`Num. of Catalan Parents`),
`Education Level (median)` = median(`Education Level`),
`Economic Class (median)` = median(`Economic Class`),
`Work Status (median)` = median(`Economic Class`),
`Age` = median(`Age`),
`Female` = .49) %>%
mutate(`First Language (most common)` = replace(`First Language (most common)`,
`First Language (most common)` == 2, 'Catalan'),
`Education Level (median)` = 'HS (higher)',
`Economic Class (median)` = 'Middle',
`Work Status (median)` = 'Between Work') %>%
t()
stargazer(samp, type = 'latex', summary = F, title = 'Sample Characteristics',
label = 'samp-char', out = 'Figures/samp-char.tex')
# Analysis ----------------------------------------------------------------
eval_dv = function(dv){
# restrict data
df_c_2017 = df_2017[c(dv, 'treat', 'C705', 'num_p_cata', 'C500', 'C800',
'C401', 'Edat_CEO', 'SEXE')]
df_c_2017 = df_c_2017[complete.cases(df_c_2017),]
# set up variables
vars = c('C705', 'num_p_cata', 'C500', 'C800',
'C401', 'Edat_CEO', 'SEXE')
# manual coarsening
C705.grp = list(c(0), c(1), c(2), c(3))
C500.grp = list(c(1,2,3), c(4,5,6,7) , c(8,9,10,11))
# perform the match
mat = matchit(formula(paste0('treat ~ ', paste(vars, collapse = '+'))),
data = df_c_2017, method = 'cem',
grouping = list(C705=C705.grp,
C500=C500.grp))
# treatment effects
form = as.formula(paste(dv, '~ treat'))
est = Zelig::zelig(form, data = match.data(mat), model = 'ls',
weights = 'weights',
cite = F)
# pull out estimate and coef for plotting
eff = data.frame(est = est$get_coef()[[1]][2],
std = est$get_se()[[1]][2],
outcome = dv)
# get output table for P31
if(dv == "P31")
{
z_est = Zelig::from_zelig_model(est) %>%
broom::tidy() %>%
janitor::clean_names() %>%
select(Term = term, Estimate = estimate, `Standard Error` = std_error,
`Statistic` = statistic, `P-value` = p_value) %>%
mutate(Term = c("Intercept", "Treatment")) %>%
mutate_if(is_numeric, funs(round(., 3)))
# stargazer
stargazer(z_est, summary = FALSE, title = "Matching estimate of effect of crackdown on pro-independence attitudes. OLS using matching weights. N = 2,302.", label = "p31-mat", style = "io", out = "Figures/p31-mat.tex")
}
return(eff)
}
# plot results
pDat = rbind(
eval_dv(dv = 'P31'),
eval_dv(dv = 'P21A'),
eval_dv(dv = 'P21D'),
eval_dv(dv = 'P21F'),
eval_dv(dv = 'P21H'),
eval_dv(dv = 'P21I'),
eval_dv(dv = 'P21J'),
eval_dv(dv = 'P21L'),
eval_dv(dv = 'P21M'),
eval_dv(dv = 'P21R')
)
# add labels
outLabs = c('Independence',
'Courts of Justice',
'Central Government',
'Catalan Government',
'Catalan Parliament',
'The EU',
'Spanish Monarchy',
'Spanish Police',
'Catalan Police',
'Constitutional Court')
# add confidence intervals
pDat$conf95 = paste0('(', round(pDat$est - 1.96*pDat$std,3), ',',
round(pDat$est + 1.96*pDat$std,3), ')')
# output table
pDat %>%
mutate(Outcome = outLabs, Estimate = round(est,3)) %>%
select(Outcome, Estimate, Interval = conf95) %>%
mutate(`Outcome Scale` = c('0-1', rep('0-10', 9))) %>%
stargazer(summary = F, rownames = F,
title = 'Estimates of repression effect by outcome.',
label = 'est-effect',
out = 'Figures/effects-table.tex')
# support for key actors plot
pDat$label = outLabs
pDat$actors = ifelse(str_detect(pDat$label, 'Catalan'), 'Catalan', 'Spanish/European')
#pDat[pDat['label']=='The EU', 'actors'] = 'European'
pDat$type = c('Independence', rep('Support for Key Actors', 9))
# pDat$colors = ifelse(str_detect(pDat$label, 'Catalan'), "#0B775E",
# "#F2300F")
# key actors
ggplot(pDat[pDat$outcome != 'P31',], aes(label = label, shape = actors)) +
geom_hline(yintercept = 0, colour = gray(1/2), lty = 2) +
geom_pointrange(aes(x = outcome, y = est, ymin = est - std*1.96,
ymax = est + std*1.96), size = .8,
lwd = 1/2, position = position_dodge(width = 1/2)) +
hrbrthemes::theme_ipsum_rc(grid = 'X') +
coord_flip() +
ggrepel::geom_label_repel(aes(x = outcome, y = est),
nudge_x = -.5, nudge_y = -.02) +
theme(axis.text.y = element_blank(),
legend.position = 'top') +
labs(y = 'Change in level of confidence in actor (0-10 scale)',
x = '',
shape = "Institutional association:",
NULL)
ggsave(filename = 'Figures/cem_effects_actors.pdf',
height = 7,
width = 9,
device = cairo_pdf)
ggsave(filename = 'Figures/cem_effects_actors.png',
height = 7,
width = 9,
type = 'cairo')