# Libraries -----------------
library(ggplot2)
library(cowplot)
library(RColorBrewer)
library(reshape2)
library(forcats)

library(knitr)
library(kableExtra)
library(rlang)
library(wesanderson)
library(haven)
library(margins)

library(broom)
library(stargazer)
library(scales)
library(dplyr)
library(tidyr)
library(xlsx)

detach(package:mfx)
detach(package:MASS)

#### 1 Preparing data ####

a<-readRDS("gi.rds")

#### 1a Independent variables ####
#age
table(a$age) 

a$age<-ifelse(a$age<0, NA, a$age)


#Age


a<-a %>% mutate(age=ifelse(age<=2, TRUE, FALSE))
table(a$country, a$age)



# gender, factor
a<-a %>% 
  mutate(female=case_when(.$gender==1 ~ 1, #female
                          .$gender==0 ~ 0, #male
                          .$gender==2 ~ 2, #prefer not to say & missings
                          .$gender==-99 ~ 2)) %>%
  mutate(female = factor(female))

# partner
table(a$partner, useNA = "always")

# Todo later in general file: na of natlang_partner in INDIA (4 cases)
a<-a %>% mutate(natlang_partner2=ifelse(is.na(natlang_partner1), natlang_partner3, natlang_partner2),
                natlang_partner3=ifelse(is.na(natlang_partner1), "-9", natlang_partner3),
                natlang_partner1=ifelse(is.na(natlang_partner1), "Hindu", natlang_partner1))

a<-a %>% mutate(natlang_partner3=ifelse(is.na(natlang_partner2), "-9", natlang_partner3),
                natlang_partner2=ifelse(is.na(natlang_partner2), "English", natlang_partner2))

a<-a %>% mutate(natlang_partner3=ifelse(is.na(natlang_partner3), "-9", natlang_partner3))


a<-a %>% 
  mutate(part= ifelse(partner<0, NA, ifelse(partner==2, 0, ifelse(natlang_partner1=="German" | natlang_partner2=="German" | natlang_partner3=="German", 1, 2))))       


with(a[a$natlang_partner1=="German",], table(a$natlang_partner2, a$natlang_partner3))



table(a$natlang_partner1, a$partner)
table(a$natlang_partner2, a$partner)
table(a$natlang_partner3, a$partner)

table(a$partner, a$part, useNA = "always")


# children
table(a$children)

a<-a %>% 
  mutate(children_d=case_when(.$children<0 ~ NA,
                              .$children==0 ~ FALSE,
                              .$children>0 ~ TRUE),
         children_d_na=case_when(.$children==-999 ~ NA,
                                 .$children==-99 ~ FALSE,
                                 .$children==0 ~ FALSE,
                                 .$children>0 ~ TRUE))

table(a$children, a$children_d_na, useNA = "always")


# risk/patience/applic_gen/comm_skills
table(a$risk, useNA="always")
table(a$patient, useNA="always")
table(a$applic_gen, useNA="always")
table(a$mig_phase, useNA="always")
table(a$mig_decision, useNA="always")

na <- function(x) (ifelse(x<0, NA, x))
a<-a %>% mutate_at(c("risk", "patient", "applic_gen",
                     "mig_decision"), na)

# degree
table(a$deg_participant)

a<-a %>% 
  mutate(degree=deg_participant) %>% 
  mutate(degree=replace(degree, .$deg_participant==5 & .$occupation_participant==0, 0), #pupils
         degree=replace(degree, .$deg_participant< 0 & .$occupation_participant==0, 0), #pupils
         degree=replace(degree, .$deg_participant==5 & .$occupation_participant==1, 2), #students
         degree=replace(degree, .$deg_participant< 0 & .$occupation_participant==1, 2)) %>% #students
  mutate(degree=replace(degree, .$degree==-999 | .$degree==-99, NA)) #Nas

  
table(a$degree, a$deg_participant, useNA = "always")


# labour market status
table(a$occupation_participant, a$empstat, useNA="always")

