Page 1 of 1
Creating an equation from a coefficient vector and a group
Posted: Sat Jan 05, 2013 8:58 am
by mtimmes
Hope this isn't too basic... but I couldn't find a solution on my own or on the forum.
I'm passing a group of exo series and a dependent series to R and retrieving a coefficient array (n rows x 1 col) from the NNLS package from R.
I'd like to create a new equation using the new coefficient array and the group of exo series.
The issue is that this is in a loop so the size of the group (and series therein) and length of the coeff array will constantly be changing. I think it's the equivalent of vector multiplication between the coef array and the series within the group, but I'm not sure how to do this.
Any suggestions? Thanks.
Re: Creating an equation from a coefficient vector and a gro
Posted: Sat Jan 05, 2013 1:54 pm
by EViews Gareth
I'm not quite sure what you're trying to do. Estimate an equation with fixed coefficients?
Re: Creating an equation from a coefficient vector and a gro
Posted: Mon Jan 07, 2013 2:18 pm
by mtimmes
Well, I don't know if its estimate per se, but yes, create a number of equations based on a varying coefs vector and different groups of variables. The equations will then be used for forecasting and to feed other estimation methods. In concept it's reducing a large number of Econometric variables through NNLS (almost like stepwise LS) and then continuing the analysis back in eviews. Thanks for any help.
Re: Creating an equation from a coefficient vector and a gro
Posted: Mon Jan 07, 2013 2:24 pm
by EViews Gareth
Unfortunately you cannot create an equation with fixed coefficients for use in forecasting.
However, you can use a model object (indeed, that's what a model object is for).
You'll still have to write a loop to add them all though. You'll have to loop through the members of the group using group.@seriesname, and loop through the elements of the coefficient vector.
Re: Creating an equation from a coefficient vector and a gro
Posted: Tue Jan 08, 2013 1:12 am
by mtimmes
So I'm sure there is a more elegant way to do this, both statistically and programmatically, but...
The approach is to (1) bring the fitted values back from the NNLS function in R (2) fit an EViews LS equation with the full group of independent variables (fortunately EViews always seems to come up with the exact same coefficients) and (3) clean-up the "near 0" coefficients in the new LS equation, by adding any variables with coefficients greater than a particular value.
Code: Select all
smpl 2002m01 2012m11 'set sample period for NNLS
xopen(type=r, case=upper) 'open a connection to R with upper-case default output names
xput(rtype=data.frame,name=m1) econs 'put regressors and dependent variable into R
xput(rtype=data.frame, name=m2) depen
xrun "library (nnls)" 'load NNLS package in R
xrun "rm1 <- as.matrix(m1)" 'NNLS needs data in matrix form
xrun "rm2 <- as.matrix(m2)"
xrun "nnlsout <- nnls (rm1,rm2)" 'run NNLS
xrun "nncoef <-coef(nnlsout)" 'get NNLS coefficients
xrun "nnfit <- fitted(nnlsout)" 'get NNLS fitted data
xget(name=nncoef, type=vector) nncoef 'retrieve coefs into EViews
xget(name=nnfit, type=series) nnfit 'retrieve NNLS fitted data into EViews
equation e1.ls nnfit econs 'create first EViews equation with regressors
%nnvarslist = e1.@varlist 'get list of E1 variables
%nnvarslist2 = @replace(%nnvarslist,"NNFIT","") 'remove fitted series from list
group econs2 'create new group
econs2.drop *
!i=1
for %e2 {%nnvarslist2} 'put NNLS variables into a reduced group of regressor
%nnvar = @wlookup(%e2)
if e1.@coef(!i) > .00001 then
econs2.add {%nnvar} 'add only variables with significant coefficients
endif
!i = !i +1
next
equation eq2.ls nnfit econs3 'fit the reduced variable group to the NNLS output
xclose 'close R connection