Page 4 of 8

Re: An introduction to EViews programming.

Posted: Fri Aug 03, 2012 4:53 am
by Diederick
Dear all,

I am following this topic in order to write a short program to estimate market model regressions.

I have 100 dependent variables , named Y 1-100
and 100 independent variables , named x 1-100.
I would like to regress all the dependent variables y on a constant and the corresponding x.
So equations: y1 c x1 ,y2 c x2 etc.
Then I would like to store all the coefficients of c and x(i) in a matrix.
I have written the following program, which works for the regressions but does not succeed in storing the coefficients.

matrix(2,100) coefs
equation eq
for !i=1 to 100
for !j=1 to 100
if !i=!j then
equation eq{!i}_{!j}.ls y{!i} c x{!j}
colplace(coefs, eq.@coefs, !i)
endif
next
next


Could anyone tell me what is wrong here? Thanks in advance for your answer!

Re: An introduction to EViews programming.

Posted: Fri Aug 03, 2012 6:36 am
by EViews Gareth
You have called your equation

Code: Select all

eq{!i}_{!j}


But then you are asking for the coefficients from an equation called

Code: Select all

eq

Re: An introduction to EViews programming.

Posted: Wed Sep 05, 2012 5:26 am
by kevenbestchan
EViews Gareth wrote:Often, rather than requiring the output from each equation, only some of the summary statistics are required. In this case we can store just those summary statistics, rather than store each equation:

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

'create vector to store r-squares
vector(10) r2s

'create empty equation to be used inside the loop
equation eq

''counter of how many equations we have run
!rowcounter=1

'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)
      eq.ls {%iname} c {%jname}
      r2s(!rowcounter) = eq.@r2
      !rowcounter = !rowcounter+1
   next
next


Program D stores the R-squared from each regression into a vector called R2S. The program creates the vector prior to the loop, and sizes it to the number of equations that will be run (10). The program also creates an empty equation prior to the FOR loop. This equation will be reused inside the loop. We also declare a program variable, !rowcounter. This counter will count how many equations we have estimated, and so which row of R2S we are on.

The FOR loop is almost identical to that in Program C. However rather than create a new named equation, we just re-estimate the previously declared equation, EQ, just changing the specification of the equation each time through the loop. Then we store the R-squared of each equation into the vector R2S at row !rowcounter. Note we use the @r2 data member of the equation to retrieve the R-squared.

Thus the first time through the outer and inner loops, !rowcounter will equal 1, %iname will equal GDP, %jname will equal UNEMP, so that the equation will be specified as GDP C UNEMP, and the R-squared from that equation will be stored in the first row of the vector R2S.



If we want to store the coefficients from each equation, rather than a single summary statistic, we can do so by using a matrix to store the coefficients, rather than a vector:

Code: Select all

'create a workfile
wfcreate q 1990 2010

'create a y series
series y=nrnd

'create 15 X series
for !i=1 to 15
   series x{!i}=nrnd
next

'create matrix to store coefficients.  We'll be running 15 regressions (so 15 columns) with two coefficients in each, so two rows
matrix(2,15) coefs

'create empty equation to be used inside the loop
equation eq

'run pairwise regressions between Y and each X
for !i=1 to 15
   'estimate equation
   eq.ls y c x{!i}
   
   'store coefficients into matrix
   colplace(coefs, eq.@coefs, !i)
next


This program is similar to the first program, where we regress Y against each of 15 X variables, one at a time. However this time the coefficients from each of the 15 regressions are stored in a matrix, called Coefs. Note the command colplace is used to store the column of coefficient estimates into the matrix at the appropriate column number (!i).
I Can‘t run this program on my eviews software of Eviews 6.0

Re: An introduction to EViews programming.

Posted: Wed Sep 05, 2012 8:02 am
by EViews Gareth
I just ran it in EViews 6 without any problems.

Re: An introduction to EViews programming.

Posted: Tue Oct 16, 2012 2:26 am
by bintangdwitya
'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



Hei Garet, if i want the series is not a nrnd numbers, then what should i do?

Thanx

Re: An introduction to EViews programming.

Posted: Tue Oct 16, 2012 7:53 am
by EViews Gareth
Don't use the command that generates random numbers. (The nrnd command).

Re: An introduction to EViews programming.

Posted: Tue Mar 19, 2013 6:14 am
by icprag
Dear Gareth,

i would like to construct a loop in estimating leads in a logl object as presented below (it is a LSTAR model)