a<-a %>% 
  mutate(empstat=case_when(.$occupation_participant<0 ~ 3,
                           .$occupation_participant==12 ~ 3,
                           .$occupation_participant==11 ~ 3,
                           .$occupation_participant==0 ~ 1,
                           .$occupation_participant==1 ~ 1,
                           .$occupation_participant==2 ~ 1,
                           .$occupation_participant==3 ~ 1,
                           .$occupation_participant==4 ~ 2,
                           .$occupation_participant==5 ~ 2,
                           .$occupation_participant==6 ~ 2,
                           .$occupation_participant==7 ~ 2,
                           .$occupation_participant==8 ~ 3,
                           .$occupation_participant==9 ~ 3,
                           .$occupation_participant==10 ~3))


# English speaker (dummy)
a<-a %>% 
  mutate(english=case_when((lang_1name == "English" & lang_1level >= 4) ~ 1,
                           (lang_2name == "English" & lang_2level >= 4) ~ 1,
                           (lang_3name == "English" & lang_3level >= 4) ~ 1,
                           (lang_1name == "English" & lang_1level <  4) ~ 0,
                           (lang_2name == "English" & lang_2level <  4) ~ 0,
                           (lang_3name == "English" & lang_3level <  4) ~ 0,
                           (lang_1name != "English" & 
                            lang_2name != "English" & 
                            lang_3name != "English") ~ 0,
                           natlang_participant1 == "English" ~ 1,
                           natlang_participant2 == "English" ~ 1,
                           natlang_participant3 == "English" ~ 1,
                           natlang_participant4 == "English" ~ 1
))
a$english <- as.logical(a$english) 


# International applicability of skills
a<-a %>% 
  mutate(applic_gen_d=case_when(applic_gen <= 3 ~ 0, 
                                applic_gen >  3 ~ 1))
a$applic_gen_d <- as.logical(a$applic_gen_d)


a<-a %>%
  mutate(occupationac=case_when((empstat == 2 &
                                   comm_skills >=5 &
                                   applic_gen  >=4 ~ 2), #in occupation, high app, high comm.
                                (empstat == 2 &
                                   comm_skills < 5 & comm_skills >= 1 &
                                   applic_gen >=4 ~ 1),  #in occupation, high app, low comm.
                                (empstat == 2 &
                                   comm_skills >= 1 &
                                   applic_gen <4 ~ 0),  #in occupation, low app.
                                (empstat == 1 ~ 3),      #in education
                                (empstat == 3 ~ 4)       #other occ./n/a
  ))
a$occupationac <- as.factor(a$occupationac)


# (Main) Reason for migration
a <- a %>% 
  mutate(mainrea_educ = ifelse((mig_phase>=1 & mreason_mig1 == 0), 1, 
                         ifelse((mig_phase<0| mreason_mig1<0), 2, 0)),
         mainrea_labor = ifelse((mig_phase>=1 & 
                                   (mreason_mig1 >= 1 & mreason_mig1 <= 6)), 1,
                         ifelse((mig_phase < 0 | mreason_mig1 < 0), 2, 0)),
         mainrea_other = ifelse((mig_phase>=1 & 
                                   (mreason_mig1 >= 7 & mreason_mig1 <= 14)), 1,
                         ifelse((mig_phase < 0 | mreason_mig1 < 0), 2, 0)),
         mainrea_educlab = ifelse((mainrea_educ == TRUE | mainrea_labor == TRUE), 1, 
                           ifelse((mig_phase < 0 | mreason_mig1 < 0), 2, 0))
         )

# Add further independent variables here

indv<- a %>% select(country, institute, id_uni, age, female, part, children_d_na, risk, patient, 
                            degree, 
                            occupationac, english,
                            mainrea_educ, mainrea_labor, mainrea_other, mainrea_educlab,
                            mreason_mig1, 
                            mig_phase)


# Check for share of NAs
apply(indv[,4:ncol(indv)], 2, function(x) round(mean(is.na(x)), digits = 3))

table(indv$applic_gen, indv$country, useNA = "always")

table(indv$applic_gen, indv$country, useNA = "always")


