suppressPackageStartupMessages(library(tidyverse))
d <- read_csv("data/democracy.csv", show_col_types=FALSE) |> rename_with(tolower) |>
  mutate(ln_gdpw = log(gdpw))
maxT <- max(table(d$country))
keep_id <- d |> group_by(country) |>
  summarise(n_obs = n(), n_complete = sum(!is.na(ln_gdpw)), name = first(ctyname),
            .groups="drop") |>
  filter(n_obs == maxT, n_complete == maxT)
cat("Balanced country names (sample):\n")
print(head(keep_id$name, 20))
cat("\nIs USA in balanced? "); print("USA" %in% keep_id$name)
cat("Is INDIA? "); print("INDIA" %in% keep_id$name)
cat("Is ARGENTINA? "); print("ARGENTINA" %in% keep_id$name)
cat("Is ZIMBABWE? "); print("ZIMBABWE" %in% keep_id$name)
# always-dictatorship candidates (reg == 1 throughout, sample ends in 1990)
ad <- d |> group_by(country, ctyname) |>
  summarise(always_dict = all(reg == 1, na.rm = TRUE), n=n(), last_year=max(year),
            .groups="drop") |>
  filter(always_dict, n >= 25)
cat("\nAlways-dictatorship candidates with >=25 obs:\n")
print(head(ad, 12))
