Q: combining IRFs into subplotted figures & iterating?

Use

dynare model1
mat1 = oo_.endo_simul;
save model1_results.mat oo_ M_;

dynare model2
mat2 = oo_.endo_simul;
save model2_results.mat oo_ M_;

M1=load('model1_results.mat');
M2=load('model2_results.mat');

HOR=1:13;
var={'y_N_gap', 'pi_N', 'i'};
figure
for jj=1:length(var)
    subplot(2,2,jj)
    hold on
    plot(HOR,M1.oo_.endo_simul(strmatch(var{1,jj},M1.M_.endo_names,'exact'),M1.M_.maximum_lag+1:M1.M_.maximum_lag+13),'-k',...
        HOR,M2.oo_.endo_simul(strmatch(var{1,jj},M2.M_.endo_names,'exact'),M2.M_.maximum_lag+1:M2.M_.maximum_lag+13),'--r','LineWidth',2) ;
    title([var{1,jj}] )
end

It is working now, thank you so much for your help.

Dear Professor,
I have a problem with subplotting IRFs. I have two TANK models, one without idiosyncratic risk and the second with risk; and one shock that is a government spending shock. Here are my two models in Dynare:
tank_cw.mod (4.2 KB)
tankcw1.mod (1.3 KB)
from which I get the IRFs.
Now I have to compare the two models for each endogenous variable; therefore, I have to plot in the same chart the IRFs of labor hours in the two models, then in other chart the IRFs of consumption and so on…
I tried in the following way but nothing changed that is I get again the original IRFs as if I had not applied the following code:
Plotting.m (1.1 KB)

Thank you in advance

Use

clear,clc

% Run and collect dynare outputs

dynare tank_cw;
irf1 = oo_.irfs;
save tank_cw_results.mat irf1;

dynare tankcw1;
irf2 = oo_.irfs;
save tankcw1_results.mat irf2;

ending_cell={'_eps'};

for ii=1:length(ending_cell)
    HOR=1:options_.irf;
    var={'n', 'nW', 'w', 'c', 'cC', 'cW', 'rn', 'r', 'pi', 'b', 'bC', 'bW', 't', 'g', 'd', 'csl', 'chl', 'bsl', 'bhl', 'ls'}; % variables to be plotted as called in Dynare
    fig(ii)= figure('Name',['Shock to',sprintf(ending_cell{1,ii})],'NumberTitle','off');
    % plot the model variables 
    for jj=1:length(var)
        subplot(5,5,jj)
        hold on
        plot(HOR,irf2.([var{1,jj},ending_cell{1,ii}]),HOR,irf1.([var{1,jj},ending_cell{1,ii}]),'LineWidth',1.3);      
        xlim([1 options_.irf]);
        hline = refline(0, 0);
        title([var{1,jj}] )
    end
    
    legend('Model1', 'Model2', 'AutoUpdate','off')
   
end
1 Like

Thank you very much! And in order to increase the dimension of the subplots, for example instead of having subplot (5,5,jj), having subplot (3,3,jj) and subplots(2,2,jj), how should I write the code?

A clean version would be

ending_cell={'_eps'};
var={'n', 'nW', 'w', 'c', 'cC', 'cW', 'rn', 'r', 'pi', 'b', 'bC', 'bW', 't', 'g', 'd', 'csl', 'chl', 'bsl', 'bhl', 'ls'}; % variables to be plotted as called in Dynare
HOR=1:options_.irf;
n_plots=3;
for ii=1:length(ending_cell)
    for jj=1:length(var)
        if mod(jj,n_plots^2)==1
            plot_iter=1;
            fig(ii)= figure('Name',['Shock to',sprintf(ending_cell{1,ii})],'NumberTitle','off');
        end
        subplot(n_plots,n_plots,plot_iter)
        hold on
        plot(HOR,irf2.([var{1,jj},ending_cell{1,ii}]),HOR,irf1.([var{1,jj},ending_cell{1,ii}]),'LineWidth',1.3);      
        xlim([1 options_.irf]);
        hline = refline(0, 0);
        title([var{1,jj}] )
        if plot_iter==n_plots^2 ||  jj==length(var)
            legend('Model1', 'Model2', 'AutoUpdate','off')
        end
        plot_iter=plot_iter+1;
    end
end

I am attempting to plot 5 IRFs on a single graph for a TFP shock and a monetary policy shock. I have modified the code from AITANAgf (reply 37/67 above) to allow for my 5 different models. First, you will need to run calibration4.m and model_run4.m, load the 5 .mod files, then run the jointgraphs.m file. The error message that I obtain is the following:
Reference to non-existent field ‘pit_eps_e’.
Error in jointgraphs (line 36)
eval([‘irf1.’ var{1,jj},ending_cell{1,ii}]);

Many thanks for your help!
model_run4.m (3.1 KB)
calibration4.m (2.5 KB)
thesisdynare4.mod (6.8 KB)
thesisdynarenu.mod (6.8 KB)
thesisdynaretheta.mod (6.8 KB)
thesisdynareeta.mod (6.8 KB)
thesisdynarejoint.mod (6.8 KB)
jointgraphs.m (1.4 KB)

You did not adjust the name of the shock:

ending_cell={'_e_z'};
% 'y', 'bt','ct', 'cu', 'hc', 'hz','hu', 'dp', 'q', 'qz', 'r', 'l', 'dy', pit mc c Y n u lfp w Z
for ii=1:length(ending_cell)
    HOR=1:options_.irf;
    var={ 'pit', 'mc','c', 'Y', 'n', 'u', 'lfp', 'w', 'Z'};
    figure
    for jj=1:length(var)
        subplot(3,3,jj)
        eval(['irf1.' var{1,jj},ending_cell{1,ii}]);
        eval(['irf2.' var{1,jj},ending_cell{1,ii}]);
        eval(['irf3.' var{1,jj},ending_cell{1,ii}]);
        eval(['irf4.' var{1,jj},ending_cell{1,ii}]);
        eval(['irf5.' var{1,jj},ending_cell{1,ii}]);
        hold on
        plot(HOR,[eval(['irf1.' var{1,jj},ending_cell{1,ii}])],'-k',HOR,[eval(['irf2.' var{1,jj},ending_cell{1,ii}])],'--r', HOR,[eval(['irf3.' var{1,jj},ending_cell{1,ii}])], ':m' ,'LineWidth',1) ;
        title([var{1,jj}] )
    end
end

Yes, that worked, thank you! Now, I am assuming that in the plot command on line 42, the ‘-k’, ‘—r’ and ‘:m’ arguments are changing the color/type of line for the IRF plot. After doing a quick search in the Dynare manual, I could not find information on the total possible plot colors and types. Where can I find this information?

Sorry for asking without checking thoroughly enough, I found the information. For those reading in the future, I found plot line types here: