### 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)


detach(package:mfx)
detach(package:MASS)

# Read data --------------------------------
a<-readRDS("est.RDS")

#### first descriptive based on reasons ####
table((a$mreason_lang), useNA = "always")
a<-a %>% mutate(mreas_na=ifelse(a$mreason_lang=="No answer", TRUE, FALSE))
table(a$country, a$mreas_na)

a <- a %>% filter(!is.na(reason_move_prof) | !is.na(reason_move_other))

a %>% group_by(country) %>%  summarise(mean_na=mean(mreas_na))

a<-a %>% mutate(no_reas=reason_stud+reason_comp+reason_trade+reason_cust+reason_inc+ reason_requ+reason_lab+ reason_partner+reason_soc+reason_move_prof+reason_move_other+reason_holi+reason_cult+reason_inter)


d<-a %>% group_by(country,mreas_na) %>%  summarise(mean_na=mean(no_reas)) %>% ungroup

a<-a %>% select(-reason_stud,-reason_comp,-reason_trade,-reason_cust,-reason_inc,- reason_requ,-reason_lab,- reason_partner,-reason_soc,-reason_move_prof,-reason_move_other,-reason_holi,-reason_cult,-reason_inter)

#### Prepare for estimations #####
est<-a
#1. type logit (max and min sample x stepwise introduction X aggregate)
#2. type logit (max and min sample x stepwise introduction X country_wise)
#1. langjob logit (max and min sample x stepwise introduction X aggregate)
#2. langjob logit (max and min sample x stepwise introduction X country_wise)


#2. part (ref cat part=0)
est <- within(est, part <- relevel(factor(part), ref = "0"))
# part=0 no partner
# part=1 partner with German native language
# part=2 partner with other native language


#3. Degree (ref cat part=2)
est <- within(est, degree <- relevel(factor(degree), ref = "2"))

# 0	No degree	
# 1	School leaving certificate which cannot lead to higher education	
# 2	School leaving certificate which can lead to higher education	
# 3	University degree 
# 4	PhD


stargazer(est, type="text")



#Aggregate 0+1 and 3+4
est<-est %>% mutate(degree2=case_when(is.na(.$degree) ~ NA,
                                      .$degree =="0" ~ FALSE,
                                      .$degree =="1" ~ FALSE,
                                      .$degree =="2" ~ FALSE,
                                      .$degree =="3" ~ TRUE,
                                      .$degree =="4" ~ TRUE,
                                      .$degree =="5" ~ FALSE),
                    degree=degree2)

table(est$degree, est$degree2, useNA = "always")


table(est$mreas_na)
gi<-est


#### estimations ####
library(MASS) #MASS:: select clashes with dplyr::select, load late
library(mfx)
library(lmtest)
library(sandwich)

mreas_na_full_fe<-as.formula("mreas_na ~ no_reas+factor(age)+female+children_d_na+part+occupationac+degree+risk+patient+english+country")
mreas_na_full<-as.formula("mreas_na ~ no_reas+factor(age)+female+children_d_na+part+occupationac+degree+risk+patient+english")



TOT<-probitmfx(formula =  mreas_na_full_fe, data=gi, atmean = FALSE, robust = TRUE, clustervar1 = NULL, clustervar2 = NULL, 
               start = NULL, control = list())
BIH<-probitmfx(formula =  mreas_na_full, data=gi[gi$country=="BIH",], atmean = FALSE, robust = TRUE, clustervar1 = NULL, clustervar2 = NULL, 
               start = NULL, control = list())
CZE<-probitmfx(formula =  mreas_na_full, data=gi[gi$country=="CZE",], atmean = FALSE, robust = TRUE, clustervar1 = NULL, clustervar2 = NULL, 
               start = NULL, control = list())
ESP<-probitmfx(formula =  mreas_na_full, data=gi[gi$country=="ESP",], atmean = FALSE, robust = TRUE, clustervar1 = NULL, clustervar2 = NULL, 
               start = NULL, control = list())
GBR<-probitmfx(formula =  mreas_na_full, data=gi[gi$country=="GBR",], atmean = FALSE, robust = TRUE, clustervar1 = NULL, clustervar2 = NULL, 
               start = NULL, control = list())
ITA<-probitmfx(formula =  mreas_na_full, data=gi[gi$country=="ITA",], atmean = FALSE, robust = TRUE, clustervar1 = NULL, clustervar2 = NULL, 
               start = NULL, control = list())
JPN<-probitmfx(formula =  mreas_na_full, data=gi[gi$country=="JPN",], atmean = FALSE, robust = TRUE, clustervar1 = NULL, clustervar2 = NULL, 
               start = NULL, control = list())
