Title: | Africa Macroeconomic Monitor Database API |
---|---|
Description: | An R API providing access to a relational database with macroeconomic data for Africa. The database contains >700 macroeconomic time series from mostly international sources, grouped into 50 macroeconomic and development-related topics. Series are carefully selected on the basis of data coverage for Africa, frequency, and relevance to the macro-development context. The project is part of the 'Kiel Institute Africa Initiative' <https://www.ifw-kiel.de/institute/initiatives/kiel-institute-africa-initiative/>, which, amongst other things, aims to develop a parsimonious database with highly relevant indicators to monitor macroeconomic developments in Africa, accessible through a fast API and a web-based platform at <https://africamonitor.ifw-kiel.de/>. The database is maintained at the Kiel Institute for the World Economy <https://www.ifw-kiel.de/>. |
Authors: | Sebastian Krantz [aut, cre] |
Maintainer: | Sebastian Krantz <[email protected]> |
License: | GPL-3 |
Version: | 0.2.4 |
Built: | 2024-10-29 04:31:26 UTC |
Source: | https://github.com/cran/africamonitor |
An R API providing access to a relational database with macroeconomic data for Africa. The database is maintained at the Kiel Institute for the World Economy.
Functions and data providing information about the available data
am_sources()
am_series()
am_countries
am_countries_wld
am_entities
Function to retrieve the data from the database
Functions to reshape data and add temporal identifiers
am_pivot_wider()
am_pivot_longer()
am_expand_date()
Helper functions to convert inputs to R dates
Global Macros with core ID variables in the database
The macro .AMID
contains the string c("ISO3", "Series")
denoting
the names of ID variables that identify the cross-sectional dimension in the database.
The macro .AMT
contains the string
c("Date", "Year", "Quarter", "FY", "QFY", "Month", "Day")
denoting temporal identifiers generated by am_expand_date
.
The "Date" variable is sufficient to uniquely identify a point in time in the database.
Each value in the database is uniquely identified by ISO3, Series and Date.
.AMID .AMT
.AMID .AMT
.AMID .AMT
.AMID .AMT
This function coerces date strings i.e. "YYYY-MM-DD"
or "YYYY-MM"
, years e.g. 2015
(numeric or character),
year-quarters e.g. "2015Q1"
or "2015-Q1"
, year-months e.g. "2015M01"
or "2015-M01"
, fiscal years e.g. "1997/98"
or numeric values representing dates (e.g. previously imported Excel date) to a regular R date.
am_as_date(x, end = FALSE, origin = "1899-12-30")
am_as_date(x, end = FALSE, origin = "1899-12-30")
x |
a character date string |
end |
logical. |
origin |
a date or date-string that can be used as reference for converting numeric values to dates. The default corresponds to dates generated in Excel for Windows. See |
A Date
vector.
am_as_date("2011-05") am_as_date(2011) am_as_date("2011/12") am_as_date("2011/12", end = TRUE) am_as_date("2011Q1") am_as_date("2011Q1", end = TRUE)
am_as_date("2011-05") am_as_date(2011) am_as_date("2011/12") am_as_date("2011/12", end = TRUE) am_as_date("2011Q1") am_as_date("2011Q1", end = TRUE)
am_countries
is a data.frame containing standardized codes of 55 African countries
(including Western Sahara) according to various classifications
and regional aggregations. am_countries_wld
provides the same information for 195
countries, which includes the 193 UN Member States, Western Sahara and Taiwan. The API
generally provides data for all 195 countries, but by default only requests data for Africa.
Note that the API (am_data
) only supports "ISO3" character codes.
am_countries am_countries_wld
am_countries am_countries_wld
A data frame with 55 (wld = 195) rows and 9 variables (sorted by Country):
Short Country Name
ISO Standardized Country Name
ISO 2-Character Country Code
ISO 3-Character Country Code
ISO Numeric Country Code
IMF Numeric Country Code
2-Region Classification (UN except for Sudan)
5-Region Classification (Former World Bank)
Main Official Currency
An object of class data.table
(inherits from data.frame
) with 195 rows and 9 columns.
countrycode R package (with some modification of regional aggregates).
am_entities
, am_sources
, am_series
, africamonitor
head(am_countries)
head(am_countries)
This is the main function of the package to retrieve data from the database.
am_data( ctry = africamonitor::am_countries$ISO3, series = NULL, from = NULL, to = NULL, labels = TRUE, wide = TRUE, expand.date = FALSE, drop.1iso3c = TRUE, ordered = TRUE, return.query = FALSE, ... )
am_data( ctry = africamonitor::am_countries$ISO3, series = NULL, from = NULL, to = NULL, labels = TRUE, wide = TRUE, expand.date = FALSE, drop.1iso3c = TRUE, ordered = TRUE, return.query = FALSE, ... )
ctry |
character. (Optional) the ISO3 code of countries (see |
series |
character. (Optional) codes of series matching the 'Series' column of the series table (retrieved using |
from |
set the start time of the data retrieved by either supplying a start date, a date-string of the form |
to |
same as |
labels |
logical. |
wide |
logical. |
expand.date |
logical. |
drop.1iso3c |
logical. If only one country is selected through |
ordered |
logical. |
return.query |
logical. |
... |
further arguments passed to |
If labels = FALSE
, the series table is not joined to the data table, and ordered = TRUE
will order series retrieved in alphabetic order.
If labels = TRUE
data is ordered by series and date, preserving the order of columns in the dataset. If multiple countries are received they are ordered alphabetically according to the 'ISO3' column.
Series at different frequencies can be queried, but, if wide = TRUE
, this will result in missing values for all but the first observations per period in the lower frequency series.
A data.table
with the result of the query.
am_pivot_wider
, am_expand_date
, africamonitor
# Return all indicators for Kenya from 2000 am_data("KEN", from = 2000) # Return all indicators for Kenya from 2000 in long format am_data("KEN", from = 2000, wide = FALSE) # Return with date expanded am_data("KEN", from = 2000, expand.date = TRUE) # Same thing in multiple steps (with additional customization options): am_data("KEN", from = 2000, wide = FALSE) |> am_pivot_wider() |> am_expand_date() # Getting only GDP growth am_data("KEN", "NGDP_RPCH", from = 2000) # Getting GDP growth for all countries am_data(series = "NGDP_RPCH", from = 2000) # Reshaping to wider format am_data(series = "NGDP_RPCH", from = 2000) |> am_pivot_wider(id_cols = "Date", names_from = "ISO3", values_from = "NGDP_RPCH") # Getting growth and inflation for the EAC countries (all available years) am_data(ctry = c("UGA", "KEN", "TZA", "RWA", "BDI", "SSD"), series = c("NGDP_RPCH", "PCPIPCH"))
# Return all indicators for Kenya from 2000 am_data("KEN", from = 2000) # Return all indicators for Kenya from 2000 in long format am_data("KEN", from = 2000, wide = FALSE) # Return with date expanded am_data("KEN", from = 2000, expand.date = TRUE) # Same thing in multiple steps (with additional customization options): am_data("KEN", from = 2000, wide = FALSE) |> am_pivot_wider() |> am_expand_date() # Getting only GDP growth am_data("KEN", "NGDP_RPCH", from = 2000) # Getting GDP growth for all countries am_data(series = "NGDP_RPCH", from = 2000) # Reshaping to wider format am_data(series = "NGDP_RPCH", from = 2000) |> am_pivot_wider(id_cols = "Date", names_from = "ISO3", values_from = "NGDP_RPCH") # Getting growth and inflation for the EAC countries (all available years) am_data(ctry = c("UGA", "KEN", "TZA", "RWA", "BDI", "SSD"), series = c("NGDP_RPCH", "PCPIPCH"))
A dataset mapping African countries to various economic and regional entities.
am_entities
am_entities
A data frame with 54 rows (one for each country, excluding Western Sahara) and 27 variables, of which 6 are country identifiers and the remaining 21 are logical variables indicating country membership to various economic and regional entities.
Own compilation.
am_countries
, am_sources
, am_series
, africamonitor
head(am_entities)
head(am_entities)
This function expands a date column and generates additional temporal identifiers from it (such as the year, month, quarter, fiscal year etc.).
am_expand_date( x, gen = c("Year", "Quarter", "Month"), origin = "1899-12-30", keep.date = TRUE, remove.missing.date = TRUE, sort = TRUE, as.factor = TRUE, name = "Date", ... )
am_expand_date( x, gen = c("Year", "Quarter", "Month"), origin = "1899-12-30", keep.date = TRUE, remove.missing.date = TRUE, sort = TRUE, as.factor = TRUE, name = "Date", ... )
x |
either a vector of class 'Date', or coercible to date using |
gen |
character. A vector of identifiers to generate from |
origin |
character / Date. Passed to |
keep.date |
logical. |
remove.missing.date |
logical. |
sort |
logical. |
as.factor |
|
name |
character. The name of the date variable to expand. |
... |
not used. |
A data.table
containing the computed identifiers as columns. See Examples.
# First a basic example x <- seq.Date(as.Date("1999-01-01"), as.Date("2000-01-01"), by = "month") am_expand_date(x) am_expand_date(x, gen = .AMT[-1L], keep.date = FALSE) # Now using the API am_expand_date(am_data("KEN")) # Same thing am_data("KEN", expand.date = TRUE)
# First a basic example x <- seq.Date(as.Date("1999-01-01"), as.Date("2000-01-01"), by = "month") am_expand_date(x) am_expand_date(x, gen = .AMT[-1L], keep.date = FALSE) # Now using the API am_expand_date(am_data("KEN")) # Same thing am_data("KEN", expand.date = TRUE)
This function automatically reshapes wide (column-based) data into a long format akin to the format of the raw data coming from the database (am_data(..., wide = FALSE)
).
It can also be used as a general purpose reshaping command - with an additional capability to handle variable labels.
am_pivot_longer( data, id_cols = intersect(c("ISO3", .AMT), names(data)), to_value = setdiff(names(data), id_cols), variable_name = "Series", value_name = "Value", label_name = "Label", na.rm = TRUE, variable.factor = TRUE, label.factor = TRUE, ... )
am_pivot_longer( data, id_cols = intersect(c("ISO3", .AMT), names(data)), to_value = setdiff(names(data), id_cols), variable_name = "Series", value_name = "Value", label_name = "Label", na.rm = TRUE, variable.factor = TRUE, label.factor = TRUE, ... )
data |
a wide format data frame where all series have their own column. |
id_cols |
character. Temporal identifiers of the data. By default all variables in |
to_value |
character. The names of all series to be stacked into the long format data frame. |
variable_name |
character. The name of the variable to store the names of the series. |
value_name |
character. The name of the variable to store the data values. |
label_name |
character. The name of the variable to store the series labels. |
na.rm |
logical. |
variable.factor , label.factor
|
logical. |
... |
further arguments passed to |
A data.table
with the reshaped data.
# Return all indicators for Kenya and Nigeria from the year 2000 onwards data <- am_data(c("KEN", "NGA"), from = 2000) am_pivot_longer(data)
# Return all indicators for Kenya and Nigeria from the year 2000 onwards data <- am_data(c("KEN", "NGA"), from = 2000) am_pivot_longer(data)
This function automatically reshapes long (stacked) raw data from the API (am_data(..., wide = FALSE)
) to a wide format where each variable has its own column.
It can also be used as a general purpose reshaping command - with an additional capability to handle variable labels.
am_pivot_wider( data, id_cols = intersect(c("ISO3", .AMT), names(data)), names_from = "Series", values_from = "Value", labels_from = if (any(names(data) == "Label")) "Label" else NULL, expand.date = FALSE, ... )
am_pivot_wider( data, id_cols = intersect(c("ISO3", .AMT), names(data)), names_from = "Series", values_from = "Value", labels_from = if (any(names(data) == "Label")) "Label" else NULL, expand.date = FALSE, ... )
data |
raw data from the API: A long format data frame where all values are stacked in a value column. |
id_cols |
character. Temporal identifiers of the data. By default all variables in |
names_from |
character. The column containing the series codes. These will become the names of the new columns in the wider data format. |
values_from |
character. The column containing the data values. |
labels_from |
character. The column containing the labels describing the series. |
expand.date |
logical. |
... |
further arguments passed to |
A data.table
with the reshaped data.
am_pivot_longer
, africamonitor
# Return all indicators for Kenya and Nigeria from the year 2000 onwards am_pivot_wider(am_data(c("KEN", "NGA"), from = 2000, wide = FALSE))
# Return all indicators for Kenya and Nigeria from the year 2000 onwards am_pivot_wider(am_data(c("KEN", "NGA"), from = 2000, wide = FALSE))
This function pulls information about the data series available in the database.
am_series( dsid = NULL, source.info = TRUE, ordered = TRUE, return.query = FALSE )
am_series( dsid = NULL, source.info = TRUE, ordered = TRUE, return.query = FALSE )
dsid |
character. (Optional) id's of datasources matching the 'DSID' column of the data sources table (retrieved using |
source.info |
logical. |
ordered |
logical. |
return.query |
logical. |
The series table gives information about all of the time series in the database. Each series is given a unique code, and
has a label describing the series. Further information recorded are the minimum and maximum time coverage, and (optionally) a separate series source and url.
The default source.info = TRUE
adds the source, the frequency of the data (homogeneous within source), and the date when the source was last updated.
A data.table
with information about the available series in the database.
am_countries
, am_sources
, am_data
, africamonitor
# By default returns all series with additional information am_series() # Raw series table am_series(source.info = FALSE) # Only series in the WEO am_series("IMF_WEO")
# By default returns all series with additional information am_series() # Raw series table am_series(source.info = FALSE) # Only series in the WEO am_series("IMF_WEO")
This function retrieves a table with information about the sources of data in the database, and when data from different sources was updated.
am_sources(ordered = TRUE)
am_sources(ordered = TRUE)
ordered |
logical. |
The data source table gives information about the various sources / providers of data in this database, including the source website, frequency and time coverage of data, a description of the source, when data from the source was updated and the way data is accessed from the source.
A data.table
with information about the sources of data in the database.
am_countries
, am_series
, africamonitor
am_sources()
am_sources()