Code
library(dplyr)
library(ggplot2)
library(here)
library(knitr)
library(readr)
library(stringr)Reproducible cleaning and cautious cross-country comparison
Miguelangel Palma Rojas
November 15, 2026
This historical data-quality exercise shows the complete analysis directly in the notebook. It reads a preserved snapshot, validates its schema, summarizes numeric fields, ranks reported testing coverage, and compares Canada with Colombia.
Countries reported at different dates and used different testing and case definitions. This is not a current public-health dashboard.
covid <- read_csv(
here("data", "covid_testing_snapshot.csv"),
show_col_types = FALSE
)
required_columns <- c(
"country",
"report_date",
"tested",
"confirmed",
"tested_population_pct",
"confirmed_population_pct"
)
missing_columns <- setdiff(required_columns, names(covid))
if (length(missing_columns) > 0) {
stop(
"COVID snapshot is missing columns: ",
paste(missing_columns, collapse = ", ")
)
}
stopifnot(
nrow(covid) >= 50,
anyDuplicated(covid$country) == 0,
!any(covid$tested < 0, na.rm = TRUE),
!any(covid$confirmed < 0, na.rm = TRUE)
)
covid |>
summarise(
countries = n(),
first_report = min(report_date, na.rm = TRUE),
last_report = max(report_date, na.rm = TRUE),
missing_test_counts = sum(is.na(tested)),
missing_case_counts = sum(is.na(confirmed))
) |>
kable()| countries | first_report | last_report | missing_test_counts | missing_case_counts |
|---|---|---|---|---|
| 173 | .mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}body.skin-vector-2022 .mw-parser-output .reflist-columns-2{column-width:27em}body.skin-vector-2022 .mw-parser-output .reflist-columns-3{column-width:22.5em}.mw-parser-output .references[data-mw-group=upper-alpha]{list-style-type:upper-alpha}.mw-parser-output .references[data-mw-group=upper-roman]{list-style-type:upper-roman}.mw-parser-output .references[data-mw-group=lower-alpha]{list-style-type:lower-alpha}.mw-parser-output .references[data-mw-group=lower-greek]{list-style-type:lower-greek}.mw-parser-output .references[data-mw-group=lower-roman]{list-style-type:lower-roman}.mw-parser-output div.reflist-liststyle-upper-alpha .references{list-style-type:upper-alpha}.mw-parser-output div.reflist-liststyle-upper-roman .references{list-style-type:upper-roman}.mw-parser-output div.reflist-liststyle-lower-alpha .references{list-style-type:lower-alpha}.mw-parser-output div.reflist-liststyle-lower-greek .references{list-style-type:lower-greek}.mw-parser-output div.reflist-liststyle-lower-roman .references{list-style-type:lower-roman} ↑ Local time. ↑ For some countries it is unclear whether they report samples or cases. One person tested twice is recorded as one case and two samples. ↑ Excluding Taiwan. ↑ Excluding Northern Cyprus. ↑ Excluding Greenland and the Faroe Islands. ↑ Excluding Overseas France. ↑ Testing data from 4 May to 12 May is missing because of the transition to the new reporting system SI-DEP. ↑ Excluding Abkhazia and South Ossetia. ↑ Data for residents only. ↑ Excluding Transnistria. ↑ Northern Cyprus is not recognized as a sovereign state by any country except Turkey. ↑ Includes data for Liechtenstein. ↑ Not a United Nations member. | 9 Sep 2022 | 1 | 1 |
The validated snapshot contains 173 distinct country or regional records.
| tested | confirmed | tested_population_pct | confirmed_population_pct | |
|---|---|---|---|---|
| Min. : 3880 | Min. : 0 | Min. : 0.006 | Min. : 0.000 | |
| 1st Qu.: 512037 | 1st Qu.: 37839 | 1st Qu.: 9.475 | 1st Qu.: 0.425 | |
| Median : 3029859 | Median : 281196 | Median : 46.950 | Median : 6.100 | |
| Mean : 31377219 | Mean : 2508340 | Mean : 175.504 | Mean :12.769 | |
| 3rd Qu.: 12386725 | 3rd Qu.: 1278105 | 3rd Qu.: 156.500 | 3rd Qu.:16.250 | |
| Max. :929349291 | Max. :90749469 | Max. :3223.000 | Max. :74.400 | |
| NA’s :1 | NA’s :1 | NA’s :1 | NA’s :1 |
Coverage above 100% is possible because this measure counts tests rather than unique people.
coverage_data <- covid |>
filter(is.finite(tested_population_pct)) |>
slice_max(
order_by = tested_population_pct,
n = 15,
with_ties = FALSE
) |>
mutate(country = reorder(country, tested_population_pct))
coverage_data |>
select(country, tested_population_pct) |>
arrange(desc(tested_population_pct)) |>
kable(
col.names = c("Country", "Tests / population (%)"),
digits = 2
)| Country | Tests / population (%) |
|---|---|
| Cyprus[d] | 3223 |
| Austria | 2312 |
| Northern Cyprus[k] | 2177 |
| United Arab Emirates | 2070 |
| Faroe Islands | 1493 |
| Denmark[e] | 1162 |
| Greece | 943 |
| United Kingdom | 774 |
| Luxembourg[i] | 679 |
| Bahrain | 674 |
| San Marino | 563 |
| Iceland | 546 |
| Israel | 451 |
| Italy | 446 |
| France[f][g] | 417 |
ggplot(
coverage_data,
aes(x = tested_population_pct, y = country)
) +
geom_col(fill = "#2A9D8F") +
labs(
title = "Countries with the highest reported testing coverage",
x = "Tests as % of population",
y = NULL
) +
theme_minimal(base_size = 12) +
theme(
plot.title = element_text(face = "bold"),
panel.grid.major.y = element_blank()
)
country_comparison <- covid |>
filter(country %in% c("Canada", "Colombia")) |>
select(
country,
report_date,
tested,
confirmed,
tested_population_pct,
confirmed_population_pct
) |>
mutate(
country = factor(
country,
levels = c("Canada", "Colombia")
)
) |>
arrange(country)
country_comparison |>
kable(
col.names = c(
"Country",
"Report date",
"Tests",
"Confirmed cases",
"Tests / population (%)",
"Confirmed / population (%)"
),
format.args = list(big.mark = ",")
)| Country | Report date | Tests | Confirmed cases | Tests / population (%) | Confirmed / population (%) |
|---|---|---|---|---|---|
| Canada | 26 Nov 2022 | 66,343,123 | 4,423,053 | 175.0 | 11.7 |
| Colombia | 24 Nov 2022 | 36,875,818 | 6,314,769 | 76.4 | 13.1 |
Different report dates, eligibility rules, repeat testing, and surveillance capacity prevent a causal ranking.
data-raw/prepare-data.R retrieves the source and detects the testing table from its column schema instead of relying on table order. The published notebook intentionally reads the dated snapshot so future website changes cannot alter historical results.
See the Spanish version.
---
title: "Global COVID-19 Testing Data"
subtitle: "Reproducible cleaning and cautious cross-country comparison"
author: "Miguelangel Palma Rojas"
date: last-modified
lang: en
---
## Purpose
This historical data-quality exercise shows the complete analysis directly in the notebook. It reads a preserved snapshot, validates its schema, summarizes numeric fields, ranks reported testing coverage, and compares Canada with Colombia.
> Countries reported at different dates and used different testing and case definitions. This is not a current public-health dashboard.
## Load packages
```{r}
#| label: packages
#| message: false
#| warning: false
library(dplyr)
library(ggplot2)
library(here)
library(knitr)
library(readr)
library(stringr)
```
## Read and validate the snapshot
```{r}
#| label: read-data
covid <- read_csv(
here("data", "covid_testing_snapshot.csv"),
show_col_types = FALSE
)
required_columns <- c(
"country",
"report_date",
"tested",
"confirmed",
"tested_population_pct",
"confirmed_population_pct"
)
missing_columns <- setdiff(required_columns, names(covid))
if (length(missing_columns) > 0) {
stop(
"COVID snapshot is missing columns: ",
paste(missing_columns, collapse = ", ")
)
}
stopifnot(
nrow(covid) >= 50,
anyDuplicated(covid$country) == 0,
!any(covid$tested < 0, na.rm = TRUE),
!any(covid$confirmed < 0, na.rm = TRUE)
)
covid |>
summarise(
countries = n(),
first_report = min(report_date, na.rm = TRUE),
last_report = max(report_date, na.rm = TRUE),
missing_test_counts = sum(is.na(tested)),
missing_case_counts = sum(is.na(confirmed))
) |>
kable()
```
The validated snapshot contains `r nrow(covid)` distinct country or regional records.
## Inspect numeric distributions
```{r}
#| label: numeric-summary
covid |>
select(
tested,
confirmed,
tested_population_pct,
confirmed_population_pct
) |>
summary() |>
kable()
```
## Reported testing coverage
Coverage above 100% is possible because this measure counts tests rather than unique people.
```{r}
#| label: coverage-data
coverage_data <- covid |>
filter(is.finite(tested_population_pct)) |>
slice_max(
order_by = tested_population_pct,
n = 15,
with_ties = FALSE
) |>
mutate(country = reorder(country, tested_population_pct))
coverage_data |>
select(country, tested_population_pct) |>
arrange(desc(tested_population_pct)) |>
kable(
col.names = c("Country", "Tests / population (%)"),
digits = 2
)
```
```{r}
#| label: coverage-chart
#| fig-alt: "Horizontal bars showing the countries with the highest reported tests as a percentage of population."
ggplot(
coverage_data,
aes(x = tested_population_pct, y = country)
) +
geom_col(fill = "#2A9D8F") +
labs(
title = "Countries with the highest reported testing coverage",
x = "Tests as % of population",
y = NULL
) +
theme_minimal(base_size = 12) +
theme(
plot.title = element_text(face = "bold"),
panel.grid.major.y = element_blank()
)
```
## Canada and Colombia
```{r}
#| label: country-comparison
country_comparison <- covid |>
filter(country %in% c("Canada", "Colombia")) |>
select(
country,
report_date,
tested,
confirmed,
tested_population_pct,
confirmed_population_pct
) |>
mutate(
country = factor(
country,
levels = c("Canada", "Colombia")
)
) |>
arrange(country)
country_comparison |>
kable(
col.names = c(
"Country",
"Report date",
"Tests",
"Confirmed cases",
"Tests / population (%)",
"Confirmed / population (%)"
),
format.args = list(big.mark = ",")
)
```
Different report dates, eligibility rules, repeat testing, and surveillance capacity prevent a causal ranking.
## Reproducible update
`data-raw/prepare-data.R` retrieves the source and detects the testing table from its column schema instead of relying on table order. The published notebook intentionally reads the dated snapshot so future website changes cannot alter historical results.
## Conclusion
- The validated snapshot contains `r nrow(covid)` country or regional records, with only `r sum(is.na(covid$tested))` missing testing count and `r sum(is.na(covid$confirmed))` missing confirmed-case count.
- `r str_remove(as.character(coverage_data$country[[1]]), "\\[.*\\]")` has the highest reported testing coverage at `r format(coverage_data$tested_population_pct[[1]], big.mark = ",")`% of population. Values far above 100% confirm that the numerator counts tests, including repeated tests, rather than unique people.
- Canada reports testing equivalent to `r country_comparison$tested_population_pct[[1]]`% of population and confirmed cases equal to `r country_comparison$confirmed_population_pct[[1]]`%; Colombia reports `r country_comparison$tested_population_pct[[2]]`% and `r country_comparison$confirmed_population_pct[[2]]`%, respectively.
- The higher confirmed-population percentage in Colombia despite lower reported testing coverage cannot be interpreted as a causal testing effect because report dates, eligibility, repeat testing, surveillance intensity, and epidemic timing are not standardized.
See the [Spanish version](es/index.qmd).