-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest_irf_func.R
executable file
·511 lines (395 loc) · 16.7 KB
/
test_irf_func.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# create function for IRFs with idealstan
# Bob Kubinec
require(idealstan)
require(dplyr)
require(bayesplot)
unemp1 <- readRDS('C:/Users/rkubinec/Documents/idalstan_compare/data/unemp1_fit.rds')
# first modify id_plot_cov function to allow for calculating effects differently for high/low discrim
id_plot_cov <- function(object,
calc_varying=T,
label_high="Liberal",
label_low="Conservative",
cov_type='person_cov',
pred_outcome = NULL,
high_quantile=0.95,
low_quantile=0.05,
filter_cov=NULL,
new_cov_names=NULL,
recalc_vals=NULL,
...) {
# determine which outcome to predict
if(is.null(pred_outcome)) {
if(object@model_type %in% c(1,2,3,4,5,6,13,14)) {
# ask user for predicted outcome
pred_outcome <- svDialogs::dlg_list(levels(object@score_data@score_matrix$outcome),
title="Select which level of the outcome to predict using covariates.")$res
} else if(object@model_type %in% c(7,8)) {
pred_outcome <- "Mean Count"
} else if(object@model_type %in% c(9,10,11,12)) {
pred_outcome <- "Mean"
}
}
# adjust labels to match predicted outcome
if(object@model_type %in% c(1,2,3,4,5,6,13,14)) {
pred_outcome_high <- paste0("Pr(",pred_outcome,"|",label_high,")")
pred_outcome_low <- paste0("Pr(",pred_outcome,"|",label_low,")")
xlabel <- "Marginal Change in Probability"
} else if(object@model_type %in% c(7,8)) {
pred_outcome_high <- paste0("Mean Count|",label_high)
pred_outcome_low <- paste0("Mean Count|",label_low)
xlabel <- "Marginal Change in Mean Count"
} else {
pred_outcome_high <- paste0("Mean|",label_high)
pred_outcome_low <- paste0("Mean|",label_low)
xlabel <- "Marginal Change in Mean"
}
# pull hierarchical covariates
param_name <- switch(cov_type,person_cov='legis_x',
discrim_reg_cov='sigma_reg_x',
discrim_infl_cov='sigma_abs_x')
to_plot <- as.array(object@stan_samples,
pars=param_name)
# reset names of parameters
new_names <- switch(cov_type,person_cov=object@score_data@person_cov,
discrim_reg=object@score_data@item_cov,
discrim_abs=object@score_data@item_cov_miss)
# recode these names if user supplies option
if(!is.null(new_cov_names)) {
new_names <- recode(new_names,!!!new_cov_names)
}
attributes(to_plot)$dimnames$parameters <- new_names
# remove unwanted coefficients
if(!is.null(filter_cov)) {
to_plot <- to_plot[,,!(new_names %in% filter_cov),drop=F]
new_names <- new_names[!(new_names %in% filter_cov)]
new_cov_names <- new_cov_names[!(new_cov_names %in% filter_cov)]
}
# set up values to re-calculate
if(!is.null(recalc_vals)) {
if(length(recalc_vals)!=3) {
stop("Option recalc_vals can only be a character vector of length 3 indicating which two variables to add together and their name.")
}
val1 <- which(attributes(to_plot)$dimnames$parameters==recalc_vals[1])
val2 <- which(attributes(to_plot)$dimnames$parameters==recalc_vals[2])
if(is.null(val1) || is.null(val2)) {
stop("The parameter names you passed to re-calculate did not match existing parameters. Please be sure to use recoded parameter names not original parameter names.")
}
}
if(calc_varying) {
# get all sigmas
sigma_all <- rstan::extract(object@stan_samples,"sigma_reg_free")
# iterate over posterior draws and calculate effect conditional on pos/neg discrimination
# for all params in to_plot
neg_eff <- lapply(1:nrow(sigma_all[[1]]), function(i) {
this_discrim <- sigma_all[[1]][i,]
neg_discrim <- this_discrim[this_discrim<0]
#calculate marginal changes in probability
to_plot_neg <- apply(to_plot,3,function(c) {
mean(plogis(c[i]*neg_discrim)-0.5)
})
tibble(estimate=to_plot_neg,
parameter=names(to_plot_neg)) %>%
mutate(Type=pred_outcome_low)
}) %>% bind_rows
pos_eff <- lapply(1:nrow(sigma_all[[1]]), function(i) {
this_discrim <- sigma_all[[1]][i,]
pos_discrim <- this_discrim[this_discrim>0]
#calculate marginal changes in probability
to_plot_neg <- apply(to_plot,3,function(c) {
mean(plogis(c[i]*pos_discrim)-0.5)
})
tibble(estimate=to_plot_neg,
parameter=names(to_plot_neg)) %>%
mutate(Type=pred_outcome_high)
}) %>% bind_rows
# do the same for re-calculated values
neg_eff_recalc <- lapply(1:nrow(sigma_all[[1]]), function(i) {
this_discrim <- sigma_all[[1]][i,]
neg_discrim <- this_discrim[this_discrim<0]
#calculate marginal changes in probability
to_plot_neg <- mean(plogis((to_plot[i,,val1]+to_plot[i,,val2])*neg_discrim)-0.5)
tibble(estimate=to_plot_neg,
parameter=recalc_vals[3]) %>%
mutate(Type=pred_outcome_low)
}) %>% bind_rows
pos_eff_recalc <- lapply(1:nrow(sigma_all[[1]]), function(i) {
this_discrim <- sigma_all[[1]][i,]
pos_discrim <- this_discrim[this_discrim>0]
#calculate marginal changes in probability
to_plot_neg <- mean(plogis((to_plot[i,,val1]+to_plot[i,,val2])*pos_discrim)-0.5)
tibble(estimate=to_plot_neg,
parameter=recalc_vals[3]) %>%
mutate(Type=pred_outcome_high)
}) %>% bind_rows
to_plot <- bind_rows(neg_eff,
pos_eff,
neg_eff_recalc,
pos_eff_recalc)
sum_func <- function(this_data,high=high_quantile,
low=low_quantile) {
tibble(y=median(this_data),
ymin=quantile(this_data,low_quantile),
ymax=quantile(this_data,high_quantile))
}
# if new levels exist, reorder
if(!is.null(new_cov_names)) {
if(is.null(recalc_vals)) {
to_plot$parameter <- fct_relevel(factor(to_plot$parameter),rev(new_cov_names))
} else {
to_plot$parameter <- fct_relevel(factor(to_plot$parameter),c(rev(new_cov_names),recalc_vals[3]))
}
}
outplot <- to_plot %>%
ggplot(aes(x=parameter,y=estimate)) +
stat_summary(fun.data=sum_func) +
coord_flip() +
geom_hline(yintercept=0,linetype=2) +
facet_wrap(~Type) +
theme(panel.grid=element_blank(),
panel.background = element_blank(),
strip.background = element_blank(),
strip.text = element_text(face="bold"),
axis.ticks.y=element_blank()) +
xlab("") +
ylab(xlabel)
if(object@model_type %in% c(1,2,3,4,5,6,13,14)) {
outplot <- outplot + scale_y_continuous(labels=scales::percent)
}
return(outplot)
} else {
mcmc_intervals(to_plot) + xlab("Ideal Point Score")
}
}
id_plot_cov(object,label_high = "Conservative",
label_low="Liberal",pred_outcome = "Yes",
new_cov_names = c(`unemp_rate:party_codeR`="Republican X\nUnemployment",
`unemp_rate:party_codeI`="Independent X\nUnemployment",
unemp_rate="Unemployment",
party_codeR="Republican",
party_codeI="Independent",
`(Intercept)`="Intercept"),
recalc_vals = c("Republican X\nUnemployment",
"Unemployment",
"Combined\nRepublican"),
filter_cov = "Intercept")
ggsave("overall_eff_ar1.png")
# need to select and run all Democrats/Republicans separately
rep_ids <- select(object@score_data@score_matrix,person_id,group_id) %>%
distinct %>% filter(group_id=="R")
id_plot_irf(object,label_high = "Conservative",
label_low="Liberal",pred_outcome = "Yes",
recalc_vals = T,
line_type=1,
line_width = .4,
line_alpha = 0.3,
time_label= "Months Since Unemployment Rate Increase",
line_color='black',
include=rep_ids$person_id,
cov_name = c("unemp_rate:party_codeR",
"unemp_rate"),
use_ci=F)
ggsave("irf_rep.png")
dem_ids <- select(object@score_data@score_matrix,person_id,group_id) %>%
distinct %>% filter(group_id=="D")
id_plot_irf(object,label_high = "Conservative",
label_low="Liberal",pred_outcome = "Yes",
recalc_vals = F,
line_type=1,
line_width = .4,
line_alpha = 0.3,
time_label= "Months Since Unemployment Rate Increase",
line_color='black',
include=dem_ids$person_id,
cov_name = c("unemp_rate"),
use_ci=F)
ggsave("irf_dem.png")
# function to generate IRFs
id_plot_irf <- function(object,
label_high="Liberal",
label_low="Conservative",
pred_outcome=NULL,
recalc_vals=F,
include=NULL,
time_calc=10,
cov_name=NULL,
line_type=2,
line_width=1,
line_alpha=1,
line_color="red",
ci_color='black',
ci_alpha=0.5,
use_ci=TRUE,
high_quantile=0.95,
low_quantile=0.05,
calc_varying=T,
...) {
# figure out which covariate to iterate over
# determine which outcome to predict
if(is.null(pred_outcome)) {
if(object@model_type %in% c(1,2,3,4,5,6,13,14)) {
# ask user for predicted outcome
pred_outcome <- svDialogs::dlg_list(levels(object@score_data@score_matrix$outcome),
title="Select which level of the outcome to predict using covariates.")$res
} else if(object@model_type %in% c(7,8)) {
pred_outcome <- "Mean Count"
} else if(object@model_type %in% c(9,10,11,12)) {
pred_outcome <- "Mean"
}
}
# adjust labels to match predicted outcome
if(object@model_type %in% c(1,2,3,4,5,6,13,14)) {
pred_outcome_high <- paste0("Pr(",pred_outcome,"|",label_high,")")
pred_outcome_low <- paste0("Pr(",pred_outcome,"|",label_low,")")
xlabel <- "Marginal Change in Probability"
} else if(object@model_type %in% c(7,8)) {
pred_outcome_high <- paste0("Mean Count|",label_high)
pred_outcome_low <- paste0("Mean Count|",label_low)
xlabel <- "Marginal Change in Mean Count"
} else {
pred_outcome_high <- paste0("Mean|",label_high)
pred_outcome_low <- paste0("Mean|",label_low)
xlabel <- "Marginal Change in Mean"
}
# pull hierarchical covariates
param_name <- "legis_x"
# figure out which prameter to use
# reset names of parameters
new_names <- switch(cov_type,person_cov=object@score_data@person_cov,
discrim_reg=object@score_data@item_cov,
discrim_abs=object@score_data@item_cov_miss)
if(is.null(cov_name)) {
cov_name <- svDialogs::dlg_list(new_names,multiple=T,
title="Select at least one variable to compute IRFs. If you want to combine two variables, select two variables (but not more than two).")$res
}
to_plot <- as.array(object@stan_samples,
pars=param_name)
attributes(to_plot)$dimnames$parameters <- new_names
to_plot <- to_plot[,,(new_names %in% cov_name),drop=F]
# now need to loop over persons/item IDs to generate IRFs
if(object@use_groups) {
all_ids <- unique(object@score_data@score_matrix$group_id)
} else {
all_ids <- unique(object@score_data@score_matrix$person_id)
}
ar1 <- rstan::extract(object@stan_samples,"L_AR1")[[1]]
# keep some if user specifies
if(!is.null(include)) {
ar1 <- ar1[,all_ids %in% include]
all_ids <- all_ids[all_ids %in% include]
}
# calc IRF first then convert to marginal changes if wanted
# get cov values
cov1 <- to_plot[,,1]
if(recalc_vals) {
cov1 <- cov1 + to_plot[,,2]
}
# iterate over all the possible AR1 parameters
all_irfs <- apply(ar1,2, function(arp) {
this_irf <- .irf(total_t=time_calc,
adj_in=arp,
shock=cov1)
return(this_irf)
})
names(all_irfs) <- all_ids
all_irfs <- bind_rows(all_irfs,.id="Person")
if(calc_varying) {
# pull sigmas if we want to calculate marginal changes
sigma_all <- rstan::extract(object@stan_samples,"sigma_reg_free")[[1]]
# iterate over persons and time points
all_eff <- lapply(1:nrow(sigma_all), function(i) {
this_discrim <- sigma_all[i,]
pos_discrim <- this_discrim[this_discrim>0]
neg_discrim <- this_discrim[this_discrim<0]
this_data <- filter(all_irfs,iter==i) %>%
group_by(Person,time) %>%
mutate(marg_neg=mean(plogis(y_shock*neg_discrim)-0.5),
marg_pos=mean(plogis(y_shock*pos_discrim)-0.5))
}) %>% bind_rows
outplot <- all_eff %>% gather(key=Type,value=estimate,marg_neg,marg_pos) %>%
group_by(Person,time,Type) %>%
summarize(mean_est=mean(estimate),
high_est=quantile(estimate,high_quantile),
low_est=quantile(estimate,low_quantile)) %>%
ungroup %>%
mutate(time=factor(time),
Type=recode(Type,marg_neg=pred_outcome_low,
marg_pos=pred_outcome_high)) %>%
ggplot(aes(y=mean_est,x=time,group=Person))
if(use_ci) {
outplot <- outplot + geom_ribbon(aes(ymin=low_est,
ymax=high_est),
alpha=ci_alpha,
fill=ci_color)
}
outplot + geom_line(linetype=line_type,colour=line_color,size=line_width,
alpha=line_alpha) +
geom_hline(yintercept=0,linetype=3) +
facet_wrap(~Type) +
theme(panel.grid=element_blank(),
panel.background = element_blank(),
strip.background = element_blank(),
strip.text = element_text(face="bold"),
axis.ticks.y=element_blank()) +
scale_y_continuous(labels = scales::percent) +
xlab(time_label) +
ylab(xlabel)
} else {
# don't need as many fancy options if we aren't calculating marginal changes
outplot <- all_irfs %>% group_by(Person,time) %>%
summarize(mean_est=mean(y_shock),
high_est=quantile(y_shock,high_quantile),
low_est=quantile(y_shock,low_quantile)) %>%
ungroup %>%
mutate(time=factor(time)) %>%
ggplot(aes(y=mean_est,x=time,group=Person))
if(use_ci) {
outplot <- outplot + geom_ribbon(aes(ymin=low_est,
ymax=high_est),
alpha=ci_alpha,
fill=ci_color)
}
outplot + geom_line(linetype=line_type,colour=line_color,size=line_width,
alpha=line_alpha) +
geom_hline(yintercept=0,linetype=3) +
theme(panel.grid=element_blank(),
panel.background = element_blank(),
strip.background = element_blank(),
strip.text = element_text(face="bold"),
axis.ticks.y=element_blank()) +
xlab(time_label) +
ylab("Ideal Point Scale")
}
}
.irf <- function( time=1,shock=1,
adj_in=NULL,
y_1=0,
total_t=10,
old_output=NULL) {
# set up the exogenous shock
# unless the shock comes from an exogenous covariate beta_x
if(time==1) {
x_1 <- shock
} else {
x_1 <- 0
}
print(paste0('Now processing time point ',time))
# Calculate current values of y and x given posterior uncertainty
output <- data_frame(y_shock= adj_in*y_1 + x_1,
time=time,
iter=1:length(adj_in))
if(!is.null(old_output)) {
new_output <- bind_rows(old_output,output)
} else {
new_output <- output
}
# run function recursively until time limit is reached
if(time<total_t) {
.irf(time=time+1,
adj_in=adj_in,
y_1=output$y_shock,
total_t=total_t,
old_output=new_output)
} else {
return(new_output)
}
}