#### Libraries ----------
library(ggplot2)
library(cowplot)
library(RColorBrewer)
library(reshape2)
library(forcats)

library(gridExtra)
library(grid)
library(ggpubr)

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)

#### Start ---------------

est<-readRDS("est.RDS")

# Info on ind. var.:
#1. empstat: ref. cat. empstat=2
table(est$empstat)
est <- within(est, empstat <- relevel(factor(empstat), ref = "2"))


#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

#4. female: ref. cat. female=0
table(est$female)
est <- within(est, female <- relevel(factor(female), ref = "0"))


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$mreason_lang, useNA="always")

#use only those with completed cases

table(est$complete)

 table(est$type, est$type2, useNA = "always")
 
 levels(est$type2)
  
  est$type2[is.na(est$type2)] = "No answer"
 
  est<-est %>%  mutate(complete=ifelse(complete.cases(est), TRUE, FALSE))

a<-est[est$complete=="TRUE",]
b<-a

table(a$mreason_lang)
a$mreason_lang<-as.character(a$mreason_lang)
a$mreason_lang<-as.factor(a$mreason_lang)
table(a$mreason_lang)


b<- a %>% select(starts_with("reason")) %>% select(-reason_err, -reason_nans, -reason_cons, -reason_inv, -reason_culture, -reason_education, -reason_domestic, -reason_foreign, -reason_social, -reason_other, -reason_move, -reason_om)

b<-b %>% 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)
with(b[b$no_reas==1,], table())
summary(b$no_reas)


b<-b %>% select(-no_reas)

btab = data.frame(values=colSums(b, na.rm=TRUE), names = names(b))
btab <- btab %>% mutate(values=values/nrow(b)*100,
                        names=as.factor(case_when(.$names=="reason_stud"~"Education",
                                                  .$names=="reason_comp"~"Company communication",
                                                  .$names=="reason_trade"~"Trading partners",
                                                  .$names=="reason_cust"~"Customers",
                                                  .$names=="reason_inc"~"Income",
                                                  .$names=="reason_requ"~"Employer",
                                                  .$names=="reason_lab"~ "Labour market",
                                                  .$names=="reason_partner"~"Family",
                                                  .$names=="reason_soc"~"Friends",
                                                  .$names=="reason_move_prof"~"Educational and labour migration",
                                                  .$names=="reason_move_other"~ "Other migration",
                                                  .$names=="reason_holi"~"Holiday",
                                                  .$names=="reason_cult"~"Culture",
                                                  .$names=="reason_inter"~"Interest"
                                                  )))


#### Tetrachoric correlation and factor analysis - all categories --------------
library(polycor)
library(correlation)

# Tetrachoric correlation matrix
b<-b %>% select("Interest"=reason_inter,
                "Culture"=reason_cult,
                "Holiday"=reason_holi,
                "Friends"=reason_soc,
                "Family"=reason_partner,
                "Other migration"=reason_move_other,
                "Educ. and labour migration"=reason_move_prof,
                "Education"=reason_stud,
                "Labour market"=reason_lab,
                "Income"=reason_inc,
                "Employer"=reason_requ,
                "Customer"=reason_cust,
                "Trade partner"=reason_trade,
                "Company communic."=reason_comp
)

het.mat <- hetcor(b)

het.mat <- round(het.mat$cor, 2)
head(het.mat[, 1:6])
lower.tri(het.mat, diag=TRUE)

upper<-het.mat
upper[upper.tri(het.mat, diag=FALSE)]<-""
upper<-as.data.frame(upper)
upper %>%  kable(format = "latex",
                 digits=2, booktabs=TRUE, linesep = "\\addlinespace",
                 caption="Correlation coefficients between reasons") %>%
  kable_styling(latex_options = c("hold_position")) %>%
  column_spec(1, width = "3.3cm") %>%
  column_spec(2:15, width= "2cm")

corstars <-function(x, method=c("pearson", "spearman"), 
                    removeTriangle=c("upper", "lower"),
                    result=c("none", "html", "latex")){
  #Compute correlation matrix
  require(Hmisc)
  x <- as.matrix(x)
  correlation_matrix<-hetcor(b)
  p <- cor_to_p(correlation_matrix$cor, n=4604, method = "tetrachoric")
  R <- correlation_matrix$cor # Matrix of correlation coeficients
  p <- p$p # Matrix of p-value
  
  ## Define notions for significance levels; spacing is important.
  mystars <- ifelse(p < .01, "***  ", ifelse(p < .05, "**  ", ifelse(p<.1, "*   ", "    ")))
  
  ## trunctuate the correlation matrix to two decimal
  R <- format(round(cbind(rep(-1.11, ncol(x)), R), 2))[,-1]
  
  ## build a new matrix that includes the correlations with their apropriate stars
  Rnew <- matrix(paste(R, mystars, sep=""), ncol=ncol(x))
  diag(Rnew) <- paste(diag(R), " ", sep="")
  rownames(Rnew) <- colnames(x)
  colnames(Rnew) <- paste(colnames(x), "", sep="")
  
  ## remove upper triangle of correlation matrix
  if(removeTriangle[1]=="upper"){
    Rnew <- as.matrix(Rnew)
    Rnew[upper.tri(Rnew, diag = TRUE)] <- ""
    Rnew <- as.data.frame(Rnew)
  }
  
  ## remove lower triangle of correlation matrix
  else if(removeTriangle[1]=="lower"){
    Rnew <- as.matrix(Rnew)
    Rnew[lower.tri(Rnew, diag = TRUE)] <- ""
    Rnew <- as.data.frame(Rnew)
  }
  
  ## remove last column and return the correlation matrix
  Rnew <- cbind(Rnew[1:length(Rnew)-1])
  if (result[1]=="none") return(Rnew)
  else{
    if(result[1]=="html") print(xtable(Rnew), type="html")
    else print(xtable(Rnew), type="latex")
  }
}
corstars(het.mat, result="latex")


