On this page are some additional checks, concerning migration background, that lead to remarks made in the manuscript (without being presented as central analysis results).

Let’s check some migration numbers

These are just to check whether our mapping goes wrong as predicted, see Footnote 3 in the manuscript. We are using numbers received via the Einwohnermeldeamt.

load("prep.RData")
library(binom)
prop.table(table(combined_all_schooltypes$german_at_home[combined_all_schooltypes$year == 2010])) # 91%
## numeric(0)
prop.table(table(combined_all_schooltypes$german_at_home[combined_all_schooltypes$year == 2015])) # 91%
## numeric(0)
prop.table(table(combined_all_schooltypes$german_at_home[combined_all_schooltypes$year == 2023])) # 88%
## numeric(0)
ewo <- readRDS("Files/Ewo_PLZ_Geschl_Migration.rds")
ewo <- ewo[ewo$Alter >= 12 & ewo$Alter <= 18,]
table(ewo$Zuwanderungshintergrund)
## 
##                                         Ausländer 
##                                              1271 
##                                        Aussiedler 
##                                               840 
##                                      Einbürgerung 
##                                              1156 
##   einseitiger elterlicher Zuwanderungshintergrund 
##                                               567 
##        ohne (erkennbaren) Zuwanderungshintergrund 
##                                              1451 
## persönl. Zuwanderungshintergrund aber Eltern ohne 
##                                               803
total <- sum(ewo$Anzahl)

sum(ewo$Anzahl[ewo$Zuwanderungshintergrund == "Ausländer"])/total # 12%
## [1] 0.1216921
sum(ewo$Anzahl[ewo$Zuwanderungshintergrund == "Aussiedler"])/total # 2%
## [1] 0.02168009
sum(ewo$Anzahl[ewo$Zuwanderungshintergrund == "Einbürgerung"])/total # 6%
## [1] 0.05757721
sum(ewo$Anzahl[ewo$Zuwanderungshintergrund == "einseitiger elterlicher Zuwanderungshintergrund"])/total # 01%
## [1] 0.01121259
sum(ewo$Anzahl[ewo$Zuwanderungshintergrund == "ohne (erkennbaren) Zuwanderungshintergrund"])/total # 77%
## [1] 0.7705324
sum(ewo$Anzahl[ewo$Zuwanderungshintergrund == "persönl. Zuwanderungshintergrund aber Eltern ohne"])/total # 2%
## [1] 0.01730561
ewo$german_at_home <- NA
ewo$german_at_home[ewo$Zuwanderungshintergrund == "Ausländer"] <- 0
ewo$german_at_home[ewo$Zuwanderungshintergrund == "Einbürgerung"] <- 0

ewo$german_at_home[ewo$Zuwanderungshintergrund == "Aussiedler"] <- 1
ewo$german_at_home[ewo$Zuwanderungshintergrund == "einseitiger elterlicher Zuwanderungshintergrund"] <- 1
ewo$german_at_home[ewo$Zuwanderungshintergrund == "ohne (erkennbaren) Zuwanderungshintergrund"] <- 1
ewo$german_at_home[ewo$Zuwanderungshintergrund == "persönl. Zuwanderungshintergrund aber Eltern ohne"] <- 1

sum(ewo$Anzahl[ewo$Jahr == 2010 & ewo$german_at_home == 1])/sum(ewo$Anzahl[ewo$Jahr == 2010]) # 89.9%
## [1] 0.8993833
sum(ewo$Anzahl[ewo$Jahr == 2015 & ewo$german_at_home == 1])/sum(ewo$Anzahl[ewo$Jahr == 2015]) # 85%
## [1] 0.8534373
sum(ewo$Anzahl[ewo$Jahr == 2023 & ewo$german_at_home == 1])/sum(ewo$Anzahl[ewo$Jahr == 2023]) # 75%
## [1] 0.75498
# Let's do by age, gender, and year -- in the sample versus in the population

