Building an array from the data to use in a loop

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Moderator, EViews Jason, EViews Matt

Davidth
Posts: 10
Joined: Thu Feb 06, 2014 12:59 pm

Building an array from the data to use in a loop

Postby Davidth » Thu Feb 06, 2014 1:09 pm

I'm working with panel data based upon FYQuarters and location. I am running an LS regression with 5 dependent variable, of which 2 have unique lags. I've created a loop to run the unique lags, but I can't seem to figure out how to build an array to restrict the sample for each location and run the LS regressions separately. I currently have it hard coded, but everytime I make changes to the LS model, I'm having to change it multiple times.

Is there a way to take the unique values determining the cross section and have it build an array of those values and then loop through the regression for each of those? Ideally, I would want it to determine that array from the data rather than having me hard code each location.

I'm using Eviews 7 and my code is:

Code: Select all

!maxi=5 'max number of lags for CERP !maxk=9 'mac number of lags for State ' The following section is setting the headings row for the Results output table '******NOT DONE YET/INSERT HERE ********** 'Regression for RC-Capital smpl if rc="RC-CAPITAL" table RCC ' Results table to store regression statistics !r = 2 ' row for collecting each models data for !i=0 to !maxi for !k=4 to !maxk equation RCC!i!k RCC!i!k.ls cas cerpspnd_sa_ln(-!i) stspnd_sa_ln(-!k) time log(nato) rcc(!r,1) = "RCC - " + @str(!i) + ", " + @str(!k) rcc(!r,2) = rcc!i!k.@rbar2 !maxn = rcc!i!k.@ncoef for !n = 1 to !maxn rcc(!r,!n*2+1) = rcc!i!k.@coefs(!n) !pval = @tdist(rcc!i!k.@tstats(!n), rcc!i!k.@regobs - rcc!i!k.@ncoef) rcc(!r,!n*2+2) = !pval next !r = !r+1 next next 'Regression for RC-EAST smpl if rc="RC-EAST" table RCE ' Results table to store regression statistics !r = 2 ' row for collecting each models data for !i=0 to !maxi for !k=4 to !maxk equation RCE!i!k RCE!i!k.ls cas cerpspnd_sa_ln(-!i) stspnd_sa_ln(-!k) time log(nato) RCE(!r,1) = "RCE - " + @str(!i) + ", " + @str(!k) RCE(!r,2) = rce!i!k.@rbar2 !maxn = rce!i!k.@ncoef for !n = 1 to !maxn RCE(!r,!n*2+1) = rce!i!k.@coefs(!n) !pval = @tdist(rce!i!k.@tstats(!n), rce!i!k.@regobs - rce!i!k.@ncoef) RCE(!r,!n*2+2) = !pval next !r = !r+1 next next 'Regression for RC-North smpl if rc="RC-NORTH" table RCN ' Results table to store regression statistics !r = 2 ' row for collecting each models data for !i=0 to !maxi for !k=4 to !maxk equation RCN!i!k RCN!i!k.ls cas cerpspnd_sa_ln(-!i) stspnd_sa_ln(-!k) time log(nato) RCN(!r,1) = "RCN - " + @str(!i) + ", " + @str(!k) RCN(!r,2) = rcn!i!k.@rbar2 !maxn = rcn!i!k.@ncoef for !n = 1 to !maxn RCN(!r,!n*2+1) = rcn!i!k.@coefs(!n) !pval = @tdist(rcn!i!k.@tstats(!n), rcn!i!k.@regobs - rcn!i!k.@ncoef) RCN(!r,!n*2+2) = !pval next !r = !r+1 next next smpl if rc="RC-SOUTH" table RCS ' Results table to store regression statistics !r = 2 ' row for collecting each models data for !i=0 to !maxi for !k=4 to !maxk equation RCS!i!k RCS!i!k.ls cas cerpspnd_sa_ln(-!i) stspnd_sa_ln(-!k) time log(nato) RCS(!r,1) = "RCS - " + @str(!i) + ", " + @str(!k) RCS(!r,2) = rcs!i!k.@rbar2 !maxn = rcs!i!k.@ncoef for !n = 1 to !maxn RCS(!r,!n*2+1) = rcs!i!k.@coefs(!n) !pval = @tdist(rcs!i!k.@tstats(!n), rcs!i!k.@regobs - rcs!i!k.@ncoef) RCS(!r,!n*2+2) = !pval next !r = !r+1 next next smpl if rc="RC-SOUTHWEST" table RCSW ' Results table to store regression statistics !r = 2 ' row for collecting each models data for !i=0 to !maxi for !k=4 to !maxk equation RCSW!i!k RCSW!i!k.ls cas cerpspnd_sa_ln(-!i) stspnd_sa_ln(-!k) time log(nato) RCSW(!r,1) = "RCSW - " + @str(!i) + ", " + @str(!k) RCSW(!r,2) = rcsw!i!k.@rbar2 !maxn = rcs!i!k.@ncoef for !n = 1 to !maxn RCSW(!r,!n*2+1) = rcsw!i!k.@coefs(!n) !pval = @tdist(rcsw!i!k.@tstats(!n), rcsw!i!k.@regobs - rcsw!i!k.@ncoef) RCSW(!r,!n*2+2) = !pval next !r = !r+1 next next smpl if rc="RC-WEST" table RCW ' Results table to store regression statistics !r = 2 ' row for collecting each models data for !i=0 to !maxi for !k=4 to !maxk equation RCW!i!k RCW!i!k.ls cas cerpspnd_sa_ln(-!i) stspnd_sa_ln(-!k) time log(nato) RCW(!r,1) = "RCW - " + @str(!i) + ", " + @str(!k) RCW(!r,2) = rcw!i!k.@rbar2 !maxn = rcw!i!k.@ncoef for !n = 1 to !maxn RCW(!r,!n*2+1) = rcw!i!k.@coefs(!n) !pval = @tdist(rcw!i!k.@tstats(!n), rcw!i!k.@regobs - rcw!i!k.@ncoef) RCW(!r,!n*2+2) = !pval next !r = !r+1 next next

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13604
Joined: Tue Sep 16, 2008 5:38 pm