# Factor analysis 
fa2 <- factanal(covmat = het.mat, factors = 2, rotation = "varimax")
fa5 <- factanal(covmat = het.mat, factors = 5, rotation = "varimax")

colnames(fa5$loadings)<-c("dom. lab. market","cult. interest","pers. reasons",
                          "education", "educ. lab. mig.")
print(loadings(fa5), digits = 2, cutoff = .2, sort = TRUE)

colnames(fa2$loadings)<-c("investment", "consumption")
print(loadings(fa2), digits = 2, cutoff = .2, sort = TRUE)

#### Tetrachoric correlation - 5 categories ####

b <- a %>% select("Cultural interest" = reason_culture,
                  "Education" = reason_education,
                  "Domestic labour market" = reason_domestic,
                  "Educational and labour migration" = reason_foreign,
                  "Personal reasons" = reason_social)

library(polycor)
library(correlation)

het.mat <- hetcor(b)

het.mat <- round(het.mat$cor, 2)
head(het.mat[, 1:5])
lower.tri(het.mat, diag=TRUE)

upper<-het.mat
upper[upper.tri(het.mat, diag=FALSE)]<-""
upper<-as.data.frame(upper)
upper %>%  kable(format = "latex",
                 digits=2, booktabs=TRUE, linesep = "\\addlinespace",
                 caption="Correlation coefficients between reasons") %>%
  kable_styling(latex_options = c("hold_position")) %>%
  column_spec(1, width = "3.3cm") %>%
  column_spec(1:5, width= "2cm")

corstars <-function(x, method=c("pearson", "spearman"), 
                    removeTriangle=c("upper", "lower"),
                    result=c("none", "html", "latex")){
  #Compute correlation matrix
  require(Hmisc)
  x <- as.matrix(x)
  correlation_matrix<-hetcor(b)
  p <- cor_to_p(correlation_matrix$cor, n=4604, method = "tetrachoric")
  R <- correlation_matrix$cor # Matrix of correlation coeficients
  p <- p$p # Matrix of p-value
  
  ## Define notions for significance levels; spacing is important.
  mystars <- ifelse(p < .01, "***  ", ifelse(p < .05, "**  ", ifelse(p<.1, "*   ", "    ")))
  
  ## trunctuate the correlation matrix to two decimal
  R <- format(round(cbind(rep(-1.11, ncol(x)), R), 2))[,-1]
  
  ## build a new matrix that includes the correlations with their apropriate stars
  Rnew <- matrix(paste(R, mystars, sep=""), ncol=ncol(x))
  diag(Rnew) <- paste(diag(R), " ", sep="")
  rownames(Rnew) <- colnames(x)
  colnames(Rnew) <- paste(colnames(x), "", sep="")
  
  ## remove upper triangle of correlation matrix
  if(removeTriangle[1]=="upper"){
    Rnew <- as.matrix(Rnew)
    Rnew[upper.tri(Rnew, diag = TRUE)] <- ""
    Rnew <- as.data.frame(Rnew)
  }
  
  ## remove lower triangle of correlation matrix
  else if(removeTriangle[1]=="lower"){
    Rnew <- as.matrix(Rnew)
    Rnew[lower.tri(Rnew, diag = TRUE)] <- ""
    Rnew <- as.data.frame(Rnew)
  }
  
  ## remove last column and return the correlation matrix
  Rnew <- cbind(Rnew[1:length(Rnew)-1])
  if (result[1]=="none") return(Rnew)
  else{
    if(result[1]=="html") print(xtable(Rnew), type="html")
    else print(xtable(Rnew), type="latex")
  }
}
corstars(het.mat, result="latex")


#### 2 Graphs Reasons####

table(a$langjob, useNA = "always")


#### 2a1 Graph mreason_lang _all 
a %>%
  count(mreason_lang) %>% 
  mutate(perc = ( n / nrow(a) ) * 100) -> mreason_tab

mreason_tab  %>%  
  rename(perc_main = perc) %>% 
  mutate(mreason_lang = fct_reorder(mreason_lang, -perc_main))  %>%  
  mutate(mreason_lang= fct_relevel(mreason_lang, "No answer", after=Inf),
         type= case_when(.$mreason_lang=="Education" ~ 2,
                         .$mreason_lang=="Company communication" ~ 2,
                         .$mreason_lang=="Trading partners" ~ 2,
                         .$mreason_lang=="Customers" ~ 2,
                         .$mreason_lang=="Income"~ 2,
                         .$mreason_lang=="Employer"~ 2,
                         .$mreason_lang== "Labour market"~ 2,
                         .$mreason_lang=="Family" ~ 1,
                         .$mreason_lang=="Friends" ~ 1,
                         .$mreason_lang=="Educational and labour migration"~ 2,
                         .$mreason_lang== "Other migration"~ 1,
                         .$mreason_lang=="Holiday"~ 1,
                         .$mreason_lang=="Culture"~ 1,
                         .$mreason_lang=="Interest"~ 1,
                         .$mreason_lang=="No answer"~ 3 ))   -> mreason_tab 

btab  %>%  
  rename(mreason_lang = names,
         perc_reason = values) %>% 
         mutate(type= case_when(.$mreason_lang=="Education" ~ 2,
                         .$mreason_lang=="Company communication" ~ 2,
                         .$mreason_lang=="Trading partners" ~ 2,
                         .$mreason_lang=="Customers" ~ 2,
                         .$mreason_lang=="Income"~ 2,
                         .$mreason_lang=="Employer"~ 2,
                         .$mreason_lang== "Labour market"~ 2,
                         .$mreason_lang=="Family" ~ 1,
                         .$mreason_lang=="Friends" ~ 1,
                         .$mreason_lang=="Educational and labour migration"~ 2,
                         .$mreason_lang== "Other migration"~ 1,
                         .$mreason_lang=="Holiday"~ 1,
                         .$mreason_lang=="Culture"~ 1,
                         .$mreason_lang=="Interest"~ 1,
                         .$mreason_lang=="No answer"~ 3 ))   -> btab



