Time and Data

library(tdata)

Introduction

In this vignette, I will introduce you to the main features of the tdata package. I will use various datasets to demonstrate how to perform common tasks, such as defining frequency types and converting data between frequencies.

Please note that currently, only one section is provided in this vignette. Additional examples will be added in subsequent updates.

Let’s get started!

Oil Price

In the first example, I will use oil price data. The required data can be downloaded from the Quandl package using the following code (Note that the end date in this example may differ from yours):

oil_price <- Quandl::Quandl("OPEC/ORB", start_date="2010-01-01")

1. Create a Variable

To manipulate data using the tdata package, we generally need to create a variable. In this example, we’ll create a variable from the oil price data. First, we’ll use the values in the first column to define a frequency. Since the first column contains a list of dates, we’ll use a ‘List-Date’ frequency:

start_freq <- f.list.date(oil_price$Date)

Now that we have defined the frequency, we can create a variable using the following code:

var_dl <- variable(oil_price$Value, start_freq, "Oil Price")

This creates an array where each element is labeled by a date. We can print this variable using the print function:

print(var_dl)
## Variable:
##     Name = Oil Price
##     Length = 3466
##     Frequency Class = List (Date): Ld
##     Start Frequency = 20230608
##     Fields: NULL

We can also convert the variable back to a data.frame using the as.data.frame function:

df_var_dl <- as.data.frame(var_dl)

2. Daily Variable

In this section, we’ll convert var_dl to a daily variable. This can be done by sorting the data and filling in any gaps. The convert.to.daily function can do this for us:

var_daily <- convert.to.daily(var_dl)

Using this function is more efficient than manually sorting the data and filling in gaps because var_daily, as a daily variable, only stores a single date: the frequency of the first observation. Other frequencies (or dates) are inferred from this first date (except for ‘Lists’, this is true for other types of frequencies in the tdata package). We can print the starting frequency using the print function:

print(var_daily$startFrequency)
## Frequency: 20100104 (Daily: d)

Each frequency in the tdata package has a string representation and a class ID. We can get these values using the following code:

class_id <- get.class.id(var_daily$startFrequency)
str_rep <- as.character(var_daily$startFrequency)
## [1] "class_id: d, str_rep: 20100104"

Plotting the data is straightforward. We simply convert the data to a data.frame using the as.data.frame function and then plot it. However, I won’t plot the daily variable in this example because, since the original data was a ‘List-Date’, there are many NA values. In the next section, I’ll aggregate the data and plot it.

3. Weekly Variable

In this section, we’ll convert the daily variable to a weekly variable. Unlike the previous conversion, this involves aggregating the data rather than sorting and filling in gaps. To do this, we’ll need to use an aggregator function that takes an array of data as an argument and returns a scalar value. Summary statistic functions such as mean and median are natural choices for this (we’ll also need to handle NA values). In this example, I’ll use a built-in function to get the last available data point in each week as the representative value for that week. Here’s the code:

var_weekly <- convert.to.weekly(var_daily, "mon", "last")

The second argument, "mon", specifies that the week starts on Monday. Note that the weekly frequency points to the first day of the week. We can now convert the variable to a data.frame and plot it using the following code:

df_var_weekly <- as.data.frame(var_weekly)
par(las = 2, cex.axis = 0.8)
plot(factor(rownames(df_var_weekly)), 
     df_var_weekly$`Oil Price`, 
     xlab = NULL, ylab = "$", 
     main = "Weekly Oil Price")
Plotting the generated weekly data

Plotting the generated weekly data

There are other frequency types and conversion functions available in the tdata package that you can explore on your own.

Other functions

In this subsection, I will talk about some other functions in tdata package. These are not the main functions, but just some related subject to time and data.

1. Long-run growth

In this subsection, we will discuss long-run growth and use the tdata package to calculate and plot it. First, let’s review some mathematical concepts.

A variable can change continuously or discretely over time.

The starting condition is represented by y0 and y(0), while yt and y(t) represents the value of the variable t periods. The gi × 100 for i = 1…t are the discrete or continuous growth rates in different periods.

Recall that we have two formulas for calculating growth rate in one period:

for yi > 0 for all i. Also, recall that we have this approximation: ln(1 + x) ≈ x when x is small. Therefore, it might not be important which one we choose for variables such as GDP. However, assume that the growth rates are large, e.g., in one period the value increases from 1 to 2. It is clear that the growth rate is 100%. Using the continuous formula gives us 69.3% and by using the discrete one we get 100%. Does this mean the continuous formula is wrong? Definitely not. When we say “the value increases from 1 to 2”, we are assuming that the growth is discrete. Therefore, if you are wondering which of these formulas to use, think about your assumptions and your mathematical model. It is generally easier to work with the continuous formula because the derivative of ln is easier to compute.

