*01_CompareMergedToAllContributors.do
*Determine how representative contributors merged to 2012 CCES are to all contributors.

set more off
window manage close graph _all
set scheme s1mono

*Matched contributors in CCES
use "final_analysis_dataset.dta" if _merge==3, clear
drop  binned_2012amountR_withmissing 
gen matchedcase=1
keep matchedcase V103 binned*2012* anyindistrict12R  wtrecipcf12R cfscoreR isdem
 
*Names have an R.
rename binned_2012numR binned_2012num
rename binned_2012amount binned_2012amount
rename anyindistrict12R anyindistrict12
rename wtrecipcf12R wtrecipcf12
rename cfscoreR bonica_cfscore

*All contributors.
append using "rawdata/processed_contributionsByCID_2010_and_2012.dta", gen(allcontributors)
drop bonica_cid allcontrib
replace matchedcase=0 if matchedcase==.
label variable matchedcase "Contributor matched to CCES respondent"
*Create weight for all contributors.
replace V103 = 1 if matchedcase == 0

* Label values
label values binned_2012amount amt12
label define num12 0 "None" 1 "1" 2 "2" 3 "3" 4 "4" 5 "5-6" 6 "7-10" 7 "More than 10" 
label values binned_2012num num12

label var bonica_cfscore "Bonica CF Score (Liberal to Conservative)"
label var wtrecipcf12 "Dollar Weighted 2012 CF Score (Liberal to Conservative)"
label var binned_2012amount "Amount of contributions 2012"
label var binned_2012num "Number of contributions 2012"

*Chi-square tests of categorical variables.
foreach var of varlist binned_2012num binned_2012amount anyindistrict12 {
  disp _n _n "`var':"
  *Note, it won't calculate chi square test when aweight.
  tab `var' matchedcase [aweight=V103], col
}

*Bar charts of proportions of categorical variables by contributor.
foreach var of varlist binned_2012num binned_2012amount {
  local templabel : var label `var'
  disp _n _n "`var' (`templabel'):"
  preserve
  gen matched=V103 if matchedcase==1
  replace V103=0 if matchedcase==1
  drop if `var'==.
  collapse (sum) matched V103, by(`var')
  summ matched
  replace matched=matched/`r(sum)'
  summ V103
  replace V103=V103/`r(sum)'
  graph bar (asis) matched V103, over(`var', label(labcolor(black) angle(forty_five) labsize(vsmall))) subtitle("`templabel'") ytitle(Proportion of Data) title("Benchmarking CCES Matching Process") legend(order(1 "Matched Cases" 2 "All Donations Data")) name(match`var', replace)
  
  * For amount and number of contributions, repeat without 0's
  if "`var'"=="binned_2012num" | "`var'"=="binned_2012amount" {
   drop if `var'==0
   summ matched
   replace matched=matched/`r(sum)'
   summ V103
   replace V103=V103/`r(sum)'
     graph bar (asis) matched V103, over(`var', label(labcolor(black) angle(forty_five) labsize(vsmall))) subtitle("`templabel'") ytitle(Proportion of Data) title("Benchmarking CCES Matching Process, excluding 0s") legend(order(1 "Matched Cases" 2 "All Donations Data")) name(match`var'no0, replace) 
  }
  
  restore
}

preserve
*Histograms of CF scores.
foreach var of varlist bonica_cfscore wtrecipcf12 {
  local templabel : var label `var'
  disp _n _n "`var' (`templabel'):"
  _pctile `var', percentiles(.5 95.5) 
  replace `var'=`r(r2)' if `var'>`r(r2)' & `var'~=.
  replace `var'=`r(r1)' if `var'<`r(r1)' & `var'~=.
  twoway (kdensity `var' [aweight=V103] if matchedcase==1) (kdensity `var' if matchedcase==0), xtitle("`templabel'") ytitle(Density) title("Benchmarking CCES Matching Process") legend(order(1 "Matched Cases" 2 "All Donations Data")) name(match`var', replace) 
  twoway (kdensity `var' [aweight=V103] if matchedcase==1) (kdensity `var' if matchedcase==0) if binned_2012amount>0, xtitle("`templabel'") ytitle(Density) title("Benchmarking CCES Matching Process, excluding 2012 0s", size(small)) legend(order(1 "Matched Cases" 2 "All Donations Data")) name(match`var'large, replace) 

}
restore


*Create Supplementary Figure versions.
graph combine matchbinned_2012amountno0, cols(1) iscale(*.90) imargin(small) title("Figure S1", size(vsmall) nobox) note("Source: Merged CCES/DIME dataset.") name(FigureS01, replace) xsize(7.5) ysize(10) scale(.80)
graph export "figures/FigureS01.pdf", as(pdf) replace

graph combine matchwtrecipcf12large, cols(1) iscale(*.90) imargin(small) title("Figure S2", size(vsmall) nobox) note("Source: Merged CCES/DIME dataset.") name(FigureS02, replace) xsize(7.5) ysize(10) scale(.80)
graph export "figures/FigureS02.pdf", as(pdf) replace


