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.
For a list of other statistics you can store in a similar way from an equation, view the data members section of the Equation section of the Object Reference.
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).