Skip to content

Instantly share code, notes, and snippets.

@jonrobinson2
Last active December 15, 2020 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonrobinson2/6a4f214cf4da5c555e56888c9cb4e62b to your computer and use it in GitHub Desktop.
Save jonrobinson2/6a4f214cf4da5c555e56888c9cb4e62b to your computer and use it in GitHub Desktop.
Mark B CCES 2016 Mail Intention Validation
require(rio)
require(sqldf)
cces = import('~/Downloads/CCES16_Common_OUTPUT_Feb2018_VV.dta')
.cces = (cces[,c('CL_voterstatus','CC16_364','CC16_403','CL_E2016GVM','commonweight_vv','V101','CL_state')])
.cces = .cces[.cces$CL_voterstatus %in% c('active','inactive'),]
.cces$VOTE_LIKELIHOOD = car::recode(var = as.numeric(.cces$CC16_364),
recodes = "1='Yes'; 2='Probably' ; 3='Already voted' ; 4 = 'No'; else = NA")
.cces$VOTE_METHOD_SELFREPORT = car::recode(var = as.numeric(.cces$CC16_403),
recodes = "1='EDay'; 2='Early' ; 3='Mail' ; 4 = 'DK'; else = NA")
state= unique(c(sqldf("select count(*), state from (select count(*) as n,
CL_E2016GVM as vm,
CL_state as state
from \".cces\"
where CL_E2016GVM is not null
group by 2,3) a
where vm in ('earlyVote','absentee') and n>10
group by 2
having count(*)>1")$state,
sqldf("select count(*) as n,
CL_E2016GVM as vm,
CL_state as state
from \".cces\"
where CL_E2016GVM ='mail'
group by 2,3
order by 3,2")$state))
.cces = .cces[.cces$CL_state %in% state,]
vl = sqldf("select sum(commonweight_vv) as n,
sum(case when CL_E2016GVM!='' then commonweight_vv else 0 end)/sum(commonweight_vv) as voted,
sum(case when CL_E2016GVM in ('absentee','mail') then commonweight_vv else 0 end)/sum(commonweight_vv) as absentee,
sum(case when CL_E2016GVM in ('absentee','mail') then commonweight_vv when CL_E2016GVM!='' then 0 else null end)/sum(case when CL_E2016GVM!='' then commonweight_vv else 0 end) as absentee_onlyvoters,
VOTE_LIKELIHOOD,
case when CL_state in ('UT','CO','OR','WA') then 'All Mail' else 'Not' end as regime
from \".cces\"
where VOTE_LIKELIHOOD is not null
group by 5,6")
vl$p = vl$n/sum(vl$n)
vm = sqldf("select sum(commonweight_vv) as n,
sum(case when CL_E2016GVM!='' then commonweight_vv else 0 end)/sum(commonweight_vv) as voted,
sum(case when CL_E2016GVM in ('absentee','mail') then commonweight_vv else 0 end)/sum(commonweight_vv) as absentee,
sum(case when CL_E2016GVM in ('absentee','mail') then commonweight_vv when CL_E2016GVM!='' then 0 else null end)/sum(case when CL_E2016GVM!='' then commonweight_vv else 0 end) as absentee_onlyvoters,
VOTE_METHOD_SELFREPORT,
case when CL_state in ('UT','CO','OR','WA') then 'All Mail' else 'Not' end as regime
from \".cces\"
where VOTE_METHOD_SELFREPORT is not null
group by 5,6")
vm$p = vm$n/sum(vm$n)
> vm
n voted absentee absentee_onlyvoters VOTE_METHOD_SELFREPORT regime p
1 5.229242 0.3952201 0.39522015 1.00000000 DK All Mail 0.0003777992
2 45.752792 0.0421794 0.01109287 0.26299260 DK Not 0.0033055206
3 126.990018 0.8248018 0.30044121 0.36425865 EDay All Mail 0.0091746995
4 3780.408532 0.9076089 0.04727233 0.05208447 EDay Not 0.2731247145
5 134.192384 0.9815030 0.67605209 0.68879271 Early All Mail 0.0096950518
6 3977.242963 0.9755753 0.04896948 0.05019549 Early Not 0.2873454918
7 2062.131392 0.9687823 0.91963775 0.94927181 Mail All Mail 0.1489836464
8 3709.379846 0.9582471 0.83254813 0.86882402 Mail Not 0.2679930761
> vl
n voted absentee absentee_onlyvoters VOTE_LIKELIHOOD regime p
1 81.02850 0.9784796 0.9098736 0.9298851 Already voted All Mail 0.005104347
2 470.10502 0.9283224 0.6990688 0.7530454 Already voted Not 0.029614013
3 32.06346 0.6143469 0.4433207 0.7216130 No All Mail 0.002019820
4 300.67241 0.4965004 0.1471620 0.2963986 No Not 0.018940697
5 109.36894 0.7583679 0.6491968 0.8560446 Probably All Mail 0.006889638
6 650.99839 0.6612443 0.1307119 0.1976757 Probably Not 0.041009294
7 2346.43909 0.9508615 0.8661768 0.9109391 Yes All Mail 0.147812670
8 11883.73528 0.9296883 0.2846754 0.3062052 Yes Not 0.748609521
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment