Q: combining IRFs into subplotted figures & iterating?


#1

Hi all,

I am really frustrated by the following issue but didn’t find answers in the forum, so I’m afraid I had to raise the frequently asked problem again. The setup is that I have two models, and want to compare the IRFs of each endogenous variables due to exo shocks. Say, model 1 and 2, both have endo variables y and c, exo shocks ea and em. So, the names of two models’ variables are totally the same. Now, I want to plot two IRF figures, one for response of shock ea, another em.Each figure has two subfigures,one for response of y,another c.And each subfigure has two curves, one stands for IRF of model 1, another model 2. By learning from related posts in this forum, I just write the following code, but failed to run it out.

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

dynare model2.mod
load('model2_results.mat', 'oo_')
irf2=oo_.irfs;
save irf2
load irf2

ending_cell={'_ea','_em'};

for ii=1:length(ending_cell)
HOR=1:1:40;
var={'y','c'};
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}])],'LineWidth',1,'-k',HOR,[eval(['irf2.' var{1,jj},ending_cell{1,ii}])],'LineWidth',1,'--r')
title([var{1,jj}] )
end
end

Matlab just stopped by warning"Undefined variable “irf1” or class “irf1.y_ea”."I guess the contents of irf1 are just overlapped by irf2, because all the variable names are the same among the two models. But I don’t know how to get rid of the problem.Could anyone kindly give me another hand please?

Many thanks indeed.

GOODLUCK


Multiple IRFs on one graph
Plotting IRFs from different models in the same figure
Plot multiple lines in one graph
Plotting of IRFs without running model
IRFs
How to use matlab to plot multiple irfs from multiple .mat
Plots
Irfs in one figure
Two-sector DSGE, (One of the eigenvalues is close to 0/0)
Eigenvalues and BK conditions (65 Log-Lin Equations)
#2

I’ve tried dealing with this problem by reinstalling DYNARE4.40 exclusively in my personal computer under path C:, but with things unchanged. The error just displays as :

Undefined variable “irf1.” or class “irf1.y_epsilon_A”.

Error in plotting (line 37)
eval(‘irf1.’ var{1,jj},ending_cell{1,ii}]);

I am wondering whether things could be improved or fixed. I have no idea how to figure out the wrongs since I am also quite new to Matlab. So sorry that I have so many questions, yet I have to ask anyone has any idea?

Best,

GOODLUCK


#3

You either need to load irf1 after the last call to Dynare or use the noclearall option.


#4

Thank you very much, jpfeifer. I’ve done it.


#5

Hi,GOODLUCK,

I meet the same question as you, but I still don’t know how to plot them? Can you tell me how you do it? Thank you!

yufeng


#6

I d like to see an example solution as well :slight_smile:
In particular I want to combine 2 irf-outputs from stoch_simul showing the IRFs for certain endogenous variables, for the same model (same endogenous variables, same shock, but different shocksizes) in one picture.


#7

Hello!

I am very new to Matlab. I’ve spent so many days searching for the correct codes in combining plots. I tried also this code, but yes, it is with error. How to remove the error? Jpfeifer suggested to use a noclearall option or load irf1. How exactly to do the suggestion?

Thanks so much for the help in advance.


#8

Please provide more information. Provide both the files and precise instructions on what you are trying to plot from the two mod-files.


#9
dynare Flexible_1.mod
load('Flexible_1_results.mat', 'oo_')
irf1=oo_.irfs;
save irf_1
load irf_1

dynare Flexible_2.mod
load('Flexible_2_results.mat', 'oo_')
irf2=oo_.irfs;
save irf_2
load irf_2

ending_cell={'_eps_A'};

for ii=1:length(ending_cell)
HOR=1:1:20;
var={'Welf'};
figure
for jj=1:length(var)
subplot(1,2,jj) 
eval('IRF_1.' var{1,jj},ending_cell{1,ii}]);
eval('IRF_2.' var{1,jj},ending_cell{1,ii}]);
load IRF_2
end
end

#10

he doen’t work with me at all ! could you help me please


#11

You must either load the first mat-file after the second call to Dynare or invoke the second call to Dynare with the

option. Otherwise, everything you do before

will be deleted.


#12

Hi all

Please hint me in this code:

    dynare dr.mod
    load('dr_results.mat', 'oo_')
    irf1=oo_.irfs;
    save irf_1
    load irf_1


    dynare rd.mod noclearall
    load('rd_results.mat', 'oo_')
    irf2=oo_.irfs;
    save irf_2
    load irf_2
    
    ending_cell={'_epsr'};

    for ii=1:length(ending_cell)
HOR=1:1:40;
var={'y','paii','gam'};
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}])],'LineWidth',1,'-k',HOR,[eval(['irf2.' var{1,jj},ending_cell{1,ii}])],'LineWidth',1,'--r')
title([var{1,jj}] )
end
end

with this Error:

ERROR: Untitled.mod: line 18, col 4: syntax error, unexpected EQUAL

Thank you!
Best wishes…


#13

This is a problem with the mod-file Untitled.mod. The code you pasted is Matlab code and not Dynare code. Run this directly from Matlab.


#14

Thank you very much, jpfeifer.
But after ruining all irf1 and irf2 separately, this error occurred again:

Thank you for your kindly reply.


#15

Be more careful. If you define

irf1
you cannot save

irf_1

Also, you need to load the files after the last call to Dynare, not after you saved them.


#16

Dear all,
For the same reasons, I have used the following code to combine two IRF’s:

 dynare MJT.mod
load('MJT_results.mat', 'oo_')
irf1=oo_.irfs;
save irf1
load irf1


dynare TMJ.mod noclearall
load('TMJ_results.mat', 'oo_')
irf2=oo_.irfs;
save irf2
load irf2

ending_cell={'_E_POS'};

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

the error message is:

Error using plot
Data must be a single matrix Y or a list of pairs X,Y.

What can I do to get the right results please?


#17

You can only have one LineWidth- command. It should be

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') ;

#18
dynare ssfriction_fixe.mod
load('ssfriction_fixe_results.mat', 'oo_')
irf1=oo_.PosteriorIRF.dsge.Var;
save irf1
load irf1

dynare ssfriction_amenage.mod
load('ssfriction_amenage_results.mat', 'oo_')
irf2=oo_.PosteriorIRF.dsge.Var;
save irf2
load irf2

ending_cell={'_e_z'};

for ii=1:length(ending_cell)
HOR=1:1:25;
var={'lab','inv'};
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}]);
load irf1
load irf2
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',25,'--r')
title([var{1,jj}] )
end
end

when i run this code i get this error msg:

"Error using plot Vectors must be the same length.

Error in pf (line 26)
plot(HOR,[eval([‘irf1.’ var{1,jj},ending_cell{1,ii}])],’-k’,HOR,[eval([‘irf2.’
var{1,jj},ending_cell{1,ii}])],‘LineWidth’,25,’–r’)"

can you help me plz !


#19

Did you try load irf1 and irf2 right before the loop?

for jj=1:length(var)

and did you check the lengths of the plotted vectors as the error message claims…?
Cheers
Reuben


#20

yes, when i changed the lengths of the plotted vectors it solved the problem. But now i get a different error msg : " Data must be a single matrix Y or a list of pairs X,Y" even if i’m using a one LineWidth- command.