Skip to content

Instantly share code, notes, and snippets.

@tjmahr
Last active October 18, 2024 18:10
Show Gist options
  • Save tjmahr/b7b888bf56918c7fb2fbb5d8be64836a to your computer and use it in GitHub Desktop.
Save tjmahr/b7b888bf56918c7fb2fbb5d8be64836a to your computer and use it in GitHub Desktop.
library(tidyverse)
# d <- readr::read_tsv("test.tsv") |>
#   janitor::clean_names()
#
# d$total <- d$biden + d$trump + d$other
# d$prop_votes_in <- d$ballots_accepted / d$total
# d$prop_biden <- d$biden / d$total
# datapasta::tribble_paste(d)

d <- tibble::tribble(
                                              ~county, ~ballots_accepted, ~biden, ~trump, ~other, ~total,     ~prop_votes_in,        ~prop_biden,
                                            "APPLING",              1468,   1784,   6570,     36,   8390,  0.174970202622169,  0.212634088200238,
                                           "ATKINSON",               274,    825,   2300,     30,   3155, 0.0868462757527734,   0.26148969889065,
                                              "BACON",              1190,    625,   4017,     25,   4667,  0.254981787015213,  0.133919005785301,
                                              "BAKER",               237,    652,    897,      6,   1555,  0.152411575562701,  0.419292604501608,
                                            "BALDWIN",              4214,   9140,   8903,    218,  18261,  0.230765018345107,  0.500520234379278,
                                              "BANKS",              2262,    932,   7795,     78,   8805,  0.256899488926746,  0.105848949460534,
                                             "BARROW",              7210,  10453,  26804,    689,  37946,  0.190006851842091,  0.275470405312813,
                                             "BARTOW",              7625,  12091,  37672,    723,  50486,  0.151031969258804,  0.239492136433863,
                                           "BEN HILL",              1879,   2393,   4111,     60,   6564,  0.286258379037172,  0.364564290067032,
                                            "BERRIEN",              1505,   1269,   6419,     56,   7744,  0.194344008264463,  0.163868801652893,
                                               "BIBB",             12239,  43408,  26559,    796,  70763,  0.172957619094725,  0.613427921371338,
                                           "BLECKLEY",              1443,   1312,   4329,     69,   5710,  0.252714535901926,  0.229772329246935,
                                           "BRANTLEY",              1791,    700,   6993,     56,   7749,  0.231126596980256, 0.0903342366757001,
                                             "BROOKS",              1191,   2791,   4261,     49,   7101,  0.167722855935784,  0.393043233347416,
                                              "BRYAN",              5601,   6738,  14240,    371,  21349,  0.262354208628039,  0.315611972457726,
                                            "BULLOCH",              4645,  11248,  18387,    474,  30109,  0.154272808794713,  0.373576007173935,
                                              "BURKE",              1550,   5208,   5400,     77,  10685,   0.14506317267197,  0.487412260177819,
                                              "BUTTS",              3347,   3274,   8406,     96,  11776,   0.28422214673913,  0.278023097826087,
                                            "CALHOUN",               236,   1263,    923,     12,   2198,    0.1073703366697,  0.574613284804368,
                                             "CAMDEN",              4488,   7967,  15249,    482,  23698,  0.189383070301291,   0.33618870790784,
                                            "CANDLER",               966,   1269,   3133,     29,   4431,  0.218009478672986,  0.286391333784699,
                                            "CARROLL",              8254,  16236,  37476,    790,  54502,   0.15144398370702,  0.297897324868812,
                                            "CATOOSA",              7716,   6932,  25167,    527,  32626,  0.236498498130326,  0.212468583338442,
                                           "CHARLTON",               898,   1105,   3419,     44,   4568,  0.196584938704028,  0.241900175131349,
                                            "CHATHAM",             25446,  78247,  53232,   2005, 133484,  0.190629588564922,  0.586190105181145,
                                      "CHATTAHOOCHEE",               319,    667,    880,     35,   1582,  0.201643489254109,  0.421618204804045,
                                          "CHATTOOGA",              1949,   1854,   8064,    135,  10053,  0.193872475877847,  0.184422560429722,
                                           "CHEROKEE",             25635,  42779,  99585,   2495, 144859,  0.176965186836855,  0.295314754347331,
                                             "CLARKE",              5321,  36055,  14450,    916,  51421,  0.103479123315377,  0.701172672643472,
                                               "CLAY",               285,    791,    637,      8,   1436,  0.198467966573816,    0.5508356545961,
                                            "CLAYTON",             22973,  95466,  15811,   1114, 112391,  0.204402487743681,  0.849409650238898,
                                             "CLINCH",               528,    744,   2105,     13,   2862,  0.184486373165618,  0.259958071278826,
                                               "COBB",             70796, 221847, 165436,   6739, 394022,  0.179675246559837,  0.563032013440874,
                                             "COFFEE",              2681,   4511,  10578,    125,  15214,  0.176219271723413,   0.29650322071776,
                                           "COLQUITT",              2890,   4190,  11777,    120,  16087,  0.179648163113073,  0.260458755516877,
                                           "COLUMBIA",             14682,  29232,  50013,   1375,  80620,  0.182113619449268,  0.362589928057554,
                                               "COOK",              1564,   2059,   4900,     78,   7037,  0.222253801335797,   0.29259627682251,
                                             "COWETA",             16983,  24210,  51501,   1134,  76845,  0.221003318368144,  0.315049775522155,
                                           "CRAWFORD",              1260,   1615,   4428,     59,   6102,  0.206489675516224,  0.264667322189446,
                                              "CRISP",              1937,   2982,   4985,     69,   8036,  0.241040318566451,  0.371080139372822,
                                               "DADE",              1760,   1261,   6066,    120,   7447,  0.236336779911374,  0.169329931516047,
                                             "DAWSON",              4457,   2486,  13398,    200,  16084,  0.277107684655558,   0.15456354140761,
                                            "DECATUR",              2288,   4782,   6755,     91,  11628,  0.196766425868593,   0.41124871001032,
                                             "DEKALB",             67061, 308162,  58377,   4338, 370877,  0.180817359933347,  0.830900810780933,
                                              "DODGE",              1442,   2172,   5843,     57,   8072,  0.178642220019822,  0.269078295341923,
                                              "DOOLY",               708,   1911,   2159,     36,   4106,  0.172430589381393,  0.465416463711642,
                                          "DOUGHERTY",              5021,  24568,  10441,    281,  35290,  0.142278265797676,  0.696174553697931,
                                            "DOUGLAS",             14475,  42814,  25454,    871,  69139,  0.209360852774845,  0.619245288476836,
                                              "EARLY",               940,   2450,   2710,     28,   5188,  0.181187355435621,  0.472243639167309,
                                             "ECHOLS",               264,    167,   1256,     19,   1442,  0.183079056865465,  0.115811373092926,
                                          "EFFINGHAM",              5751,   7718,  23361,    500,  31579,  0.182114696475506,  0.244402925995123,
                                             "ELBERT",              1732,   2879,   6226,     71,   9176,  0.188753269398431,  0.313753269398431,
                                            "EMANUEL",              1373,   2886,   6553,     68,   9507,  0.144419901125486,  0.303565793625749,
                                              "EVANS",               827,   1324,   2888,     36,   4248,  0.194679849340866,  0.311676082862524,
                                             "FANNIN",              3286,   2570,  12169,    110,  14849,  0.221294363256785,  0.173075627988417,
                                            "FAYETTE",             15735,  33062,  37956,    994,  72012,  0.218505249125146,  0.459117924790313,
                                              "FLOYD",              8820,  11917,  28906,    542,  41365,  0.213223739876707,  0.288093799105524,
                                            "FORSYTH",             28565,  42208,  85123,   2046, 129377,  0.220788857370321,   0.32624036729867,
                                           "FRANKLIN",              2420,   1593,   9069,    105,  10767,  0.224760843317544,  0.147952075787127,
                                             "FULTON",            114632, 380212, 137247,   6472, 523931,  0.218792169197852,  0.725690978392193,
                                             "GILMER",              3272,   2932,  13429,    166,  16527,  0.197979064561021,  0.177406667876808,
                                           "GLASCOCK",               305,    155,   1402,      8,   1565,  0.194888178913738, 0.0990415335463259,
                                              "GLYNN",              9727,  15882,  25617,    495,  41994,   0.23162832785636,  0.378196885269324,
                                             "GORDON",              4829,   4384,  19405,    255,  24044,  0.200840126434869,  0.182332390617202,
                                              "GRADY",              2112,   3619,   7034,     54,  10707,  0.197254132810311,  0.338003175492668,
                                             "GREENE",              3257,   4087,   7066,     94,  11247,  0.289588334667022,  0.363385791766693,
                                           "GWINNETT",             66750, 241994, 166400,   5956, 414350,  0.161095692047786,  0.584032822493061,
                                          "HABERSHAM",              4419,   3562,  16637,    243,  20442,  0.216172585852656,  0.174249095000489,
                                               "HALL",             20441,  25033,  64183,   1386,  90602,  0.225613121123154,   0.27629632899936,
                                            "HANCOCK",               679,   2976,   1154,     23,   4153,  0.163496267758247,  0.716590416566338,
                                           "HARALSON",              2691,   1791,  12330,    127,  14248,  0.188868613138686,  0.125701852891634,
                                             "HARRIS",              3956,   5457,  14319,    226,  20002,  0.197780221977802,  0.272822717728227,
                                               "HART",              3132,   3157,   9465,    112,  12734,  0.245955709125177,  0.247918957122664,
                                              "HEARD",              1015,    824,   4519,     51,   5394,  0.188172043010753,  0.152762328513163,
                                              "HENRY",             26095,  73443,  48259,   1314, 123016,  0.212126877804513,  0.597019899850426,
                                            "HOUSTON",             17755,  32239,  41540,   1093,  74872,  0.237138048936852,  0.430588203867935,
                                              "IRWIN",               864,   1008,   3134,     26,   4168,  0.207293666026871,  0.241842610364683,
                                            "JACKSON",              8074,   7642,  29502,    541,  37685,  0.214249701472735,  0.202786254477909,
                                             "JASPER",              1581,   1761,   5822,     64,   7647,  0.206747744213417,   0.23028638681836,
                                         "JEFF DAVIS",               877,   1028,   4695,     51,   5774,  0.151887772774506,  0.178039487357118,
                                          "JEFFERSON",               944,   4058,   3537,     44,   7639,    0.1235763843435,  0.531221364052887,
                                            "JENKINS",               592,   1266,   2161,     28,   3455,  0.171345875542692,  0.366425470332851,
                                            "JOHNSON",               775,   1222,   2850,     28,   4100,  0.189024390243902,  0.298048780487805,
                                              "JONES",              2808,   4882,   9940,    118,  14940,  0.187951807228916,  0.326773761713521,
                                              "LAMAR",              2152,   2620,   6331,     94,   9045,  0.237921503593145,  0.289662797125484,
                                             "LANIER",               657,   1019,   2509,     48,   3576,  0.183724832214765,  0.284955257270693,
                                            "LAURENS",              3251,   8074,  14493,    165,  22732,  0.143014253035369,  0.355182122118599,
                                                "LEE",              3146,   4558,  12007,    154,  16719,   0.18816914887254,  0.272623960763204,
                                            "LIBERTY",              4995,  13104,   7959,    331,  21394,  0.233476675703468,  0.612508179863513,
                                            "LINCOLN",               863,   1432,   3173,     36,   4641,  0.185951303598362,  0.308554190907132,
                                               "LONG",              1212,   2035,   3527,     98,   5660,  0.214134275618375,  0.359540636042403,
                                            "LOWNDES",              8975,  20116,  25692,    567,  46375,  0.193530997304582,  0.433768194070081,
                                            "LUMPKIN",              4366,   3126,  12163,    256,  15545,  0.280862013509167,  0.201093599228048,
                                              "MACON",               875,   2858,   1783,     22,   4663,  0.187647437272142,  0.612910143684323,
                                            "MADISON",              3000,   3411,  11326,    208,  14945,  0.200736032117765,  0.228236868517899,
                                             "MARION",               637,   1312,   2275,     39,   3626,  0.175675675675676,  0.361831218974076,
                                           "MCDUFFIE",              1928,   4168,   6169,    119,  10456,  0.184391736801836,  0.398622800306044,
                                           "MCINTOSH",              1641,   2612,   4016,     68,   6696,  0.245071684587814,  0.390083632019116,
                                         "MERIWETHER",              1875,   4287,   6524,     69,  10880,  0.172334558823529,  0.394025735294118,
                                             "MILLER",               537,    748,   2066,     20,   2834,  0.189484827099506,   0.26393789696542,
                                           "MITCHELL",              1454,   3993,   4935,     35,   8963,  0.162222470155082,  0.445498159098516,
                                             "MONROE",              3821,   4385,  11057,    150,  15592,  0.245061570035916,   0.28123396613648,
                                         "MONTGOMERY",               375,    980,   2960,     28,   3968, 0.0945060483870968,  0.246975806451613,
                                             "MORGAN",              2961,   3353,   8231,    126,  11710,  0.252860802732707,  0.286336464560205,
                                             "MURRAY",              3731,   2301,  12944,    150,  15395,  0.242351412796362,  0.149464111724586,
                                           "MUSCOGEE",             15981,  49446,  30107,    975,  80528,  0.198452712100139,  0.614022451818001,
                                             "NEWTON",              9283,  29789,  23869,    605,  54263,  0.171074212631075,  0.548974439304867,
                                             "OCONEE",              5262,   8162,  16595,    436,  25193,  0.208867542571349,  0.323978883023062,
                                         "OGLETHORPE",              1696,   2439,   5592,    107,   8138,  0.208405013516835,  0.299705087245023,
                                           "PAULDING",             13477,  29695,  54517,   1205,  85417,  0.157778896472599,  0.347647423814931,
                                              "PEACH",              3164,   5922,   6506,    126,  12554,  0.252031225107535,  0.471722160267644,
                                            "PICKENS",              3610,   2824,  14110,    238,  17172,   0.21022594921966,  0.164453761938039,
                                             "PIERCE",              2234,   1100,   7898,     50,   9048,  0.246905393457118,   0.12157382847038,
                                               "PIKE",              2481,   1505,   9127,     89,  10721,  0.231414979945901,  0.140378696017163,
                                               "POLK",              4410,   3657,  13587,    155,  17399,  0.253462842692109,  0.210184493361687,
                                            "PULASKI",              1176,   1230,   2815,     36,   4081,  0.288164665523156,  0.301396716491056,
                                             "PUTNAM",              2951,   3448,   8291,    118,  11857,  0.248882516656827,  0.290798684321498,
                                            "QUITMAN",               263,    497,    604,      5,   1106,  0.237793851717902,  0.449367088607595,
                                              "RABUN",              2716,   1984,   7474,    116,   9574,  0.283684980154585,  0.207227908919992,
                                           "RANDOLPH",               540,   1671,   1390,     12,   3073,  0.175724048161406,   0.54376830458835,
                                           "RICHMOND",             13411,  59119,  26780,   1178,  87077,  0.154013114829404,  0.678927845470101,
                                           "ROCKDALE",              8949,  31237,  13014,    448,  44699,   0.20020582115931,  0.698829951453053,
                                             "SCHLEY",               487,    462,   1800,     13,   2275,  0.214065934065934,  0.203076923076923,
                                            "SCREVEN",              1362,   2661,   3915,     53,   6629,  0.205460853824106,  0.401418011766481,
                                           "SEMINOLE",               656,   1256,   2613,     19,   3888,  0.168724279835391,  0.323045267489712,
                                           "SPALDING",              6435,  11828,  18104,    287,  30219,  0.212945497865581,  0.391409378205765,
                                           "STEPHENS",              3313,   2386,   9367,    132,  11885,  0.278754732856542,  0.200757257046698,
                                            "STEWART",               340,   1182,    801,      7,   1990,  0.170854271356784,  0.593969849246231,
                                             "SUMTER",              2048,   6314,   5733,    103,  12150,  0.168559670781893,  0.519670781893004,
                                             "TALBOT",               557,   2114,   1392,     18,   3524,  0.158059023836549,   0.59988649262202,
                                         "TALIAFERRO",               167,    561,    360,      7,    928,  0.179956896551724,  0.604525862068966,
                                           "TATTNALL",              1107,   2062,   6054,     71,   8187,  0.135214364235984,  0.251862709173079,
                                             "TAYLOR",               887,   1388,   2420,     34,   3842,  0.230869338885997,  0.361270171785528,
                                            "TELFAIR",               653,   1488,   2825,     22,   4335,  0.150634371395617,  0.343252595155709,
                                            "TERRELL",               775,   2376,   2004,     36,   4416,  0.175498188405797,   0.53804347826087,
                                             "THOMAS",              5343,   8708,  12969,    200,  21877,  0.244229099053801,  0.398043607441605,
                                               "TIFT",              3393,   5318,  10784,    178,  16280,  0.208415233415233,  0.326658476658477,
                                             "TOOMBS",              1915,   2938,   7873,    103,  10914,  0.175462708447865,  0.269195528678761,
                                              "TOWNS",              2525,   1550,   6384,     45,   7979,  0.316455696202532,  0.194259932322346,
                                           "TREUTLEN",               469,    952,   2101,     24,   3077,  0.152421189470263,   0.30939226519337,
                                              "TROUP",              5176,  11577,  18142,    338,  30057,  0.172206141664171,  0.385168180457131,
                                             "TURNER",               989,   1409,   2349,     33,   3791,  0.260881034027961,  0.371669744130836,
                                             "TWIGGS",               592,   2044,   2370,     30,   4444,  0.133213321332133,   0.45994599459946,
                                              "UNION",              4030,   2800,  12650,    112,  15562,  0.258964143426295,  0.179925459452513,
                                              "UPSON",              3219,   4203,   8606,     98,  12907,   0.24939955063144,  0.325637251104052,
                                             "WALKER",              2988,   5770,  23173,    431,  29374,  0.101722611833594,  0.196432218969156,
                                             "WALTON",             10592,  12683,  37839,    576,  51098,  0.207287956475792,  0.248209323261184,
                                               "WARE",              2814,   4169,   9903,    117,  14189,  0.198322644301924,  0.293819155683981,
                                             "WARREN",               501,   1468,   1166,     16,   2650,  0.189056603773585,  0.553962264150943,
                                         "WASHINGTON",              1660,   4743,   4668,     69,   9480,  0.175105485232068,  0.500316455696203,
                                              "WAYNE",              2972,   2688,   9987,    107,  12782,  0.232514473478329,  0.210295728368018,
                                            "WEBSTER",               234,    640,    748,      3,   1391,  0.168224299065421,  0.460100647016535,
                                            "WHEELER",               297,    689,   1583,     13,   2285,  0.129978118161926,  0.301531728665208,
                                              "WHITE",              3924,   2411,  12222,    198,  14831,  0.264580945317241,    0.1625648978491,
                                          "WHITFIELD",              5491,  10680,  25644,    442,  36766,  0.149349942882011,  0.290485774900723,
                                             "WILCOX",               516,    861,   2402,     16,   3279,   0.15736505032022,  0.262580054894785,
                                             "WILKES",               883,   2160,   2823,     48,   5031,  0.175511826674617,  0.429338103756708,
                                          "WILKINSON",               615,   2074,   2665,     31,   4770,  0.128930817610063,  0.434800838574423,
                                              "WORTH",              1477,   2395,   6830,     60,   9285,  0.159073774905762,  0.257942918686053
                                      )