p_mreason <- ggplot(mreason_tab,
                    aes(x = reorder(mreason_lang, desc(mreason_lang)),
                        y = perc_main, fill  = factor(type)     ))                                   +
  geom_bar(stat = "identity", width=0.8)  +
  xlab("Reasons for learning German")   +
  xlab("")   +
  ylab("% of participants")  +
  coord_flip()     +
  theme() +
  scale_fill_manual(values=c("#818181", "#333333", "#CCCCCC"), labels=c("Consumption", "Investment", "No answer")) +
  theme(legend.text = element_text(size = 16),
        legend.title = element_blank(),
        legend.position = "bottom",
        axis.text.y = element_text(size = 18),
        axis.title.x = element_text(size = 14),
        axis.title.y = element_text(size = 16),
        axis.text.x = element_text(size = 16))


p_mreason

pdf("../graphics/mainreason_wNA.pdf", paper = "special", width = 10) 
p_mreason
dev.off() 


#### 2b Graph type 2 ####

table(a$type2, useNA="always")
is.factor(a$type2)

table(a$type, useNA = "always")


#### 2b1 Graph type 2 all ####
a %>%
  count(type2) %>% 
  mutate(perc = ( n / nrow(a) ) * 100) -> type2_tab

type2_tab  %>%  
  mutate(type2 = fct_reorder(type2, -perc))  %>%  
  mutate(type2= fct_relevel(type2, "No answer", after=Inf),
         type= case_when(.$type2=="Education" ~ 2,
                         .$type2=="Domestic labour market" ~ 2,
                         .$type2=="Educational and labour migration" ~ 2,
                         .$type2=="Personal reasons" ~ 1,
                         .$type2=="Cultural interest"~ 1,
                         .$type2=="No answer"~ 3))   %>% 
  mutate(type2 = factor(type2,
                        labels = c(
                        "Education",
                        "Cultural interest",
                        "Educ. and lab. mig.",
                        "Personal reasons",
                        "Dom. lab. market",
                        "No answer"))) -> type2_tab

p_type2 <- ggplot(type2_tab, 
                  aes(x = reorder(type2, desc(type2)), 
                      y = perc, fill  = factor(type)     ))                                   +  
  geom_bar(stat = "identity", width=0.7)  +   
  xlab("")   + 
  ylab("% of participants")  +
  coord_flip()     +
  theme() +
  scale_fill_manual(values=c("#818181", "#333333", "#CCCCCC"), labels=c("Consumption", "Investment", "No answer")) + 
  theme(legend.text = element_text(size = 16),
        legend.title = element_blank(),
        legend.position = "bottom",
        axis.text.y = element_text(size = 18),
        axis.title.x = element_text(size = 14),
        axis.text.x = element_text(size = 16),
        axis.title.y = element_text(size = 16))

p_type2

pdf("../graphics/groups.pdf", paper = "special", width = 10) 
p_type2
dev.off() 


#### 2d1 Graph type/country all ####

table(a$type)

ntype<-a %>% filter(!is.na(type))
nrow(ntype)

a %>% filter(!is.na(type)) %>%  group_by(country) %>% 
  mutate(one=1,
         country_n=sum(one)) %>% 
  select(country, country_n) %>% 
  unique->country_n_type


a %>% filter(!is.na(type)) %>%  group_by(country) %>% 
  count(type) %>% ungroup() -> typec_tab


typec_tab<- left_join(typec_tab,country_n_type) 

typec_tab %>% 
  mutate(perc = ( n / country_n ) * 100) -> typec_tab



typec_tab  %>%  
  mutate( type= case_when(     .$type==FALSE ~ "Consumption",
                               .$type==TRUE ~ "Investment")) %>% 
  group_by(country) %>% 
  mutate(con=ifelse(type=="Consumption", perc, 0),
         con=max(con) )%>% ungroup %>%  
  arrange(con)           -> typec_tab 


typec_tab$country <- as.factor(typec_tab$country)
typec_tab<- typec_tab %>% mutate(country = fct_reorder(country, con))




p_type <- ggplot(typec_tab, 
                 aes(x = reorder(type, type), 
                     y = perc, fill  = factor(type)     ))                                   +  
  geom_bar(
           stat = "identity")  +   
  xlab("")   + 
  ylab("% of participants")  +
  theme(axis.text.y = element_text(size = 11)) +
  scale_fill_grey(labels=c("Consumption", "Investment")) +
  facet_wrap(~country, scales="fixed", ncol=14) +
  theme(axis.text.x = element_blank(),
        legend.title = element_blank(), 
        legend.position="bottom")
p_type


#### 2d2 Graph type/country young ####


a %>% filter(age==TRUE) %>%  filter(!is.na(type)) %>%  group_by(country) %>% 
  mutate(one=1,
         country_n=sum(one)) %>% 
  select(country, country_n) %>% 
  unique->country_n_type_young


a %>% filter(age==TRUE) %>% filter(!is.na(type)) %>%  group_by(country) %>% 
  count(type) %>% ungroup() -> typec_tab_young


typec_tab_young<- left_join(typec_tab_young,country_n_type_young) 

typec_tab_young %>% 
  mutate(perc = ( n / country_n ) * 100) -> typec_tab_young



typec_tab_young  %>%  
  mutate( type= case_when(     .$type==FALSE ~ "Consumption",
                               .$type==TRUE ~ "Investment")) %>% 
  group_by(country) -> typec_tab_young 
 

p_type_young <- ggplot(typec_tab_young, 
                 aes(x = reorder(type, type), 
                     y = perc, fill  = factor(type)     ))                                   +  
  geom_bar(color = "black",
           stat = "identity")  +   
  xlab("")   + 
  ylab("% of participants")  +
  theme(axis.text.y = element_text(size = 11)) +
  scale_fill_grey(labels=c("Consumption", "Investment")) +
  facet_wrap(~factor(country,levels=c("KOR","IND","IDN", "UKR", "MEX", "ROU", "ITA", "BIH", "CZE", "ESP", "POL", "NLD", "JPN", "GBR")), scales="fixed", ncol=14) +
  theme(axis.text.x = element_blank(),
        legend.title = element_blank(), 
        legend.position="bottom")

