-
Notifications
You must be signed in to change notification settings - Fork 12
/
test_person_cov.R
121 lines (101 loc) · 4.51 KB
/
test_person_cov.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
# test covariates + redoing person time trends
library(idealstan)
library(dplyr)
require(tidyr)
require(idealstan)
require(lubridate)
setwd("~/idalstan_compare")
rollcalls <- readRDS('data/rollcalls.rds') %>%
select(cast_code,rollnumber,congress,year,district_code,state_abbrev,date,
bioname,party_code,date_month,unemp_rate) %>%
mutate(item=paste0(congress,"_",rollnumber),
cast_code=recode_factor(cast_code,Abstention=NA_character_),
cast_code=as.numeric(cast_code)-1,
bioname=factor(bioname),
bioname=relevel(bioname,"DeFAZIO, Peter Anthony")) %>%
mutate(bioname=factor(bioname)) %>%
distinct %>%
filter(date_month>ymd("2017-12-31"))
# drop legislators who vote on fewer than 25 unanimous bills
# drop bills where >95% of the votes are the same
check_bills <- group_by(rollcalls,item,cast_code) %>% count %>%
group_by(item) %>%
mutate(n_prop=n/(sum(n))) %>%
summarize(high_perc=max(n_prop,na.rm=T)) %>%
filter(high_perc>0.95)
rollcalls <- anti_join(rollcalls, check_bills)
legis_count <- group_by(rollcalls, item) %>%
mutate(unan=all(cast_code[!is.na(cast_code)]==1) || all(cast_code[!is.na(cast_code)]==0)) %>%
group_by(bioname) %>%
summarize(n_votes_nonunam=length(unique(item[!unan])))
# check number of days in legislature
num_days <- distinct(rollcalls,bioname,date_month) %>%
count(bioname)
rollcalls <- anti_join(rollcalls, filter(legis_count, n_votes_nonunam<25),by="bioname") %>%
anti_join(filter(num_days,n<10),by="bioname")
# we probably want to drop unanimous votes
unam_votes <- group_by(rollcalls, item,cast_code) %>%
#summarize(unan=all(cast_code[!is.na(cast_code)]==1) || all(cast_code[!is.na(cast_code)]==0))
count %>%
spread(key="cast_code",value = 'n') %>%
mutate(perc_miss=`<NA>`/(`<NA>` + `0` + `1`))
# polarizing bills
polar_bills <- count(rollcalls, item, cast_code) %>%
filter(!is.na(cast_code)) %>%
group_by(item) %>%
summarize(vote_split=abs((n[1] - n[2])/sum(n))) %>%
filter(vote_split==0)
# check % miss by year
miss_year <- group_by(rollcalls, bioname, date_month) %>%
summarize(perc_miss=sum(is.na(cast_code))/n()) %>%
ungroup %>%
complete(bioname,date_month) %>%
group_by(bioname) %>%
arrange(bioname,date_month) %>%
mutate(perc_miss=case_when(is.na(perc_miss)~0,
perc_miss==1~0,
TRUE~1))
rollcalls$bioname <- factor(rollcalls$bioname)
unemp1 <- rollcalls %>%
filter(bioname %in% c("SCHIFF, Adam",
"PELOSI, Nancy",
"ROHRABACHER, Dana",
"BARTON, Joe Linus")) %>%
id_make(outcome_disc="cast_code",
item_id="item",
person_id="bioname",
group_id="party_code",
time_id = "date_month",
person_cov = ~unemp_rate*party_code)
unemp1@person_cov <- c(unemp1@person_cov[1],unemp1@person_cov[4:5])
unemp1@score_matrix <- select(unemp1@score_matrix,item_id:unemp_rate,
`unemp_rate:party_codeR`:discrete)
unemp1_fit <- id_estimate(unemp1,model_type=2,
vary_ideal_pts = 'AR1',
niters=300,
warmup=300,ignore_db = select(miss_year,
person_id="bioname",
time_id="date_month",
ignore="perc_miss"),
nchains=3,
ncores=parallel::detectCores(),
grainsize=1,
restrict_ind_high = "115_919",
restrict_ind_low="115_952",
#restrict_ind_high = "BARTON, Joe Linus",
#restrict_ind_low="DeFAZIO, Peter Anthony",
restrict_sd_low = .01,
restrict_sd_high = .01,
time_var=5000,
#max_treedepth=12,
#adapt_delta=0.95,
#fix_low=0,
fixtype="prefix",restrict_var = F,
fix_low=-1,const_type="items",
compile_optim=F,
# pars=c("steps_votes_grm",
# "steps_votes",
# "B_int_free",
# "A_int_free"),
#include=F,
id_refresh=100)