generate trend by sequence

For requesting general information about EViews, sharing your own tips and tricks, and information on EViews training or guides.

Moderators: EViews Gareth, EViews Moderator

EViews Matt
EViews Developer
Posts: 161
Joined: Thu Apr 25, 2013 7:48 pm

Re: generate trend by sequence

Postby EViews Matt » Wed May 10, 2017 9:46 am

For (1), there are generally multiple ways to form these types of expression in EViews. Some are easier to adapt than others. Here are two alternatives to your code for the first week,

Code: Select all

series first_week2 = @datediff(@date, @datefloor(@date, "mm"), "ww") = 0
series first_week3 = @recode(@month <> @month(-1) or @isna(@month(-1)), 1, 0)

For (2-1) and (2-2), it sounds like you want to take a dummy and make a trend at all the 1s. Your code is on the right track, but you've switched some arguments and made it more complicated than it needs to be. For example, if you have a dummy for the first week, you can build the trend like this,

Code: Select all

series mytrend = @recode(first_week, @cumsum(first_week), 0)

If you didn't want to create the dummy series beforehand, you can of course replace it with the dummy's generating expression. For example,

Code: Select all

series mytrend = @recode(@date < @datefloor(@date, "mm") + 7, @cumsum(@date < @datefloor(@date, "mm") + 7), 0)

For (2-3) and (2-5), you're trying to generate something that is trend-like but is constant within each month. A quick way to implement such pattern variants is find a formula that maps another pattern to the one you want. Based on the examples we've gone through, you should be able to create a basic trend for the first two weeks of each month, similar to (2-1) and (2-2). Ignoring the zeros, that trend of course follows the sequence 1, 2, 3, 4, 5, 6, etc., where as you want 1, 1, 2, 2, 3, 3, etc. You can easily map each number in the first sequence to the corresponding number in the second sequence with a simple formula, f(x) = floor((x + 1) / 2). For (2-3), this could translate into code as follows (using a dummy to keep the line from being too long),

Code: Select all

series first_second_week = @date < @datefloor(@date, "mm") + 14
series mytrend = @recode(first_second_week, @floor((@cumsum(first_second_week) + 1) / 2), 0)

You can use the same strategy for (2-4) and (2-6). Instead of mapping a basic trend to 1, 1, 2, 2, 3, 3, etc., you map it to 1, 2, 1, 2, 1, 2, etc.

Since there are usually may ways to generate any particular pattern, adapting a previous pattern won't always be the most obvious or most concise method. For example, in (2-6) we can make use of the fact that the non-zero elements you want in each month are both consecutive and one more than their preceedings value. These properties can be exploited to generate the series you want in a different way. For example,

Code: Select all

series second_third_week = @date >= @datefloor(@date, "mm") + 7 and @date < @datefloor(@date, "mm") + 21
series mytrend = @recode(second_third_week, mytrend(-1) + 1, 0)

mmc23
Posts: 21
Joined: Sat Jan 11, 2014 9:44 am

Re: generate trend by sequence

Postby mmc23 » Wed May 10, 2017 12:14 pm

Perfect, thank you a lot.
for (2-4) how can fix NA of the first two observation by code?

Code: Select all

series first_second_week = @date < @datefloor(@date, "mm") + 14
series mytrend = @recode(first_second_week, mytrend(-1) + 1, 0)

EViews Matt
EViews Developer
Posts: 161
Joined: Thu Apr 25, 2013 7:48 pm

Re: generate trend by sequence

Postby EViews Matt » Wed May 10, 2017 1:09 pm

One way is to use @nan to replace any NAs generated by mytrend(-1) with zeros.

mmc23
Posts: 21
Joined: Sat Jan 11, 2014 9:44 am

Re: generate trend by sequence

Postby mmc23 » Wed May 10, 2017 1:53 pm

If there is not the function @nan? how can do solve this?

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

Re: generate trend by sequence

Postby EViews Gareth » Wed May 10, 2017 1:57 pm

Write an @nan function.
Follow us on Twitter @IHSEViews

mmc23
Posts: 21
Joined: Sat Jan 11, 2014 9:44 am

Re: generate trend by sequence

Postby mmc23 » Wed May 10, 2017 2:03 pm

I ask if @nan does't exist.
Great!, @nan(mytrend(-1) , 0) + 1.


Return to “General Information and Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 4 guests