indv<-indv %>% 
  mutate(compl=ifelse(complete.cases(indv), TRUE, FALSE))


table(indv$compl)


# Dataset of independent variables with only complete cases
indv_c<-indv[indv$compl==TRUE,]

#### 1b Dependent variables ####

##In IDN, there was additionally the item "Vocational Training". We merge that to education, study, training, phd.
table(a$mreason_lang)
a$mreason_lang[a$mreason_lang==-14]<- -99
a$mreason_lang[a$mreason_lang==17]<-0

table(a$reason_stud, a$reason_train)

a$reason_stud[a$reason_train==TRUE]<-TRUE

a<-select(a, -reason_train)

# For surveys in JPN, BIH, GBR and POL instead of having the two categories "(Possible) move to a German-speaking country for professional reasons" (mreason_)
# (mreason_lang=9, reason_move_prof=TRUE) and "(Possible) move to a German-speaking country for other reasons" (mreason_lang=10, reason_move_other=TRUE)
# we have "(Possible) move to a German-speaking country" (mreason_lang=16, reason_move=TRUE)
# Here we deal with that issue in order to split up the single response into the more fine-grained ones.

table(a$mreason_lang)

# Variable that indicates that observation was done in JPN, BIH, GBR and POL
a<-a %>% mutate(early=ifelse(country=="JPN" | country=="BIH" | country=="GBR" | country=="POL", TRUE, FALSE) )

# First, for those we have to decide whether their migration intention is for professional reasons or other reason (early==TRUE & mreason_lang==16) 
# we create a variable mig_reason_ger that indicates their main reason to migrate (mreason_mig1-3) to their highest preferred German-speaking 
# destination country (mig_1-3). 
#Then we recode mreason_lang in such a way, that those who indicate to migrate professional reasons (mig_reason_ger>=0 & mig_reason_ger<7)
# as mreason_lang=9,  and those who indicate to migrate non-professional reasons (mig_reason_ger>6 &  mig_reason_ger<16) as mreason_lang=10.
# Those who have no German speaking country among their first three preferred destination countries (mig_reason_ger=-9999) we code as mreason_lang==-999
#(and drop them from our observations (in analysis they are dropped anyway then.))
a <- a %>% 
  mutate(mig_mreason_ger= ifelse(early==TRUE & mreason_lang==16 & (mig_1=="DEU" | mig_1=="CHE" | mig_1=="AUT"), mreason_mig1, ifelse(
    early==TRUE & mreason_lang==16 & (mig_2=="DEU" | mig_2=="CHE" | mig_2=="AUT"), mreason_mig2, ifelse(
      early==TRUE & mreason_lang==16 & (mig_3=="DEU" | mig_3=="CHE" | mig_3=="AUT"), mreason_mig3, ifelse(  
        early==TRUE & mreason_lang==16, -9999, 9999)))),
    mreason_lang= ifelse(mig_mreason_ger>=0 & mig_mreason_ger<7,9, ifelse(
      mig_mreason_ger>6 &  mig_mreason_ger<16, 10, ifelse(
        mig_mreason_ger<0, -999, mreason_lang))),
    mig_reason_ger= ifelse(early==TRUE & reason_move==TRUE & (mig_1=="DEU" | mig_1=="CHE" | mig_1=="AUT"), mreason_mig1, ifelse(
      early==TRUE & reason_move==TRUE & (mig_2=="DEU" | mig_2=="CHE" | mig_2=="AUT"), mreason_mig2, ifelse(
        early==TRUE & reason_move==TRUE & (mig_3=="DEU" | mig_3=="CHE" | mig_3=="AUT"), mreason_mig3, ifelse(
          early==TRUE & reason_move==TRUE, -9999, 9999)))),
    reason_move_prof=ifelse(mig_reason_ger>=0 & mig_reason_ger<7,TRUE, ifelse(
      mig_reason_ger>6 &  mig_reason_ger<16, FALSE, ifelse(
        mig_reason_ger<0, NA, reason_move_prof))),
    reason_move_other=ifelse(mig_reason_ger>=0 & mig_reason_ger<7,FALSE, ifelse(
      mig_reason_ger>6 &  mig_reason_ger<16, TRUE, ifelse(
        mig_reason_ger<0, NA, reason_move_other)))         ) %>%
  filter(mig_mreason_ger>=0 & !(mig_reason_ger<0 & mreason_lang<0)) %>% ### second condition maximizes number of observations conditional on later types, Needs rethinking if analysing all reasons)
  select(-mig_mreason_ger, -mig_reason_ger)


