An introduction to EViews programming.
Moderators: EViews Gareth, EViews Jason, EViews Moderator
Re: An introduction to EViews programming.
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 1100
and 100 independent variables , named x 1100.
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!
I am following this topic in order to write a short program to estimate market model regressions.
I have 100 dependent variables , named Y 1100
and 100 independent variables , named x 1100.
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!

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 11730
 Joined: Tue Sep 16, 2008 5:38 pm
Re: An introduction to EViews programming.
You have called your equation
But then you are asking for the coefficients from an equation called
Code: Select all
eq{!i}_{!j}
But then you are asking for the coefficients from an equation called
Code: Select all
eq
Follow us on Twitter @IHSEViews

 Posts: 1
 Joined: Sun Sep 02, 2012 10:11 am
Re: An introduction to EViews programming.
I Can‘t run this program on my eviews software of Eviews 6.0EViews 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 rsquares
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.@count1
%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 Rsquared 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 reestimate the previously declared equation, EQ, just changing the specification of the equation each time through the loop. Then we store the Rsquared of each equation into the vector R2S at row !rowcounter. Note we use the @r2 data member of the equation to retrieve the Rsquared.
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 Rsquared 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).

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 11730
 Joined: Tue Sep 16, 2008 5:38 pm
Re: An introduction to EViews programming.
I just ran it in EViews 6 without any problems.
Follow us on Twitter @IHSEViews

 Posts: 1
 Joined: Sun Oct 14, 2012 7:45 pm
Re: An introduction to EViews programming.
'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.@count1
%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
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.@count1
%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

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 11730
 Joined: Tue Sep 16, 2008 5:38 pm
Re: An introduction to EViews programming.
Don't use the command that generates random numbers. (The nrnd command).
Follow us on Twitter @IHSEViews
Re: An introduction to EViews programming.
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 @last17
lstar.ml(showopts, m=1000, c=1e5)
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?
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 @last17
lstar.ml(showopts, m=1000, c=1e5)
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?

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 11730
 Joined: Tue Sep 16, 2008 5:38 pm
Re: An introduction to EViews programming.
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
Follow us on Twitter @IHSEViews
Re: An introduction to EViews programming.
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

 Posts: 1
 Joined: Tue May 21, 2013 5:05 am
Re: An introduction to EViews programming.
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.

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 11730
 Joined: Tue Sep 16, 2008 5:38 pm
Re: An introduction to EViews programming.
Code: Select all
vector(17) ses
%eqs = @wlookup("*", "equation")
for !i=1 to 17
%eq = @word(%eqs, !i)
ses(!i) = {%eq}.@se
next
Follow us on Twitter @IHSEViews

 Posts: 202
 Joined: Tue Jul 17, 2012 9:47 am
Re: An introduction to EViews programming.
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:
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
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

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 11730
 Joined: Tue Sep 16, 2008 5:38 pm
Re: An introduction to EViews programming.
You can't use negative lags in a VAR.
Follow us on Twitter @IHSEViews

 Posts: 202
 Joined: Tue Jul 17, 2012 9:47 am
Re: An introduction to EViews programming.
Of course  sorry for misunderstanding! The i! corresponds to y(1), y(2),...,y(!maxlags) in your original example. I would therefore expect:
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?
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.
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
Who is online
Users browsing this forum: Google [Bot] and 2 guests