Graph Tips – III: Data of Different Frequencies (eg. monthly & quarterly)

The following two examples show two different ways of dealing with data of different frequencies. The first example uses getFredData’s ability to convert monthly data to a quarterly frequency, and then just plots this (you also choose aggregation method, eg. sum of the months or average of the months). The second example gets both monthly and quarterly data, and then uses some plotly trickery with a hidden extra x-axis to plot them on the same graph (it also uses two separate y-axes). (List of frequencies and aggregation methods) (The first graph doesn’t have much point except to give an example of combining different frequencies as FRED does not provide a quarterly version of Federal Funds Rate, while GDP is not available at anything higher than quarterly)

GDPandFedFunds
GreatRecession_HousePrices_US

Matlab Code for First Example:

% Real Gross Domestic Product (Percent Change from Quarter One Year Ago, Quarterly, Seasonally Adjusted)
fred_GDPg = getFredData(‘A191RO1Q156NBEA’, ‘1990-01-01’, ‘2014-12-31’)
% Effective Federal Funds Rate (Percent, Monthly, Not Seasonally Adjusted)
fred_FEDFUNDS = getFredData(‘FEDFUNDS’, ‘1990-01-01’, ‘2014-12-31′,’lin’,’q’,’avg’)

trace1= struct(‘x’, {cellstr(datestr(fred_GDPg.Data(:,1),’yyyy-QQ’))},’y’,fred_GDPg.Data(:,2),’name’, ‘GDP Growth’,’type’, ‘scatter’,’line’, struct(‘color’, ‘hsv(0,80,100)’));
trace2= struct(‘x’, {cellstr(datestr(fred_FEDFUNDS.Data(:,1),’yyyy-QQ’))},’y’,fred_FEDFUNDS.Data(:,2),’name’, ‘Fed Funds (right)’,’type’, ‘scatter’,’yaxis’,’y2′,’line’, struct(‘color’, ‘hsv(210,80,100)’));
data = {trace1,trace2};
layout = struct(‘title’, ‘US: Real GDP Growth and Federal Funds Rate’,’showlegend’, true,’width’, 800,…
‘xaxis’, struct(‘title’,’Year’,’domain’,[0.1,1],’showgrid’,false,’anchor’,’y2′,’tickangle’,-45,’nticks’,20), …
‘yaxis’, struct(‘title’, fred_GDPg.Units(:),’titlefont’, struct(‘color’, ‘hsv(0,80,100)’),’tickfont’, struct(‘color’, ‘hsv(0,80,100)’),’position’,0,’anchor’, ‘free’,’showgrid’,false,’zeroline’,false),…
‘yaxis2’, struct(‘title’,’Percent (Average of Daily Rates)’,’titlefont’, struct(‘color’, ‘hsv(210,80,100)’),’tickfont’, struct(‘color’, ‘hsv(210,80,100)’),’position’,0.03,’overlaying’,’y’,’anchor’, ‘free’,’showgrid’,false,’zeroline’,false),…
‘legend’,struct(‘x’,0.05,’y’,0.1));
response = plotly(data, struct(‘layout’, layout, ‘filename’, ‘GDPandFedFunds’, ‘fileopt’, ‘overwrite’));
response.data=data; response.layout=layout;
saveplotlyfig(response, ‘./Graphs/GDPandFedFunds.pdf’)

Matlab Code for Second Example (House prices in lead up to Great Recession):

%% House Prices
StartDate=’1990-01-01′
EndDate=’2014-12-31′
index2000=41;

% S&P/Case-Shiller 20-City Composite Home Price Index (Index Jan 2000=100, Monthly, Seasonally Adjusted)
fred_HousePrices_CaseShiller = getFredData(‘SPCS20RSA’, StartDate, EndDate)
% S&P Case-Shiller 20-City Home Price Sales Pair Counts (Units, Monthly, Not Seasonally Adjusted)
fred_HouseSales_CaseShiller = getFredData(‘SPCS20RPSNSA’, StartDate, EndDate)
% All-Transactions House Price Index for the United States (Index 1980:Q1=100, Quarterly, Not Seasonally Adjusted)
fred_HousePrices_FHFA = getFredData(‘USSTHPI’, StartDate, EndDate) %FHFA=Federal Housing Finance Agency

% Trick to putting the monthly and quarterly data on same graph is a hidden second x-axis which has a different frequency
trace1= struct(‘x’, fred_HousePrices_FHFA.Data(:,1),’y’,100*fred_HousePrices_FHFA.Data(:,2)./fred_HousePrices_FHFA.Data(index2000,2),’name’, ‘House Prices (FHFA)’,’type’, ‘scatter’,’yaxis’, ‘y1′,’xaxis’,’x2′);% ,’line’, struct(‘color’, ColorString)); %,’yaxis’, ‘y1’
trace1dates= struct(‘x’, {cellstr(datestr(fred_HousePrices_FHFA.Data(:,1),’yyyy-QQ’))},’y’,100*fred_HousePrices_FHFA.Data(:,2)./fred_HousePrices_FHFA.Data(index2000,2),’name’, ‘House Prices (FHFA)’,’type’, ‘scatter’,’yaxis’, ‘y1′,’xaxis’,’x1′,’showlegend’,false);% ,’line’, struct(‘color’, ColorString)); %,’yaxis’, ‘y1’
trace2= struct(‘x’, fred_HousePrices_CaseShiller.Data(:,1),’y’,fred_HousePrices_CaseShiller.Data(:,2),’name’, ‘House Price Index (Case-Shiller)’,’type’, ‘scatter’,’yaxis’, ‘y1′,’xaxis’,’x2′);% ,’line’, struct(‘color’, ColorString)); %,’yaxis’, ‘y1’
trace3= struct(‘x’, fred_HouseSales_CaseShiller.Data(:,1),’y’,fred_HouseSales_CaseShiller.Data(:,2),’name’, ‘House Sales (Case-Shiller; right-axis)’,’type’, ‘scatter’,’yaxis’, ‘y2′,’xaxis’,’x2′,’line’, struct(‘dash’, ‘dot’));% ,’line’, struct(‘color’, ColorString)); %,’yaxis’, ‘y1’
data = {trace1dates,trace2,trace3,trace1};
layout = struct(‘title’, ‘US House Prices and the Great Recession’,’showlegend’, true,’width’, 800,…
‘xaxis’, struct(‘domain’, [0, 1],’title’,’Year’), …
‘xaxis2’, struct(‘domain’, [0, 1],’showgrid’,false,’ticks’,”,’showticklabels’,false,’showline’,0), …
‘legend’, struct(‘x’,0.05,’y’,0.9),…
‘yaxis’, struct(‘title’, ‘Index (2000==1)’,’titlefont’, struct(‘color’, ‘black’),’tickfont’, struct(‘color’, ‘black’),’anchor’, ‘free’,’side’, ‘left’,’position’,0), …
‘yaxis2’, struct(‘title’, ‘Units’,’titlefont’, struct(‘color’, ‘black’),’tickfont’, struct(‘color’, ‘black’),’anchor’, ‘free’,’side’, ‘right’,’position’,1,’overlaying’,’y1′,’showgrid’,false) );%, …
response = plotly(data, struct(‘layout’, layout, ‘filename’, ‘GreatRecession_HousePrices_US’, ‘fileopt’, ‘overwrite’));
response.data=data; response.layout=layout;
saveplotlyfig(response, ‘./Graphs/GreatRecession_HousePrices_US.pdf’)