Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12,593 changes: 6,807 additions & 5,786 deletions comparison-data/ozpolls.csv

Large diffs are not rendered by default.

41 changes: 21 additions & 20 deletions model-2pp/model-2pp.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
#----------------set up----------------
source("setup.R")
rstan_options(auto_write = TRUE)
options(mc.cores = 7)
options(mc.cores = 4)

ozpolls <- ozpolls %>%
mutate(firm = ifelse(firm == "YouGov/Galaxy", "Galaxy", firm))


#--------------Data preparation-------------------
first_election <- as.Date("2007-11-24")
next_election <- as.Date("2019-05-18")
next_election <- as.Date("2022-05-21")
firms_today <- ozpolls %>%
filter(election_year == 2019) %>%
filter(election_year == 2022) %>%
distinct(firm) %>%
pull(firm)

# We don't have sample sizes except in the build up to 2016, so we'll just get
# the average sample size for firms from them and assume that's what they stick to:
sample_sizes <- ozpolls_2016 %>%
sample_sizes <- ozpolls_2022 %>%
mutate(firm = str_squish(gsub("\\(.+\\)", "", firm)),
firm = ifelse(firm == "Morgan", "Roy Morgan", firm),
firm = ifelse(firm == "Newspoll-YouGov", "Newspoll", firm),
firm = ifelse(firm == "ReachTel", "ReachTEL", firm)) %>%
filter(firm != "Election result") %>%
group_by(firm) %>%
Expand All @@ -32,7 +33,7 @@ sample_sizes <- sample_sizes %>%
mutate(ss = ifelse(is.na(ss), min(sample_sizes$ss, na.rm = TRUE), ss))