#table(a$mig_reason_ger, a$reason_move_other)
table(a$mig_mreason_ger, a$mig_reason_ger)

table(a$reason_move, a$reason_move_other, useNA = "always")
table(a$mreason_lang, useNA="always")
table(a[a$reason_visa==TRUE,]$reason_move_other,a[a$reason_visa==TRUE,]$reason_move_prof)
## Similiarily we deal with "Requirement for visa"
a <- a %>%   mutate(mig_mreason_ger= ifelse(mreason_lang==11 & (mig_1=="DEU" | mig_1=="CHE" | mig_1=="AUT"), mreason_mig1, 
                                            ifelse(mreason_lang==11 & (mig_2=="DEU" | mig_2=="CHE" | mig_2=="AUT"), mreason_mig2,
                                                   ifelse(mreason_lang==11 & (mig_3=="DEU" | mig_3=="CHE" | mig_3=="AUT"), mreason_mig3, 
                                                          ifelse(mreason_lang==11, -9999, 9999)))),
                    mreason_lang=      ifelse(mig_mreason_ger>=0 & mig_mreason_ger<7,9, 
                                              ifelse(mig_mreason_ger>6 &  mig_mreason_ger<16, 10, 
                                                     ifelse(mig_mreason_ger<0, -999, mreason_lang))),
                    mig_reason_ger=      ifelse(reason_visa==TRUE & reason_move_other==FALSE & reason_move_prof==FALSE & (mig_1=="DEU" | mig_1=="CHE" | mig_1=="AUT"), mreason_mig1,
                                                ifelse(reason_visa==TRUE & reason_move_other==FALSE & reason_move_prof==FALSE & (mig_2=="DEU" | mig_2=="CHE" | mig_2=="AUT"), mreason_mig2,
                                                       ifelse(reason_visa==TRUE & reason_move_other==FALSE & reason_move_prof==FALSE & (mig_3=="DEU" | mig_3=="CHE" | mig_3=="AUT"), mreason_mig3,
                                                              ifelse(reason_visa==TRUE & reason_move_other==FALSE & reason_move_prof==FALSE , -9999, 9999)))),
                    reason_move_prof=    ifelse(mig_reason_ger>=0 & mig_reason_ger<7,TRUE, 
                                                ifelse(mig_reason_ger>6 &  mig_reason_ger<16, FALSE,
                                                       ifelse(mig_reason_ger<0, NA, reason_move_prof))),
                    reason_move_other=     ifelse(mig_reason_ger>=0 & mig_reason_ger<7,FALSE,
                                                  ifelse(mig_reason_ger>6 &  mig_reason_ger<16, TRUE,
                                                         ifelse(mig_reason_ger<0, NA, reason_move_other)))) %>%
  filter(mig_mreason_ger>=0 & !(mig_reason_ger<0 & mreason_lang<0)) %>% ### second condition maximizes number of observations conditional on later types, Needs rethinking if analysing all reasons)
  select(-mig_mreason_ger, -mig_reason_ger, -reason_visa)


#deal with mreason_lang<0
test<-a %>% select(country, mreason_lang) %>% 
  mutate(answered=ifelse(mreason_lang>=0, TRUE, FALSE)) %>% 
  group_by(country) %>% 
  mutate(mean_na=1-mean(answered)) %>% 
  ungroup() %>% 
  select(-mreason_lang, -answered) %>% 
  unique()



table(a$country, a$reason_oth)

### For now we drop some of "Other", i.e. main reason = Other or NA if reason= other
table(a$mreason_lang, useNA = "always")
table(a$reason_other, a$mreason_lang)

