Aggregating to lower frequency
1. Aggregating to lower frequency
While it's common to do analysis on daily financial data, most economic data are only released monthly or quarterly.2. Low frequency data
Though they only occur monthly or quarterly, the observations usually include a timestamp with a day of the month. Some data providers use the first day of the observation period, while others use the last day. And it's not uncommon to use the middle month for quarterly data.3. Example
Let's look at an example of trying to merge two low-frequency datasets with different timestamp conventions. Assume you want to compare the daily 10-year Treasury constant maturity rate with United States gross domestic product, which is only released quarterly. The FRED symbols for these series are DGS10 and GDP, respectively.4. Merge aggregated data with low-frequency data
You can use apply-dot-quarterly() to calculate the median of the daily values for each quarter. Recall that apply-dot-quarterly() output will use the timestamp from the last observation in the quarter. And the GDP data is timestamped with the first day of the quarter, so the columns of the merged data do not have any timestamps in common.5. Low frequency date-time classes
The zoo package provides two classes to handle this situation. The yearmon class represents monthly observations and yearqtr class represents quarterly observations. These classes remove the ambiguity of which day represents a specific month or quarter.6. Convert index to lowest frequency
Let's go back to our GDP and Treasury rate example. It's often easiest to use the class with the lowest frequency given your data. In this case, you can convert both indexes to the quarterly yearqtr class using as-dot-yearqtr(). Then assign the output of as-dot-yearqtr() to the index of each object. Now merging the two objects works and there are no missing values. If you're not able to use the yearqtr class for some reason, the second-best option is to align with the first timestamp of the period, because months and quarters have different number of days.7. Align with beginning-of-period timestamp
You can do that by using na-dot-locf() on the merged result, with the fromLast argument set to TRUE so the last observation will be carried backward. Now the missing values are filled, so the median interest rate now has a non-NA value for the first observation in each quarter. But there are still two observations per quarter; one at the beginning, and one at the end. You can extract just the first observation by subsetting using the index of the object with the index values you want to keep. In this case, that's the GDP index.8. Let's practice!
You've learned how low-frequency date-time classes can be helpful, so now it's time to practice using them.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.