Assignment 10B

Author

Daanish Rasheed

Nobel Prize API

Approach

I will use both Nobel Prize APIs to pull laureate and prize data as JSON into R, looping through the paginated results until every record is collected. The data will load directly from the API URLs, and I will flatten the nested JSON into two tidy data frames, one row per laureate-prize and one row per prize, pulling out fields like birth country, gender, category, and affiliation along the way.

I plan to ask four questions. Each question will include the question, the code, and the answer as a table or plot. I expect to handle organizations with no birth or gender data, incomplete birth dates, and years when no prize was awarded explicitly.

Code Base

library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.6.1
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(jsonlite)

Attaching package: 'jsonlite'

The following object is masked from 'package:purrr':

    flatten
base_url <- "https://api.nobelprize.org/2.1"

fetch_all <- function(endpoint, key, page_size = 100) {
  out <- list()
  offset <- 0
  repeat {
    url <- sprintf("%s/%s?offset=%d&limit=%d", base_url, endpoint, offset, page_size)
    res <- fromJSON(url, simplifyVector = FALSE)
    batch <- res[[key]]
    if (length(batch) == 0) break
    out <- c(out, batch)
    offset <- offset + page_size
    if (!is.null(res$meta$count) && offset >= res$meta$count) break
  }
  out
}

laureates_raw <- fetch_all("laureates", "laureates")
prizes_raw    <- fetch_all("nobelPrizes", "nobelPrizes")
length(laureates_raw)
[1] 1028
length(prizes_raw)
[1] 688
laureates <- tibble(raw = laureates_raw) |>
  hoist(raw,
        id            = "id",
        name          = c("knownName", "en"),
        org_name      = c("orgName", "en"),
        gender        = "gender",
        birth_date    = c("birth", "date"),
        birth_country = c("birth", "place", "countryNow", "en"),
        prizes        = "nobelPrizes") |>
  select(-raw)

laureates
# A tibble: 1,028 × 7
   id    name              org_name gender birth_date birth_country prizes    
   <chr> <chr>             <chr>    <chr>  <chr>      <chr>         <list>    
 1 745   A. Michael Spence <NA>     male   1943-11-07 USA           <list [1]>
 2 102   Aage N. Bohr      <NA>     male   1922-06-19 Denmark       <list [1]>
 3 779   Aaron Ciechanover <NA>     male   1947-10-01 Israel        <list [1]>
 4 259   Aaron Klug        <NA>     male   1926-08-11 Lithuania     <list [1]>
 5 1004  Abdulrazak Gurnah <NA>     male   1948-00-00 <NA>          <list [1]>
 6 114   Abdus Salam       <NA>     male   1926-01-29 Pakistan      <list [1]>
 7 982   Abhijit Banerjee  <NA>     male   1961-02-21 India         <list [1]>
 8 981   Abiy Ahmed Ali    <NA>     male   1976-08-15 Ethiopia      <list [1]>
 9 843   Ada E. Yonath     <NA>     female 1939-06-22 Israel        <list [1]>
10 866   Adam G. Riess     <NA>     male   1969-12-16 USA           <list [1]>
# ℹ 1,018 more rows
laureate_prizes <- laureates |>
  unnest_longer(prizes) |>
  hoist(prizes,
        award_year = "awardYear",
        category = c("category", "en"),
        aff_name = list("affiliations", 1, "name", "en"),
        aff_country = list("affiliations", 1, "countryNow", "en")) |>
  mutate(
    award_year = as.integer(award_year),
    birth_year = as.integer(str_sub(birth_date, 1, 4)),
    age_at_award = award_year - birth_year,
    decade = floor(award_year / 10) * 10
  ) |>
  select(-prizes)

laureate_prizes
# A tibble: 1,036 × 13
   id    name       org_name gender birth_date birth_country award_year category
   <chr> <chr>      <chr>    <chr>  <chr>      <chr>              <int> <chr>   
 1 745   A. Michae… <NA>     male   1943-11-07 USA                 2001 Economi…
 2 102   Aage N. B… <NA>     male   1922-06-19 Denmark             1975 Physics 
 3 779   Aaron Cie… <NA>     male   1947-10-01 Israel              2004 Chemist…
 4 259   Aaron Klug <NA>     male   1926-08-11 Lithuania           1982 Chemist…
 5 1004  Abdulraza… <NA>     male   1948-00-00 <NA>                2021 Literat…
 6 114   Abdus Sal… <NA>     male   1926-01-29 Pakistan            1979 Physics 
 7 982   Abhijit B… <NA>     male   1961-02-21 India               2019 Economi…
 8 981   Abiy Ahme… <NA>     male   1976-08-15 Ethiopia            2019 Peace   
 9 843   Ada E. Yo… <NA>     female 1939-06-22 Israel              2009 Chemist…