Re: Building an array from the data to use in a loop

Postby EViews Gareth » Thu Feb 06, 2014 2:22 pm

Something like:

Code: Select all

for %cross %code "RC-CAPITAL" "RCC" "RC-EAST" "RCE" "RC-NORTH" "RCN" smpl if rc=%cross table {%code} ' Results table to store regression statistics !r = 2 ' row for collecting each models data for !i=0 to !maxi for !k=4 to !maxk equation {%code}!i!k {%code}!i!k.ls cas cerpspnd_sa_ln(-!i) stspnd_sa_ln(-!k) time log(nato) {%code}(!r,1) = %code + " - " + @str(!i) + ", " + @str(!k) {%code}(!r,2) = {%code}!i!k.@rbar2 !maxn = {%code}!i!k.@ncoef for !n = 1 to !maxn {%code}(!r,!n*2+1) = {%code}!i!k.@coefs(!n) !pval = @tdist({%code}!i!k.@tstats(!n), {%code}!i!k.@regobs - {%code}!i!k.@ncoef) {%code}(!r,!n*2+2) = !pval next !r = !r+1 next next next

Davidth
Posts: 10
Joined: Thu Feb 06, 2014 12:59 pm

Re: Building an array from the data to use in a loop

Postby Davidth » Fri Feb 07, 2014 7:37 am

Eviews Gareth,

This is almost perfect. The thing I'm missing is if there is a way for this line:

Code: Select all

for %cross %code "RC-CAPITAL" "RCC" "RC-EAST" "RCE" "RC-NORTH" "RCN"
to determine these for the data. For example, I'm currently using 6 cross sections, but I have another dataset that I would like to use the same program on with 20 cross sections. Rather than having to change that code each time I use it on a new workfile, I'm trying to have it determine on its own that there are x-unique cross sections and thus it needs to use those unique cross sections for the loop.

Thanks again for your help and this is helpful regardless.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13604
Joined: Tue Sep 16, 2008 5:38 pm

Re: Building an array from the data to use in a loop

Postby EViews Gareth » Fri Feb 07, 2014 8:40 am

Depends - is there something in the data that contains the cross-sections, and codes?

Davidth
Posts: 10
Joined: Thu Feb 06, 2014 12:59 pm

Re: Building an array from the data to use in a loop

Postby Davidth » Fri Feb 07, 2014 8:52 am

Cross sections, yes. Codes, no, but I can build those later if I understand how to do the cross section.

The data is in panel format over 2008Q1 to 2013Q4 and there is an alpha series with the names of the RCs that provide the cross-section. I have included a picture of the data which I would want it to grab for the loop. Does that answer your question?
Attachments
RCs.JPG
RCs.JPG (107.62 KiB) Viewed 5458 times

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13604
Joined: Tue Sep 16, 2008 5:38 pm

Re: Building an array from the data to use in a loop

Postby EViews Gareth » Fri Feb 07, 2014 8:57 am

Code: Select all

svector crosssections = @uniquevals(rc) for !i=1 to @rows(crosssections) %cross = crosssections(!i) 'do all your loop stuff in here next

Davidth
Posts: 10
Joined: Thu Feb 06, 2014 12:59 pm

Re: Building an array from the data to use in a loop

Postby Davidth » Fri Feb 07, 2014 9:01 am

ahhh, we were trying rowvector.

Thanks a million. This works perfectly.


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 1 guest