This vignette shows how to download time series on lending interest rates. In the first example, the user knows the BCCR cuadro numbers. In the second, he uses the provided bancarias data frame to get those numbers.

library(bccr)
library(ggplot2)   # to make plots
library(magrittr)  # for pipe operators
library(tidyr)     # to manipulate the data frame
## 
## Attaching package: 'tidyr'
## 
## The following object is masked from 'package:magrittr':
## 
##     extract
library(dplyr)
## 
## Attaching package: 'dplyr'
## 
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## 
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(data.table)
## data.table 1.9.6  For help type ?data.table or https://github.com/Rdatatable/data.table/wiki
## The fastest way to learn (by data.table authors): https://www.datacamp.com/courses/data-analysis-the-data-table-way
## 
## Attaching package: 'data.table'
## 
## The following objects are masked from 'package:dplyr':
## 
##     between, last

User knows table numbers

Here, We are going to compare the local-currency interest rates on housing morgages from public (table 495) and private (table 496) banks. Those two tables contain daily data.

vars <- list(public=495, private=496)
vivienda <- read_daily_series(vars)
print(vivienda)
##            fecha   public  private
##    1: 1996-02-21 27.68957 32.20182
##    2: 1996-02-22 27.68957 32.20182
##    3: 1996-02-23 27.68957 32.20182
##    4: 1996-02-24 27.68957 32.20182
##    5: 1996-02-25 27.68957 32.20182
##   ---                             
## 7290: 2016-02-05 10.93515 13.38981
## 7291: 2016-02-06 10.93515 13.38981
## 7292: 2016-02-07 10.93515 13.38981
## 7293: 2016-02-08 10.93515 13.38981
## 7294: 2016-02-09 10.93515 13.38981

By default, read_daily_series will read all available data. To limit the sample, you can specify the first and last years to read. Moreover, we can request to change the frequency from daily to monthly by setting the option freq=‘m’.

For example, let’s get monthly data from 2000 to 2015, by taking the mean of the daily observations.

vivienda <- read_daily_series(vars, 2000, 2015, 'm', mean)
print(vivienda)
##           fecha   public  private
##   1: 2000-01-31 24.67877 32.73019
##   2: 2000-02-29 23.39444 32.45842
##   3: 2000-03-31 23.36809 31.99919
##   4: 2000-04-30 23.36809 31.71950
##   5: 2000-05-31 23.36809 31.62772
##  ---                             
## 188: 2015-08-31 13.35811 12.67187
## 189: 2015-09-30 13.23185 12.56542
## 190: 2015-10-31 13.17534 12.60448
## 191: 2015-11-30 12.22870 12.37407
## 192: 2015-12-31 10.92997 13.02792
vivienda %>% 
  gather('owner','rate',2:3) %>%
  ggplot(aes(x=fecha, y=rate, color=owner)) + 
  geom_line(size=2.0) + 
  labs(title='Lending interest rate for housing, by type of bank', x='',y='% rate ')

Using bancarias dataframe

The bccr package includes a bancarias data.table, which summarizes the available banking rates. In this example, we compare the lending rates of state banks by currency and activity.

First, we take the bancarias data.table to filter the desired rates and make a name (‘rate’) for the individual time series.

setkey(bancarias, tipo, entidad)
myvars <- bancarias[.('Activas','Bancos Estatales'), .(rate=paste(actividad, moneda,sep = "_"), cuadro)]

We use the read_daily_series to download the data

estatal <- read_daily_series(myvars)

Now let’s compare the rates my loan currency, for each activity

estatal %>% 
  gather("activ_moneda", "rate", 2:ncol(estatal)) %>% 
  separate(activ_moneda,c("actividad", "moneda"),"_") %>% 
  ggplot(aes(fecha, rate, color=moneda)) + 
  geom_line() + 
  facet_wrap(~actividad)