!* version 0.1.0 19nov2004 program define utilstat, nclass version 8 syntax varlist(numeric) [if] [in] [, TRansform(numlist min=10 max=10) EXclude(string) OUTput(string) /* */ SAVe SET(string) MAXUtil SHUtil SUtil GUtil HPTYUtil LUtil OVerlap ALL] /* Create variable to use to mark whether case is included considering if and in statements */ tempvar _touse mark `_touse' `if' `in' /* If no option is selected, automatically select ALL */ if ("`maxutil'" != "") + ("`shutil'" != "") + ("`sutil'" != "") + ("`gutil'" != "") + ("`hptyutil'" != "") /* */ + ("`lutil'" != "") + ("`overlap'" != "") == 0 { local all "all" } /* If save option is set, automatically select ALL */ if ("`save'" != "") { local all "all" } /* Warn user if output is set without overlap or all */ if ("`output'" != "" & "`all'" == "" & "`overlap'" == "") { display in red "Warning: the {cmd:output} command has no effect when neither {cmd:all} or {cmd:overlap} are set." } /* Generate transformation variables */ if ("`transform'" != "") { forvalues i = 1/10 { gettoken tr transform : transform local tr`i' = `tr' } } else { local multiplier = 1 / 9 forvalues i = 1/10 { local tr`i' = `multiplier' * (`i' - 1) } } /* Generate temporary, transformed variables for each variable in varlist */ local i = 1 foreach var of varlist `varlist' { tempvar t_pty`i' quietly generate double `t_pty`i'' = `var' quietly recode `t_pty`i'' (1=`tr1') (2=`tr2') (3=`tr3') (4=`tr4') (5=`tr5') (6=`tr6') (7=`tr7') /* */ (8=`tr8') (9=`tr9') (10=`tr10') quietly replace `t_pty`i'' = . if `t_pty`i'' > 10 global t_pty`i'o "`var'" global t_pty`i' "`t_pty`i''" local lbl`i' : variable label `var' if ("`lbl`i''" == "") { local lbl`i' = "`var'" } global g_lbl`i' = substr("`lbl`i''", 1, 12) local i = `i' + 1 } local nparties = `i' - 1 /* Generate temporary, transformed variable for parties in set */ while ("`set'" != "") { gettoken subset set : set, parse(";") gettoken void set : set, parse(";") local nparties = `nparties' + 1 global g_lbl`nparties' = "" /* Calculate maximum value of those in the set */ local maxline = "" local first = 1 foreach var in `subset' { if `first' == 1 { local maxline = "`var'" local first = 0 } else { local maxline = "`maxline',`var'" } local lblvar = "g_lbl`nparties'" local lbl : variable label `var' if ("`lbl'" == "") { local lbl = "`var'" } local lbl = substr("`lbl'", 1, 2) if ("$`lblvar'" == "") { global g_lbl`nparties' = substr("`lbl'",1,12) } else { global g_lbl`nparties' = substr("$`lblvar' + `lbl'",1,12) } } tempvar t_pty`nparties' quietly generate `t_pty`nparties'' = max(`maxline') tempvar t_pty`nparties'oc quietly generate `t_pty`nparties'oc' = `t_pty`nparties'' global t_pty`nparties'o = "`t_pty`nparties'oc'" global t_pty`nparties' = "`t_pty`nparties''" quietly recode `t_pty`nparties'' (1=`tr1') (2=`tr2') (3=`tr3') (4=`tr4') (5=`tr5') (6=`tr6') (7=`tr7') /* */ (8=`tr8') (9=`tr9') (10=`tr10') quietly replace `t_pty`nparties'' = . if `t_pty`nparties'' > 10 } /* Handle exclusion patterns */ while ("`exclude'" != "") { gettoken pattern exclude : exclude, parse(";") gettoken void exclude : exclude, parse(";") tempvar t_excl quietly generate `t_excl' = 1 tokenize "`pattern'" forvalues p = 1/`nparties' { local var = "t_pty`p'o" quietly replace `t_excl' = 0 if $`var' != ``p'' } quietly replace `_touse' = 0 if `t_excl' == 1 } /* Remove cases where all are missing */ tempvar t_excl quietly generate `t_excl' = 1 forvalues p = 1/`nparties' { local var = "t_pty`p'o" quietly replace `t_excl' = 0 if $`var' != . } quietly replace `_touse' = 0 if `t_excl' == 1 /* Generate string with all variable names */ local pty_str = "" forvalues i = 1/`nparties' { local pty_str = "`pty_str' `t_pty`i''" local var = "t_pty`i'o" local pty_str_o = "`pty_str_o' $`var'" } /* Turn some local macros into global variables */ global g_pty_str = "`pty_str'" global g_pty_str_o = "`pty_str_o'" global g_nparties = `nparties' global g_touse = "`_touse'" /* Run the selected subroutines */ if ("`maxutil'" != "") | ("`all'" != "") { capture noisily MaxUTIL `0' } if ("`shutil'" != "") | ("`all'" != "") { capture noisily ShUTIL `0' } if ("`sutil'" != "") | ("`all'" != "") { capture noisily SUTIL `0' } if ("`gutil'" != "") | ("`all'" != "") { capture noisily GUTIL `0' } if ("`hptyutil'" != "") | ("`all'" != "") { capture noisily HptyUTIL `0' } if ("`lutil'" != "") | ("`all'" != "") { capture noisily LUTIL `0' } if ("`overlap'" != "") | ("`all'" != "") { capture noisily Overlap `0' } /* Most of the save functionality is included in maxutil, gutil, and shutil, but not the 'how many parties more preferred' */ if ("`save'" != "") { capture quietly MorePreferred `0' } end program define MaxUTIL syntax varlist(numeric) [if] [in] [, Transform(numlist min=10 max=10) EXclude(string) Output(string) /* */ Save SET(string) MAXUtil SHUtil SUtil GUtil HPTYUtil LUtil OVERlap ALL] matrix mcell = (0) matrix mrow = (0) tempvar t_maxutil local maxline = subinstr("$g_pty_str_o"," ",",",.) quietly generate `t_maxutil' = max(`maxline') quietly tabulate `t_maxutil' if $g_touse == 1, matcell(mcell) matrow(mrow) if ("`save'" != "") { quietly generate maxutil = `t_maxutil' if $g_touse == 1 quietly label variable maxutil "Maximum utility score" } display _newline display in green "{hline 82}" display in green " Distribution of score of most favoured party" display in green "{hline 82}" display in green " 1 2 3 4 5 6 7 8 9 10 N/Total" display in green "{hline 82}" display in green _continue "Respondents " local i = 1 local sum = 0 forvalues col = 1/10 { if (mrow[`i',1] == `col') { display in yellow _continue %5.0f mcell[`i',1] " " local sum = `sum' + mcell[`i',1] local i = `i' + 1 } else { display in yellow _continue %5.0f 0 " " } } display in yellow %9.0g `sum' display in green _continue " " local i = 1 forvalues col = 1/10 { if (mrow[`i',1] == `col') { display in yellow _continue %5.0f mcell[`i',1] * 100 / `sum' " " local i = `i' + 1 } else { display in yellow _continue %5.0g 0 " " } } display in green " 100 %" display in green "{hline 82}" end program define ShUTIL syntax varlist(numeric) [if] [in] [, Transform(numlist min=10 max=10) EXclude(string) Output(string) /* */ Save SET(string) MAXUtil SHUtil SUtil GUtil HPTYUtil LUtil OVERlap ALL] /* Find the maximum score */ tempvar t_maxutil generate `t_maxutil' = 0 tokenize $g_pty_str_o while "`1'" != "" { quietly replace `t_maxutil' = `1' if `1' > `t_maxutil' & `1' < . macro shift } /* Generate the shutil count */ tempvar t_shutil generate `t_shutil' = 0 tokenize $g_pty_str_o while "`1'" != "" { quietly replace `t_shutil' = `t_shutil' + 1 if `1' == `t_maxutil' macro shift } /* Display our table */ display _newline display in green "{hline 82}" display in green " Number of parties sharing an individual's highest score" display in green "{hline 82}" display in green _continue " " forvalues col = 1/$g_nparties { display in green _continue %6.0g `col' } display in green " N/Total" display in green "{hline 82}" display in green _continue "Respondents " matrix shcell = (0) matrix shrow = (0) quietly tabulate `t_shutil' if $g_touse == 1 & `t_shutil' > 0, matcell(shcell) matrow(shrow) if ("`save'" != "") { quietly generate shutil = `t_shutil' if $g_touse == 1 & `t_shutil' > 0 quietly label variable shutil "Number of parties with highest utility score" } local sum = 0 local i = 1 forvalues col = 1/$g_nparties { if (shrow[`i',1] == `col') { display in yellow _continue %5.0f shcell[`i',1] " " local sum = `sum' + shcell[`i',1] local i = `i' + 1 } else { display in yellow _continue %5.0g 0 " " } } display in yellow %9.0g `sum' display _continue " " local i = 1 forvalues col = 1/$g_nparties { if (shrow[`i',1] == `col') { display in yellow _continue %5.0f shcell[`i',1] / `sum' * 100 " " local i = `i' + 1 } else { display in yellow _continue %5.0g 0 " " } } display in green " 100 %" display in green "{hline 82}" end program define SUTIL syntax varlist(numeric) [if] [in] [, Transform(numlist min=10 max=10) EXclude(string) Output(string) /* */ Save SET(string) MAXUtil SHUtil SUtil GUtil HPTYUtil LUtil OVERlap ALL] display _newline display in green "{hline 82}" display in green " Number of respondents with each score, by party" display in green "{hline 82}" display in green " 1 2 3 4 5 6 7 8 9" /* */ " 10 N/Total" display in green "{hline 82}" forvalues p = 1/$g_nparties { local var = "t_pty`p'o" local lblvar = "g_lbl`p'" display in green "$`lblvar' {col 14}" _continue matrix scell = (0) matrix srow = (0) quietly tabulate $`var' if $g_touse == 1, matcell(scell) matrow(srow) matrix sum = scell' * vecdiag(I(rowsof(scell)))' local i = 1 forvalues col = 1/10 { if (srow[`i',1] == `col') { display in yellow _continue %5.0f scell[`i',1] " " local i = `i' + 1 } else { display in yellow _continue %5.0f 0 " " } } display in yellow %9.0g sum[1,1] display in green "{col 14}" _continue local i = 1 forvalues col = 1/10 { if (srow[`i',1] == `col') { display in yellow _continue %5.0f scell[`i',1] / sum[1,1] * 100 " " local i = `i' + 1 } else { display in yellow _continue %5.0f 0 " " } } display in green " 100 %" display in green "{hline 82}" } end program define GUTIL syntax varlist(numeric) [if] [in] [, Transform(numlist min=10 max=10) EXclude(string) Output(string) /* */ Save SET(string) MAXUtil SHUtil SUtil GUtil HPTYUtil LUtil OVERlap ALL] display _newline display in green "{hline 82}" display in green " Gap between highest and next highest party utility score" display in green "{hline 82}" display in green " 0 1 2 3 4 5 6 7 8" /* */ " 9 N/Total" display in green "{hline 82}" /* Find the maximum score */ tempvar t_maxutil local maxline = subinstr("$g_pty_str_o"," ",",",.) quietly generate `t_maxutil' = max(`maxline') /* Find the gap */ tempvar t_gap generate `t_gap' = 10 tempvar t_count1 generate `t_count1' = 0 forvalues p = 1/$g_nparties { local var = "t_pty`p'o" quietly replace `t_gap' = `t_maxutil' - $`var' if `t_maxutil' - $`var' < `t_gap' & `t_maxutil' - $`var' > 0 & `t_maxutil' < . quietly replace `t_count1' = `t_count1' + 1 if $`var' == `t_maxutil' & `t_maxutil' < . } quietly replace `t_gap' = 0 if `t_count1' > 1 if ("`save'" != "") { quietly generate gutil = `t_gap' if $g_touse == 1 & `t_gap' < 10 quietly label variable gutil "Gap between two highest utility scores" } /* Display the gaps */ display in green _continue "Respondents {col 14}" matrix scell = (0) matrix srow = (0) quietly tabulate `t_gap' if $g_touse == 1 & `t_maxutil' > 0, matcell(scell) matrow(srow) local i = 1 local sum = 0 forvalues col = 0/9 { if (srow[`i',1] == `col') { display in yellow _continue %5.0f scell[`i',1] " " local sum = `sum' + scell[`i',1] local i = `i' + 1 } else { display in yellow _continue %5.0f 0 " " } } display in yellow %9.0g `sum' display in green "{col 14}" _continue local i = 1 forvalues col = 0/9 { if (srow[`i',1] == `col') { display in yellow _continue %5.0f scell[`i',1] / `sum' * 100 " " local i = `i' + 1 } else { display in yellow _continue %5.0f 0 " " } } display in green " 100 %" display in green "{hline 82}" end program define HptyUTIL syntax varlist(numeric) [if] [in] [, Transform(numlist min=10 max=10) EXclude(string) Output(string) /* */ Save SET(string) MAXUtil SHUtil SUtil GUtil HPTYUtil LUtil OVERlap ALL] display _newline display in green "{hline 82}" display in green " Distribution of highest utility score, by party" display in green "{hline 82}" display in green " 1 2 3 4 5 6 7 8 9" /* */ " 10 N/Total" display in green "{hline 82}" /* Find the maximum score */ tempvar t_maxutil local maxline = subinstr("$g_pty_str_o"," ",",",.) quietly generate `t_maxutil' = max(`maxline') /* Set temporary variable to score when highest and tabulate */ tempvar t_hutil quietly generate `t_hutil' = . forvalues p = 1/$g_nparties { local var = "t_pty`p'o" local lblvar = "g_lbl`p'" display in green "$`lblvar' {col 14}" _continue matrix hcell = (0) matrix hrow = (0) quietly replace `t_hutil' = $`var' if $`var' == `t_maxutil' quietly tabulate `t_hutil' if $g_touse == 1, matcell(hcell) matrow(hrow) local i = 1 local sum = 0 forvalues col = 1/10 { if (hrow[`i',1] == `col') { display in yellow _continue %5.0f hcell[`i',1] " " local sum = `sum' + hcell[`i',1] local i = `i' + 1 } else { display in yellow _continue %5.0f 0 " " } } display in yellow %9.0g `sum' display in green "{col 14}" _continue local i = 1 forvalues col = 1/10 { if (hrow[`i',1] == `col') { display in yellow _continue %5.0f hcell[`i',1] / `sum' * 100 " " local i = `i' + 1 } else { display in yellow _continue %5.0f 0 " " } } display in green " 100 %" quietly replace `t_hutil' = . display in green "{hline 82}" } end program define LUTIL syntax varlist(numeric) [if] [in] [, Transform(numlist min=10 max=10) EXclude(string) Output(string) /* */ Save SET(string) MAXUtil SHUtil SUtil GUtil HPTYUtil LUtil OVERlap ALL] display _newline display in green "{hline 82}" display in green " Distribution of lowest utility score, by party" display in green "{hline 82}" display in green " 1 2 3 4 5 6 7 8 9" /* */ " 10 N/Total" display in green "{hline 82}" /* Find the minimum score */ tempvar t_minutil local minline = subinstr("$g_pty_str_o"," ",",",.) quietly generate `t_minutil' = min(`minline') /* Set temporary variable to score when highest and tabulate */ tempvar t_hutil quietly generate `t_hutil' = . forvalues p = 1/$g_nparties { local var = "t_pty`p'o" local lblvar = "g_lbl`p'" display in green "$`lblvar' {col 14}" _continue matrix hcell = (0) matrix hrow = (0) quietly replace `t_hutil' = $`var' if $`var' == `t_minutil' quietly tabulate `t_hutil' if $g_touse == 1, matcell(hcell) matrow(hrow) local i = 1 local sum = 0 forvalues col = 1/10 { if (hrow[`i',1] == `col') { display in yellow _continue %5.0f hcell[`i',1] " " local sum = `sum' + hcell[`i',1] local i = `i' + 1 } else { display in yellow _continue %5.0f 0 " " } } display in yellow %9.0g `sum' display in green "{col 14}" _continue local i = 1 forvalues col = 1/10 { if (hrow[`i',1] == `col') { display in yellow _continue %5.0f hcell[`i',1] / `sum' * 100 " " local i = `i' + 1 } else { display in yellow _continue %5.0f 0 " " } } display in green " 100 %" quietly replace `t_hutil' = . display in green "{hline 82}" } end program define Overlap syntax varlist(numeric) [if] [in] [, Transform(numlist min=10 max=10) EXclude(string) Output(string) /* */ Save SET(string) MAXUtil SHUtil SUtil GUtil HPTYUtil LUtil OVERlap ALL] display _newline display in green "{hline 82}" display in green " Potential electorate" display in green "{hline 82}" display in green "{col 16}" _continue /* Display the party names */ forvalues p=1/$g_nparties { local var = "g_lbl`p'" display in green %-5s substr("$`var'",1,5) " " _continue } display "" display in green "{hline 82}" display in green "Potential{col 14}" _continue /* Calculate the potential vote */ forvalues p=1/$g_nparties { local var = "t_pty`p'" quietly summarize $`var' if $g_touse == 1 display in yellow %5.0f r(mean) * 100 " " _continue local potv`p' = r(mean) } display "" forvalues p=1/$g_nparties { /* Calculate uniqueness of support for this party */ tempvar t_uniq`p' quietly generate `t_uniq`p'' = 0 forvalues q=1/$g_nparties { if (`p' != `q') { local var = "t_pty`q'" quietly replace `t_uniq`p'' = $`var' if $g_touse == 1 & $`var' > `t_uniq`p'' & $`var' < . } } local var = "t_pty`p'" quietly replace `t_uniq`p'' = max(0,$`var' - `t_uniq`p'') quietly summarize `t_uniq`p'' if $g_touse == 1 & $`var' < . local uniq`p' = r(mean) /* Calculate max of all pairs with this party */ forvalues q=1/$g_nparties { tempvar t_mn`p'_`q' local var1 = "t_pty`p'" local var2 = "t_pty`q'" quietly generate `t_mn`p'_`q'' = min($`var1',$`var2') quietly summarize `t_mn`p'_`q'' if $g_touse == 1 & $`var1' < . & $`var2' < . local min`p'_`q' = r(sum) } } display in green " support (%)" display in green "{hline 82} display in green "Unique{col 14}" _continue forvalues p = 1/$g_nparties { display in yellow %5.0f `uniq`p'' * 100 " " _continue } display "" display in green " support (%)" display in green "{hline 82}" /* Display overlap percentages */ forvalues p = 1/$g_nparties { local var = "t_pty`p'o" local lblvar = "g_lbl`p'" display in green "$`lblvar' {col 14}" _continue forvalues q = 1/$g_nparties { local var = "t_pty`q'" quietly summarize $`var' if $g_touse == 1 local sum_q = r(sum) display in yellow %5.0f `min`p'_`q'' / `sum_q' * 100 " " _continue } display "" } display in green "{hline 82}" if ("`output'" != "") { file open outfile using `output', write file write outfile "party_id,name,potvote,uniqvote" _n forvalues p=1/$g_nparties { local lblvar = "g_lbl`p'" file write outfile "`p',$`lblvar',`potv`p'',`uniq`p''" _n } file close outfile display _newline display in green "The file `output' now contains the potential and unique vote for each party. You can " _continue display in green "with the command " _newline "{cmd:. insheet using `output'}." } end program define MorePreferred syntax varlist(numeric) [if] [in] [, Transform(numlist min=10 max=10) EXclude(string) Output(string) /* */ Save SET(string) MAXUtil SHUtil SUtil GUtil HPTYUtil LUtil OVERlap ALL] forvalues p = 1/$g_nparties { quietly generate rutil`p' = 0 local var = "g_lbl`p'" quietly label variable rutil`p' "$`var': Number of parties preferred more" local var2 = "t_pty`p'" forvalues q = 1/$g_nparties { if (`p' != `q') { local var1 = "t_pty`q'" quietly replace rutil`p' = rutil`p' + 1 if $`var1' > $`var2' & $`var1' < . } } quietly replace rutil`p' = . if $`var2' >= . } end