Adding Recession Bars to a line plot
Show older comments
Hi all
I am trying to figure out how to add NBER recession bars to my graph.
I understand that I need to format my date correctly using datenum for the recessionplot function to work.
To summarise, I am running a rolling regression. The date vector (datevec) has been generated using a rolling vector created in my code. Once I've got my coefficient (timevardelta + standard error bands) I am graphing it against the datevec.
Now I understand that the format of the datevec is not appropriate to use recessionplot.
Below is the code I am using for my graph:
clear all, clc
load datevec
load adjtimevarsd
load timevardelta
% Plot rolling regression results in Figure 1:
figure(1); clf ;
plot(datevec,timevardelta','LineWidth',1.5) ;
hold on ;
plot(datevec,timevardelta'+1.96*adjtimevarsd',':','Color',[.4 .4 .4],'LineWidth',1.5) ;
plot(datevec,timevardelta'-1.96*adjtimevarsd',':','Color',[.4 .4 .4],'LineWidth',1.5) ;
plot(datevec,ones(size(datevec)),'k', datevec, zeros(size(datevec)),'k') ;
ylim = get(gca,'YLim') ;
axis([2005, 2016.05, -0.50, 3.00]) ;
ylim = get(gca,'YLim') ;
xlim = get(gca,'XLim');
% redraw lines, tick marks, and axis lines
plot(datevec,timevardelta','LineWidth',1.5) ;
plot(datevec,timevardelta'+1.96*adjtimevarsd',':','Color',[.4 .4 .4],'LineWidth',1.5) ;
plot(datevec,timevardelta'-1.96*adjtimevarsd',':','Color',[.4 .4 .4],'LineWidth',1.5) ;
plot(datevec,ones(size(datevec)),'k', datevec, zeros(size(datevec)),'k') ;
ticks = get(gca,'XTick') ;
for tick = ticks ;
plot([tick,tick],[ylim(1),ylim(1)+.015*(ylim(2)-ylim(1))],'k') ;
plot([tick,tick],[ylim(2),ylim(2)-.015*(ylim(2)-ylim(1))],'k') ;
end ;
plot([xlim(1),xlim(2)],[ylim(1),ylim(1)],'k') ;
plot([xlim(1),xlim(2)],[ylim(2),ylim(2)],'k') ;
plot([xlim(1),xlim(1)],[ylim(1),ylim(2)],'k') ;
plot([xlim(2),xlim(2)],[ylim(1),ylim(2)],'k') ;
hold off ;
I have provided the data files, for your consideration.
I would be very grateful if you could please help me through (any hints, tips...anything!) how to graph NBER recession bars on my graph.
Thank you all.
Regards Parvesh
11 Comments
dpb
on 1 Nov 2018
datevec is a builtin Matlab function; be better to not alias it with a variable.
What is the definition for the fractional portion of the year--is it based on days in the given year, 365 days, 365.25 days, ...???
The best way by far to proceed will be to convert to datetime; plot and friends are datetime aware.
dpb
on 1 Nov 2018
Also, what is the recessionplot function????
Parveshsingh Seeballack
on 1 Nov 2018
Edited: dpb
on 1 Nov 2018
dpb
on 1 Nov 2018
You didn't answer the question about the time scaling of the year data...that's the first step however you approach it.
I don't have the Econometrics TB so can't look at what the function actually uses...surprising it hasn't been updated to handle datetime but still need answer to the above Q? to convert to datenum
Parveshsingh Seeballack
on 2 Nov 2018
That may be for whatever the regression was, but what's the real time representation of the (unfortunately-named) datevec array?
Per the documentation, the plot is based on datenum so to compute that time it needs must be able to be related back to actual y,m,d that was used to calculate that fractional year; the question is what was that correlation?
If it really was based on trading days, that's tough nut to crack because then you need to also have the actual market holiday schedule as well...
I did a quick look-see and it appears that it is based on actual number of days in the given year; please confirm if this is/is not true.
Where did those values come from; that source should state how they were created....or maybe the real dates are also available from the source to use directly would be even easier???
ADDENDUM And, for the two plots to line up when and if you are able to call recessionplot, the base plot will also have to be in datenum units on the time/x axis or the recession bands won't be in the correct place.
Parveshsingh Seeballack
on 3 Nov 2018
yr = PI(:, 1);
mo = PI(:, 2);
dy = PI(:, 3);
...
date = datenum(yr,mo,dy) ; % for graphing
is the right way to proceed except that again you chose a variable name that happens to be a Matlab builtin function...
>> which date
C:\ML_R2017\toolbox\matlab\timefun\date.m
>>
The code editor will highlight these for you; I'd recommend as you're learning to not get into the habit of ignoring the orange markers on the RH side just routinely...
As far as whether the rest of the code "look ok to you?" I don't have the TB to run it and don't know a thing about the particular econometric analysis so can't comment on it, but the code itself appears reasonable on the surface. Does it give you something that looks expected? Can you compare it to a known result as a check, maybe?
MUSINGS FOLLOW:
For datenum, since my coding style is to typically use pretty short (even terse, maybe?) variable names for temporaries and obvious variables, I typically will just write
dn=datenum(yr,mo,dy) ; % for graphing
to remind me it is a date number. (For datetime that would be dt, but since I historically did a lot of signal processing work that to me innately means "delta-time" so I use dtm instead).
Everybody has to develop a style of their own with which they are comfortable between verboseness and too terse so these are simply my own choices. I am, admittedly, also an elder statesmen who grew up in days of limited memory and punch cards and upper-case-only FORTRAN so "shorter was better" to keep lines within columns 7-72 on a card and old habits may loosen some but never go away entirely. :)
Parveshsingh Seeballack
on 3 Nov 2018
Edited: dpb
on 3 Nov 2018
Parveshsingh Seeballack
on 9 Nov 2018
dpb
on 10 Nov 2018
That says you've not selected the same number of elements from the two variables. It looks like your plot statement hasn't been modified to use a subset of the full time array dn x variable commensurate with the y variable.
Answers (0)
Categories
Find more on Title in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!