with(a[a$mreason_lang==15,], table(country))
a <- a %>% filter(mreason_lang!="15") 


#table(a$mreason_lang)
# Convert numeric to factor and change labels
a$mreason_lang  <- factor(a$mreason_lang, levels = c(-999, -99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14)) #15 deleted
a$mreason_lang  <- fct_recode(a$mreason_lang, 
                              "No answer"                                                         = "-999",
                              "No answer"                                                         = "-99" ,
                              "Education"                                                = "0"   ,
                              "Company communication"                                                  = "1"   ,
                              "Trading partners"                                               = "2"   ,
                              "Customers"                                                       = "3"   ,
                              "Income"                                                    = "4"   ,
                              "Employer"                                    = "5"   ,
                              "Labour market"                     = "6"   ,
                              "Family"                                                       = "7"   , 
                              "Friends"                                                      = "8"   ,
                              "Educational and labour migration"    = "9"   ,
                              "Other migration"        = "10"  ,
                              "Holiday"                                                                        = "12"  ,
                              "Culture"                                                                        = "13"  ,
                              "Interest"                                                         = "14" 
                              )
table(a$mreason_lang, a$reason_move, useNA = "always")


###create type consumption vs. investment ###

a<- a %>% mutate(type= case_when(.$mreason_lang=="Education" ~ TRUE, 
                                 .$mreason_lang=="Company communication" ~ TRUE,
                                 .$mreason_lang=="Trading partners" ~ TRUE,
                                 .$mreason_lang=="Customers" ~ TRUE,
                                 .$mreason_lang=="Income"~ TRUE,
                                 .$mreason_lang=="Employer"~ TRUE,
                                 .$mreason_lang== "Labour market"~ TRUE,
                                 .$mreason_lang=="Family" ~ FALSE,
                                 .$mreason_lang=="Friends" ~ FALSE,
                                 .$mreason_lang=="Educational and labour migration"~ TRUE,
                                 .$mreason_lang== "Other migration"~ FALSE,
                                 .$mreason_lang=="Holiday"~ FALSE,
                                 .$mreason_lang=="Culture"~ FALSE,
                                 .$mreason_lang=="Interest"~ FALSE,
                                 .$mreason_lang=="No answer"~ NA),
                 reason_cons=ifelse(reason_partner==TRUE | reason_soc==TRUE | reason_move_other==TRUE | reason_holi==TRUE | 
                                      reason_cult==TRUE | reason_inter==TRUE, TRUE, FALSE) ,  
                 reason_inv=ifelse(reason_stud==TRUE | reason_trade==TRUE | reason_comp==TRUE | reason_cust==TRUE | 
                                     reason_inc==TRUE | reason_requ==TRUE | reason_lab==TRUE | reason_move_prof==TRUE , TRUE, FALSE),
                 type=ifelse(is.na(type) & reason_cons==TRUE & reason_inv==FALSE, FALSE, type),
                 type=ifelse(is.na(type) & reason_cons==FALSE & reason_inv==TRUE, TRUE, type)  )



with(a[is.na(a$type),], table(reason_cons, reason_inv))

table(a$reason_cons, a$reason_inv)

a<-a %>% mutate(reason_om=ifelse(reason_cons==TRUE & reason_inv==TRUE, TRUE, FALSE))
table(a$reason_om)

table(a$type, a$mreason_lang, useNA="always")


### create more detailed type ###

