-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d9bce1
commit d8fa136
Showing
285 changed files
with
2,474 additions
and
7,325 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using Clustering | ||
using CSV | ||
using DataFrames | ||
using Plots | ||
using Statistics | ||
|
||
df = CSV.read("objs/margins.csv",DataFrame) | ||
|
||
function cluster_data(df::DataFrame, n_clusters::Int=3) | ||
# Input validation | ||
if nrow(df) < n_clusters | ||
error("Number of rows ($(nrow(df))) must be greater than number of clusters ($n_clusters)") | ||
end | ||
|
||
# Create position array based on order in dataframe | ||
positions = collect(1:nrow(df)) | ||
|
||
# Extract features into a matrix - using position and margin | ||
features = hcat(positions, df.margin)' | ||
|
||
# Normalize features to give equal weight | ||
features_normalized = (features .- mean(features, dims=2)) ./ std(features, dims=2) | ||
|
||
# Perform k-means clustering | ||
result = kmeans(features_normalized, n_clusters) | ||
|
||
# Add cluster assignments to the original dataframe | ||
df_clustered = copy(df) | ||
df_clustered.cluster = result.assignments | ||
|
||
# Calculate cluster statistics | ||
cluster_stats = DataFrame() | ||
for i in 1:n_clusters | ||
cluster_indices = findall(x -> x == i, df_clustered.cluster) | ||
cluster_data = df_clustered[cluster_indices, :] | ||
stats = ( | ||
cluster = i, | ||
size = nrow(cluster_data), | ||
mean_position = mean(positions[cluster_indices]), | ||
mean_margin = mean(cluster_data.margin), | ||
std_position = std(positions[cluster_indices]), | ||
std_margin = std(cluster_data.margin) | ||
) | ||
push!(cluster_stats, stats) | ||
end | ||
|
||
# Define custom colors for clusters | ||
cluster_colors = [:green, :orange, :yellow] | ||
|
||
# Create visualization with all specified formatting | ||
plt = Plots.scatter( | ||
positions, | ||
100 .* df_clustered.margin, # Multiply by 100 to convert to percentage | ||
group=df_clustered.cluster, | ||
xlabel="Order of AP Call", | ||
ylabel="Democratic Margin (%)", | ||
title="2020 Election", | ||
legend=:topright, | ||
yformatter=y->string(round(Int, y)) * "%", # Format as percentage with no decimals | ||
markersize=8, | ||
framestyle=:box, | ||
grid=true, | ||
gridlinewidth=0.5, | ||
gridstyle=:dash, | ||
gridcolor=:gray, | ||
palette=cluster_colors, | ||
legend_title="Clusters" | ||
) | ||
|
||
# Add emphasized zero line | ||
hline!(plt, [0], color=:black, linewidth=1.5, label=nothing) | ||
|
||
return df_clustered, cluster_stats, plt | ||
end | ||
|
||
# Example usage: | ||
df_with_clusters, stats, cluster_plot = cluster_data(df) | ||
display(cluster_plot) | ||
println(stats) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
st,cohort,totpop,cpop,reg,vot | ||
AZ,18 to 24,653,629,371,327 | ||
AZ,25 to 34,941,850,619,579 | ||
AZ,35 to 44,929,756,565,509 | ||
AZ,45 to 64,1833,1627,1301,1242 | ||
AZ,65+,1282,1213,1023,992 | ||
GA,18 to 24,990,925,556,489 | ||
GA,25 to 34,1496,1354,913,794 | ||
GA,35 to 44,1287,1083,723,690 | ||
GA,45 to 64,2634,2454,1854,1783 | ||
GA,65+,1625,1584,1187,1132 | ||
MI,18 to 24,932,887,546,469 | ||
MI,25 to 34,1344,1257,910,777 | ||
MI,35 to 44,1130,1063,764,666 | ||
MI,45 to 64,2663,2547,1969,1798 | ||
MI,65+,1721,1713,1324,1284 | ||
NV,18 to 24,284,262,110,96 | ||
NV,25 to 34,463,415,275,236 | ||
NV,35 to 44,352,310,193,176 | ||
NV,45 to 64,804,734,505,481 | ||
NV,65+,500,477,373,363 | ||
NC,18 to 24,872,820,465,404 | ||
NC,25 to 34,1412,1259,809,706 | ||
NC,35 to 44,1315,1068,705,637 | ||
NC,45 to 64,2524,2303,1699,1638 | ||
NC,65+,1992,1941,1483,1396 | ||
PA,18 to 24,1049,1017,601,519 | ||
PA,25 to 34,1789,1747,1354,1207 | ||
PA,35 to 44,1360,1281,948,866 | ||
PA,45 to 64,3212,3122,2491,2315 | ||
PA,65+,2492,2453,1943,1850 | ||
PA,18 to 24,581,577,333,323 | ||
PA,25 to 34,762,714,582,557 | ||
PA,35 to 44,650,617,437,426 | ||
PA,45 to 64,1604,1572,1241,1171 | ||
PA,65+,941,941,797,776 | ||
"Source: U.S. Census Bureau, Current Population Survey, November 2022",,,,, | ||
"url: ""https://www.census.gov/data/tables/time-series/demo/voting-and-registration/p20-585.html""",,,,, | ||
"Table 4c. Reported Voting and Registration of the Total Voting-Age Population, by Age, for States: November 2022",,,,, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
st,totpop,eligible,registered,pctreg`,voted,pctvot | ||
AZ,5638,5075,3878,76.4,3649,71.9 | ||
GA,8032,7400,5233,70.7,4888,66.1 | ||
MI,7790,7467,5513,73.8,4994,66.9 | ||
NV,2402,2198,1455,66.2,1351,61.5 | ||
NC,8113,7391,5161,69.8,4780,64.7 | ||
PA,9902,9621,7337,76.3,6756,70.2 | ||
WI,4538,4421,3391,76.7,3253,73.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
demograph,gender,cohort,registered,voted | ||
Male over 18,m,121870,65.1,59.5 | ||
18 to 24 years,m,14382,54.7,45.8 | ||
25 to 34 years,m,22406,58.1,51.0 | ||
35 to 44 years,m,20466,59.6,53.3 | ||
45 to 54 years,m,19442,65.8,60.7 | ||
55 to 64 years,m,20318,71.2,67.1 | ||
65 to 74 years,m,15401,75.8,73.2 | ||
75 years and over,m,9456,77.1,72.5 | ||
18 years,m,1998,42.9,37.2 | ||
19 years,m,1936,52.7,43.2 | ||
20 years,m,2361,56.8,47.2 | ||
21 years,m,2134,56.0,44.1 | ||
22 years,m,1909,58.5,47.2 | ||
23 years,m,2025,58.7,51.6 | ||
24 years,m,2018,57.0,50.0 | ||
25 years,m,2045,53.3,45.6 | ||
26 years,m,2060,50.6,43.3 | ||
27 years,m,2347,61.0,53.5 | ||
28 years,m,2331,58.5,49.6 | ||
29 years,m,2456,58.3,51.2 | ||
30 years,m,2233,57.3,50.8 | ||
31 years,m,2396,59.5,52.3 | ||
32 years,m,2113,62.4,56.3 | ||
33 years,m,2080,58.9,53.1 | ||
34 years,m,2344,60.6,53.0 | ||
35 years,m,2210,58.7,51.7 | ||
36 years,m,2325,58.0,51.6 | ||
37 years,m,2002,60.1,55.5 | ||
38 years,m,2083,60.5,52.6 | ||
39 years,m,1954,62.4,54.4 | ||
40 years,m,2049,56.6,51.2 | ||
41 years,m,1924,56.6,51.3 | ||
42 years,m,1972,62.0,55.6 | ||
43 years,m,1960,61.9,56.0 | ||
44 years,m,1986,59.8,53.8 | ||
45 years,m,1921,62.4,57.6 | ||
46 years,m,1918,64.5,59.1 | ||
47 years,m,1834,66.4,59.1 | ||
48 years,m,1937,64.2,58.7 | ||
49 years,m,1925,64.0,60.5 | ||
50 years,m,2005,63.4,59.7 | ||
51 years,m,2082,68.4,61.7 | ||
52 years,m,1958,68.5,65.1 | ||
53 years,m,1999,68.0,61.8 | ||
54 years,m,1863,68.1,63.3 | ||
55 years,m,2186,66.6,61.5 | ||
56 years,m,1951,72.0,67.6 | ||
57 years,m,1989,69.8,65.4 | ||
58 years,m,2091,70.2,65.3 | ||
59 years,m,1961,72.3,67.7 | ||
60 years,m,2196,69.0,66.0 | ||
61 years,m,2119,68.0,64.3 | ||
62 years,m,1902,73.1,68.3 | ||
63 years,m,1962,77.8,74.4 | ||
64 years,m,1960,74.8,71.2 | ||
65 years,m,1909,73.7,70.4 | ||
66 years,m,1765,74.5,72.0 | ||
67 years,m,1705,75.9,73.1 | ||
68 years,m,1610,75.4,72.6 | ||
69 years,m,1519,75.9,73.3 | ||
70 years,m,1441,73.3,71.5 | ||
71 years,m,1347,74.7,73.0 | ||
72 years,m,1536,76.9,75.2 | ||
73 years,m,1318,82.7,78.8 | ||
74 years,m,1251,76.9,73.3 | ||
75 years,m,1073,76.6,75.2 | ||
76 years,m,901,82.0,77.7 | ||
77 years,m,806,79.9,76.3 | ||
78 years,m,859,81.5,77.4 | ||
79 years,m,749,79.7,76.1 | ||
80-84 years,m,2838,77.7,73.0 | ||
85 years and over,m,2229,71.0,64.1 | ||
Female 18 years and over,f,130404,68.2,63.0 | ||
18 to 24 years,f,14277,56.8,50.2 | ||
25 to 34 years,f,22466,63.8,56.4 | ||
35 to 44 years,f,21092,64.7,59.3 | ||
45 to 54 years,f,20316,68.5,64.0 | ||
55 to 64 years,f,21837,73.4,69.7 | ||
65 to 74 years,f,17553,76.0,72.8 | ||
75 years and over,f,12864,74.4,68.5 | ||
18 years,f,1928,48.4,43.1 | ||
19 years,f,1872,55.4,50.5 | ||
20 years,f,2213,58.0,50.8 | ||
21 years,f,2045,53.7,48.0 | ||
22 years,f,2108,62.7,53.8 | ||
23 years,f,1935,62.4,53.8 | ||
24 years,f,2176,56.7,50.7 | ||
25 years,f,2010,62.1,54.2 | ||
26 years,f,2201,61.6,54.4 | ||
27 years,f,2389,63.9,55.2 | ||
28 years,f,2187,62.7,54.4 | ||
29 years,f,2413,64.5,58.6 | ||
30 years,f,2504,64.2,55.4 | ||
31 years,f,2225,64.0,56.4 | ||
32 years,f,2212,67.4,61.5 | ||
33 years,f,2304,62.9,56.5 | ||
34 years,f,2022,64.0,57.3 | ||
35 years,f,2295,64.4,57.5 | ||
36 years,f,2264,65.4,59.1 | ||
37 years,f,1992,61.2,55.8 | ||
38 years,f,2325,70.5,64.5 | ||
39 years,f,1970,66.1,61.3 | ||
40 years,f,2250,64.3,59.2 | ||
41 years,f,2028,65.6,60.1 | ||
42 years,f,2052,61.9,58.4 | ||
43 years,f,1944,63.2,59.4 | ||
44 years,f,1974,63.6,57.2 | ||
45 years,f,2101,62.9,59.0 | ||
46 years,f,1896,67.4,61.9 | ||
47 years,f,1956,65.2,60.8 | ||
48 years,f,1984,68.5,63.0 | ||
49 years,f,2038,68.6,64.1 | ||
50 years,f,2192,71.9,68.4 | ||
51 years,f,2180,67.7,62.6 | ||
52 years,f,2042,70.4,65.4 | ||
53 years,f,1975,72.8,68.9 | ||
54 years,f,1951,69.9,65.3 | ||
55 years,f,2299,70.4,67.5 | ||
56 years,f,2126,68.1,63.9 | ||
57 years,f,2006,77.7,71.9 | ||
58 years,f,2117,70.5,66.9 | ||
59 years,f,2293,74.0,69.6 | ||
60 years,f,2326,74.6,71.4 | ||
61 years,f,2241,71.5,68.3 | ||
62 years,f,2125,76.5,73.0 | ||
63 years,f,2116,77.2,74.3 | ||
64 years,f,2187,74.1,70.8 | ||
65 years,f,2084,73.8,71.7 | ||
66 years,f,1979,77.5,73.8 | ||
67 years,f,1895,74.9,71.6 | ||
68 years,f,1826,74.3,71.8 | ||
69 years,f,1766,78.0,74.6 | ||
70 years,f,1740,72.6,69.7 | ||
71 years,f,1642,76.5,73.0 | ||
72 years,f,1640,79.5,75.5 | ||
73 years,f,1633,78.4,74.3 | ||
74 years,f,1348,75.4,72.5 | ||
75 years,f,1176,81.6,76.2 | ||
76 years,f,1228,77.1,73.6 | ||
77 years,f,1138,77.1,74.3 | ||
78 years,f,1077,81.1,78.1 | ||
79 years,f,873,75.0,70.0 | ||
80-84 years,f,3715,74.0,67.9 | ||
85 years and over,f,3656,68.7,59.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
st,cohort,totpop,cpop,reg,vot | ||
AZ,18 to 24 ,751,694,322,170 | ||
AZ,25 to 34 ,911,782,509,300 | ||
AZ,35 to 44 ,937,800,532,428 | ||
AZ,45 to 64 ,1800,1530,1148,975 | ||
AZ,65 and over,1332,1287,1047,971 | ||
GA,18 to 24 ,924,859,466,322 | ||
GA,25 to 34 ,1620,1395,918,640 | ||
GA,35 to 44 ,1400,1176,812,628 | ||
GA,45 to 64 ,2610,2451,1695,1457 | ||
GA,65 and over,1760,1720,1384,1276 | ||
NC,18 to 24 ,921,866,372,40.3 | ||
NC,25 to 34 ,1395,1236,652,46.7 | ||
NC,35 to 44 ,1290,1069,675,52.3 | ||
NC,45 to 64 ,2687,2523,1581,58.9 | ||
NC,65 and over,1881,1838,1303,69.3 | ||
MI,18 to 24 ,826,799,532,371 | ||
MI,25 to 34 ,1304,1223,852,622 | ||
MI,35 to 44 ,1228,1183,894,689 | ||
MI,45 to 64 ,2639,2545,2025,1710 | ||
MI,65 and over,1779,1767,1494,1365 | ||
NV,18 to 24 ,297,287,122,63 | ||
NV,25 to 34 ,414,355,222,153 | ||
NV,35 to 44 ,406,358,229,170 | ||
NV,45 to 64 ,789,694,458,378 | ||
NV,65 and over,544,511,405,358 | ||
PA,18 to 24 ,1145,1093,610,437 | ||
PA,25 to 34 ,1680,1611,1041,776 | ||
PA,35 to 44 ,1531,1397,1007,784 | ||
PA,45 to 64 ,3139,3043,2328,1995 | ||
PA,65 and over,2630,2597,2024,1851 | ||
WI,18 to 24 ,588,579,341,286 | ||
WI,25 to 34 ,724,679,474,371 | ||
WI,35 to 44 ,693,669,447,373 | ||
WI,45 to 64 ,1473,1440,1107,908 | ||
WI,65 and over,1113,1095,856,777 | ||
"Source: U.S. Census Bureau, Current Population Survey, November 2022",,,,, | ||
https://www.census.gov/data/tables/time-series/demo/voting-and-registration/p20-586.html,,,,, | ||
"Table 4c. Reported Voting and Registration of the Total Voting-Age Population, by Age, for States: November 2022",,,,, |
Oops, something went wrong.