knitr::opts_chunk$set( error = FALSE, warning = FALSE, message = FALSE, collapse = TRUE, comment = "#>" )
library(oxcgrt) library(magrittr)
The calculate_*
functions are based on the OxCGRT's methodology described here. There are two sets of calculate functions included in oxcgrt
. The first calculates the OxCGRT sub-indices described in the table below:
tab <- codebook[!stringr::str_detect(string = codebook$Name, pattern = "_Flag"), ] %>% dplyr::mutate(Name = stringr::str_remove_all(string = Name, pattern = "[A-Z]{1}[0-9]{1}\\_")) knitr::kable(x = tab)
The second calculates the four OxCGRT indices which are composed of various combinations of the indicators described in the table above. These combinations are described in the table below:
library(rvest) library(xml2) x <- xml2::read_html("https://github.com/OxCGRT/covid-policy-tracker/blob/master/documentation/index_methodology.md") %>% rvest::xml_nodes(css = ".markdown-body table") %>% rvest::html_table() x <- data.frame(t(x[[1]])) tab <- x[3:nrow(x), 1:4] tab <- data.frame(row.names(tab), tab) row.names(tab) <- 1:nrow(tab) names(tab) <- c("ID", "Government response index", "Containment and health index", "Stringency index", "Economic support index") tab <- tab %>% dplyr::mutate(`Government response index` = ifelse(`Government response index` == "'x'", "x", `Government response index`), `Containment and health index` = ifelse(`Containment and health index` == "'x'", "x", `Containment and health index`)) tab <- codebook[!stringr::str_detect(string = codebook$Name, pattern = "_Flag"), c("ID", "Name")] %>% dplyr::mutate(Name = stringr::str_remove_all(string = Name, pattern = "[A-Z]{1}[0-9]{1}\\_")) %>% merge(tab, by = "ID") knitr::kable(x = tab)
The OxCGRT subindices can be calculated using the calculate_subindex
and calculate_subindices
functions. To calculate a specific sub-index, the following code is used:
## Given the C1 data in indicatorData, calculate C1 sub-index calculate_subindex(indicator_code = indicatorData[1, "indicator"], value = indicatorData[1, "value"], flag_value = indicatorData[1, "flag_value"])
This gives a C1 index value of:
## Given the C1 data in indicatorData, calculate C1 sub-index calculate_subindex(indicator_code = indicatorData[1, "indicator"], value = indicatorData[1, "value"], flag_value = indicatorData[1, "flag_value"])
To calculate all OxCGRT subindices, the following code is used:
## Given the indicatorData dataset, calculate all sub-indices indicatorData %>% calculate_subindices(indicator_code = "indicator", value = "value", flag_value = "flag_value", add = TRUE)
This results in the following output:
## Given the indicatorData dataset, calculate all sub-indices indicatorData %>% calculate_subindices(indicator_code = "indicator", value = "value", flag_value = "flag_value", add = TRUE)
It can be noted that the results of the calculations are added to the input data.frame under the column name score.1
. Comparing this with the value in the column named score
that is included in the indicatorData
dataset, the results are the same.
The OxCGRT indices can be calculated using the calculate_index
and calculate_indices
functions. To calculate a specific sub-index, the following code can be used:
indicatorData %>% calculate_subindices(indicator_code = "indicator", value = "value", flag_value = "flag_value", add = FALSE) %>% calculate_index(codes = c(paste("C", 1:8, sep = ""), paste("E", 1:2, sep = ""), paste("H", 1:3, sep = ""), "H6"), tolerance = 1)
This code calculates the government response index
which is:
indicatorData %>% calculate_subindices(indicator_code = "indicator", value = "value", flag_value = "flag_value", add = FALSE) %>% calculate_index(codes = c(paste("C", 1:8, sep = ""), paste("E", 1:2, sep = ""), paste("H", 1:3, sep = ""), "H6"), tolerance = 1)
The same result can be reached by using the specialised function calculate_gov_response
as follows:
indicatorData %>% calculate_subindices(indicator_code = "indicator", value = "value", flag_value = "flag_value", add = FALSE) %>% calculate_gov_response()
which results in the same value as the previous code:
indicatorData %>% calculate_subindices(indicator_code = "indicator", value = "value", flag_value = "flag_value", add = FALSE) %>% calculate_gov_response()
To calculate all four OxCGRT indices, the following code can be implemented:
indicatorData %>% calculate_subindices(indicator_code = "indicator", value = "value", flag_value = "flag_value", add = FALSE) %>% calculate_indices()
which outputs the following results:
indicatorData %>% calculate_subindices(indicator_code = "indicator", value = "value", flag_value = "flag_value", add = FALSE) %>% calculate_indices()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.