DSGE multiple shocks, subfig for each shock/variable?!

This forum is closed. You can read the posts but cannot write. We have migrated the forum to a new location where you will have to reset your password.
Forum rules
This forum is closed. You can read the posts but cannot write. We have migrated the forum to a new location (https://forum.dynare.org) where you will have to reset your password.

DSGE multiple shocks, subfig for each shock/variable?!

Postby Philipp » Thu Feb 28, 2013 8:59 am

Hi all,

I want to do the following:

I have a file "xy_results.mat", which has inside of it among others an object called "oo_", and therein among others an object called "irfs". In "irfs" I have several vectors {y_a, c_a, i_a, y_b,c_b, i_b,...}. Now I would like to have a loop which creates subplots for all {y_a,y_b,...}, {c_a,c_b,...} etc and also subplots in another figure which contains all elements of {_a}, {_b}, etc.

Who can help me on this? Doing it manually I tried something like

load xy_results.mat
HOR=1:1:40;
plot(HOR,oo_.irfs.y_a,'y',HOR,oo_.irfs.c_a,'b')
which works fine, but as I have many entries in irfs and many differens ".mat" I'd prefer to have a loop that does the copy paste for me.

Could somebody please help me out on this issue, most likely with a sample code, I would appreciate your help very much! Best, Philipp
Philipp
 
Posts: 36
Joined: Mon Apr 18, 2011 9:01 pm
Location: State Secretariate for Economic Affairs

Re: DSGE multiple shocks, subfig for each shock/variable?!

Postby kyri82 » Thu Feb 28, 2013 3:15 pm

What I could suggest to you is to have "extentions" as numbers, instead of letters. That is, instead of having y_a and y_b, you could have y_1 and y_2. Then, you could use the eval command to do something like:

for i=1:3
eval(['plot(HOR,oo_.irfs.y_', num2str(i), ','y',HOR,oo_.irfs.c_', num2str(i),'b')']) ;
end

That would execute:

plot(HOR,oo_.irfs.y_1,'y',HOR,oo_.irfs.c_1,'b')
plot(HOR,oo_.irfs.y_2,'y',HOR,oo_.irfs.c_2,'b')
plot(HOR,oo_.irfs.y_3,'y',HOR,oo_.irfs.c_3,'b')
... and so on

You can also load in a similar way .mat files if they have the same name but different numbers at the end.
eval[(load xy_results', num2str(i), ']) ;

Just a warning: the command above (the one with plot) might not work because eval becomes a bit complicated if there are " ' " within the command itself. In any case, check "eval" and "num2str" in help.

Hope that helps.

Kyriacos
kyri82
 
Posts: 141
Joined: Thu Feb 10, 2011 6:14 pm
Location: Toulouse - France

Re: DSGE multiple shocks, subfig for each shock/variable?!

Postby Philipp » Thu Feb 28, 2013 6:22 pm

Hi kyri82,

I have tried with eval(), but my problem is that I have troubles editing the strings in my objects. The extensions "_a" and "_b" come from shocks in dynare, usually something like "_ea" (technology) or else. Now how do I change "_ea" to "1" and "_eb" to "2" and so forth?

Best

Philipp
Philipp
 
Posts: 36
Joined: Mon Apr 18, 2011 9:01 pm
Location: State Secretariate for Economic Affairs

Re: DSGE multiple shocks, subfig for each shock/variable?!

Postby jpfeifer » Thu Feb 28, 2013 11:00 pm

Define a cell containing the blocks over which you want to iterate, e.g.
Code: Select all
ending_cell={'_a',...
'_b'}

Then loop over the endings
Code: Select all
for ii=1:length(ending_cell)
end

Inside of the loop, use the eval command, where you access the ending
Code: Select all
eval(['plot(HOR,oo_.irfs.y_', ending_cell{ii,1}, ','y',HOR,oo_.irfs.c_', ending_cell{ii,1},'b')']) ;
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: DSGE multiple shocks, subfig for each shock/variable?!

Postby kyri82 » Fri Mar 01, 2013 9:24 am

very clever!! ;)
kyri82
 
Posts: 141
Joined: Thu Feb 10, 2011 6:14 pm
Location: Toulouse - France

Re: DSGE multiple shocks, subfig for each shock/variable?!

Postby Philipp » Fri Mar 01, 2013 3:07 pm

indeed, thank you very much for your helpful comments!
Philipp
 
Posts: 36
Joined: Mon Apr 18, 2011 9:01 pm
Location: State Secretariate for Economic Affairs

Re: DSGE multiple shocks, subfig for each shock/variable?!

Postby Philipp » Wed Mar 13, 2013 8:34 pm

Hi guys,

I just wanted to update on the figure discussion, I got a pretty nice figure using

Code: Select all
HOR=1:1:40;
var  ={'y','pi','w','k','i','c','r','q','e'};
ending_cell={'_na','_nb','_ni','_nl','_ng','_npi','_er','_ew','_ep','_eq'};
figure
for jj=1:length(var)
     subplot(3,3,jj)
for ii=1:length(ending_cell)
    [eval(['oo_.irfs.' var{1,jj}, ending_cell{1,ii}])];
    hold on
    plot(HOR,[eval(['oo_.irfs.' var{1,jj}, ending_cell{1,ii}])],'LineWidth',1)
    title(['Response of ' var{1,jj}] )
end
end


thanks again for your ideas!
Philipp
Philipp
 
Posts: 36
Joined: Mon Apr 18, 2011 9:01 pm
Location: State Secretariate for Economic Affairs


Return to Dynare help

Who is online

Users browsing this forum: Google [Bot] and 4 guests