lm(prop_biden ~ prop_votes_in, d) |> summary()
#> 
#> Call:
#> lm(formula = prop_biden ~ prop_votes_in, data = d)
#> 
#> Residuals:
#>      Min       1Q   Median       3Q      Max 
#> -0.26416 -0.10722 -0.02732  0.08851  0.50546 
#> 
#> Coefficients:
#>               Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)    0.57615    0.05804   9.927  < 2e-16 ***
#> prop_votes_in -1.13599    0.28571  -3.976 0.000107 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 0.1518 on 157 degrees of freedom
#> Multiple R-squared:  0.09148,    Adjusted R-squared:  0.08569 
#> F-statistic: 15.81 on 1 and 157 DF,  p-value: 0.0001067
lm(prop_biden ~ prop_votes_in, d, weights = d$total) |> summary()
#> 
#> Call:
#> lm(formula = prop_biden ~ prop_votes_in, data = d, weights = d$total)
#> 
#> Weighted Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -84.093 -28.343 -14.346  -3.509 194.530 
#> 
#> Coefficients:
#>               Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)    0.73619    0.09695   7.593 2.62e-12 ***
#> prop_votes_in -1.24277    0.49222  -2.525   0.0126 *  
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 35.68 on 157 degrees of freedom
#> Multiple R-squared:  0.03902,    Adjusted R-squared:  0.0329 
#> F-statistic: 6.375 on 1 and 157 DF,  p-value: 0.01257


