Quick (easy) question

For questions regarding programming in the EViews programming language.

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

jason_ll
Posts: 43
Joined: Thu Sep 01, 2011 11:38 am

Quick (easy) question

Postby jason_ll » Tue Apr 30, 2013 9:32 am

Hi there, pleaes help me with this quick question.

I have a panel data, where I'd like to assign a value to a series as follows:

Code: Select all

series test = matrix(age,3)
where "matrix" is the name of a matrix object and "age" is a series that contains the age of every person in the panel.

The problem is that, instead of using the actual age of every cross-section in a given time, EViews always assumes that age is equal to the age of the very first cell in my sample.

For example, if the age of crossid = 1 and dateid = 1 was 25, then "test" would be set equal to matrix(25,3) for all cross-sections and all time period.

How can I make EViews understand that I want age to equal the age of each observation in the panel? If only @elem existed for panels, I would have written:

Code: Select all

test = matrix(@elem(age,@year,@crossid),3)
but @elem doesn't seem to work in a panel.

Thanks very much!

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

Re: Quick (easy) question

Postby EViews Gareth » Tue Apr 30, 2013 9:39 am

I have no idea what you're trying to achieve.

jason_ll
Posts: 43
Joined: Thu Sep 01, 2011 11:38 am

Re: Quick (easy) question

Postby jason_ll » Tue Apr 30, 2013 9:51 am

Ok, I'll try to explain better.

Let's suppose this is what the matrix "matrix" looks like:

1 25%
. .
. .
. .
20 5%
21 20%
22 15%
23 12%
24 40%
25 50%

etc...

It has 100 rows. Column 1 contains the row number (from 1 to 100). Column 2 is empty. Column 3 has some percentage numbers.

I have a panel with 1,000 cross-sections and 10 years of data (2011 to 2010).
I have a series in the panel called "age", which contains the age of each cross-section at a given point in time.

By doing this:

Code: Select all

series test = matrix(age,3)
I was hoping/expecting that EViews would set each value of "test" to the percentage figure in column 3, based on the age series value of each observation.

For example, for the year 2014, let's say the "age" value for crossid 10 was equal to 20.

I would want "test" to be computed for crossid 10 and year 2014 as follows:

test = matrix(20,3)

But instead, I get one single value in all the "test" observations. Instead of computing matrix (20,3) (for that particular observation), I get test = matrix (1,3) for ALL the observations in my panel!

I.e. all the "test" cells have the same value, instead of varying by "age".

I hope this clarifies what I'm trying to do? If not, I'd be happy send you my program.

Thanks again

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

Re: Quick (easy) question

Postby EViews Gareth » Tue Apr 30, 2013 10:11 am

So you want to do a look up in the first column of a matrix, based upon the value of a series, and return the value in the third column of the matrix, and do that series-wise.

Yeah, that's not going to happen in one line. Something like this might work though:

Code: Select all

series test for !i=1 to @obsrange !j = age(!i) test(!i) = x(!j,3) next

jason_ll
Posts: 43
Joined: Thu Sep 01, 2011 11:38 am

Re: Quick (easy) question

Postby jason_ll » Tue Apr 30, 2013 10:21 am

So you want to do a look up in the first column of a matrix, based upon the value of a series, and return the value in the third column of the matrix, and do that series-wise.

Yeah, that's not going to happen in one line. Something like this might work though:

Code: Select all

series test for !i=1 to @obsrange !j = age(!i) test(!i) = x(!j,3) next
THANK YOU, this worked very nicely.
I didn't know you could refer to a series in such a way in a panel. This is pretty handy.

jason_ll
Posts: 43
Joined: Thu Sep 01, 2011 11:38 am

Re: Quick (easy) question

Postby jason_ll » Tue Apr 30, 2013 4:41 pm

Ok, sorry to keep bugging you, but I am still struggling with this.

While your solution worked, I still ran into two problems:

1. I'm working in a sub-sample. I have sample restrictions based on both year and cross-sections. This create problems because if I do:

Code: Select all

For !i = 1 to @obsrange
Some of the observations will be outside the sample, which gives me errors, since EViews interprets the NAs as zeros.
It returns an error saying x(0,3) is not a valid matrix reference.