logl lstar
lstar.append @logl logl
lstar.append depended_var = alpha(1) + alpha(2)*variable_1(1) + alpha(3)*variable_2(1)+ alpha(4)*d(depended_var(-1))+alpha(5)*d(depended_var(-2)) +(beta(1)+beta(2)*variable_1(1)+beta(3)*variable_2(1)+beta(4)*d(depended_var(-1))+beta(5)*d(depended_var(-2)))/(1+@exp(-theta(1)*(1/1.669)*(variable_1(-1)-theta(2))))
lstar.append res = depended_var - depended_varf
lstar.append var = sig(1)^2
lstar.append z = res/@sqrt(var)
lstar.append logl = log(@dnorm(z)) - log(var)/2
smpl @first+16 @last-17
lstar.ml(showopts, m=1000, c=1e-5)

I would like to estimate it using differents leads ( from 1 to 12) for variable_1 and variable_2 (all these combinations). And then select according to the lowest AIC.

Can you please help me?

Re: An introduction to EViews programming.

Posted: Tue Mar 19, 2013 9:42 am
by EViews Gareth
Just write a loop that loops over the leads of the variable from 1 to 12. Something like:

Code: Select all

for !i=1 to 12
'use variable_1(!i) in your code
next

Re: An introduction to EViews programming.

Posted: Wed Mar 20, 2013 12:53 am
by icprag
Thank you for your direct response. Could you please be more specific i.e in which line of my code shall i add the loop? e.t.c

Re: An introduction to EViews programming.

Posted: Tue May 21, 2013 5:10 am
by nogueira.debora
I would like to store in a vector (or in a group, I don't know) the "s.e. of regression"s for all my 17 equations that I have in a file. What would be the command line for that? Thank you a lot.

Re: An introduction to EViews programming.

Posted: Tue May 21, 2013 7:55 am
by EViews Gareth

Code: Select all

vector(17) ses
%eqs = @wlookup("*", "equation")
for !i=1 to 17
   %eq = @word(%eqs, !i)
   ses(!i) = {%eq}.@se
next

Re: An introduction to EViews programming.

Posted: Mon Jul 22, 2013 7:41 pm
by CharlieEVIEWS
Dear all,

Gradually trying to teach myself how to program after a couple of years of window driven estimations. Relating to the automatic AIC selection on the first page of this topic, I have tried to extend it to a VAR framework, with little success. With minor modifications only to the earlier code:

Code: Select all


wfcreate q 1990 2010
series y = nrnd
series x = nrnd
series z = nrnd

'create empty VAR to be used inside the loop
var vecautoreg

!aic = 99999999

!maxlags = 12

!bestlag = 0

smpl @first+!maxlags @last

for !i=1 to !maxlags
  vecautoreg.ls 1 (-1 to -!i) y x z         ' run the var from lags 1 to i the lag which minimizes the AIC
   if vecautoreg.@aic < !aic then
      !bestlag = !i     
      !aic = eq.@aic
   endif
next

show vecautoreg.ls 1 (-1 to -!i) y x z c   

'reset sample
smpl @all

 


comes the error message "Illegal lag specification in "DO_VECAUTOREG.LS 1(-1 to -1) Y X Z C"".

How do I specific the lag length correctly in this scenario?

Many thanks,

Charlie

Re: An introduction to EViews programming.

Posted: Tue Jul 23, 2013 7:42 am
by EViews Gareth
You can't use negative lags in a VAR.

Re: An introduction to EViews programming.

Posted: Thu Jul 25, 2013 7:08 am
by CharlieEVIEWS
Of course - sorry for misunderstanding! The -i! corresponds to y(-1), y(-2),...,y(-!maxlags) in your original example. I would therefore expect:

Code: Select all

 vecautoreg.ls 1 (1 to !i) y x z         ' run the var from lags 1 to i the lag which minimizes the AIC


to work in my multivariate example, but it does not (giving the same error) - do you have any suggestions as to what a suitable replacement may be?

Re: An introduction to EViews programming.

Posted: Thu Jul 25, 2013 7:36 am
by trubador
There are several other syntax errors in your code. Below you can find the corrected version:

Code: Select all

wfcreate q 1990 2010
series y = nrnd
series x = nrnd
series z = nrnd

'create empty VAR to be used inside the loop
var vecautoreg

!aic = 99999999

!maxlags = 12

!bestlag = 0

smpl @first+!maxlags @last

for !i=1 to !maxlags
  vecautoreg.ls 1 !i y x z         ' run the var from lags 1 to i the lag which minimizes the AIC
   if vecautoreg.@aic < !aic then
      !bestlag = !i     
      !aic = vecautoreg.@aic
   endif
next

show vecautoreg.ls 1 !bestlag y x z   

'reset sample
smpl @all