With the upcoming election in Germany, uncertainty about election results is high. This small project aims to make use of facebook's latest package, prophet ( https://facebookincubator.github.io/prophet/ )
First, we need to collect data. Therefore, we jump to Wahlrecht.de (www.wahlrecht.de) and scrape the historic survey results from different survey institutes listed there.
require(prophet)
require(rvest)
require(stringr)
url <- "http://www.wahlrecht.de/umfragen/politbarometer.htm"
mv <- read_html(url)
tab <- html_table(mv, header = NA, trim = TRUE, fill = TRUE)
tab <- tab[[2]]
tab <- tab[4:nrow(tab),]
colnames(tab) <- c("date", "leer", "CDUCSU", "SPD", "Gruene", "FDP","Linke" ,"AfD", "Rest","leer2", "N", "Frame")
#transform the relevant variable:
tab$CDUCSU <- str_split_fixed(tab$CDUCSU, " ", 2)[,1]
tab$SPD <- str_split_fixed(tab$SPD, " ", 2)[,1]
tab$AfD <- str_split_fixed(tab$AfD, " ", 2)[,1]
require(lubridate)
df <- data.frame(tab$date, tab$AfD)
colnames(df) <- c("ds", "y")
df$ds <- dmy(as.character(df$ds))
df$y <- as.numeric(as.character(df$y))
m <- prophet(df)
future <- make_future_dataframe(m, periods = 10, freq = "month")
forecast <- predict(m, future)
plot(m, forecast)
prophet_plot_components(m, forecast)
As we can see, the AfD is projected to score around 12-15% in the upcoming elections...
Next Steps:
- average over all the available survey providers
- increase the length of the period for parties, where possible