Interest rate is a similar concept to growth rate, and let’s study these formulas from that perspective. Assume that you lend y0 amount of money at time t = 0. If the annual interest rate is r, at the end of the first year you will have y0 * (1 + r). Let’s assume that interest is paid monthly and you lend the interest too. At the end of the first month, you will have $y_0*(1+\frac{r}{12})$. Then, at the end of the second month, you will have $y_0*(1+\frac{r}{12})^2$. And so on. At the end of the year, you will have $y_0*(1+\frac{r}{12})^12$. At the end of the second year, you will have $y_0*(1+\frac{r}{12})^24$. At the end of t years, you will have $y_0*(1+\frac{r}{12})^12t$. We can similarly split the year into smaller periods and calculate the limit when we move from 12 toward infinity in the formula. If we use L’Hopital’s rule and calculate such a limit, we will find the continuous formula: yt = y0ert. Compared to the previos discussion, we have one scalar value r here.

Let’s get back to the long-run formulas. We are looking for a scalar value g in which we can summarize all the other gis, such that:

This single value does what all other growth rates achieve together: it take us from the starting point (y0 or y(0)) to the end point (yt or y(t)). We call c = gc × 100 and d = gd × 100 the continuous and discrete long-run growth rates. It is important to note that c and d are not exactly equal (The discussion is similar to the discussion about Gc and Gd above).

Given the data {yi}i = 0t where yi > 0 for all i, the following formula calculates the long-run continuous growth rates:

in which Gci is the continuous growth rate at period i. Recall that ln x + lny = ln xy for x, y ∈ ℝ.

The two similar formulas for the discrete case are:

These are more complicated compared to the continuous case. As I said before, it is much easier to deal with continuous assumption.

It is important to note that the formulas for calculating growth rates may not be applicable when the data can be negative or {yi}i = 0t contains negative values. In such cases, alternative methods may be needed to measure growth or change over time. One possible approach is to add a constant to all values to make them positive before calculating the growth rates. However, this approach may not always be suitable and it is crucial to carefully evaluate the context of the data before applying any transformation.

The following code creates two tdata variables which contain the real GDP per capita (PPP) of Korea and Iran from 1990 to 2021. Data source is “World Development Indicators” and data code is “NY.GDP.PCAP.PP.KD”.

y_kor <- variable(data = c(12656, 13882, 14591, 15436, 16698, 18120, 19365, 20368, 19184, 21233, 22964, 23894, 25591, 26260, 27516, 28641, 29991, 31570, 32275, 32364, 34394, 35389, 36049, 37021, 37967, 38829, 39815, 40957, 41966, 42759, 42397, 44232),
                    startFrequency = f.yearly(1990),
                    name = "Korea, GDP per capita, PPP (constant 2017 international $), World Bank")

y_iri <- variable(data = c(9442, 10240, 10331, 10114, 9904, 10007, 10504, 10495, 10548, 10590, 11026, 11098, 11879, 12786, 13127, 13329, 13781, 14690, 14526, 14474, 15099, 15302, 14542, 14113, 14539, 14011, 14969, 15163, 14629, 14084, 14432, 15005),
                    startFrequency = f.yearly(1990),
                    name = "Iran, Rep., GDP per capita, PPP (constant 2017 international $), World Bank")

We can use the get.longrun.growth function to calculate the long-run growth rates and plot the trends.

GDP per capita of IRI and KOR: Data and longrun trend over time

GDP per capita of IRI and KOR: Data and longrun trend over time

2. NA starategies

Data cleaning is a crucial early step in the data analytics process, which may involve handling missing or NA observations. There are several methods to deal with missing values, including replacing them with substitutes from other records or datasets, estimating values based on other available information through imputation, or using a mathematical function to fit a curve to the available data points through interpolation. However, sometimes it may be necessary to remove NA observations from the data before analysis. In this section, we will discuss a common scenario when working with cross-sectional data where NA values exist in some variables.

Let’s begin with a simple example. Consider the following data table, where the variables are in columns and the observations are in rows:

If we remove V2, the resulting data table will have 8 observations. However, if we remove O2 and O3 observations, the final data table will have only 6 observations. Assuming that all observations are equally important, removing V2 would be the best strategy. Now, consider the following structure:

In this case, the best strategy would be to remove V1 and O1. Now, consider the following data table:

Can you see what the best strategy would be in this case? What if it is more important to keep variables instead of observations.

The remove.na.strategies function in the tdata package can help you decide on a proper order for removing columns and rows. Here is an example using the data from the previous example:

st <- remove.na.strategies(data = matrix(c(NA,2,NA,NA,5,6,7,8,9,NA,
                                           11,12,13,14,NA,NA,17,18,19,20),
                                         ncol = 4,
                                         byrow = TRUE))

The first element of the output shows the best strategy where the final data table has 9 observations by removing columns with indices reported in colRemove (which is 2) and rows with indices reported in rowRemove (which is 1, 4). The second-best strategy results in a final matrix with 8 observations and suggests removing rows with indices reported in rowRemove (which is 1, 3, 4) and no columns.

We can use the countFun argument to increase the weight of columns. For example, if we use countFun = function(nRows, nCols) nRows * nCols^2, the best strategy would be to remove rows with indices reported in rowRemove (which is 1, 3, 4).