table(a$type2, useNA = "always")
is.factor(a$type2)
a<- a %>% mutate(type2= case_when(.$mreason_lang=="Education" ~ "Education",
                                  .$mreason_lang=="Company communication" ~ "Domestic labour market",
                                  .$mreason_lang=="Trading partners" ~ "Domestic labour market",
                                  .$mreason_lang=="Customers" ~ "Domestic labour market",
                                  .$mreason_lang=="Income"~ "Domestic labour market",
                                  .$mreason_lang=="Employer"~ "Domestic labour market",
                                  .$mreason_lang=="Labour market"~ "Domestic labour market",
                                  .$mreason_lang=="Family" ~ "Personal reasons",
                                  .$mreason_lang=="Friends" ~ "Personal reasons",
                                  .$mreason_lang=="Educational and labour migration"~ "Educational and labour migration",
                                  .$mreason_lang=="Other migration"~ "Personal reasons",
                                  .$mreason_lang=="Holiday"~ "Cultural interest",
                                  .$mreason_lang=="Culture"~ "Cultural interest",
                                  .$mreason_lang=="Interest"~ "Cultural interest",
                                  .$mreason_lang=="No answer"~ "No answer"),
                 type2=ifelse(type2=="No answer", NA, type2),
                 reason_culture   =ifelse(reason_holi==TRUE | reason_cult==TRUE | reason_inter==TRUE, TRUE, FALSE) , 
                 reason_education =ifelse(reason_stud==TRUE , TRUE, FALSE),
                 reason_domestic  =ifelse(reason_trade==TRUE | reason_comp==TRUE | reason_cust==TRUE | reason_inc==TRUE |
                                            reason_requ==TRUE | reason_lab==TRUE , TRUE, FALSE),
                 reason_foreign   =ifelse(reason_move_prof==TRUE , TRUE, FALSE),
                 reason_social    =ifelse(reason_partner==TRUE | reason_soc==TRUE | reason_move_other==TRUE , TRUE, FALSE) ,
                 type2=ifelse(is.na(type2) & reason_culture==TRUE   & reason_education==FALSE & reason_domestic==FALSE & reason_foreign==FALSE & reason_social==FALSE , "Cultural interest", type2),
                 type2=ifelse(is.na(type2) & reason_education==TRUE & reason_culture==FALSE & reason_domestic==FALSE & reason_foreign==FALSE & reason_social==FALSE, "Education", type2),
                 type2=ifelse(is.na(type2) & reason_domestic==TRUE  & reason_education==FALSE & reason_culture==FALSE & reason_foreign==FALSE & reason_social==FALSE, "Domestic labour market", type2),
                 type2=ifelse(is.na(type2) & reason_foreign==TRUE   & reason_education==FALSE & reason_domestic==FALSE & reason_culture==FALSE & reason_social==FALSE, "Educational and labour migration", type2),
                 type2=ifelse(is.na(type2) & reason_social==TRUE    & reason_education==FALSE & reason_domestic==FALSE & reason_foreign==FALSE & reason_culture==FALSE, "Personal reasons", type2))


#change order of factors
a$type2 <- factor(a$type2,levels=c( "Education", "Domestic labour market", "Educational and labour migration",  "Personal reasons", "Cultural interest", "No answer"))
levels(a$type2)
table(a$type2, a$type, useNA = "always")

table(a$type2, a$mreason_lang, useNA = "always")



##Analyse missings
dv<-select(a, id_uni, type, type2, langjob, reason_culture, reason_education, reason_domestic, reason_foreign, reason_social, reason_cons, reason_inv)

table(dv$langjob, useNA="always")


apply(dv, 2, function(x) mean(is.na(x)))
apply(dv, 2, function(x) sum(is.na(x)))

table(a$reason_cons, a$reason_inv)

# Other reasons
ab<-select(a, starts_with("reason"))
apply(ab, 2, function(x) mean(is.na(x)))



## Dataset of DV with NAs
dv<-select(a, id_uni, type, type2, langjob, mreason_lang, starts_with("reason"))
dv$langjob<-ifelse(dv$langjob < 0, NA, dv$langjob)

dv<-dv %>% 
  mutate(compl=ifelse(complete.cases(dv), TRUE, FALSE))

apply(dv, 2, function(x) mean(is.na(x)))



table(dv$compl)
mean(dv$compl)

dv<-select(dv, -compl) #before remove uncomplete cases, we join ind. var. 

table(dv$langjob, useNA="always")

#### Country variables ####
table(indv$country)