I could solve the issue by adding an if statement that skips the loop if there are NAs, but this brings me to my next problem:

2. It is way too compute-intensive. My sample size is actually about 5 million observations. And I'd need about 6 of these loops. This takes an insane amount of time.

This is really frustrating. I just don't know why matrix(age,3) won't take the age of each observation - it seems like the obvious thing to do...

Anyway, if you have another solution I would be very grateful.

jason_ll
Posts: 43
Joined: Thu Sep 01, 2011 11:38 am

Re: Quick (easy) question

Postby jason_ll » Tue Apr 30, 2013 5:46 pm

And one more issue. I don't know if it's a glitch in the system, but my new variables always seem to be lagging. I'll explain by providing some code.

Code: Select all

wfcreate(wf=Smoking_Cessation, page=Panel) a 2011 2035 1000 series age = @round(rnd*100) For !x = 1 to 25000 !j = age(!x) series test(!x) = !j next show age test
When I ran it, I got this for example:

obs AGE TEST
1 - 11 3
1 - 12 42 3
1 - 13 88 42
1 - 14 34 88
1 - 15 9 34
1 - 16 58 9
1 - 17 38 58

as you can see test is equal to the past age instead of the current one. I would also appreciate an explanation and fix. Thanks

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

Re: Quick (easy) question

Postby EViews Gareth » Tue Apr 30, 2013 6:02 pm

You can use @obssmpl to find the number of observations in the current sample.

There is no more efficient method.

As to your second question, declare the series outside the loop, then don't use the series declaration inside the loop.

jason_ll
Posts: 43
Joined: Thu Sep 01, 2011 11:38 am

Re: Quick (easy) question

Postby jason_ll » Tue Apr 30, 2013 7:40 pm

As to your second question, declare the series outside the loop, then don't use the series declaration inside the loop.
Thanks.
There is no more efficient method.
Fair enough. I really appreciate your help.

jason_ll
Posts: 43
Joined: Thu Sep 01, 2011 11:38 am

Re: Quick (easy) question

Postby jason_ll » Tue Apr 30, 2013 8:43 pm

You can use @obssmpl to find the number of observations in the current sample.
But is there a way I can stop the If loop from running if the observation lies outside the sample?

In particular, I have:

Code: Select all

smpl if age<50 For !i = 1 to @obsrange !j = age(!i) If !j>0 then test(!i) = x(!j,3) else endif next
I added the If loop hoping that it would prevent the For loop from running whenever the observation lies outside the sample.

But that didn't happen. Instead, "test(!i) = x(!j,3)" was computed over the entire sample.

It seems that !j = age(!i) will compute even if !i is outside the sample.

Any ideas?

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

Re: Quick (easy) question

Postby EViews Gareth » Tue Apr 30, 2013 8:46 pm

You're probably better off not using samples.

jason_ll
Posts: 43
Joined: Thu Sep 01, 2011 11:38 am

Re: Quick (easy) question

Postby jason_ll » Tue Apr 30, 2013 8:50 pm

You're probably better off not using samples.
I'm not sure I have that option. In my model, observations (people) will behave differently based on their age. Therefore, it is necessary that I restrict the sample.

I guess my only other option is to use the recode?

With 100 age groups, that's a really long and ugly line of code, but if it's my only choice...

jason_ll
Posts: 43
Joined: Thu Sep 01, 2011 11:38 am

Re: Quick (easy) question

Postby jason_ll » Tue Apr 30, 2013 9:07 pm

I guess my only other option is to use the recode?
Even @recode doesn't work. I have so many recodes that it says "equation too complex". :cry:

Is there really no way to prevent the For loop from computing out of sample!!? :x

jason_ll
Posts: 43
Joined: Thu Sep 01, 2011 11:38 am

Re: Quick (easy) question

Postby jason_ll » Tue Apr 30, 2013 10:24 pm

Well I ended up lumping in the age in groups. This made the @recode equation more manageable and the program ended up solving.

So it's an acceptable compromise for now, but if anyone has any ideas, I'll be checking so thanks.


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 2 guests