p_type_young


#### 2d3 Graph type/country old ####


a %>% filter(age==FALSE) %>%  filter(!is.na(type)) %>%  group_by(country) %>% 
  mutate(one=1,
         country_n=sum(one)) %>% 
  select(country, country_n) %>% 
  unique->country_n_type_old


a %>% filter(age==FALSE) %>% filter(!is.na(type)) %>%  group_by(country) %>% 
  count(type) %>% ungroup() -> typec_tab_old


typec_tab_old<- left_join(typec_tab_old,country_n_type_old) 

typec_tab_old %>% 
  mutate(perc = ( n / country_n ) * 100) -> typec_tab_old



typec_tab_old  %>%  
  mutate( type= case_when(     .$type==FALSE ~ "Consumption",
                               .$type==TRUE ~ "Investment")) %>% 
  group_by(country) -> typec_tab_old 




p_type_old <- ggplot(typec_tab_old, 
                       aes(x = reorder(type, type), 
                           y = perc, fill  = factor(type)     ))                                   +  
  geom_bar(color = "black",
           stat = "identity")  +   
  xlab("")   + 
  ylab("% of participants")  +
  theme(axis.text.y = element_text(size = 11)) +
  scale_fill_grey(labels=c("Consumption", "Investment")) +
  facet_wrap(~factor(country,levels=c("KOR","IND","IDN", "UKR", "MEX", "ROU", "ITA", "BIH", "CZE", "ESP", "POL", "NLD", "JPN", "GBR")), scales="fixed", ncol=14) +
  theme(axis.text.x = element_blank(),
        legend.title = element_blank(), 
        legend.position="bottom")
p_type_old


#### 2d4 Graph type/country female ####


a %>% filter(female==1) %>%  filter(!is.na(type)) %>%  group_by(country) %>% 
  mutate(one=1,
         country_n=sum(one)) %>% 
  select(country, country_n) %>% 
  unique->country_n_type_female


a %>% filter(female==1) %>% filter(!is.na(type)) %>%  group_by(country) %>% 
  count(type) %>% ungroup() -> typec_tab_female


typec_tab_female<- left_join(typec_tab_female,country_n_type_female) 

typec_tab_female %>% 
  mutate(perc = ( n / country_n ) * 100) -> typec_tab_female



typec_tab_female  %>%  
  mutate( type= case_when(     .$type==FALSE ~ "Consumption",
                               .$type==TRUE ~ "Investment")) %>% 
  group_by(country) -> typec_tab_female 



p_type_female <- ggplot(typec_tab_female, 
                       aes(x = reorder(type, type), 
                           y = perc, fill  = factor(type)     ))                                   +  
  geom_bar(color = "black",
           stat = "identity")  +   
  xlab("")   + 
  ylab("% of participants")  +
  theme(axis.text.y = element_text(size = 11)) +
  scale_fill_grey(labels=c("Consumption", "Investment")) +
  facet_wrap(~factor(country,levels=c("KOR","IND","IDN", "UKR", "MEX", "ROU", "ITA", "BIH", "CZE", "ESP", "POL", "NLD", "JPN", "GBR")), scales="fixed", ncol=14) +
  theme(axis.text.x = element_blank(),
        legend.title = element_blank(), 
        legend.position="bottom")
p_type_female

#### 2d5 Graph type/country male ####


a %>% filter(female==0) %>%  filter(!is.na(type)) %>%  group_by(country) %>% 
  mutate(one=1,
         country_n=sum(one)) %>% 
  select(country, country_n) %>% 
  unique->country_n_type_male


a %>% filter(female==0) %>% filter(!is.na(type)) %>%  group_by(country) %>% 
  count(type) %>% ungroup() -> typec_tab_male


typec_tab_male<- left_join(typec_tab_male,country_n_type_male) 

typec_tab_male %>% 
  mutate(perc = ( n / country_n ) * 100) -> typec_tab_male



typec_tab_male  %>%  
  mutate( type= case_when(     .$type==FALSE ~ "Consumption",
                               .$type==TRUE ~ "Investment")) %>% 
  group_by(country) -> typec_tab_male 

p_type_male <- ggplot(typec_tab_male, 
                       aes(x = reorder(type, type), 
                           y = perc, fill  = factor(type)     ))                                   +  
  geom_bar(color = "black",
           stat = "identity")  +   
  xlab("")   + 
  ylab("% of participants")  +
  theme(axis.text.y = element_text(size = 11)) +
  scale_fill_grey(labels=c("Consumption", "Investment")) +
  facet_wrap(~factor(country,levels=c("KOR","IND","IDN", "UKR", "MEX", "ROU", "ITA", "BIH", "CZE", "ESP", "POL", "NLD", "JPN", "GBR")), scales="fixed", ncol=14) +
  theme(axis.text.x = element_blank(),
        legend.title = element_blank(), 
        legend.position="bottom")
p_type_male

#### 2d6 Graph type/country no uni deg ####


a %>% filter(degree==FALSE) %>%  filter(!is.na(type)) %>%  group_by(country) %>% 
  mutate(one=1,
         country_n=sum(one)) %>% 
  select(country, country_n) %>% 
  unique->country_n_type_nodeg


a %>% filter(degree==FALSE) %>% filter(!is.na(type)) %>%  group_by(country) %>% 
  count(type) %>% ungroup() -> typec_tab_nodeg


typec_tab_nodeg<- left_join(typec_tab_nodeg,country_n_type_nodeg) 

typec_tab_nodeg %>% 
  mutate(perc = ( n / country_n ) * 100) -> typec_tab_nodeg



typec_tab_nodeg  %>%  
  mutate( type= case_when(     .$type==FALSE ~ "Consumption",
                               .$type==TRUE ~ "Investment")) %>% 
  group_by(country) -> typec_tab_nodeg 