10 866   Adam G. R… <NA>     male   1969-12-16 USA                 2011 Physics 
# ℹ 1,026 more rows
# ℹ 5 more variables: aff_name <chr>, aff_country <chr>, birth_year <int>,
#   age_at_award <int>, decade <dbl>
prizes <- tibble(raw = prizes_raw) |>
  hoist(raw,
        award_year = "awardYear",
        category   = c("category", "en"),
        laureates  = "laureates") |>
  mutate(
    award_year = as.integer(award_year),
    decade = floor(award_year / 10) * 10,
    n_laureates = lengths(laureates)   # 0 when a prize was not awarded
  ) |>
  select(-raw, -laureates)

prizes
# A tibble: 688 × 4
   award_year category               decade n_laureates
        <int> <chr>                   <dbl>       <int>
 1       1901 Chemistry                1900           1
 2       1901 Literature               1900           1
 3       1901 Peace                    1900           2
 4       1901 Physics                  1900           1
 5       1901 Physiology or Medicine   1900           1
 6       1902 Chemistry                1900           1
 7       1902 Literature               1900           1
 8       1902 Peace                    1900           2
 9       1902 Physics                  1900           2
10       1902 Physiology or Medicine   1900           1
# ℹ 678 more rows

Question #1: For each decade, what percentage of individual Nobel Prize awards went to women?

women_by_decade <- laureate_prizes |>
  filter(!is.na(gender)) |>
  group_by(decade) |>
  summarise(awards = n(),
            women = sum(gender == "female"),
            pct_women = round(100 * women / awards, 1),
            .groups = "drop")

women_by_decade
# A tibble: 13 × 4
   decade awards women pct_women
    <dbl>  <int> <int>     <dbl>
 1   1900     56     3       5.4
 2   1910     38     1       2.6
 3   1920     54     2       3.7
 4   1930     55     3       5.5
 5   1940     40     3       7.5
 6   1950     71     0       0  
 7   1960     75     3       4  
 8   1970    103     4       3.9
 9   1980     94     4       4.3
10   1990    101     7       6.9
11   2000    119    11       9.2
12   2010    117    13      11.1
13   2020     82    16      19.5
ggplot(women_by_decade, aes(decade, pct_women)) +
  geom_col(fill = "steelblue") +
  labs(title = "Share of Nobel awards going to women, by decade",
       x = "Decade", y = "% of individual awards")

Question #2: What is the median age at which laureates receive each prize, and how does that compare before and after 1970?

age_summary <- laureate_prizes |>
  filter(!is.na(age_at_award)) |>
  mutate(era = if_else(award_year < 1970, "before_1970", "1970_onward")) |>
  group_by(category, era) |>
  summarise(median_age = median(age_at_award), n = n(), .groups = "drop") |>
  pivot_wider(names_from = era, values_from = c(median_age, n)) |>
  mutate(change_in_median = median_age_1970_onward - median_age_before_1970) |>
  arrange(desc(change_in_median))

age_summary
# A tibble: 6 × 6
  category           median_age_1970_onward median_age_before_1970 n_1970_onward
  <chr>                               <dbl>                  <dbl>         <int>
1 Physics                              64                     47             141
2 Chemistry                            63                     50.5           124
3 Physiology or Med…                   63                     54             134
4 Literature                           69.5                   61              58
5 Economic Sciences                    67                     70              99
6 Peace                                58.5                   64              60
# ℹ 2 more variables: n_before_1970 <int>, change_in_median <dbl>
laureate_prizes |>
  filter(!is.na(age_at_award)) |>
  ggplot(aes(reorder(category, age_at_award, median), age_at_award)) +
  geom_boxplot(fill = "grey85") +
  coord_flip() +
  labs(title = "Age at award by category", x = NULL, y = "Age (years)")

Question #3: What is the average number of laureates per prize in each category and decade?

sharing <- prizes |>
  filter(n_laureates > 0) |>
  group_by(category, decade) |>
  summarise(avg_laureates = round(mean(n_laureates), 2), .groups = "drop")

sharing |>
  pivot_wider(names_from = category, values_from = avg_laureates)
