Re: An introduction to EViews programming.
Posted: Fri Aug 12, 2011 7:56 am
EViews doesn't have function plotting abilities yet.
is this possible?This program is estimates a number of different equations, each with the same dependent variable, but with different numbers of lags of the dependent variable as regressors. It then selects the number of lags that produces the highest Akaike Information Criterion (AIC). An important aspect of this program is the ability in EViews to specify multiple lags at once by using the form X(-i to -j), which tells EViews to include all lags of X from the ith lag to the jth.
Note that when comparing models with differing numbers of lags, it is important to ensure the sample (the number of observations) used in each model is the same. Thus the program sets the workfile sample to miss the first !maxlags observations, where !maxlags is the maximum number of lags that will be included in the models.The program uses a program variable, !aic, to keep track of the lowest AIC value. This value is initialized to a large number (so that the first equation will definitely have an AIC lower than the initial value). The variable !maxlags is used to specify the maximum number of lags that will be compared. The variable !bestlag is used to keep track of the lag associated with the lowest AIC value.Code: Select all
'create a workfile wfcreate q 1990 2010 'create a variable series y = nrnd 'create empty equation to be used inside the loop equation eq 'variable to store the minimum AIC. Initialise it to a large number !aic = 99999999 'variable saying how many lags to go up to !maxlags = 12 'Variable to store the "best" number of lags !bestlag = 0 'set sample to be the !maxlag'th value onwards smpl @first+!maxlags @last for !i=1 to !maxlags eq.ls y c y(-1 to -!i) 'run regression of Y on a constant and lagged values of itself up to the iTH lag. if eq.@aic < !aic then !bestlag = !i 'if this lag specification has the best AIC, then store this lag as !bestlag. !aic = eq.@aic endif next show eq.ls y c y(-1 to -!bestlag) 'reset sample smpl @all
The smpl command is used to change the workfile sample. The sample is specified by a pair of dates (separated by space), or observation numbers. The @first keyword to specifies the first observation of the workfile, and the @last keyword specifies the last. Thus the line smpl @first+!maxlags @last sets the sample to the !maxlags’th plus one observation to the last observation of the workfile . i.e. if !maxlags I set at 12, the sample will be the 13th observation onwards.
The FOR loop runs from 1 to !maxlags, and thus will run 12 times (since !maxlags has been set to 12). Each iteration re-specifies the equation EQ to include lags up to !i. Once the equation has been re-specified and estimated, an if condition is used to test whether the AIC value from that equation (referenced with the @aic data member) is smaller than the current “best” AIC value stored in !aic. If the current value is smaller, then the current lag is recorded as !bestlag, and the “best” AIC value is reset to the current AIC.
Finally after the loop has finished, the equation is re-estimated using a number of lags equal to !bestlag.
Code: Select all
%waldstring = ""
for !i=2 to !bestlags
%waldstring = %waldstring + "C(" + @str(!i) + ") = 0, "
next
%waldstring = @left(%waldstring, @length(%waldstring)-2) 'trim off the last comma and space
eq.wald {%waldstring}
Program 3 is almost the same as the second program, above, but rather than creating series via numerical program variables, a string variable is used instead:In this program, after creating the workfile we create an empty group that will later be used to hold all of the X variables. We name this group XS.Code: Select all
'create a workfile wfcreate q 1990 2010 'create a group which will contain the xs group xs 'create 5 series for %i GDP UNEMP INFL CPI M1 series {%i}=nrnd xs.add {%i} next 'run pairwise regressions between each series for !i=1 to xs.@count-1 %iname = xs.@seriesname(!i) for !j=!i+1 to xs.@count %jname = xs.@seriesname(!j) equation eq_{%iname}_{%jname}.ls {%iname} c {%jname} next next
The FOR loop that creates the series loops through the %i string variable which takes the values of “GDP”, “”UNEMP”, “INFL”, “CPI” and finally “M1”. Each iteration of the loop creates a series with a name equal to the current value of %i. The newly created series is then added to the XS group. Thus the first time through the loop a series called GDP is created, and then added to the XS group. The second time through the loop, a variable called UNEMP is created and added to the XS group.
The second FOR loop is a little more complicated than Program 2. The first thing to notice is the use of the @count group data member in the loops. The @count member returns the number of series in a group.
Thus since there are 5 series in the XS group, xs.@count will equal 5, and xs.@count-1 will equal 4.
The second element of note in the second loop is the use of the @seriesname(i) data member. This data member returns the name of the ith series in the group. Thus xs.@seriesname(1) would return “GDP”.
The first time through the outer loop, %iname will be equal to “GDP” (since !i = 1, xs.@seriesname(!i) will return the name of the first series in XS). The first time through the inner loop, %jname will be equal to “UNEMP” (since !j=!i+1 = 2, xs.@seriesname(!j) will return the name of the second series). The second time through the inner loop, %jname will equal “INFL”, and so on.
At the end of the outer for loop, we will have a number of equations created:Code: Select all
Value of !i(outer loop) Value of !j(inner loop) Resulting Equation Name 1 2 EQ_GDP_UNEMP 1 3 EQ_GDP_INFL 1 4 EQ_GDP_CPI 1 5 EQ_GDP_M1 2 3 EQ_UNEMP_INFL 2 4 EQ_UNEMP_CPI 2 5 EQ_UNEMP_M1 3 4 EQ_INFL_CPI 3 5 EQ_INFL_M1 4 5 EQ_CPI_M1
Code: Select all
'create a workfile
wfcreate q 1990 2010
'create a dependent variable
series y = nrnd
'create 3 series
series x1 = nrnd
series x2 = nrnd
series x3 = nrnd
%regs = "x1 x2 x3"
!L = @wcount(%regs)
'create vector to store r-squares, AICs, BICs
vector(2^!L) r2s
vector(2^!L) aics
vector(2^!L) bics
svector(2^!L) specs
'create empty equation to be used inside the loop
equation eq
'1. Make an index vector which contains either 0 or 1 (Convert2Binary)
'2. drop x if index=0, otherwise take x
!rowcounter=1 'counter of how many equations we have run
for !i = 0 to (2^!L)-1
vector(!L) index
!temp= !i
call Convert2Binary(index, !temp)
%xs = ""
for !j = 1 to !L
%reg = @word(%regs,!j)
if index(!j)==1 then
%xs = %xs + %reg + " "
endif
next
eq.ls y c {%xs}
r2s(!rowcounter) = eq.@r2
aics(!rowcounter) = eq.@aic
bics(!rowcounter) = eq.@schwarz
specs(!rowcounter) = eq.@spec
!rowcounter = !rowcounter+1
next
d(noerr) index
subroutine local Convert2Binary(vector out, scalar N)
if N<0 then
stop
return
endif
!i = @rows(out)
while N > 1
!a = @floor(N/2)
out(!i) = N-2*!a
!i = !i - 1
N = !a
wend
out(!i) = N
endsub