1. Rewrite the following code chunks using the %>% operator.
log(sd(c(5, 13, 89)))
# [1] 3.8
c(5, 13, 89) %>% sd() %>% log()
# [1] 3.8
as.numeric(scale(c(100, 32, 45)))
# [1]  1.14 -0.75 -0.39
c(100, 32, 45) %>% scale() %>% as.numeric()
# [1]  1.14 -0.75 -0.39
  1. In which among all ESS countries is perceived racial discrimination among persons of immigrant origin most prevalent?
    • That is, give a country-level summary of the percent of minority respondents who experienced discrimination explicitly based on their group's race.
ESS %>% # Use the ESS, then
  transmute( # Create new variables and only keep these new ones
    # Make the following variables factors:
    cntry = as_factor(cntry), 
    facntr = as_factor(facntr),
    mocntr = as_factor(mocntr),
    # Make the following variables numeric:
    pspwght = zap_labels(pspwght),
    # Experienced racial discrimination #<<
    # Turn vars into factors:
    dscrgrp = as_factor(dscrgrp), # Belonging to discriminated group.
    dscrrce = as_factor(dscrrce), # Discriminated because of race.
    racial_discr = case_when(
      dscrgrp == "Yes" & dscrrce == "Marked" ~ 1, # Only racial discrimination counts as 1 #<<
      dscrgrp == "No" | (dscrgrp == "Yes" & dscrrce != "Marked") ~ 0, # No, or all other forms of discr. count as 0 #<<
      TRUE ~ as.numeric(NA) # all others missing
    ) 
  ) %>% # then
  dplyr::filter( # Keep only respondents of immigrant origin,
    facntr == "No" & mocntr == "No") %>% # then
  group_by(cntry) %>% # Group data by country, then
  dplyr::summarize(
    # Weighted number of cases.
    wn = sum(pspwght, na.rm = TRUE), 
    # Weighted number of persons who see their group face racial discrimination.
    wn_rd = sum(racial_discr * pspwght, na.rm = TRUE), 
    # Weighted percent who see their group face racial discrimination.
    wperc_rd = (wn_rd / wn)  * 100) %>%
  arrange(desc(wperc_rd))
# # A tibble: 29 × 4
#    cntry             wn wn_rd wperc_rd
#    <fct>          <dbl> <dbl>    <dbl>
#  1 United Kingdom 378.  50.2     13.3 
#  2 France         333.  39.1     11.7 
#  3 Netherlands    198.  22.1     11.2 
#  4 Finland         73.8  7.37     9.98
#  5 Norway         147.  13.3      9.03
#  6 Sweden         298.  25.9      8.69
#  7 Belgium        356.  26.8      7.51
#  8 Portugal        88.1  5.99     6.81
#  9 Italy          269.  17.6      6.56
# 10 Denmark         95.8  5.44     5.67
# # … with 19 more rows
  1. In which among all ESS countries do people on average show the highest agreement to the statement that gay women and men should be allowed to adopt children?
    • That is, give country-level summaries of the average agreement plus its 95% confidence interval.
    • wtd.mean() from the Hmsic package estimates weighted means.
    • Moreover, wtd.var() estimates weighted variances, and remember: \(\text{SD} = \sqrt{\text{Var}}\).
    • Beware that I asked for the average agreement.
ESS %>% # Use the ESS, så
  transmute( # Create new variables and only keep these new ones
    # Make the following variables factors:
    cntry = as_factor(cntry), 
    # Make the following variables numeric:
    pspwght = zap_labels(pspwght),
    hmsacld = max(zap_labels(hmsacld), na.rm = TRUE) - zap_labels(hmsacld), #<<
  ) %>%
  group_by(cntry) %>% # Group data by country, så
  dplyr::summarize(
    n = sum(pspwght, na.rm = TRUE),
    mean_hmsacld = wtd.mean(hmsacld, weights = pspwght, na.rm = TRUE),
    sd_hmsacld = wtd.var(hmsacld, weights = pspwght, na.rm = TRUE) %>% sqrt(),
    se_hmsacld = sd_hmsacld / sqrt(n),
    min95 = mean_hmsacld - se_hmsacld * qt(p = 0.975, df = n),
    max95 = mean_hmsacld + se_hmsacld * qt(p = 0.975, df = n)) %>%
  arrange(desc(mean_hmsacld))
# # A tibble: 29 × 7
#    cntry              n mean_hmsacld sd_hmsacld se_hmsacld min95 max95
#    <fct>          <dbl>        <dbl>      <dbl>      <dbl> <dbl> <dbl>
#  1 Iceland         861.         3.45      0.943     0.0321  3.39  3.52
#  2 Netherlands    1673.         3.13      1.02      0.0250  3.08  3.18
#  3 Spain          1668.         3.09      1.10      0.0269  3.03  3.14
#  4 Sweden         1539.         3.07      1.10      0.0281  3.02  3.13
#  5 Norway         1406.         3.02      1.14      0.0305  2.96  3.08
#  6 Ireland        2216.         2.86      1.17      0.0248  2.81  2.91
#  7 United Kingdom 2204.         2.81      1.17      0.0249  2.76  2.86
#  8 Belgium        1767.         2.76      1.29      0.0308  2.70  2.82
#  9 Denmark        1572.         2.72      1.34      0.0339  2.66  2.79
# 10 Germany        2358.         2.66      1.27      0.0261  2.61  2.71
# # … with 19 more rows