# Fold in confidence interval

comp_ewo <- data.frame(matrix(NA, nrow = 7*3, ncol = 10))
names(comp_ewo) <- c("year", "age", "german_girls", "german_girls_lb", "german_girls_ub","german_girls_ewo", 
                     "german_boys", "german_boys_lb", "german_boys_ub", "german_boys_ewo")
comp_ewo$year <- rep(c(2010, 2015, 2023), each = 7)
comp_ewo$age <- rep(12:18, times = 3)
n_temp <- NA
sum_temp <- NA

for (i in 1:nrow(comp_ewo)) {
  print(i)
  # In the data
  # girls, mean
  comp_ewo$german_girls[i] <- mean(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age == comp_ewo$age[i] & 
                                                             combined_all_schooltypes$gender == "female" & 
                                                             combined_all_schooltypes$year == comp_ewo$year[i]], 
                                   na.rm = TRUE)
  # girls, CI
  n_temp <- sum(!is.na(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age == comp_ewo$age[i] & 
                                                 combined_all_schooltypes$gender == "female" & 
                                                 combined_all_schooltypes$year == comp_ewo$year[i]]))
  sum_temp <- sum(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age == comp_ewo$age[i] & 
                                             combined_all_schooltypes$gender == "female" & 
                                             combined_all_schooltypes$year == comp_ewo$year[i]], 
                   na.rm = TRUE)
  comp_ewo[i, c("german_girls_lb", "german_girls_ub")] <- binom.confint(sum_temp, n_temp, conf.level = .95, methods = "wilson")[, c("lower", "upper")]
  
  # boys, mean
  comp_ewo$german_boys[i] <- mean(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age == comp_ewo$age[i] & 
                                                             combined_all_schooltypes$gender == "male" & 
                                                             combined_all_schooltypes$year == comp_ewo$year[i]], 
                                   na.rm = TRUE)
  # boys, ci
  n_temp <- sum(!is.na(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age == comp_ewo$age[i] & 
                                                 combined_all_schooltypes$gender == "male" & 
                                                 combined_all_schooltypes$year == comp_ewo$year[i]]))
  sum_temp <- sum(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age == comp_ewo$age[i] & 
                                            combined_all_schooltypes$gender == "male" & 
                                            combined_all_schooltypes$year == comp_ewo$year[i]], 
                  na.rm = TRUE)
  comp_ewo[i, c("german_boys_lb", "german_boys_ub")] <- binom.confint(sum_temp, n_temp, conf.level = .95, methods = "wilson")[, c("lower", "upper")]
  # In the city data
  comp_ewo$german_girls_ewo[i] <- sum(ewo$Anzahl[ewo$Jahr == comp_ewo$year[i] &
                                                ewo$Alter == comp_ewo$age[i] &
                                                ewo$Geschlecht == "w" &
                                                ewo$german_at_home == 1])/ # number of german speaking girls of that age in that year
    sum(ewo$Anzahl[ewo$Jahr == comp_ewo$year[i] &
                     ewo$Alter == comp_ewo$age[i] &
                     ewo$Geschlecht == "w"]) # divided by total number of girls of that age in that year
  comp_ewo$german_boys_ewo[i] <- sum(ewo$Anzahl[ewo$Jahr == comp_ewo$year[i] &
                                                   ewo$Alter == comp_ewo$age[i] &
                                                   ewo$Geschlecht == "m" &
                                                   ewo$german_at_home == 1])/ # number of german speaking boys of that age in that year
    sum(ewo$Anzahl[ewo$Jahr == comp_ewo$year[i] &
                     ewo$Alter == comp_ewo$age[i] &
                     ewo$Geschlecht == "m"]) # divided by total number of boys of that age in that year
}
## [1] 1
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## [1] 2
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## [1] 3
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## [1] 4
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## [1] 5
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## [1] 6
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## [1] 7
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## [1] 8
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## [1] 9
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## [1] 10
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## [1] 11
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## [1] 12
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## [1] 13
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## [1] 14
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## [1] 15
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## [1] 16
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## [1] 17
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## [1] 18
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## [1] 19
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## [1] 20
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## [1] 21
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
## Warning in
## mean.default(combined_all_schooltypes$german_at_home[combined_all_schooltypes$age
## == : argument is not numeric or logical: returning NA
# Lets plot the comparison
# Dont forget: 2015 is a different type of assessment in the sample