all_firms <- ozpolls %>%
filter(!firm %in% c("Election result") &
filter(!firm %in% c("Election result", "ANU", "Resolve Strategic") &
firm %in% firms_today) %>%
pull(firm) %>%
unique() %>%
Expand Down Expand Up @@ -71,7 +72,7 @@ one_pollster <- lapply(all_firms, function(x){

# Put all the data into a single list we can pass to Stan:
model_data <- list(
number_elections = 5,
number_elections = 6,
election_days = election_days,
election_results = election_results,

Expand All @@ -96,22 +97,22 @@ model_data <- list(
y4_values = one_pollster[[4]]$intended_vote,
y4_days = one_pollster[[4]]$n_days,
y4_n = nrow(one_pollster[[4]]),
y4_se = one_pollster[[4]]$se[1],
y4_se = one_pollster[[4]]$se[1]

y5_values = one_pollster[[5]]$intended_vote,
y5_days = one_pollster[[5]]$n_days,
y5_n = nrow(one_pollster[[5]]),
y5_se = one_pollster[[5]]$se[1],
# y5_values = one_pollster[[5]]$intended_vote,
# y5_days = one_pollster[[5]]$n_days,
# y5_n = nrow(one_pollster[[5]]),
# y5_se = one_pollster[[5]]$se[1],
#
# y6_values = one_pollster[[6]]$intended_vote,
# y6_days = one_pollster[[6]]$n_days,
# y6_n = nrow(one_pollster[[6]]),
# y6_se = one_pollster[[6]]$se[1]

y6_values = one_pollster[[6]]$intended_vote,
y6_days = one_pollster[[6]]$n_days,
y6_n = nrow(one_pollster[[6]]),
y6_se = one_pollster[[6]]$se[1],

y7_values = one_pollster[[7]]$intended_vote,
y7_days = one_pollster[[7]]$n_days,
y7_n = nrow(one_pollster[[7]]),
y7_se = one_pollster[[7]]$se[1]
# y7_values = one_pollster[[7]]$intended_vote,
# y7_days = one_pollster[[7]]$n_days,
# y7_n = nrow(one_pollster[[7]]),
# y7_se = one_pollster[[7]]$se[1]

)

Expand Down
35 changes: 17 additions & 18 deletions model-2pp/model-2pp.stan
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@ data {
int y4_days[y4_n];
real y4_se;

int y5_n;
real y5_values[y5_n];
int y5_days[y5_n];
real y5_se;

int y6_n;
real y6_values[y6_n];
int y6_days[y6_n];
real y6_se;
// int y5_n;
// real y5_values[y5_n];
// int y5_days[y5_n];
// real y5_se;
//
// int y6_n;
// real y6_values[y6_n];
// int y6_days[y6_n];
// real y6_se;

int y7_n;
real y7_values[y7_n];
int y7_days[y7_n];
real y7_se;
// int y7_n;
// real y7_values[y7_n];
// int y7_days[y7_n];
// real y7_se;

}

parameters {
vector[election_days[number_elections]] mu; //
real d[7]; // polling effects
real d[4]; // polling effects
real<lower=0> sigma; // sd of innovations
}

Expand All @@ -75,9 +75,8 @@ model {
y2_values ~ normal(mu[y2_days] + d[2], y2_se * inflator);
y3_values ~ normal(mu[y3_days] + d[3], y3_se * inflator);
y4_values ~ normal(mu[y4_days] + d[4], y4_se * inflator);
y5_values ~ normal(mu[y5_days] + d[5], y5_se * inflator);
y6_values ~ normal(mu[y6_days] + d[6], y6_se * inflator);
y7_values ~ normal(mu[y7_days] + d[7], y7_se * inflator);
// y5_values ~ normal(mu[y5_days] + d[5], y5_se * inflator);
// y6_values ~ normal(mu[y6_days] + d[6], y6_se * inflator);
// y7_values ~ normal(mu[y7_days] + d[7], y7_se * inflator);

}

33 changes: 14 additions & 19 deletions model-2pp/model-interpretation.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
load("output/model_2pp_2019051801014.rdata")
load("output/model_2pp.rdata")

the_caption <- ""

#-------------------------House effects-------------------

Expand Down Expand Up @@ -76,33 +78,26 @@ sd(election_sims)
# individual level swing statndard deviation is 3.2
# last election was 47.52 ALP

alp_v_coal <- oz_pendulum_2019[1:142, ] %>%
alp_v_coal <- oz_pendulum_2022%>%
filter(!division %in% c("Cooper", "Grayndler", "Indi", "Mayo", "Warringah", "Kennedy", "Melbourne", "Clark")) %>%
mutate(margin = ifelse(party_against == "ALP", -margin, margin),
pos_winner = "ALP",
alt_winner = "Lib/Nat",
alp_v_coal = 1) %>%
select(state, division, pos_winner, margin, alt_winner, alp_v_coal) %>%
rbind(tibble(
state = "VIC",
division = "Indi",
pos_winner = "ALP",
margin = -8.8,
alt_winner = "Lib/Nat",
alp_v_coal = 1
))

alp_v_other <- oz_pendulum_2019 %>%
filter(division %in% c("Wills", "Cooper", "Grayndler", "Clark")) %>%
select(state, division, pos_winner, margin, alt_winner, alp_v_coal)

alp_v_other <- oz_pendulum_2022 %>%
filter(division %in% c("Cooper", "Grayndler", "Clark")) %>%
mutate(pos_winner = "ALP",
margin = c(4.9, 1.3, 15.8, -17.8),
alt_winner = c("Grn", "Grn", "Grn", "Ind"),
margin = c(14.6, 16.3, 22.1),
alt_winner = c("Grn", "Grn", "Ind"),
alp_v_coal = 0) %>%
select(state, division, pos_winner, margin, alt_winner, alp_v_coal)

other_v_coal <- oz_pendulum_2019 %>%
filter(division %in% c("Wentworth", "Mayo", "Kennedy", "Melbourne")) %>%
other_v_coal <- oz_pendulum_2022 %>%
filter(division %in% c("Indi", "Mayo", "Warringah", "Kennedy", "Melbourne")) %>%
mutate(coal_margin = margin,
pos_winner = ifelse(incumbent %in% c("ALP", "KAP", "NXT"),
pos_winner = ifelse(incumbent %in% c("ALP", "KAP", "CA"),
incumbent,
str_to_sentence(incumbent)),
alt_winner = "Lib/Nat",
Expand Down
2 changes: 1 addition & 1 deletion pkg/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ Suggests:
License: GPL-3
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.1.1
RoxygenNote: 7.1.2
Binary file modified pkg/data/div_boundaries.rda
Binary file not shown.
Binary file modified pkg/data/div_census_2016.rda
Binary file not shown.
Binary file modified pkg/data/oz_party_cols.rda
Binary file not shown.
Binary file modified pkg/data/oz_pendulum_2019.rda
Binary file not shown.
Binary file added pkg/data/oz_pendulum_2022.rda
Binary file not shown.
Binary file modified pkg/data/ozpolls.rda
Binary file not shown.
Binary file modified pkg/data/ozpolls_2010.rda
Binary file not shown.
Binary file modified pkg/data/ozpolls_2016.rda
Binary file not shown.
Binary file modified pkg/data/results_2pp_div.rda
Binary file not shown.
4 changes: 3 additions & 1 deletion pkg/man/div_boundaries.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pkg/man/div_census_2016.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pkg/man/oz_party_cols.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pkg/man/oz_pendulum_2019.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions pkg/man/ozpol_infographic.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pkg/man/ozpolls.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pkg/man/ozpolls_2010.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pkg/man/ozpolls_2016.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pkg/man/results_2pp_div.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading