The purpose of this analysis is to forecast Two-Party Preferred (2PP) . This demonstration illustrates how historical election data can be modeled to predict future trends in primary votes and 2PP outcomes.
# Load required libraries
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.2.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(reshape2)
## Warning: package 'reshape2' was built under R version 4.2.3
library(forecast)
## Warning: package 'forecast' was built under R version 4.2.3
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
The analysis begins with a dataset containing historical voting outcomes, specifically:
Preference flows were applied to convert these primary votes into 2PP estimates for ALP and LNP, based on historical voter behavior patterns.
Using historical data on preference flows:
# Step 1: Historical Election Data (Simulated Example)
election_data <- data.frame(
Year = c(2006, 2010, 2014, 2018, 2022),
ALP = c(42, 44, 43, 45, 46), # ALP Primary Vote (%)
LNP = c(44, 43, 42, 41, 40), # LNP Primary Vote (%)
GRN = c(8, 9, 10, 11, 12), # Green Primary Vote (%)
OTH = c(6, 4, 5, 3, 2) # Other Parties (%)
)
# Preference flow (based on historical estimates)
green_prefs <- c(ALP = 0.8, LNP = 0.2) # Green preferences split
other_prefs <- c(ALP = 0.5, LNP = 0.5) # Other preferences split
# Calculate 2PP for historical data
election_data <- election_data %>%
mutate(
ALP_2PP = ALP + GRN * green_prefs["ALP"] + OTH * other_prefs["ALP"],
LNP_2PP = LNP + GRN * green_prefs["LNP"] + OTH * other_prefs["LNP"]
)
# Step 2: Forecast Primary Votes for 2026
future_year <- data.frame(Year = 2026)
# Fit linear models for each party
alp_forecast <- lm(ALP ~ Year, data = election_data)
lnp_forecast <- lm(LNP ~ Year, data = election_data)
grn_forecast <- lm(GRN ~ Year, data = election_data)
oth_forecast <- lm(OTH ~ Year, data = election_data)
# Predict primary votes for 2030
future_year$ALP <- predict(alp_forecast, newdata = future_year)
future_year$LNP <- predict(lnp_forecast, newdata = future_year)
future_year$GRN <- predict(grn_forecast, newdata = future_year)
future_year$OTH <- predict(oth_forecast, newdata = future_year)
# Step 3: Calculate 2PP for the Forecast Year
future_year <- future_year %>%
mutate(
ALP_2PP = ALP + GRN * green_prefs["ALP"] + OTH * other_prefs["ALP"],
LNP_2PP = LNP + GRN * green_prefs["LNP"] + OTH * other_prefs["LNP"]
)
# Combine historical and forecast data
forecast_data <- bind_rows(election_data, future_year)
# Step 4: Visualize the 2PP Forecast
# Melt data for ggplot
plot_data <- melt(
forecast_data,
id.vars = "Year",
measure.vars = c("ALP_2PP", "LNP_2PP"),
variable.name = "Party",
value.name = "TwoPP"
)
## Warning: attributes are not identical across measure variables; they will be
## dropped
# Plot the forecast
ggplot(plot_data, aes(x = Year, y = TwoPP, color = Party, group = Party)) +
geom_line(size = 1) +
geom_point(size = 2) +
scale_color_manual(values = c("ALP_2PP" = "red", "LNP_2PP" = "blue")) +
labs(
title = "Two-Party Preferred (2PP) Forecast",
x = "Year",
y = "2PP (%)",
color = "Party"
) +
theme_minimal()
## 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.
A line plot was created to visualize:
Key Insights from Visualization:
ALP shows a steady upward trend in 2PP over the years.
LNP exhibits a slight decline in 2PP, consistent with historical observations.
# Step 5: Print Summary for Report
cat("### Forecast Summary\n")
## ### Forecast Summary
cat("Predicted ALP 2PP for 2026:", round(future_year$ALP_2PP, 2), "%\n")
## Predicted ALP 2PP for 2026: 57.75 %
cat("Predicted LNP 2PP for 2026:", round(future_year$LNP_2PP, 2), "%\n")
## Predicted LNP 2PP for 2026: 42.25 %
The forecast predicts a 2PP (2026) of :