library(ggplot2)
ggplot(data = comp_ewo[comp_ewo$year == 2010,], aes(x = age)) +
  geom_point(aes(y = german_girls)) +
  geom_line(aes(y = german_girls)) +
  geom_ribbon(aes(ymin = german_girls_lb, ymax = german_girls_ub), alpha = .2) +
  geom_point(aes(y = german_girls_ewo), color = "red") +
  geom_line(aes(y = german_girls_ewo), color = "red") +
  theme_minimal() +
  coord_cartesian(ylim = c(.5, 1)) +
  ggtitle("girls, 2010")
## Warning: Removed 7 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 7 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 7 rows containing missing values or values outside the scale range
## (`geom_ribbon()`).

ggplot(data = comp_ewo[comp_ewo$year == 2010,], aes(x = age)) +
  geom_point(aes(y = german_boys)) +
  geom_line(aes(y = german_boys)) +
  geom_ribbon(aes(ymin = german_boys_lb, ymax = german_boys_ub), alpha = .2) +
  geom_point(aes(y = german_boys_ewo), color = "red") +
  geom_line(aes(y = german_boys_ewo), color = "red") +
  theme_minimal() +
  coord_cartesian(ylim = c(.5, 1)) +
  ggtitle("boys, 2010")
## Warning: Removed 7 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 7 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 7 rows containing missing values or values outside the scale range
## (`geom_ribbon()`).

ggplot(data = comp_ewo[comp_ewo$year == 2015,], aes(x = age)) +
  geom_point(aes(y = german_girls)) +
  geom_line(aes(y = german_girls)) +
  geom_ribbon(aes(ymin = german_girls_lb, ymax = german_girls_ub), alpha = .2) +
  geom_point(aes(y = german_girls_ewo), color = "red") +
  geom_line(aes(y = german_girls_ewo), color = "red") +
  theme_minimal() +
  coord_cartesian(ylim = c(.5, 1)) +
  ggtitle("girls, 2015")
## Warning: Removed 7 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 7 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 7 rows containing missing values or values outside the scale range
## (`geom_ribbon()`).

ggplot(data = comp_ewo[comp_ewo$year == 2015,], aes(x = age)) +
  geom_point(aes(y = german_boys)) +
  geom_line(aes(y = german_boys)) +
  geom_ribbon(aes(ymin = german_boys_lb, ymax = german_boys_ub), alpha = .2) +
  geom_point(aes(y = german_boys_ewo), color = "red") +
  geom_line(aes(y = german_boys_ewo), color = "red") +
  theme_minimal() +
  coord_cartesian(ylim = c(.5, 1)) +
  ggtitle("boys, 2015")
## Warning: Removed 7 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 7 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 7 rows containing missing values or values outside the scale range
## (`geom_ribbon()`).

ggplot(data = comp_ewo[comp_ewo$year == 2023,], aes(x = age)) +
  geom_point(aes(y = german_girls)) +
  geom_line(aes(y = german_girls)) +
  geom_ribbon(aes(ymin = german_girls_lb, ymax = german_girls_ub), alpha = .2) +
  geom_point(aes(y = german_girls_ewo), color = "red") +
  geom_line(aes(y = german_girls_ewo), color = "red") +
  theme_minimal() +
  coord_cartesian(ylim = c(.5, 1)) +
  ggtitle("girls, 2023")