KOR<-probitmfx(formula =  mreas_na_full, data=gi[gi$country=="KOR",], atmean = FALSE, robust = TRUE, clustervar1 = NULL, clustervar2 = NULL, 
               start = NULL, control = list())
NLD<-probitmfx(formula =  mreas_na_full, data=gi[gi$country=="NLD",], atmean = FALSE, robust = TRUE, clustervar1 = NULL, clustervar2 = NULL, 
               start = NULL, control = list())
POL<-probitmfx(formula =  mreas_na_full, data=gi[gi$country=="POL",], atmean = FALSE, robust = TRUE, clustervar1 = NULL, clustervar2 = NULL, 
               start = NULL, control = list())
ROU<-probitmfx(formula =  mreas_na_full, data=gi[gi$country=="ROU",], atmean = FALSE, robust = TRUE, clustervar1 = NULL, clustervar2 = NULL, 
               start = NULL, control = list())
IND<-probitmfx(formula =  mreas_na_full, data=gi[gi$country=="IND",], atmean = FALSE, robust = TRUE, clustervar1 = NULL, clustervar2 = NULL, 
               start = NULL, control = list())
IDN<-probitmfx(formula =  mreas_na_full, data=gi[gi$country=="IDN",], atmean = FALSE, robust = TRUE, clustervar1 = NULL, clustervar2 = NULL, 
               start = NULL, control = list())
UKR<-probitmfx(formula =  mreas_na_full, data=gi[gi$country=="UKR",], atmean = FALSE, robust = TRUE, clustervar1 = NULL, clustervar2 = NULL, 
               start = NULL, control = list())
MEX<-probitmfx(formula =  mreas_na_full, data=gi[gi$country=="MEX",], atmean = FALSE, robust = TRUE, clustervar1 = NULL, clustervar2 = NULL, 
               start = NULL, control = list())




stargazer(TOT$fit, 
          BIH$fit,
          CZE$fit, 
          ESP$fit,
          GBR$fit, 
          ITA$fit,
          JPN$fit, 
          KOR$fit,
          NLD$fit, 
          POL$fit,
          ROU$fit, 
          IND$fit,
          IDN$fit, 
          UKR$fit,
          MEX$fit, 
          coef = list(TOT$mfxest[,1], 
                      BIH$mfxest[,1],
                      CZE$mfxest[,1], 
                      ESP$mfxest[,1],
                      GBR$mfxest[,1], 
                      ITA$mfxest[,1],
                      JPN$mfxest[,1], 
                      KOR$mfxest[,1],
                      NLD$mfxest[,1], 
                      POL$mfxest[,1],
                      ROU$mfxest[,1], 
                      IND$mfxest[,1],
                      IDN$mfxest[,1], 
                      UKR$mfxest[,1],
                      MEX$mfxest[,1]),
          se = list(TOT$mfxest[,2],
                    BIH$mfxest[,2],
                    CZE$mfxest[,2],
                    ESP$mfxest[,2],
                    GBR$mfxest[,2],
                    ITA$mfxest[,2],
                    JPN$mfxest[,2],
                    KOR$mfxest[,2],
                    NLD$mfxest[,2],
                    POL$mfxest[,2],
                    ROU$mfxest[,2],
                    IND$mfxest[,2],
                    IDN$mfxest[,2],
                    UKR$mfxest[,2],
                    MEX$mfxest[,2]),
          column.labels = c("TOT", "BIH", "CZE", "ESP",  "GBR", "ITA", "JPN", "KOR", "NLD", "POL", "ROU", "IND", "IDN", "UKR", "MEX"),
          header=FALSE,
          style="default",
          dep.var.labels   = "main reason NA",
          covariate.labels= c("Number of reasons",
                              "Age: under 35 years",
                              "Gender: female",
                              "Gender: n/a",
                              "Children",
                              "Partner (native German)",
                              "Partner (other native)",
                              "Occ.: high appl./low comm. skills",
                              "Occ.: high appl./high comm. skills",
                              "Occ.: in education",
                              "Occ.: other occ./no answer",
                              "University degree",
                              "Risk attitude",
                              "Patience",
                              "English speaker"),
          nobs=TRUE,
          align=TRUE, 
          no.space=TRUE, 
          single.row=TRUE,
          model.names = FALSE,
          omit = c("Constant", "country"),
          font.size = "small",
          add.lines = list(c("Fixed effects?", "Yes", "No", "No","No","No","No","No","No","No","No","No","No","No","No","No")),
          notes= "Average marginal effects. Heteroscedasticity robust White standard errors in parentheses.",
          omit.stat = c("aic", "ll"),
          out = "tables/mrea_na_country_ame.txt")


