library(aqm)
## 
## Attaching package: 'aqm'
## The following object is masked from 'package:stats':
## 
##     dt
library(mapview)
library(tidyr)
library(dplyr)
## 
## 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
data(ge2017)
data(ge2015)


ge2015 %>% filter(party=="UKIP") ->ukip
ukip[,c(1,6)] -> ukip
names(ukip)<-c("CODE","UKIP_2015")
data(carto)
names(ge2017)[1]<-"CODE"
ge2017<-merge(ge2017,ukip)
ge2017$ukip_percent<-round(100*ge2017$UKIP_2015/ge2017$total,1)
carto<-merge(carto,ge2017)
all_parties<-c('blue','darkblue','green','grey','red','orange','darkgreen','black', 'darkorange')

carto %>% filter(rank==1) %>% select(c(3:9,11))%>%
  mapview(zcol="party", col.regions=all_parties)
library(sf)
## Linking to GEOS 3.5.1, GDAL 2.1.2, PROJ 4.9.3
carto %>% filter(rank==1) %>% select(c(3:9,11))%>%st_set_geometry(NULL) %>% dt()
exclude<-levels(carto$Region_Nam)[6:7]
carto %>% filter(! Region_Nam %in% exclude) -> carto
carto %>% st_set_geometry(NULL) %>% filter(rank==1) %>% group_by(party) %>% summarise(n=n())
## # A tibble: 5 x 2
##   party      n
##   <chr>  <int>
## 1 Con      305
## 2 Green      1
## 3 Labour   257
## 4 LibDem     8
## 5 Plaid      4
carto %>% st_set_geometry(NULL) %>% group_by(party) %>% summarise(tot=sum(votes)) %>% mutate(percent = round(100*tot / sum(tot),1))
## # A tibble: 9 x 3
##   party       tot percent
##   <chr>     <dbl>   <dbl>
## 1 BNP        4323     0  
## 2 Con    12947174    44.9
## 3 Green    513594     1.8
## 4 Indep    131465     0.5
## 5 Labour 12224303    42.4
## 6 LibDem  2198555     7.6
## 7 Other     74487     0.3
## 8 Plaid    164466     0.6
## 9 UKIP     591221     2
carto %>% st_set_geometry(NULL)%>% pivot_wider(id_cols= CODE,names_from = party,values_from = votes,values_fill = list(votes=0), values_fn =list(votes=sum)) -> wide

mat<-as.matrix(wide[,-1])
change<-diag(9)
change[4,]<-c(0,0.3,0,0.6,0.1,0,0,0,0)
change[5,]<-c(0,0.2,0.1,0.1,0.6,0,0,0,0)
mat2<-mat %*% change 
round(sum(apply(mat,1,sum) -apply(mat2,1,sum)),4)
## [1] 0
wide[,-1]<-mat2
wide %>% pivot_longer(cols=-CODE,names_to="party",values_to = "votes") %>% filter(votes>0) %>% group_by(CODE) %>% mutate(total=sum(votes)) %>%
  mutate(percent=round(100*votes/total,1)) %>% mutate(rank= rank(-votes,ties="first")) %>% mutate(margin=votes[rank==1]-votes[rank==2])-> sim
sim %>% filter(rank==1) %>% group_by(party) %>% summarise(n=n())
## # A tibble: 6 x 2
##   party      n
##   <chr>  <int>
## 1 Con      264
## 2 Green      1
## 3 Indep      1
## 4 Labour   246
## 5 LibDem    58
## 6 Plaid      3
sim %>% group_by(party) %>% summarise(tot=sum(votes)) %>% mutate(percent = round(100*tot / sum(tot),1))
## # A tibble: 9 x 3
##   party       tot percent
##   <chr>     <dbl>   <dbl>
## 1 BNP       4323      0  
## 2 Con    8990735.    31.2
## 3 Green   513594      1.8
## 4 Indep   131465      0.5
## 5 Labour 8629299.    29.9
## 6 LibDem 8455281.    29.3
## 7 Other    74487      0.3
## 8 Plaid   164466      0.6
## 9 UKIP   1885938.     6.5