## Warning: Removed 7 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 7 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 7 rows containing missing values or values outside the scale range
## (`geom_ribbon()`).

ggplot(data = comp_ewo[comp_ewo$year == 2023,], aes(x = age)) +
  geom_point(aes(y = german_boys)) +
  geom_line(aes(y = german_boys)) +
  geom_ribbon(aes(ymin = german_boys_lb, ymax = german_boys_ub), alpha = .2) +
  geom_point(aes(y = german_boys_ewo), color = "red") +
  geom_line(aes(y = german_boys_ewo), color = "red") +
  theme_minimal() +
  coord_cartesian(ylim = c(.5, 1)) +
  ggtitle("boys, 2023")
## Warning: Removed 7 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 7 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 7 rows containing missing values or values outside the scale range
## (`geom_ribbon()`).

round(prop.table(table(youth2010$s23aa)),2)
## 
##  121  126  129  134  137  140  142  146  150  151  152  158  160  163  166  169 
## 0.01 0.02 0.01 0.01 0.01 0.01 0.02 0.01 0.01 0.01 0.01 0.01 0.16 0.05 0.14 0.01 
##  170  221  245  326  335  348  351  361  368  386  423  425  430  432  434  436 
## 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.05 0.01 0.01 0.05 0.01 0.01 
##  437  438  439  444  450  451  461  475  476  477  536  998 
## 0.01 0.08 0.01 0.14 0.01 0.01 0.01 0.01 0.01 0.02 0.01 0.01
round(prop.table(table(youth2010$s23ba)),2)
## 
##    0  121  122  125  126  129  130  131  132  134  137  140  142  144  148  150 
## 0.01 0.01 0.00 0.01 0.00 0.00 0.00 0.00 0.00 0.00 0.02 0.00 0.01 0.01 0.00 0.00 
##  151  152  153  154  157  158  159  160  161  163  165  166  168  169  170  195 
## 0.01 0.03 0.01 0.00 0.01 0.00 0.01 0.10 0.00 0.06 0.01 0.05 0.01 0.00 0.01 0.00 
##  221  223  225  238  248  252  254  262  269  276  282  285  287  289  326  332 
## 0.03 0.01 0.00 0.00 0.00 0.01 0.02 0.00 0.00 0.00 0.00 0.01 0.01 0.00 0.00 0.01 
##  348  351  361  367  368  423  425  430  431  432  434  436  437  438  439  444 
## 0.00 0.02 0.02 0.00 0.01 0.03 0.01 0.01 0.00 0.14 0.00 0.02 0.00 0.04 0.00 0.07 
##  445  450  451  458  461  475  476  477  479  996  998 
## 0.01 0.00 0.01 0.00 0.01 0.01 0.00 0.01 0.00 0.00 0.03
round(prop.table(table(youth2010$s23ca)),2)
## 
##  121  125  126  129  130  131  134  140  142  146  151  152  155  158  159  160 
## 0.01 0.02 0.00 0.00 0.01 0.00 0.01 0.00 0.01 0.00 0.01 0.07 0.00 0.00 0.01 0.12 
##  161  163  164  165  166  169  170  223  227  262  289  326  327  332  361  423 
## 0.00 0.04 0.01 0.00 0.11 0.00 0.02 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.04 
##  425  430  432  434  436  437  438  439  444  450  451  461  462  470  475  476 
## 0.00 0.01 0.17 0.00 0.01 0.00 0.05 0.01 0.08 0.00 0.00 0.00 0.00 0.01 0.00 0.01 
##  477  996  998 
## 0.01 0.00 0.04
# Compare precise migration background
prop.table(table(youth2010$migback[youth2010$migback != "none"]))
## 
##       both     father     mother       self 
## 0.17886179 0.33333333 0.09485095 0.39295393
prop.table(table(youth2023$migback[youth2023$migback != "none"]))
## 
##      both    father    mother      self 
## 0.2500000 0.2277397 0.1438356 0.3784247
prop.table(table(youth2023$staat_geb_person)) # self
## 
##          1          2 
## 0.92068849 0.07931151
prop.table(table(youth2010$s23ad, useNA = "always"))
## 
##          1       <NA> 
## 0.93031937 0.06968063
prop.table(table(youth2023$staat_geb_vater)) # father
## 
##         1         2 
## 0.8334473 0.1665527
prop.table(table(youth2010$s23bd, useNA = "always"))
## 
##         1      <NA> 
## 0.8419743 0.1580257
prop.table(table(youth2023$staat_geb_mutter)) # mother
## 
##         1         2 
## 0.8511566 0.1488434
prop.table(table(youth2010$s23cd, useNA = "always"))
## 
##        1     <NA> 
## 0.886769 0.113231