TOT_type<-tidy(TOT)  %>% filter(term != "(Intercept)") %>% mutate(model = "Total")
BIH_type<-tidy(BIH)  %>% filter(term != "(Intercept)") %>% mutate(model = "BIH (n=158)")
CZE_type<-tidy(CZE)  %>% filter(term != "(Intercept)") %>% mutate(model = "CZE (n=417)")
ESP_type<-tidy(ESP)  %>% filter(term != "(Intercept)") %>% mutate(model = "ESP (n=567)")
GBR_type<-tidy(GBR)  %>% filter(term != "(Intercept)") %>% mutate(model = "GBR (n=399)")
ITA_type<-tidy(ITA)  %>% filter(term != "(Intercept)") %>% mutate(model = "ITA (n=298)")
JPN_type<-tidy(JPN)  %>% filter(term != "(Intercept)") %>% mutate(model = "JPN (n=252)")
KOR_type<-tidy(KOR)  %>% filter(term != "(Intercept)") %>% mutate(model = "KOR (n=456)")
NLD_type<-tidy(NLD)  %>% filter(term != "(Intercept)") %>% mutate(model = "NLD (n=121)")
POL_type<-tidy(POL)  %>% filter(term != "(Intercept)") %>% mutate(model = "POL (n=182)")
ROU_type<-tidy(ROU)  %>% filter(term != "(Intercept)") %>% mutate(model = "ROU (n=296)")
IND_type<-tidy(IND)  %>% filter(term != "(Intercept)") %>% mutate(model = "IND (n=762)")
IDN_type<-tidy(IDN)  %>% filter(term != "(Intercept)") %>% mutate(model = "IDN (n=819)")
UKR_type<-tidy(UKR)  %>% filter(term != "(Intercept)") %>% mutate(model = "UKR (n=726)")
MEX_type<-tidy(MEX)  %>% filter(term != "(Intercept)") %>% mutate(model = "MEX (n=451)")

gi %>% count(country) %>% arrange(-n)

country_l_type<-rbind(TOT_type, 
                      ESP_type, GBR_type, CZE_type, ITA_type, POL_type, ROU_type, NLD_type,
                      BIH_type,  UKR_type, 
                      MEX_type, IND_type, IDN_type,
                      KOR_type, JPN_type)

country_l_type<-country_l_type %>% mutate(sign=ifelse(model=="Total" & p.value<0.05, TRUE, FALSE)) %>% 
  group_by(term) %>% mutate(sign_5=max(sign), sign=ifelse(sign_5==0, "P>0.05 (aggr. model)", "P<0.05 (aggr. model)"), sign=as.factor(sign)) %>% filter(model!="Total")


library(dotwhisker)
library(ggsci)

p_country1<-dwplot(subset(country_l_type, 
                         (country_l_type$term=="no_reas" |
                          country_l_type$term=="typeTRUE" |
                          country_l_type$term=="factor(age)TRUE" |
                          country_l_type$term=="female1" | 
                          country_l_type$term=="children_d_naTRUE" | 
                          country_l_type$term=="part1" | 
                          country_l_type$term=="part2" | 
                          country_l_type$term=="occupationac1" | 
                          country_l_type$term=="occupationac2" | 
                          country_l_type$term=="occupationac3" | 
                          country_l_type$term=="degreeTRUE" |
                          country_l_type$term=="risk" | 
                          country_l_type$term=="patient" |
                          country_l_type$term=="englishTRUE"
                               )),
                   vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2), dodge_size= 0.7,
                   dot_args = list(aes(shape =as.factor(sign)), size=4)) +
  scale_x_continuous(limits = c(-0.7, 0.7))+
  scale_y_discrete(   labels=c( no_reas= "Number of reasons",
                                "factor(age)TRUE" = "Age: under 35 years",
                                female1 = "Gender: female",                       
                                children_d_naTRUE = "Children", 
                                "part1" = "Partner (native German)",                       
                                "part2" = "Partner (other native)",
                                degreeTRUE= "University degree",
                                "occupationac1" = "Occ.: high appl./low comm. skills",
                                "occupationac2" = "Occ.: high appl./high comm. skills",
                                "occupationac3" = "Occ.: in education",
                                risk = "Risk attitude", 
                                patient = "Patience",
                                "englishTRUE" = "English speaker"
                                ))+
  xlab("AME")+
  theme(legend.title = element_blank())

pdf("mreas_na_country1.pdf", paper="a4r", width = 12, height = 14) 
p_country1
dev.off()



##### many NAs (NLD, KOR, IND, MEX) ####
table(gi$mreas_na, gi$country, useNA = "always")

country_l_type<-rbind(TOT_type, 
                      NLD_type,
                      MEX_type, IND_type, KOR_type)

