Skip to content

Commit

Permalink
Completed work on 2020 models
Browse files Browse the repository at this point in the history
  • Loading branch information
technocrat committed May 19, 2024
1 parent 51785b2 commit 75a10cd
Show file tree
Hide file tree
Showing 16 changed files with 209 additions and 29 deletions.
164 changes: 162 additions & 2 deletions 2020model.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,118 @@ Although there will be a different electorate, for the reasons explained [here](


### Pennsylvania

~~~
<table>
<thead>
<tr class = "header">
<th style = "text-align: right;">median</th>
<th style = "text-align: right;">mean</th>
<th style = "text-align: right;">mode</th>
<th style = "text-align: right;">q025</th>
<th style = "text-align: right;">q975</th>
<th style = "text-align: right;">mcse</th>
<th style = "text-align: right;">rhat</th>
</tr>
<tbody>
<tr>
<td style = "text-align: right;">0.5059</td>
<td style = "text-align: right;">0.5059</td>
<td style = "text-align: right;">0.5058</td>
<td style = "text-align: right;">0.5055</td>
<td style = "text-align: right;">0.5063</td>
<td style = "text-align: right;">0.0</td>
<td style = "text-align: right;">1.0</td>
</tr>
</tbody>
</table>
<img src="/assets/img/models/PA_2020.png" style="width: 100%; display: block;">
~~~
### Georgia
~~~
<table>
<thead>
<tr class = "header">
<th style = "text-align: right;">median</th>
<th style = "text-align: right;">mean</th>
<th style = "text-align: right;">mode</th>
<th style = "text-align: right;">q025</th>
<th style = "text-align: right;">q975</th>
<th style = "text-align: right;">mcse</th>
<th style = "text-align: right;">rhat</th>
</tr>
</thead>
<tbody>
<tr>
<td style = "text-align: right;">0.5012</td>
<td style = "text-align: right;">0.5012</td>
<td style = "text-align: right;">0.5012</td>
<td style = "text-align: right;">0.5008</td>
<td style = "text-align: right;">0.5016</td>
<td style = "text-align: right;">0.0</td>
<td style = "text-align: right;">1.0007</td>
</tr>
</tbody>
</table>
<img src="/assets/img/models/GA_2020.png" style="width: 100%; display: block;">
~~~
### North Carolina

~~~
<table>
<thead>
<tr class = "header">
<th style = "text-align: right;">median</th>
<th style = "text-align: right;">mean</th>
<th style = "text-align: right;">mode</th>
<th style = "text-align: right;">q025</th>
<th style = "text-align: right;">q975</th>
<th style = "text-align: right;">mcse</th>
<th style = "text-align: right;">rhat</th>
</tr>
</thead>
<tbody>
<tr>
<td style = "text-align: right;">0.4932</td>
<td style = "text-align: right;">0.4932</td>
<td style = "text-align: right;">0.4933</td>
<td style = "text-align: right;">0.4927</td>
<td style = "text-align: right;">0.4936</td>
<td style = "text-align: right;">0.0</td>
<td style = "text-align: right;">1.0002</td>
</tr>
</tbody>
</table>
<img src="/assets/img/models/NC_2020.png" style="width: 100%; display: block;">
~~~
### Michigan

~~~
<table>
<thead>
<tr class = "header">
<th style = "text-align: right;">median</th>
<th style = "text-align: right;">mean</th>
<th style = "text-align: right;">mode</th>
<th style = "text-align: right;">q025</th>
<th style = "text-align: right;">q975</th>
<th style = "text-align: right;">mcse</th>
<th style = "text-align: right;">rhat</th>
</tr>
</thead>
<tbody>
<tr>
<td style = "text-align: right;">0.5141</td>
<td style = "text-align: right;">0.5141</td>
<td style = "text-align: right;">0.5142</td>
<td style = "text-align: right;">0.5137</td>
<td style = "text-align: right;">0.5145</td>
<td style = "text-align: right;">0.0</td>
<td style = "text-align: right;">1.0008</td>
</tr>
</tbody>
</table>
<img src="/assets/img/models/MI_2020.png" style="width: 100%; display: block;">
~~~
### Arizona
~~~
<table>
Expand Down Expand Up @@ -128,5 +233,60 @@ Although there will be a different electorate, for the reasons explained [here](
~~~

### Wisconsin
~~~
<table>
<thead>
<tr class = "header">
<th style = "text-align: right;">median</th>
<th style = "text-align: right;">mean</th>
<th style = "text-align: right;">mode</th>
<th style = "text-align: right;">q025</th>
<th style = "text-align: right;">q975</th>
<th style = "text-align: right;">mcse</th>
<th style = "text-align: right;">rhat</th>
</tr>
</thead>
<tbody>
<tr>
<td style = "text-align: right;">0.5032</td>
<td style = "text-align: right;">0.5032</td>
<td style = "text-align: right;">0.5033</td>
<td style = "text-align: right;">0.5027</td>
<td style = "text-align: right;">0.5037</td>
<td style = "text-align: right;">0.0</td>
<td style = "text-align: right;">1.0002</td>
</tr>
</tbody>
</table>
<img src="/assets/img/models/WI_2020.png" style="width: 100%; display: block;">
~~~
### Nevada

~~~
<table>
<thead>
<tr class = "header">
<th style = "text-align: right;">median</th>
<th style = "text-align: right;">mean</th>
<th style = "text-align: right;">mode</th>
<th style = "text-align: right;">q025</th>
<th style = "text-align: right;">q975</th>
<th style = "text-align: right;">mcse</th>
<th style = "text-align: right;">rhat</th>
</tr>
</thead>
<tbody>
<tr>
<td style = "text-align: right;">0.5122</td>
<td style = "text-align: right;">0.5122</td>
<td style = "text-align: right;">0.5123</td>
<td style = "text-align: right;">0.5114</td>
<td style = "text-align: right;">0.5131</td>
<td style = "text-align: right;">0.0</td>
<td style = "text-align: right;">1.0</td>
</tr>
</tbody>
</table>
<img src="/assets/img/models/NV_2020.png" style="width: 100%; display: block;">
~~~
Binary file added _assets/img/models/GA_2020.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _assets/img/models/MI_2020.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _assets/img/models/NC_2020.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _assets/img/models/NV_2020.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _assets/img/models/PA_2020.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _assets/img/models/WI_2020.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _assets/objs/GA_2020_p_sample.bson
Binary file not shown.
Binary file added _assets/objs/MI_2020_p_sample.bson
Binary file not shown.
Binary file added _assets/objs/NC_2020_p_sample.bson
Binary file not shown.
Binary file added _assets/objs/NV_2020_p_sample.bson
Binary file not shown.
Binary file added _assets/objs/PA_2020_p_sample.bson
Binary file not shown.
Binary file added _assets/objs/WI_2020_p_sample.bson
Binary file not shown.
59 changes: 37 additions & 22 deletions _assets/scripts/commons.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,36 +149,51 @@ end
"""
radix(df::DataFrame)
Format numerals in a DataFrame as strings with thousands separators.
Format integer columns in a DataFrame with thousands separators and return a new DataFrame
with the formatted columns appended.
# Arguments
- `df::DataFrame`: The DataFrame whose integer columns will be formatted.
- `df::DataFrame`: The input DataFrame containing the columns to be formatted.
# Description
The `radix` function iterates over each column in the provided DataFrame. If a column's element type is `Int64`, it formats the numerals in that column as strings with thousands separators (commas).
# Returns
- `DataFrame`: A new DataFrame with the original columns and the formatted columns appended.
The formatted columns have "_formatted" suffix added to their names.
# Example
```julia
using DataFrames
df = DataFrame(A = [1000, 2000, 3000], B = ["text1", "text2", "text3"], C = [4000, 5000, 6000])
radix(df)
println(df)
# Output:
# 3×3 DataFrame
# Row │ A B C
# │ String String String
# ─────┼──────────────────────────
# 1 │ 1,000 text1 4,000
# 2 │ 2,000 text2 5,000
# 3 │ 3,000 text3 6,000
julia> using DataFrames
julia> df = DataFrame(A = [1000, 2000, 3000], B = ["text1", "text2", "text3"], C = [4000, 5000, 6000])
3×3 DataFrame
Row │ A B C
│ Int64 String Int64
─────┼──────────────────────
1 │ 1000 text1 4000
2 │ 2000 text2 5000
3 │ 3000 text3 6000
julia> radix(df)
3×5 DataFrame
Row │ A B C A_formatted C_formatted
│ Int64 String Int64 String String
─────┼───────────────────────────────────────────────
1 │ 1000 text1 4000 "1,000" "4,000"
2 │ 2000 text2 5000 "2,000" "5,000"
3 │ 3000 text3 6000 "3,000" "6,000"
In the example above, the radix function is applied to a DataFrame df containing integer
columns. The function formats the integer columns with thousands separators and returns a new
DataFrame with the formatted columns appended. The formatted columns have "_formatted" suffix
added to their names.
"""
function radix(df::DataFrame)
for col in names(df)
if eltype(df[!, col]) <: Integer
df[:, col] = format.(df[:, col], commas=true)
end
end
formatted_df = copy(df)
for col in names(formatted_df)
if eltype(formatted_df[:, col]) <: Integer
formatted_col_name = Symbol(string(col) * "_formatted")
formatted_df[:, formatted_col_name] = map(x -> format(x, commas=true), formatted_df[:, col])
end
end
return formatted_df
end
#------------------------------------------------------------------
"""
Expand Down
13 changes: 9 additions & 4 deletions _assets/scripts/first_posterior.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
include("first_posterior_forepart.jl")

ST = last_election[1, "st"]
num_wins = last_election[1, "num_wins"]
num_votes = last_election[1, "num_votes"]
ST = last_election[7, "st"]
num_wins = last_election[7, "num_wins"]
num_votes = last_election[7, "num_votes"]

chain = sample(election_model(num_votes, num_wins), sampler, num_samples, init_params=init_params)

Expand All @@ -29,8 +29,13 @@ kde_result = kde(p_vec)

include("first_posterior_aftpart.jl")
# Display posterior density plot
draw_density()
posterior_interval = p_intv
fig = draw_density()
deep = deepcopy(chain)
@save ("../objs/$ST" * "_2020_p_sample.bson") deep
save(("../img/models/$ST" * "_2020.png"), fig)
out = Vector(p_df[1,:])
out = round.(out,digits = 4)
p_df[1,:] = out
pretty_table(p_df,backend=Val(:html))

2 changes: 1 addition & 1 deletion prior.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title = "The Bayesian Prior"

The model considered two starting points—one in which the 2020 election was assumed to have been a Biden win from 0% to 100% of the votes in each swing state as being equally likely. This is known as a naive, or uninformative, prior. It is unreasonable to assume that it was to be expected that one candidate would take all votes. The other proceeded from the pre-election observation that the race in the swing states were largely within survey margin of error. Therefore, it was reasonable to expect that the results were more likely to have clustered around a 0.5 proportion of votes for Biden.

Hypothetically, many contingencies could have affected voter turnout—voters who had intended to vote decided not to at the last minute due to conflicting committments. Other voters who had not intended to votes decided late to show up. This is one reason for adopting the 2020 results with some statistical noise as the starting point.
Hypothetically, many contingencies could have affected voter turnout—voters who had intended to vote but decided not to at the last minute due to conflicting committments. Other voters who had not intended to votes might have decided late to show up. This is one reason for adopting the 2020 results with some statistical noise as the starting point.

The main difference between using `Binomial(1, 1)` and `Binomial(2, 2)` as priors in a Bayesian analysis lies in the shape and concentration of the prior distribution. Let's discuss each case separately:

Expand Down

0 comments on commit 75a10cd

Please sign in to comment.