A brief look at countries of origin in 2010

This is to check how many students with a Vietnamese migration background are in our 2010 data, reported in the discussion section.

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# self born in Germany? if no, which country
table(youth2010$s23aa)
## 
## 121 126 129 134 137 140 142 146 150 151 152 158 160 163 166 169 170 221 245 326 
##   2   3   1   1   1   1   3   1   1   1   2   1  24   8  21   1   2   1   1   1 
## 335 348 351 361 368 386 423 425 430 432 434 436 437 438 439 444 450 451 461 475 
##   1   1   2   1   1   1   7   1   2   8   1   1   1  12   1  21   1   1   1   1 
## 476 477 536 998 
##   1   3   1   2
# father born in Germany? if no, which country
table(youth2010$s23ba)
## 
##   0 121 122 125 126 129 130 131 132 134 137 140 142 144 148 150 151 152 153 154 
##   4   3   1   3   1   1   1   1   1   1   6   1   3   2   1   1   2  10   4   1 
## 157 158 159 160 161 163 165 166 168 169 170 195 221 223 225 238 248 252 254 262 
##   2   1   2  31   1  20   4  17   2   1   2   1   9   2   1   1   1   4   7   1 
## 269 276 282 285 287 289 326 332 348 351 361 367 368 423 425 430 431 432 434 436 
##   1   1   1   2   3   1   1   2   1   6   5   1   3   8   3   2   1  46   1   6 
## 437 438 439 444 445 450 451 458 461 475 476 477 479 996 998 
##   1  13   1  21   2   1   2   1   2   4   1   3   1   1  11
# mother born in Germany? if no, which country
table(youth2010$s23ca)
## 
## 121 125 126 129 130 131 134 140 142 146 151 152 155 158 159 160 161 163 164 165 
##   2   5   1   1   2   1   2   1   3   1   2  16   1   1   2  29   1  10   2   1 
## 166 169 170 223 227 262 289 326 327 332 361 423 425 430 432 434 436 437 438 439 
##  25   1   4   1   1   1   1   1   1   1   1  10   1   3  40   1   3   1  12   2 
## 444 450 451 461 462 470 475 476 477 996 998 
##  18   1   1   1   1   2   1   2   2   1  10
unique_countries <- unique(c(youth2010$s23aa,
                             youth2010$s23ba,
                             youth2010$s23ca))
unique_countries <- unique_countries[!is.na(unique_countries)]
unique_countries <- sort(unique_countries)

key_table <- data.frame(matrix(NA, ncol = 2,
                               nrow = length(unique_countries)))
names(key_table) <- c("code", "country")
key_table$code <- unique_countries

key_table$n <- NA
for (i in 1:nrow(key_table)) {
  key_table$n[i] <- sum(youth2010$s23aa == key_table$code[i], na.rm = TRUE) +
    sum(youth2010$s23ba == key_table$code[i], na.rm = TRUE) +
    sum(youth2010$s23ca == key_table$code[i], na.rm = TRUE)
}

key_table$country[key_table$n < 5] <- "small"

