Title: | Download Data from Bank of Spain |
---|---|
Description: | Tools to download data series from 'Banco de España' ('BdE') on 'tibble' format. 'Banco de España' is the national central bank and, within the framework of the Single Supervisory Mechanism ('SSM'), the supervisor of the Spanish banking system along with the European Central Bank. This package is in no way sponsored endorsed or administered by 'Banco de España'. |
Authors: | Diego H. Herrero [aut, cre, cph] (<https://orcid.org/0000-0001-8457-4658>, rOpenSpain) |
Maintainer: | Diego H. Herrero <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.3.4 |
Built: | 2023-09-11 06:07:22 UTC |
Source: | https://github.com/rOpenSpain/tidyBdE |
Load the time-series catalogs provided by BdE.
bde_catalog_load(
catalog = c("ALL", "BE", "SI", "TC", "TI", "PB"),
parse_dates = TRUE,
cache_dir = NULL,
update_cache = FALSE,
verbose = FALSE
)
catalog |
A single value indicating the catalogs to be updated
or |
parse_dates |
Logical. If |
cache_dir |
A path to a cache directory. The directory can also be set
via options with |
update_cache |
Logical. If |
verbose |
Logical |
Accepted values for catalog
are:
CODE | PUBLICATION | UPDATE FREQUENCY | FREQUENCY |
BE | Statistical Bulletin | Daily | Monthly |
SI | Summary Indicators | Daily | Daily |
TC | Exchange Rates | Daily | Daily |
TI | Interest Rates | Daily | Daily |
PB | Bank Lending Survey | Quarterly | Quarterly |
Use "ALL"
as a shorthand for updating all the catalogs at a glance.
If the requested catalog is not cached bde_catalog_update()
is invoked.
A tibble
.
Time-series bulk data download
Other catalog:
bde_catalog_search()
,
bde_catalog_update()
bde_catalog_load("TI", verbose = TRUE)
Search for keywords on the time-series catalogs.
bde_catalog_search(pattern, ...)
pattern |
|
... |
Arguments passed on to
|
Note that BdE files are only provided in Spanish, for the time being. Therefore search terms should be provided in Spanish as well in order to get search results.
This function uses base::regex()
function for finding matches on
the catalogs. You can pass regular expressions to broaden
the search.
A tibble with the results of the query.
bde_catalog_load()
, base::regex()
Other catalog:
bde_catalog_load()
,
bde_catalog_update()
# Simple search (needs to be in Spanish)
# !! PIB [es] == GDP [en]
bde_catalog_search("PIB")
# More complex - Single
bde_catalog_search("Francia(.*)PIB")
# Even more complex - Double
bde_catalog_search("Francia(.*)PIB|Italia(.*)PIB|Alemania(.*)PIB")
# Search a sequential code: Exact match
# Note that this series (sequential code) appears on several tables
bde_catalog_search("^3779313$")
Update the time-series catalogs provided by BdE.
bde_catalog_update(
catalog = c("ALL", "BE", "SI", "TC", "TI", "PB"),
cache_dir = NULL,
verbose = FALSE
)
catalog |
A vector of characters indicating the catalogs to be updated
or |
cache_dir |
A path to a cache directory. The directory can also be set
via options with |
verbose |
Logical |
Accepted values for catalog
are:
CODE | PUBLICATION | UPDATE FREQUENCY | FREQUENCY |
BE | Statistical Bulletin | Daily | Monthly |
SI | Summary Indicators | Daily | Daily |
TC | Exchange Rates | Daily | Daily |
TI | Interest Rates | Daily | Daily |
PB | Bank Lending Survey | Quarterly | Quarterly |
Use "ALL"
as a shorthand for updating all the catalogs at a glance.
None. Downloads the catalog file(s) to the local machine.
Time-series bulk data download
Other catalog:
bde_catalog_load()
,
bde_catalog_search()
bde_catalog_update("TI", verbose = TRUE)
Set of helper functions for downloading some of the most relevant macroeconomic indicators of Spain.
bde_ind_gdp_var(series_label = "GDP_YoY", ...)
bde_ind_unemployment_rate(series_label = "Unemployment_Rate", ...)
bde_ind_euribor_12m_monthly(series_label = "Euribor_12M_Monthly", ...)
bde_ind_euribor_12m_daily(series_label = "Euribor_12M_Daily", ...)
bde_ind_cpi_var(series_label = "Consumer_price_index_YoY", ...)
bde_ind_ibex_monthly(series_label = "IBEX_index_month", ...)
bde_ind_ibex_daily(series_label = "IBEX_index_day", ...)
bde_ind_gdp_quarterly(series_label = "GDP_quarterly_value", ...)
bde_ind_population(series_label = "Population_Spain", ...)
series_label |
Optional. Character vector or value. Allows to specify a
custom label for the series extracted. It should have the same length than
|
... |
Arguments passed on to
|
This functions are convenient wrappers of bde_series_load()
referencing
specific series. Use verbose = TRUE, extract_metadata = TRUE
options
to see the specification and the source.
A tibble
with the required series.
bde_series_load()
, bde_catalog_search()
bde_ind_gdp_var()
Custom palettes based on the publications of BdE.
bde_vivid_pal(...)
bde_rose_pal(...)
... |
Further arguments of the functions. |
A palette of colors.
Other bde_plot:
scales_bde
,
theme_tidybde()
# BdE vivid pal
scales::show_col(bde_vivid_pal()(6), labels = FALSE)
# BdE rose pal
scales::show_col(bde_rose_pal()(6), labels = FALSE)
This function is tailored for the date formatting used on this package, so it may fail if it is used for another datasets. See Examples for checking which formats would be considered.
FREQUENCY | FORMAT | EXAMPLES |
Daily / Business day | DD MMMMYYYY | 02 FEB2019 |
Monthly | MMM YYYY | MAR 2020 |
Quarterly | MMM YYYY, where MMM is the first or the last month of the quarter, depending on the value of its variable OBSERVED. | For the first quarter of 2020: ENE 2020, MAR 2020 |
Half-yearly | MMM YYYY, where MMM is the first or the last month of the halfyear period, depending on the value of its variable OBSERVED. | For the first half of 2020: ENE 2020, JUN 2020 |
Annual | YYYY | 2020 |
bde_parse_dates(dates_to_parse)
dates_to_parse |
Dates to parse |
Tries to parse strings representing dates using as.Date()
A class "Date" object.
# Formats parsed
would_parse <- c(
"02 FEB2019", "15 ABR 1890", "MAR 2020", "ENE2020",
"2020", "12-1993", "01-02-2014", "01/02/1990"
)
parsed_ok <- bde_parse_dates(would_parse)
class(parsed_ok)
tibble::tibble(raw = would_parse, parsed = parsed_ok)
#-----------------------------------
# Formats not admitted
wont_parse <- c("JAN2001", "2010-01-12", "01 APR 2017", "01/31/1990")
parsed_fail <- bde_parse_dates(wont_parse)
class(parsed_fail)
tibble::tibble(raw = wont_parse, parsed = parsed_fail)
Load a full time-series file provided by BdE.
bde_series_full_load(
series_csv,
parse_dates = TRUE,
parse_numeric = TRUE,
cache_dir = NULL,
update_cache = FALSE,
verbose = FALSE,
extract_metadata = FALSE
)
series_csv |
csv file of a series, as defined in the field
|
parse_dates |
Logical. If |
parse_numeric |
Logical. If |
cache_dir |
A path to a cache directory. The directory can also be set
via options with |
update_cache |
Logical. If |
verbose |
Logical |
extract_metadata |
Logical |
The series name is a positional code showing the location of the table. For example, table be_6_1 represents the Table 1, Chapter 6 of the Statistical Bulletin ("BE"). Although it is a unique value, it is subject to change (i.e. a new table is inserted before).
For that reason, the function bde_series_load()
is more suitable for
extracting specific time-series.
A tibble with a field "Date" and the alias of the fields series as
described on the catalogs. See bde_catalog_load()
.
This function tries to coerce the columns to numbers. For some series a
warning may be displayed if the parser fails. You can override the default
behavior with parse_numeric = FALSE
Other series:
bde_series_load()
# Metadata
bde_series_full_load("TI_1_1.csv", extract_metadata = TRUE)
# Data
bde_series_full_load("TI_1_1.csv")
The series alias is a positional code showing the location (column and/or row) of the series in the table. However, although it is unique, it is not a good candidate to be used as the series ID, as it is subject to change. If a series changes position in the table, its alias will also change.
To ensure series can still be identified, even after these changes, they
are assigned a sequential number (series_code
on this function) which
will remain unchanged throughout the series' lifetime.
Note that a single series could be used on different tables, so it can have
several aliases. If you need to search by alias it is recommended to use
bde_series_full_load()
.
bde_series_load(
series_code,
series_label = NULL,
out_format = "wide",
parse_dates = TRUE,
parse_numeric = TRUE,
cache_dir = NULL,
update_cache = FALSE,
verbose = FALSE,
extract_metadata = FALSE
)
series_code |
a numeric (or coercible with |
series_label |
Optional. Character vector or value. Allows to specify a
custom label for the series extracted. It should have the same length than
|
out_format |
Defines if the format must be returned as a "long" dataset
or a "wide" dataset. Possible values are |
parse_dates |
Logical. If |
parse_numeric |
Logical. If |
cache_dir |
A path to a cache directory. The directory can also be set
via options with |
update_cache |
Logical. If |
verbose |
Logical |
extract_metadata |
Logical |
Load a single time-series provided by BdE.
A tibble
with a field "Date" and :
With out_format = "wide"
each series is presented in a separate
column with the name defined by series_label
.
With out_format = "long"
the tibble would have two more columns,
serie_name
with the labels of each series and serie_value
with the
value of the series.
"wide" format is more suitable for exporting to a .csv
file while "long"
format is more suitable for producing plots with ggplot2::ggplot()
.
See also tidyr::pivot_longer()
and tidyr::pivot_wider()
.
This function tries to coerce the columns to numbers. For some series a
warning may be displayed if the parser fails. You can override the default
behavior with parse_numeric = FALSE
bde_catalog_load()
,
bde_catalog_search()
, bde_indicators()
Other series:
bde_series_full_load()
# Metadata
bde_series_load(573234, verbose = TRUE, extract_metadata = TRUE)
# Data
bde_series_load(573234, extract_metadata = FALSE)
# Vectorized
bde_series_load(c(573234, 573214),
series_label = c("US/EUR", "GBP/EUR"),
extract_metadata = TRUE
)
wide <- bde_series_load(c(573234, 573214),
series_label = c("US/EUR", "GBP/EUR")
)
# Wide format
wide
# Long format
long <- bde_series_load(c(573234, 573214),
series_label = c("US/EUR", "GBP/EUR"),
out_format = "long"
)
long
# Use with ggplot
library(ggplot2)
ggplot(long, aes(Date, serie_value)) +
geom_line(aes(group = serie_name, color = serie_name)) +
scale_color_bde_d() +
theme_tidybde()
ggplot2
.Scales to be used with the ggplot2 package. Discrete palettes are
named as scale_*_bde_d
while continuous palettes are named scale_*_bde_c
.
scale_color_bde_d(palette = c("bde_vivid_pal", "bde_rose_pal"), ...)
scale_fill_bde_d(palette = c("bde_vivid_pal", "bde_rose_pal"), ...)
scale_color_bde_c(palette = c("bde_vivid_pal", "bde_rose_pal"), ...)
scale_fill_bde_c(palette = c("bde_vivid_pal", "bde_rose_pal"), ...)
palette |
Name of the BdE palette to apply. One of |
... |
Further arguments of |
A ggplot2
color scale.
bde_vivid_pal()
, ggplot2::discrete_scale()
,
ggplot2::continuous_scale()
Other bde_plot:
bde_pals
,
theme_tidybde()
library(ggplot2)
set.seed(596)
txsamp <- subset(
txhousing,
city %in% c(
"Houston", "Fort Worth",
"San Antonio", "Dallas", "Austin"
)
)
ggplot(txsamp, aes(x = sales, y = median)) +
geom_point(aes(colour = city)) +
scale_color_bde_d() +
theme_minimal()
ggplot(txsamp, aes(x = sales, y = median)) +
geom_point(aes(colour = city)) +
scale_color_bde_d("bde_rose_pal") +
theme_minimal()
ggplot2
themeA custom ggplot2 theme based on the publications of BdE.
theme_tidybde(...)
... |
Arguments passed on to
|
Theme based on ggplot2::theme_classic()
.
Other bde_plot:
bde_pals
,
scales_bde
library(ggplot2)
library(dplyr)
library(tidyr)
series_TC <- bde_series_full_load("TC_1_1.csv")
# If download was OK then plot
if (nrow(series_TC) > 0) {
series_TC <- series_TC[c(1, 2)]
series_TC_pivot <- series_TC %>%
filter(
Date >= "2020-01-01" & Date <= "2020-12-31",
!is.na(series_TC[[2]])
)
names(series_TC_pivot) <- c("x", "y")
ggplot(series_TC_pivot, aes(x = x, y = y)) +
geom_line(linewidth = 0.8, color = bde_vivid_pal()(1)) +
labs(
title = "Title",
subtitle = "Some metric",
caption = "Bank of Spain"
) +
theme_tidybde()
}