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:

Dear all,

I’m trying to replicate the irfs to a productivity shock under 4 alternative policy rules as in Galí and Monacelli (2005).

In Galí and Monacelli (2005) Figure 1 looks like this:

2.pdf (805.7 KB)

However, my replication of Figure 1 looks like this:

1.pdf (301.5 KB)

Ii would like to adjust the titles and the scaling of the axes so that the replication matches the original figure.
I’ve tried to do it with the title command (but then all variables have the same title) and rescale the axes with the xlim/ ylim command (which results in errors).

can someone tell me, what I have to do?

Many thanks for your help!

Replication_file_GM05.zip (499.9 KB)

Use something along the lines of

for ii=1: length(ending_cell)
    HOR=1:options_.irf;
    var={'pih', 'x', 'pi', 's', 'e', 'r', 'ph', 'p'};
    title_names={'Domestic Inflation', 'x', 'pi', 's', 'e', 'r', 'ph', 'p'};
    ylimits=[-0.4 0.4;
            -1 0.5
            -0.4 0.4
            0 1
            -2 1
            -0.3 0.1
            -1.5 0.5
            -1.5 0.5];

    figure
    for jj=1:length(var)
        subplot(3,4,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}]);
        hold on
        plot(HOR,[eval(['irf1.' var{1,jj}, ending_cell{1,ii}])], 'r', 'LineWidth', 1);
        plot(HOR,[eval(['irf2.' var{1,jj}, ending_cell{1,ii}])], 'b', 'LineWidth', 1);
        plot(HOR,[eval(['irf3.' var{1,jj}, ending_cell{1,ii}])], '--g', 'LineWidth', 1);
        plot(HOR,[eval(['irf4.' var{1,jj}, ending_cell{1,ii}])], 'c', 'LineWidth', 1);
        ylim(ylimits(jj,:));

        title([title_names{1,jj}])

    end
end

That was very helpful and now it works. Thank you very much, Professor!

Hello all,
I am also struggling with this plotting of IRFs on the one graph. I have two different models, for which I need to display IRFs in order to analyze the effects of ageing. I have tried some codes, but non of them is actually working, everytime I try it I got the error. Can somebody please help me?

Many thanks for help!

mladoprebivalstvo.mod (1.3 KB)
staroprebivalstvo.mod (1.3 KB)

Can you please provide your attempt based on the existing codes here?

I tired something like this, but I always get the error and I don not know why.
combined.mod (732 Bytes)

ERROR: combined.mod: line 16, col 8: syntax error, unexpected EQUAL
error: Dynare: preprocessing failed
error: called from
dynare at line 269 column 5

The file you uploaded is not a Dynare file, but rather a Matlab m-file.

Hi ,All

I was going to plot IRFs of monetary policy shock of model1 and model2 in a single figure.
Attached is the model1 and model2. Actually I tried to fix the problem according to above mentioned posts but I could not get a clear result. I was wondering what could be problem?
model1.mod (8.8 KB)
model2.mod (8.8 KB)
graphh.m (703 Bytes)

Error using plot
Data must be a single input of y-values or one or more pairs of x- and y-values.

Error in graphh (line 28)
plot(HOR,[eval([‘irf1.’ var{1,jj},ending_cell{1,ii}])],‘-k’,HOR,[eval([‘irf2.’ var{1,jj},ending_cell{1,ii}])],‘LineWidth’,2.5,‘–r’) ;

Note: model1 and model2 differs only with the value of one parameter.

graphh:
dynare model1.mod
    load('model1.mat', 'oo_')
    irf1=oo_.irfs;
    save irf1
   


    dynare model2.mod 
    load('model2_results.mat', 'oo_')
    irf2=oo_.irfs;
    save irf2
    
     load irf1
     load irf2
    
    ending_cell={'_xi_M'};

    for ii=1:length(ending_cell)
HOR=1:options_.irf;
var={'c', 'y', 'i', 'q', 's', 'pi', 'pi_h', 'pi_f', 'psi_f'};
figure

for jj=1:length(var)
subplot(1,2,jj)
eval(['irf1.' var{1,jj},ending_cell{1,ii}]);
eval(['irf2.' 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}])],'LineWidth',2.5,'--r') ;
title([var{1,jj}] )
end
end

Any help or advise is appreciated.

A clean version in Dynare 5 is

dynare model1.mod
irf1=oo_.irfs;

dynare model21.mod
irf2=oo_.irfs;

ending_cell={'_xi_M'};
var={'c', 'y', 'i', 'q', 's', 'pi', 'pi_h', 'pi_f', 'psi_f'};
for ii=1:length(ending_cell)
    figure
    for jj=1:length(var)
        subplot(3,3,jj)
        hold on
        plot(1:options_.irf,irf1.([var{1,jj},ending_cell{1,ii}]),'-k',1:options_.irf,irf2.([var{1,jj},ending_cell{1,ii}]),'--r','LineWidth',2.5) ;
        title([var{1,jj}] )
    end
end
1 Like

Thank you, professor

I do appreciate your help, it worked well