key_table$code[is.na(key_table$country)]
##  [1] 121 125 126 137 142 151 152 160 163 165 166 170 221 254 351 361 423 425 430
## [20] 432 436 438 444 475 477 998
key_table$country[key_table$code == "121"] <- "Albania"
key_table$country[key_table$code == "125"] <- "Bulgaria"
key_table$country[key_table$code == "126"] <- "Denmark"
key_table$country[key_table$code == "137"] <- "Italy"
key_table$country[key_table$code == "142"] <- "Lithuania"
key_table$country[key_table$code == "151"] <- "Austria"
key_table$country[key_table$code == "152"] <- "Poland"
key_table$country[key_table$code == "160"] <- "Russia"

key_table$country[key_table$code == "163"] <- "Turkey"
key_table$country[key_table$code == "165"] <- "Hungary"
key_table$country[key_table$code == "166"] <- "Ukraine"
key_table$country[key_table$code == "170"] <- "Serbia"

key_table$country[key_table$code == "221"] <- "Algeria"
key_table$country[key_table$code == "254"] <- "Mozambique"
key_table$country[key_table$code == "351"] <- "Cuba"
key_table$country[key_table$code == "361"] <- "Peru"
key_table$country[key_table$code == "423"] <- "Afghanistan"
key_table$country[key_table$code == "425"] <- "Azerbaijan"
key_table$country[key_table$code == "430"] <- "Georgia"
key_table$country[key_table$code == "432"] <- "Vietnam"
key_table$country[key_table$code == "436"] <- "India"
key_table$country[key_table$code == "438"] <- "Iraq"
key_table$country[key_table$code == "444"] <- "Kazakhstan"
key_table$country[key_table$code == "475"] <- "Syria"
key_table$country[key_table$code == "477"] <- "Usbekistan"
key_table$country[key_table$code == "998"] <- "small"


youth2010 <- youth2010 %>%
  left_join(key_table, by = c("s23aa" = "code")) %>%
  rename(s23aa_country = country) %>%
  left_join(key_table, by = c("s23ba" = "code")) %>%
  rename(s23ba_country = country) %>%
  left_join(key_table, by = c("s23ca" = "code")) %>%
  rename(s23ca_country = country)


youth2010$s23aa_country[youth2010$migback == "none"] <- "Germany"
youth2010$s23ba_country[youth2010$migback == "none"] <- "Germany"
youth2010$s23ca_country[youth2010$migback == "none"] <- "Germany"

youth2010 <- youth2010 %>%
  mutate(s23aa_country = ifelse(s23aa_country %in% names(which(table(s23aa_country) < 5)), 
                                "small", 
                                s23aa_country))
youth2010 <- youth2010 %>%
  mutate(s23ba_country = ifelse(s23ba_country %in% names(which(table(s23ba_country) < 5)), 
                                "small", 
                                s23ba_country))
youth2010 <- youth2010 %>%
  mutate(s23ca_country = ifelse(s23ca_country %in% names(which(table(s23ca_country) < 5)), 
                                "small", 
                                s23ca_country))

table(youth2010$s23aa_country)
## 
## Afghanistan     Germany        Iraq  Kazakhstan      Russia       small 
##           7        1983          12          21          24          44 
##      Turkey     Ukraine     Vietnam 
##           8          21           8
table(youth2010$s23ba_country)
## 
## Afghanistan     Algeria        Cuba     Germany       India        Iraq 
##           8           9           6        1983           6          13 
##       Italy  Kazakhstan  Mozambique        Peru      Poland      Russia 
##           6          21           7           5          10          31 
##       small      Turkey     Ukraine     Vietnam 
##         113          20          17          46
table(youth2010$s23ca_country)
## 
## Afghanistan    Bulgaria     Germany        Iraq  Kazakhstan      Poland 
##          10           5        1983          12          18          16 
##      Russia       small      Turkey     Ukraine     Vietnam 
##          29          70          10          25          40