indv_c<-indv_c %>% mutate(ling=case_when(.$country=="BIH" ~ 0,
                                 .$country=="CZE" ~ 0,
                                 .$country=="ESP" ~ 0,
                                 .$country=="GBR" ~ 1,
                                 .$country=="IDN"~ 0,
                                 .$country=="IND"~ 1,
                                 .$country=="ITA"~ 0,
                                 .$country=="JPN" ~ 0,
                                 .$country=="KOR" ~ 0,
                                 .$country=="MEX"~ 0,
                                 .$country=="NLD"~ 1,
                                 .$country=="POL"~ 0,
                                 .$country=="ROU"~0,
                                 .$country=="UKR"~ 0),
                europe=case_when(.$country=="BIH" ~ 1,
                               .$country=="CZE" ~ 0,
                               .$country=="ESP" ~ 0,
                               .$country=="GBR" ~ 0,
                               .$country=="IDN"~ 2,
                               .$country=="IND"~ 2,
                               .$country=="ITA"~ 0,
                               .$country=="JPN" ~ 2,
                               .$country=="KOR" ~ 2,
                               .$country=="MEX"~ 2,
                               .$country=="NLD"~ 0,
                               .$country=="POL"~ 0,
                               .$country=="ROU"~0,
                               .$country=="UKR"~ 1),
                income=case_when(.$country=="BIH" ~ 1,
                                 .$country=="CZE" ~ 2,
                                 .$country=="ESP" ~ 2,
                                 .$country=="GBR" ~ 2,
                                 .$country=="IDN"~ 0,
                                 .$country=="IND"~ 0,
                                 .$country=="ITA"~ 2,
                                 .$country=="JPN" ~ 2,
                                 .$country=="KOR" ~ 2,
                                 .$country=="MEX"~ 1,
                                 .$country=="NLD"~ 2,
                                 .$country=="POL"~ 2,
                                 .$country=="ROU"~1,
                                 .$country=="UKR"~ 0),
                cultclose=case_when(.$country=="BIH" ~ 1,
                               .$country=="CZE" ~ 1,
                               .$country=="ESP" ~ 0,
                               .$country=="GBR" ~ 0,
                               .$country=="IDN"~ 1,
                               .$country=="IND"~ 0,
                               .$country=="ITA"~ 1,
                               .$country=="JPN" ~ 1,
                               .$country=="KOR" ~ 1,
                               .$country=="MEX"~ 0,
                               .$country=="NLD"~ 0,
                               .$country=="POL"~ 0,
                               .$country=="ROU"~0,
                               .$country=="UKR"~ 1))


### Cultural distance
library(reshape2)
library(readxl)
d <- read_excel('cultdist.xlsx')

# drop some columns
d <- d[, c(2,9:10)]

# Distance to Germany
d$ltodist <- abs(d$ltowvs-82.871537)
d$ivrdist <- abs(d$ivr-40.401786)

d %>% rename(country = country.iso3) -> d

indv_c <- left_join(indv_c, d)
rm(d)

# Normalize between 0 and 1
indv_c$ltodist <- (indv_c$ltodist/max(indv_c$ltodist, na.rm = TRUE)) 
indv_c$ivrdist <- (indv_c$ivrdist/max(indv_c$ivrdist, na.rm = TRUE)) 


#### 1c Independent and dependent variables ####
## Ind. only complete cases, dep. v with missings
est<-left_join(indv_c, dv)
saveRDS(indv, "indv.RDS")
apply(est, 2, function(x) mean(is.na(x)))


##Here we only filter out cases that have neither type, type2 nor langjob, not on entirely complete cases
est<-est %>% 
  mutate(complete=ifelse(complete.cases(est), TRUE, FALSE),
         one_m= ifelse( !is.na(type) | !is.na(type2) | !is.na(langjob), TRUE, FALSE))%>% 
  filter(one_m==TRUE) %>% 
  select(-compl)

table(est$one_m, est$complete)


table(est$country, est$complete) 
apply(est[est$country=="KOR",], 2, function(x) mean(is.na(x)))
saveRDS(est, "est.RDS")

table(est$type2, est$type, useNA = "always")
table(est$complete)
