2024-03-05

Brexit polls shiny app

The data set used in the shiny app is taken from the dslabs package named brexit_polls which are surveys conducted by 16 pollsters in the United Kingdom, before brexit deal was signed to find out how many people in Britain, supported, Britain leaving Europeon Union.
The graph on shiny app webpage plots the number of people (as a fraction) that wanted Britain to leave EU against the sample size of the survey. The data was arranged based on the pollsters that conducted the most number of surveys. The top 3 pollsters were selected, for highlighting the data points on the plot.

Pollsters, the top three

library(dslabs); library(ggplot2)
data("brexit_polls")
suppressPackageStartupMessages(library(dplyr))

# The top 3 pollsters conducted maximum number of polls
brexit_polls %>% count(pollster) %>% arrange(desc(n)) %>% 
  top_n(3) %>% .$pollster
[1] ICM    YouGov ORB   
16 Levels: BMG Research ComRes Greenberg Quinlan Rosner Research ... YouGov/The Times

The Graph used in the shiny app

brexit_polls %>% ggplot(aes(samplesize, leave)) + geom_point() + 
  geom_smooth(method = "lm", se = FALSE)

Model used for the prediction

Model fit was used for the prediction, from the sample size input using the slider. The code for model and prediction are below.

fit <- lm(leave ~ samplesize, data = brexit_polls)
# For a sample size value of say 1650 the fraction that wanted brexit
# is given by
round(predict(fit, newdata = data.frame(samplesize = 1650)),4)
     1 
0.4217 

The sample size input from the slider would be converted to data frame as above and the fraction wanting brexit would be predicted using the predict function above and displayed under the graph in the webpage.