# A tibble: 13 × 7
   decade Chemistry `Economic Sciences` Literature Peace Physics
    <dbl>     <dbl>               <dbl>      <dbl> <dbl>   <dbl>
 1   1900      1                  NA          1.11  1.56    1.44
 2   1910      1.14               NA          1.12  1.17    1.11
 3   1920      1.11               NA          1     1.57    1.2 
 4   1930      1.44               NA          1     1.12    1.38
 5   1940      1.29               NA          1     1.4     1   
 6   1950      1.4                NA          1     1       2   
 7   1960      1.5                 2          1.1   1.12    1.7 
 8   1970      1.5                 1.5        1.1   1.44    2.5 
 9   1980      2.1                 1          1     1.1     2.2 
10   1990      1.8                 1.7        1     1.7     2.2 
11   2000      2.5                 2          1     1.4     2.8 
12   2010      2.7                 2          1     1.4     2.6 
13   2020      2.57                2.43       1     1.43    2.57
# ℹ 1 more variable: `Physiology or Medicine` <dbl>
ggplot(sharing, aes(decade, avg_laureates, color = category)) +
  geom_line() +
  geom_point() +
  labs(title = "Average laureates per prize, by decade",
       x = "Decade", y = "Avg laureates per prize", color = NULL)

Question #4: Comparing each laureate’s birth country to the country of their first listed affiliation at the time of the award, which countries lost the most laureates, which gained the most, and what are the biggest flows?

moves <- laureate_prizes |>
  filter(!is.na(birth_country), !is.na(aff_country)) |>
  mutate(moved = birth_country != aff_country)

mean(moves$moved)
[1] 0.3206307
lost <- moves |> filter(moved) |> count(birth_country, name = "lost")
gained <- moves |> filter(moved) |> count(aff_country, name = "gained")

net_flow <- full_join(lost, gained, by = c("birth_country" = "aff_country")) |>
  rename(country = birth_country) |>
  replace_na(list(lost = 0, gained = 0)) |>
  mutate(net = gained - lost) |>
  arrange(net)

net_flow
# A tibble: 57 × 4
   country          lost gained   net
   <chr>           <int>  <int> <int>
 1 Poland             19      0   -19
 2 Canada             15      6    -9
 3 Scotland            9      0    -9
 4 Austria            10      2    -8
 5 Hungary             8      0    -8
 6 Russia             11      3    -8
 7 the Netherlands     9      1    -8
 8 China               7      0    -7
 9 Japan               7      0    -7
10 Italy               8      2    -6
# ℹ 47 more rows
bind_rows(head(net_flow, 5), tail(net_flow, 5))
# A tibble: 10 × 4
   country         lost gained   net
   <chr>          <int>  <int> <int>
 1 Poland            19      0   -19
 2 Canada            15      6    -9
 3 Scotland           9      0    -9
 4 Austria           10      2    -8
 5 Hungary            8      0    -8
 6 Sweden             3      4     1
 7 Denmark            0      2     2
 8 Switzerland        3     12     9
 9 United Kingdom    24     35    11
10 USA                4    134   130
net_flow |>
  slice(c(1:8, (n() - 7):n())) |>
  ggplot(aes(reorder(country, net), net, fill = net > 0)) +
  geom_col(show.legend = FALSE) +
  coord_flip() +
  labs(title = "Net laureate gain/loss by country",
       subtitle = "Awarded while affiliated with the country minus born there",
       x = NULL, y = "Net laureates")

Conclusion

The 4 questions show many ways the Nobel Prize has evolved since 1901. The proportion of women individually winning in the first part of the century was very low. This went from 0% in the 1950s to around 7.5% in the 1940s up until the 1980s. However, more recently it has increased to 11.1% in the 2010s and 19.5% in the 2020s so far. It is therefore still far from equal. In the sciences the average age has significantly increased. For example Physics went from 47 to 64, Chemistry from 50.5 to 63 and Physiology/Medicine from 54 to 63. However, Peace decreased slightly to 58.5 from 64 and Economic Sciences decreased to 67 from 70. There are also more people winning awards. In Physics there have been on average 1.4 winners per prize in the 1900s, but now there are around 2.6 winners per prize in the 2020s. Similarly, Chemistry has gone from 1.0 to 2.6. However, Literature is still only 1 winner.

Around 32% of laureates with an association were awarded in the country they were not born in. The country with the biggest gain was the USA, increasing by +130. Next were the United Kingdom by +11 and Switzerland by +9. The country with the biggest loss was Poland decreasing by -19, then Canada and Scotland both decreased by -9. This is only due to the country of association being a proxy variable. Scotland is also a separate country meaning that these figures are not exact. This indicates trends in who has been acknowledged.