# do a bettering thing
m <- lm(prop_biden ~ splines::ns(prop_votes_in, 3), d, weights = d$total)
summary(m)
#> 
#> Call:
#> lm(formula = prop_biden ~ splines::ns(prop_votes_in, 3), data = d, 
#>     weights = d$total)
#> 
#> Weighted Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -90.299 -25.768 -13.752   0.561 181.298 
#> 
#> Coefficients:
#>                                Estimate Std. Error t value Pr(>|t|)   
#> (Intercept)                     0.37753    0.13527   2.791  0.00592 **
#> splines::ns(prop_votes_in, 3)1  0.05560    0.08557   0.650  0.51684   
#> splines::ns(prop_votes_in, 3)2 -0.03840    0.30209  -0.127  0.89901   
#> splines::ns(prop_votes_in, 3)3 -0.52918    0.17810  -2.971  0.00344 **
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 34.69 on 155 degrees of freedom
#> Multiple R-squared:  0.1028, Adjusted R-squared:  0.08548 
#> F-statistic: 5.923 on 3 and 155 DF,  p-value: 0.00075

newdata <- tibble(
  prop_votes_in = seq(min(d$prop_votes_in), max(d$prop_votes_in), by = .001)
)
fit <- broom::augment(m, newdata = newdata, interval = "confidence")

ggplot(d) +
  aes(x = prop_votes_in, y = prop_biden) +
  geom_hline(aes(yintercept = .5)) +
  geom_point(aes(size = total)) +
  geom_ribbon(
    aes(y = .fitted, ymin = .lower, ymax = .upper),
    data = fit, 
    fill = "grey60",
    alpha = .4
  ) +
  geom_line(
    aes(y = .fitted),
    data = fit,
    color = "#3366FF", size = 1
  ) + 
  scale_y_continuous(limits = c(0, 1), oob = scales::oob_squish) +
  labs(
    x = "County's 2024 ballots accepted / 2020 vote total",
    y = "County's 2020 proportion votes for Biden",
    size = "2020 votes"
  )
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.

Created on 2024-10-18 with reprex v2.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment