## Euromanifesto / Manifesto Project mapping scheme
## written by Nicolas Merz
## nicolas.merz@wzb.eu
## v 0.1

# Description

## script to map euromanifesto and manifesto project schemes
## the mapping scheme is very "conservative" and only maps codes that are very similar
## the euromanifesto scheme has many opposite (negative) categories for which the 
## manifesto project does not contain a category.
## most of these codes were recoded to "peruncod".
## This leads to much higher peruncod frequencies in the euromanifesto data.
## When doing eg. inferential statistics with both types of data in a pooled model,
## one should at least control for the different types of documents by adding a dummy variable 
## that indicates whether it is a euromanifesto or a "normal" one. 


## install these packages with the command 
# install.packages("manifestoR")
# install.packages("haven")
# install.packages("stringr")
# install.packages("readxl")

library(manifestoR) # is required as the script uses the aggregate_pers function to aggregate different variables
library(haven)
library(stringr)
library(readxl)


# download the euromanifesto dataset from GESIS http://dx.doi.org/10.4232/1.5162 (registration required)
ems <- read_dta("ZA5162_v1-0-0.dta")

# aggregate the euromanifesto variables from the different levels v1, v2, v3,... to one variable
variable_names <- ems %>% 
   select(contains("per_v1")) %>% 
   names() %>% 
   str_replace_all(pattern="per_v1_","") 

aggregation_list <- sapply(
   variable_names, function(x) { 
      ems %>%
         select(matches(x)) %>%
         names()
   }
)

names(aggregation_list) <- paste("per",variable_names,sep="")

ems_conv <- ems %>% aggregate_pers(groups=aggregation_list)


## load the mapping scheme

em_mapping <- read_excel("emp_mpds_mapping.xlsx") %>% 
   select(emp_2014,mp_hb4) %>%
   mutate(mp_hb4 = ifelse(mp_hb4 == 0,"per_v_099",mp_hb4))

v4_codes <- em_mapping %>% 
   distinct(mp_hb4) %>% 
   pull(mp_hb4) 
   
mapping  <- v4_codes %>%
   lapply(
   function(x) {
         em_mapping$emp_2014[x == em_mapping$mp_hb4] 
       }
) 
names(mapping) <- v4_codes
mapping

ems_remapped <- ems_conv %>% aggregate_pers(groups = mapping) %>% rename(peruncod=per_v_099)