p_type_nodeg <- ggplot(typec_tab_nodeg, 
                      aes(x = reorder(type, type), 
                          y = perc, fill  = factor(type)     ))                                   +  
  geom_bar(color = "black",
           stat = "identity")  +   
  xlab("")   + 
  ylab("% of participants")  +
  theme(axis.text.y = element_text(size = 11)) +
  scale_fill_grey(labels=c("Consumption", "Investment")) +
  facet_wrap(~factor(country,levels=c("KOR","IND","IDN", "UKR", "MEX", "ROU", "ITA", "BIH", "CZE", "ESP", "POL", "NLD", "JPN", "GBR")), scales="fixed", ncol=14) +
  theme(axis.text.x = element_blank(),
        legend.title = element_blank(), 
        legend.position="bottom")
p_type_nodeg


#### 2d7 Graph type/country uni deg ####


a %>% filter(degree==TRUE) %>%  filter(!is.na(type)) %>%  group_by(country) %>% 
  mutate(one=1,
         country_n=sum(one)) %>% 
  select(country, country_n) %>% 
  unique->country_n_type_deg


a %>% filter(degree==TRUE) %>% filter(!is.na(type)) %>%  group_by(country) %>% 
  count(type) %>% ungroup() -> typec_tab_deg


typec_tab_deg<- left_join(typec_tab_deg,country_n_type_deg) 

typec_tab_deg %>% 
  mutate(perc = ( n / country_n ) * 100) -> typec_tab_deg



typec_tab_deg  %>%  
  mutate( type= case_when(     .$type==FALSE ~ "Consumption",
                               .$type==TRUE ~ "Investment")) %>% 
  group_by(country) -> typec_tab_deg 

p_type_deg <- ggplot(typec_tab_deg, 
                       aes(x = reorder(type, type), 
                           y = perc, fill  = factor(type)     ))                                   +  
  geom_bar(color = "black",
           stat = "identity")  +   
  xlab("")   + 
  ylab("% of participants")  +
  theme(axis.text.y = element_text(size = 11)) +
  scale_fill_grey(labels=c("Consumption", "Investment")) +
  facet_wrap(~factor(country,levels=c("KOR","IND","IDN", "UKR", "MEX", "ROU", "ITA", "BIH", "CZE", "ESP", "POL", "NLD", "JPN", "GBR")), scales="fixed", ncol=14) +
  theme(axis.text.x = element_blank(),
        legend.title = element_blank(), 
        legend.position="bottom")
p_type_deg

#### Combine AGE und GENDER GRAPHS ####

 typec_tab_young <- typec_tab_young %>% mutate(group="Age: <35")  %>% dplyr::select(-n)
 typec_tab_old <- typec_tab_old %>% mutate(group="Age: >=35")  %>% select(-n)
 
 typec_tab

 typec_tab<-typec_tab %>%  mutate(group="Total") %>% select(-con, -n)
typec_age<-rbind(typec_tab, typec_tab_young, typec_tab_old)   


typec_tab_female <- typec_tab_female %>% mutate(group="Female") %>%  select(-n)
typec_tab_male <- typec_tab_male %>% mutate(group="Male") %>%  select(-n)

typec_tab_nodeg <- typec_tab_nodeg %>% mutate(group="No deg.") %>%  select(-n)
typec_tab_deg <- typec_tab_deg %>% mutate(group="Uni. deg.") %>%  select(-n)


typec_gender<-rbind(typec_tab_female, typec_tab_male)
typec_degree<-rbind(typec_tab_nodeg, typec_tab_deg)

typec_genage<- rbind(typec_gender, typec_age, typec_degree)

typec_genage<-typec_genage %>% mutate(x="") %>% mutate(group = factor(group, levels = c("Total", "Age: <35","Age: >=35", "Female", "Male", "No deg.", "Uni. deg.")))