country_l_type<-country_l_type %>% mutate(sign=ifelse(model=="Total" & p.value<0.05, TRUE, FALSE)) %>% 
  group_by(term) %>% mutate(sign_5=max(sign), sign=ifelse(sign_5==0, "P>0.05 (aggr. model)", "P<0.05 (aggr. model)"), sign=as.factor(sign)) %>% filter(model!="Total")



p_country1<-dwplot(subset(country_l_type, 
                          (country_l_type$term=="no_reas" |
                             country_l_type$term=="typeTRUE" |
                             country_l_type$term=="factor(age)TRUE" |
                             country_l_type$term=="female1" | 
                             country_l_type$term=="children_d_naTRUE" | 
                             country_l_type$term=="part1" | 
                             country_l_type$term=="part2" | 
                             country_l_type$term=="occupationac1" | 
                             country_l_type$term=="occupationac2" | 
                             country_l_type$term=="occupationac3" | 
                             country_l_type$term=="degreeTRUE" |
                             country_l_type$term=="risk" | 
                             country_l_type$term=="patient" |
                             country_l_type$term=="englishTRUE"
                          )),
                           vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2), dodge_size= 0.7,
                   dot_args = list(aes(shape =as.factor(sign)), size=4)) +
  scale_x_continuous(limits = c(-0.7, 0.7))+
  scale_y_discrete(   labels=c( no_reas= "Number of reasons",
                                "factor(age)TRUE" = "Age: under 35 years",
                                female1 = "Gender: female",                       
                                children_d_naTRUE = "Children", 
                                "part1" = "Partner (native German)",                       
                                "part2" = "Partner (other native)",
                                degreeTRUE= "University degree",
                                "occupationac1" = "Occ.: high appl./low comm. skills",
                                "occupationac2" = "Occ.: high appl./high comm. skills",
                                "occupationac3" = "Occ.: in education",
                                risk = "Risk attitude", 
                                patient = "Patience",
                                "englishTRUE" = "English speaker"
  ))+
  xlab("AME")+
  theme(legend.title = element_blank())

pdf("mreas_na_country_many1.pdf", paper="a4r", width = 12, height = 14) 
p_country1
dev.off()


#### few NAs ####

country_l_type<-rbind(TOT_type, 
                      ESP_type, GBR_type, CZE_type, ITA_type, POL_type, ROU_type, 
                      BIH_type,  UKR_type, 
                      IDN_type,
                      JPN_type)

country_l_type<-country_l_type %>% mutate(sign=ifelse(model=="Total" & p.value<0.05, TRUE, FALSE)) %>% 
  group_by(term) %>% mutate(sign_5=max(sign), sign=ifelse(sign_5==0, "P>0.05 (aggr. model)", "P<0.05 (aggr. model)"), sign=as.factor(sign)) %>% filter(model!="Total")



p_country1<-dwplot(subset(country_l_type, 
                          (country_l_type$term=="no_reas" |
                             country_l_type$term=="typeTRUE" |
                             country_l_type$term=="factor(age)TRUE" |
                             country_l_type$term=="female1" | 
                             country_l_type$term=="children_d_naTRUE" | 
                             country_l_type$term=="part1" | 
                             country_l_type$term=="part2" | 
                             country_l_type$term=="occupationac1" | 
                             country_l_type$term=="occupationac2" | 
                             country_l_type$term=="occupationac3" | 
                             country_l_type$term=="degreeTRUE" |
                             country_l_type$term=="risk" | 
                             country_l_type$term=="patient" |
                             country_l_type$term=="englishTRUE"
                          )),
                   vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2), dodge_size= 0.7,
                   dot_args = list(aes(shape =as.factor(sign)), size=4)) +
  scale_x_continuous(limits = c(-0.7, 0.7))+
  scale_y_discrete(   labels=c( no_reas= "Number of reasons",
                                "factor(age)TRUE" = "Age: under 35 years",
                                female1 = "Gender: female",                       
                                children_d_naTRUE = "Children", 
                                "part1" = "Partner (native German)",                       
                                "part2" = "Partner (other native)",
                                degreeTRUE= "University degree",
                                "occupationac1" = "Occ.: high appl./low comm. skills",
                                "occupationac2" = "Occ.: high appl./high comm. skills",
                                "occupationac3" = "Occ.: in education",
                                risk = "Risk attitude", 
                                patient = "Patience",
                                "englishTRUE" = "English speaker"
  ))+
  xlab("AME")+
  theme(legend.title = element_blank())

pdf("mreas_na_country_few1.pdf", paper="a4r", width = 12, height = 14) 
p_country1
dev.off()