p_typec_genage <- ggplot(typec_genage, 
                      aes(x = x, 
                          y = perc, fill  = factor(type)     ))                                   +  
  geom_bar( stat = "identity", position = "stack", width = 0.7)  +   
  xlab("")   + 
  ylab("% of participants") +
  scale_fill_manual(values=c("#818181","#333333"),labels=c("Consumption", "Investment")) +
  scale_y_continuous(breaks=seq(0, 100, 50))+
  theme(axis.text.y = element_text(size = 12),
        axis.title.y = element_text(size = 12),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        strip.text = element_text(size = 12),
        legend.title = element_blank(), 
        legend.position="bottom",
        legend.text=element_text(size=12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank())+
  facet_grid( group ~factor(country,levels=c("KOR","IND","IDN", "UKR", "MEX", "ROU", "ITA", "BIH", "CZE", "ESP", "POL", "NLD", "JPN", "GBR")), scales="fixed")

p_typec_genage



pdf("../graphics/invest-countries_genage.pdf", paper="special", width = 10) 
p_typec_genage
dev.off() 


#### 2e Graph type2/country ####

a$country <- factor(a$country, levels=levels(typec_tab$country) )
is.factor(a$country)

#### 2f1 Graph mreason_lang/country (with "No Answer") all ####

a %>% group_by(country) %>%
  count(mreason_lang) %>% ungroup() -> mreasonc_na_tab

a %>% group_by(country) %>% 
  mutate(one=1,
         country_n=sum(one)) %>% 
  select(country, country_n) %>% 
  unique->country_mreasonc_na


mreasonc_na_tab<- left_join(mreasonc_na_tab,country_mreasonc_na)

mreasonc_na_tab %>%
  mutate(perc = ( n / country_n ) * 100) -> mreasonc_na_tab


mreasonc_na_tab  %>%
  mutate(mreason_lang = fct_reorder(mreason_lang, -perc))  %>%
  mutate(order= case_when(.$mreason_lang=="Education" ~ 2,
                          .$mreason_lang=="Company communication" ~ 1,
                          .$mreason_lang=="Trading partners" ~ 1,
                          .$mreason_lang=="Customers" ~ 1,
                          .$mreason_lang=="Income"~ 1,
                          .$mreason_lang=="Employer"~ 1,
                          .$mreason_lang== "Labour market"~ 1,
                          .$mreason_lang=="Family" ~ 4,
                          .$mreason_lang=="Friends" ~ 4,
                          .$mreason_lang=="Educational and labour migration"~ 3,
                          .$mreason_lang== "Other migration"~ 4,
                          .$mreason_lang=="Holiday"~ 5,
                          .$mreason_lang=="Culture"~ 5,
                          .$mreason_lang=="Interest"~ 5,
                          .$mreason_lang=="No answer"~ 6 )) %>% 
  mutate(mreason_lang = fct_reorder(mreason_lang, order))  %>%
  mutate(mreason_lang= fct_relevel(mreason_lang, "No answer", after=Inf),
         type= case_when(.$mreason_lang=="Education" ~ 2,
                         .$mreason_lang=="Company communication" ~ 2,
                         .$mreason_lang=="Trading partners" ~ 2,
                         .$mreason_lang=="Customers" ~ 2,
                         .$mreason_lang=="Income"~ 2,
                         .$mreason_lang=="Employer"~ 2,
                         .$mreason_lang== "Labour market"~ 2,
                         .$mreason_lang=="Family" ~ 1,
                         .$mreason_lang=="Friends" ~ 1,
                         .$mreason_lang=="Educational and labour migration"~ 2,
                         .$mreason_lang== "Other migration"~ 1,
                         .$mreason_lang=="Holiday"~ 1,
                         .$mreason_lang=="Culture"~ 1,
                         .$mreason_lang=="Interest"~ 1,
                         .$mreason_lang=="No answer"~ 3 ))   -> mreasonc_na_tab



p_mreasonc_na <- ggplot(mreasonc_na_tab,
                        aes(x = reorder(mreason_lang, desc(mreason_lang)),
                            y = perc, fill  = factor(type)     ))                                   +
  geom_bar(          stat = "identity", width = 0.8)  +
  xlab("")   + 
  ylab("% of participants")  +
  scale_y_continuous(breaks=seq(0, 50, 25))+
  theme(axis.text.y = element_text(size = 11)) +
  scale_fill_manual(values=c("#818181", "#333333", "#CCCCCC"), labels=c("Consumption", "Investment", "No answer"))+
  facet_wrap(~country, scales="fixed", ncol=14) +
  theme(axis.text.x = element_text(size=9),
        axis.title.x = element_text(size=9),
        axis.text.y = element_text(size=9),
        axis.title.y = element_text(size=9),
        legend.text = element_text(size = 9),
        legend.title = element_blank(),
        legend.position = "bottom") +
  coord_flip()
p_mreasonc_na

pdf("../graphics/mainreason-countries-wNA.pdf", paper="a4r", width = 15, height = 14) 
p_mreasonc_na
dev.off() 



ggplot(mreasonc_na_tab, aes(x=mreason_lang, y=perc)) + 
  geom_boxplot()

boxplot()


a_young<-a %>% filter(age==TRUE)
nrow(a_young)
nrow(a)


##### 3 Graphs langjob (with is.na(type)) #####

##### 3a1 Graph langjob ####
table(a$langjob, useNA="always")
a$langjob[is.na(a$langjob)]<-"-99"

# Convert numeric industry variable to factor and change labels
a$langjob  <- factor(a$langjob, levels = c(1, 2, 3, 4, 5))
a$langjob  <- fct_recode(a$langjob, 
                         "5"    = "1"   , 
                         "2"              = "4"   , 
                         "3"              = "3"   , 
                         "4"              = "2"   , 
                         "1" = "5"    
)
#change order of factors
a$langjob <- factor(a$langjob,levels=c("1", "2", "3", "4", "5"))


a<- a %>% mutate(langjob3=ifelse(as.numeric(langjob)<4, "unlikely", "likely"))

table(a$langjob, a$langjob3)



p_langjob<-ggplot(a, 
                  aes(x = langjob)) +geom_bar(aes(y = (..count..)/sum(..count..)*100), width = 0.4) +
  scale_y_continuous(name="% of participants")+
  xlab("") +
  ylab("% of participants") +
  theme(axis.text=element_text(size=16),
        axis.title=element_text(size=16))

table(a$langjob)

p_langjob


pdf("../graphics/langjob.pdf", paper="a4r", width = 10) 
p_langjob
dev.off()


#### 3a3 langjob country age #####


table(a$langjob)
a %>%  group_by(country) %>% 
  count(langjob3) %>% ungroup() -> langjobc_tab

a %>% group_by(country) %>% 
  mutate(one=1,
         country_n=sum(one)) %>% 
  select(country, country_n) %>% 
  unique->country_n_langjob




langjobc_tab<- left_join(langjobc_tab,country_n_langjob) 

langjobc_tab %>% 
  mutate(perc = ( n / country_n ) * 100) ->langjobc_tab




a %>%  filter(age==TRUE) %>%   group_by(country) %>% 
  count(langjob3) %>% ungroup() -> langjobc_tab_young

a %>%  filter(age==TRUE) %>% group_by(country) %>% 
  mutate(one=1,
         country_n=sum(one)) %>% 
  select(country, country_n) %>% 
  unique->country_n_langjob_young




langjobc_tab_young<- left_join(langjobc_tab_young,country_n_langjob_young) 

langjobc_tab_young %>% 
  mutate(perc = ( n / country_n ) * 100) ->langjobc_tab_young



a %>%  filter(age==FALSE) %>%   group_by(country) %>% 
  count(langjob3) %>% ungroup() -> langjobc_tab_old

a %>%  filter(age==FALSE) %>% group_by(country) %>% 
  mutate(one=1,
         country_n=sum(one)) %>% 
  select(country, country_n) %>% 
  unique->country_n_langjob_old




langjobc_tab_old<- left_join(langjobc_tab_old,country_n_langjob_old) 

langjobc_tab_old %>% 
  mutate(perc = ( n / country_n ) * 100) ->langjobc_tab_old




langjobc_tab<- langjobc_tab %>% mutate(group="All") %>% select(-n)

langjobc_tab_young <- langjobc_tab_young %>% mutate(group="Younger than 35")  %>% select(-n)
langjobc_tab_old <- langjobc_tab_old %>% mutate(group="35 and older")  %>% select(-n)


langjobc_age<-rbind(langjobc_tab, langjobc_tab_young, langjobc_tab_old) %>%  mutate(group = fct_relevel(group, "35 and older", after=2))  



p_langjobc_age <- ggplot(langjobc_age, 
                     aes(x = langjob3, 
                         y = perc  , fill=langjob3    ))                                   +  
  geom_bar(stat = "identity")  +  
  facet_grid(group~factor(country,levels=c("KOR","IND","IDN", "UKR", "MEX", "ROU", "ITA", "BIH", "CZE", "ESP", "POL", "NLD", "JPN", "GBR")), scales="fixed") +
  xlab("")   + 
  ylab("% of participants")  +
  theme(axis.text.y = element_text(size = 11)) +
  theme(axis.text.y =  element_text(size=11),
        axis.text.x = element_blank(),
        axis.title = element_text(size=11),
        legend.title = element_blank(), 
        legend.position="bottom")+
  scale_fill_grey(name="",labels=c("Likely", "Unlikely"))

p_langjobc_age


#### 3a4 gender langjob country ####

a %>%  filter(female==TRUE) %>%   group_by(country) %>% 
  count(langjob3) %>% ungroup() -> langjobc_tab_female

a %>%  filter(female==TRUE) %>% group_by(country) %>% 
  mutate(one=1,
         country_n=sum(one)) %>% 
  select(country, country_n) %>% 
  unique->country_n_langjob_female




langjobc_tab_female<- left_join(langjobc_tab_female,country_n_langjob_female) 

langjobc_tab_female %>% 
  mutate(perc = ( n / country_n ) * 100) ->langjobc_tab_female





a %>%  filter(female==FALSE) %>%   group_by(country) %>% 
  count(langjob3) %>% ungroup() -> langjobc_tab_male

a %>%  filter(female==FALSE) %>% group_by(country) %>% 
  mutate(one=1,
         country_n=sum(one)) %>% 
  select(country, country_n) %>% 
  unique->country_n_langjob_male




langjobc_tab_male<- left_join(langjobc_tab_male,country_n_langjob_male) 

langjobc_tab_male %>% 
  mutate(perc = ( n / country_n ) * 100) ->langjobc_tab_male




langjobc_tab<- langjobc_tab %>% mutate(group="All") %>% select(-n)

langjobc_tab_female <- langjobc_tab_female %>% mutate(group="Female")  %>% select(-n)
langjobc_tab_male <- langjobc_tab_male %>% mutate(group="Male")  %>% select(-n)


langjobc_gender<-rbind(langjobc_tab, langjobc_tab_female, langjobc_tab_male) %>%  mutate(group = fct_relevel(group, "Male", after=2))  



p_langjobc_gender <- ggplot(langjobc_gender, 
                         aes(x = langjob3, 
                             y = perc, fill=langjob3      ))                                   +  
  geom_bar(stat = "identity")  +  
  facet_grid(group~factor(country,levels=c("KOR","IND","IDN", "UKR", "MEX", "ROU", "ITA", "BIH", "CZE", "ESP", "POL", "NLD", "JPN", "GBR")), scales="fixed") +
  xlab("")   + 
  ylab("% of participants")  +
  theme(axis.text.y = element_text(size = 11)) +
  theme(axis.text.y =  element_text(size=11),
        axis.text.x = element_blank(),
        axis.title = element_text(size=11),
        legend.title = element_blank(), 
        legend.position="bottom")+
  scale_fill_grey(name="",labels=c("Likely", "Unlikely"))
p_langjobc_gender


#### neu gender/age #####
table(a$langjob)
a %>%  group_by(country) %>% 
  count(langjob3) %>% ungroup() -> langjobc_tab

a %>% group_by(country) %>% 
  mutate(one=1,
         country_n=sum(one)) %>% 
  select(country, country_n) %>% 
  unique->country_n_langjob




langjobc_tab<- left_join(langjobc_tab,country_n_langjob) 

langjobc_tab %>% 
  mutate(perc = ( n / country_n ) * 100) ->langjobc_tab




a %>%  filter(age==TRUE) %>%   group_by(country) %>% 
  count(langjob3) %>% ungroup() -> langjobc_tab_young

a %>%  filter(age==TRUE) %>% group_by(country) %>% 
  mutate(one=1,
         country_n=sum(one)) %>% 
  select(country, country_n) %>% 
  unique->country_n_langjob_young




langjobc_tab_young<- left_join(langjobc_tab_young,country_n_langjob_young) 

langjobc_tab_young %>% 
  mutate(perc = ( n / country_n ) * 100) ->langjobc_tab_young

# Age > 35

a %>%  filter(age==FALSE) %>%   group_by(country) %>% 
  count(langjob3) %>% ungroup() -> langjobc_tab_old

a %>%  filter(age==FALSE) %>% group_by(country) %>% 
  mutate(one=1,
         country_n=sum(one)) %>% 
  select(country, country_n) %>% 
  unique->country_n_langjob_old




langjobc_tab_old<- left_join(langjobc_tab_old,country_n_langjob_old) 

langjobc_tab_old %>% 
  mutate(perc = ( n / country_n ) * 100) ->langjobc_tab_old




langjobc_tab<- langjobc_tab %>% mutate(group="Total") %>% select(-n)

langjobc_tab_young <- langjobc_tab_young %>% mutate(group="Age: <35")  %>% select(-n)
langjobc_tab_old <- langjobc_tab_old %>% mutate(group="Age: >=35")  %>% select(-n)


langjobc_age<-rbind(langjobc_tab, langjobc_tab_young, langjobc_tab_old) 


# Female

a %>%  filter(female==1) %>%   group_by(country) %>% 
  count(langjob3) %>% ungroup() -> langjobc_tab_female

a %>%  filter(female==1) %>% group_by(country) %>% 
  mutate(one=1,
         country_n=sum(one)) %>% 
  select(country, country_n) %>% 
  unique->country_n_langjob_female




langjobc_tab_female<- left_join(langjobc_tab_female,country_n_langjob_female) 

langjobc_tab_female %>% 
  mutate(perc = ( n / country_n ) * 100) ->langjobc_tab_female



## MALE

a %>%  filter(female==0) %>%   group_by(country) %>% 
  count(langjob3) %>% ungroup() -> langjobc_tab_male

a %>%  filter(female==0) %>% group_by(country) %>% 
  mutate(one=1,
         country_n=sum(one)) %>% 
  select(country, country_n) %>% 
  unique->country_n_langjob_male




langjobc_tab_male<- left_join(langjobc_tab_male,country_n_langjob_male) 

langjobc_tab_male %>% 
  mutate(perc = ( n / country_n ) * 100) ->langjobc_tab_male


langjobc_tab_female <- langjobc_tab_female %>% mutate(group="Female")  %>% select(-n)
langjobc_tab_male <- langjobc_tab_male %>% mutate(group="Male")  %>% select(-n)


langjobc_gender<-rbind(langjobc_tab_female, langjobc_tab_male) 


# no uni deg

a %>%  filter(degree==FALSE) %>%   group_by(country) %>% 
  count(langjob3) %>% ungroup() -> langjobc_tab_nodeg

a %>%  filter(degree==FALSE) %>% group_by(country) %>% 
  mutate(one=1,
         country_n=sum(one)) %>% 
  select(country, country_n) %>% 
  unique->country_n_langjob_nodeg




langjobc_tab_nodeg<- left_join(langjobc_tab_nodeg,country_n_langjob_nodeg) 

langjobc_tab_nodeg %>% 
  mutate(perc = ( n / country_n ) * 100) ->langjobc_tab_nodeg


# Uni deg

a %>%  filter(degree==TRUE) %>%   group_by(country) %>% 
  count(langjob3) %>% ungroup() -> langjobc_tab_deg

a %>%  filter(degree==TRUE) %>% group_by(country) %>% 
  mutate(one=1,
         country_n=sum(one)) %>% 
  select(country, country_n) %>% 
  unique->country_n_langjob_deg



langjobc_tab_deg<- left_join(langjobc_tab_deg,country_n_langjob_deg) 

langjobc_tab_deg %>% 
  mutate(perc = ( n / country_n ) * 100) ->langjobc_tab_deg





langjobc_tab_nodeg <- langjobc_tab_nodeg %>% mutate(group="No deg.")  %>% select(-n)
langjobc_tab_deg <- langjobc_tab_deg %>% mutate(group="Uni. deg.")  %>% select(-n)


langjobc_degree<-rbind(langjobc_tab_nodeg, langjobc_tab_deg) 



langjobc_genage<-rbind(langjobc_age, langjobc_gender, langjobc_degree) %>% 
  mutate(x="") %>%   
  mutate(group = factor(group, levels = c("Total", "Age: <35","Age: >=35", "Female", "Male", "No deg.", "Uni. deg.")),
                                                                                    langjob3=ifelse(langjob3=="unlikely", 0, 1))

p_langjobc_genage <- ggplot(langjobc_genage, 
                            aes(x = x, 
                                y = perc, fill=factor(langjob3)      ))                                   +  
  geom_bar(stat = "identity",position = "stack", width = 0.7)  +  
  facet_grid(group~factor(country,levels=c("KOR","IND","IDN", "UKR", "MEX", "ROU", "ITA", "BIH", "CZE", "ESP", "POL", "NLD", "JPN", "GBR")), scales="fixed") +
  xlab("")   + 
  ylab("% of participants")  +
  scale_y_continuous(breaks=seq(0, 100, 50))+
  theme(axis.text.y = element_text(size = 12),
        axis.title.y = element_text(size = 12),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        strip.text = element_text(size = 12),
        legend.title = element_blank(), 
        legend.position="bottom",
        legend.text=element_text(size=12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank())+
  scale_fill_manual(values=c("#818181","#333333"),name="",labels=c( "Unlikely", "Likely"))
p_langjobc_genage


pdf("../graphics/langjob-countries_genage.pdf", paper="special", width = 10) 
p_langjobc_genage
dev.off()


#### 3a3 langjob100 with type ####

a <- a %>% 
  mutate(type_100= case_when(.$type==FALSE ~ "Consumption",
                             .$type==TRUE ~ "Investment"))


p_langjob_100<-ggplot(a, 
                      aes(x = langjob3, 
                          fill  = type_100   )) +geom_bar(position = "fill") +
  scale_y_continuous(name="Number of participants")+
  scale_fill_manual(values=wes_palette(n=3, name="Moonrise1"), 
                    name="Type",
                    labels=c("Consumption", "Investment", "No answer"))+
  xlab("")


p_langjob_100


p_langjobc_100<-ggplot(a, 
                       aes(x = langjob3, 
                           fill  = type_100   )) +geom_bar(position = "fill") + 
  facet_wrap(~factor(country,levels=c("KOR","IND","IDN", "UKR", "MEX", "ROU", "ITA", "BIH", "CZE", "ESP", "POL", "NLD", "JPN", "GBR")), scales="fixed", ncol=7)+
  scale_y_continuous(name="% of participants", labels = scales::percent_format())+
  scale_fill_manual(values=c("#818181","#333333"), labels=c("Consumption", "Investment", name=""))+
  theme(axis.text = element_text(size=11),
        legend.position = "bottom",legend.title = element_blank(),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank())+
  xlab("")
p_langjobc_100

pg <- ggplot_build(p_langjobc_100)$data[[1]]

pg<-pg %>% filter(group==1 | group==3) %>% group_by(PANEL) %>%  mutate( likely=max(ymin),
                                                                        unlikely=min(ymin),
                                                                        ratio= likely/unlikely)

pdf("../graphics/langjob-100-countries.pdf", paper="a4r", width = 12, height = 14) 
p_langjobc_100
dev.off()
