getting s.e. from ridgereg.prg

For questions about EViews Add-ins available from the EViews Add-ins webpage. Note each add-in available on our webpage will have its own individual thread.

Moderators: EViews Gareth, EViews Moderator, EViews Esther

phs
Posts: 9
Joined: Thu Oct 20, 2011 11:54 am

getting s.e. from ridgereg.prg

Postby phs » Thu Oct 20, 2011 12:18 pm

I use EViews 7.2 with the add-in ridgereg.prg (http://www.eviews.com/Addins/addins.shtml)

I would like to have help with modifying the code in ridgereg.prg (see the attached code) so that I can OBTAIN STANDARD ERRORS from the estimated slope coefficients in the ridge regression model. As it is now, the add-in only provides coefficient estimates.

Code: Select all

include .\ridgelib.prg

logmode +addin

%sample = @pagesmpl
%origsmpl = %sample
if @len(%args) then
   %dep = @word(%args,1)
   %regs = @wmid(%args,2)
   %lambda = @equaloption("L")
   if @len(%lambda)=0 then
      %lambda = "0"
   endif
else
   %dep = ""
   %regs = ""
   %lambda = "0"
   !result = @uidialog("caption", "Ridge Regression", "edit", %dep, "Enter a dependent variable", "edit", %regs, "Enter a list of regressors (a constant will be automatically added)",250, "edit", %lambda, "Enter a value for the biasing constant", "edit", %sample, "Sample")
   if !result = -1 then
      stop
   endif
endif


'check valid value for lambda
!l = @val(%lambda)
if !l = NA then
   @uiprompt("Invalid value for the constant")
   stop
endif

'check valid series name for dep
if @len(%dep) = 0 then
   @uiprompt("Please enter a series name for the dependent variable")
   stop
endif
if @isobject(%dep) = 0 then
   %msg = %dep + " is not a valid series in your workfile"
   @uiprompt(%msg)
   stop
endif
if {%dep}.@type <> "SERIES" then
   %msg = %dep + " is not a valid series in your workfile"
   @uiprompt(%msg)
   stop
endif

'check valid regressors
if @len(%regs) = 0 then
   @uiprompt("Please enter a list of regressors")
   stop
endif

setmaxerrs 2
%gname = @getnextname("_g")
group {%gname} {%regs}
if @lasterrnum>0 then
   d(noerr) {%gname}
   @uiprompt(   "Invalid list of regressors")
   stop
endif

'check sample
smpl {%sample}
if @lasterrnum>0 then
   d(noerr) {%gname}
   @uiprompt(   "Invalid sample")
   smpl {%origsmpl}
   stop
endif


%tab = "results"
if @isobject(%tab) then
   %tab = @getnextname("results")
endif
table {%tab}

call ridgetab({%tab}, {%dep}, {%gname}, !l, %sample)
show {%tab}

smpl {%origsmpl}
d {%gname}


'on input tab should be the table to contain output. Y is the dependent variable, x is the group of regressors, lambda is the bias constant, and smplstr is the input text for the sample
subroutine local ridgetab(table tab, series y, group x, scalar !lambda, string %smplstr)
   vector bcoef
   vector bridge
   !R2=0
   vector vifs
   
   series smplser = 1
   call ridgereg(bcoef, bridge, vifs, !r2, y, x, !lambda, smplser)
   %obs = @str(@obs(smplser))
   
   !row=1
   tab.setwidth(1) 19   
   tab.setwidth(2) 10   
   tab.setwidth(3:4) 11
   
   %depstr = "Dependent Variable: " + @upper(y.@name)
   tab(!row,1) = %depstr
   tab.setjust(!row,1) left
   !row = !row + 1
   
   %title = "Ridge Regression"
   tab(!row,1) = %title
   tab.setjust(!row,1) left
   !row = !row + 1
   
   %datestring = "Date: " + @datestr(@now,"MM/DD/YY") + "   Time: " + @datestr(@now,"HH:MI")
   tab(!row,1) = %datestring
   tab.setjust(!row,1) left
   !row = !row + 1            
   
   %smpl = "Sample: " + %smplstr
   tab(!row,1) = %smpl
   tab.setjust(!row,1) left
   !row = !row + 1
   
   %ob = "Included observations: " + %obs
   tab(!row,1) = %ob
   tab.setjust(!row,1) left
   !row = !row+1
   
   %lambda = "Lambda: " + @str(!lambda)
   tab(!row,1) = %lambda
   tab.setjust(!row,1) left
   !row = !row+1
   tab.setlines(!row, a, !row, e) +d
   !row = !row+1
   
   
   tab(!row,1) = "Variable"
   tab(!row,2) = "Raw Ridge"
   tab(!row,3) = "Std. Ridge"
   tab(!row,4) = "V.I.F"
   !row = !row+1
   tab.setlines(!row, a, !row, e) +d
   !row = !row+1
   
   for !i=2 to x.@count
      %n = x.@seriesname(!i)
      tab(!row,1) = %n
      tab(!row,2) = bcoef(!i)
      tab(!row,3) = bridge(!i-1)
      tab(!row,4) = vifs(!i-1)
      !row = !row+1
   next
   tab.setlines(!row, a, !row, e) +d
   !row = !row+1
   tab(!row,1) = "R-squared:"
   tab.setjust(!row,1) left
   tab(!row,2) = !r2
endsub



EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13318
Joined: Tue Sep 16, 2008 5:38 pm

Re: getting s.e. from ridgereg.prg

Postby EViews Gareth » Thu Oct 20, 2011 1:10 pm

The Ridgereg calculations currently do not calculate standard errors at all, so you'll have to program up the calculations yourself. The routine that calculates ridgereg itself (in the file ridgelib.prg) is pretty simple.
Follow us on Twitter @IHSEViews

phs
Posts: 9
Joined: Thu Oct 20, 2011 11:54 am

Re: getting s.e. from ridgereg.prg

Postby phs » Thu Oct 20, 2011 1:43 pm

OK, thanks! Can you give some solid hints on how to obtain these standard errors for the slope coefficient estimates? Should I use bootstrapping to obtain the standard errors, or do not you suggest something else? There is group.resample but since I cannot find a group resample (bootstrap in pairs) for matrices, so it is difficult to estimate the ridge by myself and do resampling, since ridge is done by matrices. Therefore it is best to modify ridgelib.prg and save the standard errors in some way.

Please be as concrete as possible on where and how to change the code. I want to see if the estimated slope coefficient is significantly different from zero.

Ridgelib.prg is attached below:

Code: Select all

' ridge regression  (following Neter, Kutner, et al, p. 411-413)
' on input:
'    y - dependent variable
'    x - group consisting of independent variables (including C if desired)
'    lambda - ridge adjustment
'      smplser -  a series equal to 1 at every observation you want to use.
' on output:
'   b - coefficient estimate
'    bridge - standardized coefficient estimate
'   vif - variance inflation factors
'      r2 - r-squared

subroutine local ridgereg(vector b, vector bridge, vector vif, scalar !r2, series y, group g, scalar lambda, series smplser)
   local smpl
   
   ' drop missing observations
   smpl if y<>na and @rnas(g)=0 and smplser=1
   series temp = 1
   smplser = temp
   !obs = @obs(y)
   !obsm1 = !obs - 1
   
   ' get a modified version of the original group after dropping the constnat
   group tempx g
   tempx.drop c
   
   ' compute the ridge regression and standardized ridge regression coefficients
   group tempyx y tempx
   sym ss = @cov(tempyx)*!obs ' sum-of-deviations from mean
   sym xx = @subextract(ss, 2, 2)  ' centered x'x
   matrix xy = @subextract(ss, 2, 1, @rows(ss), 1) ' centered x'y
   vector dscale = @getmaindiagonal(xx) ' diagonal scale
   sym dd = @makediagonal(@epow(dscale,  -.5) ) ' diagonal scale weighting matrix
   vector btemp =  dd * @inverse(dd*xx*dd+ lambda*@identity(@columns(xx))) * dd *  xy  ' get ridge regression coefs
   vector bridge = @makediagonal(@epow(dscale, .5)) * btemp / @sqrt(ss(1)) ' get standardized ridge regression coefficients
   matrix pmat = @inverse(dd*xx*dd + lambda*@identity(@columns(xx))) * dd*xx*dd * @inverse(dd*xx*dd + lambda*@identity(@columns(xx)))
   vif = @getmaindiagonal(pmat)
   
   ' final adjustments to handle c coefficient and to export coefficients in proper order
   group tempx2 c tempx
   %names = g.@members
   g.drop {%names}
   g.add tempx2
   vector(g.@count) b
   !j = 1
   !cval = @mean(y)
   for !i=2 to g.@count
         b(!i) = btemp(!j)
         %name = g.@seriesname(!i)
         !cval = !cval - @mean({%name})*b(!i)
         !j = !j + 1
   next
   b(1) = !cval
   
   'calculate r-squared
   call r2(!r2,b,y,g)   
endsub


subroutine local r2(scalar !R2, vector coefs, series y, group g)
   !meany = @mean(y)
   !sstot = @sumsq(y -!meany)
   series residuals = y
   for !j=1 to g.@count
      residuals = residuals - coefs(!j)*g(!j)
   next
   !ssr = @sumsq(residuals)
   !r2 = 1-!ssr/!sstot
endsub

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13318
Joined: Tue Sep 16, 2008 5:38 pm

getting s.e. from ridgereg.prg

Postby EViews Gareth » Thu Oct 20, 2011 2:14 pm

I have no idea how to calculate standard errors for ridge regressions.
Follow us on Twitter @IHSEViews

phs
Posts: 9
Joined: Thu Oct 20, 2011 11:54 am

Re: getting s.e. from ridgereg.prg

Postby phs » Thu Oct 20, 2011 3:20 pm

OK, is there anyone else on this forum with any idea on how to estimate standard errors (or t-stats or critical values) for ridge coefficients?

phs
Posts: 9
Joined: Thu Oct 20, 2011 11:54 am

Re: getting s.e. from ridgereg.prg

Postby phs » Thu Oct 20, 2011 5:16 pm

Let's say I have generated a series y with 1000 observations.
I want to see how many of these 1000 observations that are negative (like a @ count-if-routine). Is there an EViews routine for this?
Last edited by phs on Thu Oct 20, 2011 7:39 pm, edited 2 times in total.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13318
Joined: Tue Sep 16, 2008 5:38 pm

Re: getting s.e. from ridgereg.prg

Postby EViews Gareth » Thu Oct 20, 2011 7:12 pm

Code: Select all

smpl if y<0
scalar count = @obssmpl
Follow us on Twitter @IHSEViews

phs
Posts: 9
Joined: Thu Oct 20, 2011 11:54 am

Re: getting s.e. from ridgereg.prg

Postby phs » Thu Oct 20, 2011 7:42 pm

Moreover, I would like to do some bootstrapping in pairs (or nonparametric bootstrap). The part with Y.resample y_{%k}_b and Z1.resample Z1_{%k}_b works fine, but when I change to a resampling with gtmp.resample(suffix=_{%k}_b). I want to save with the suffix _{%k}_b just as for y and Z1 resample. group-resampling works fine without the suffix, but not with the suffix (even if I change the suffix just to a couple of letters instead of _{%k}_b)

I get the error message: Number of names does not match number of series in "DO_ GTMP.RESAMPLE(SUFFIX=_003_B)". What am I doing wrong?

...

Code: Select all

'something like:
for %k {%vars}
series Y = y{%k}
series Z1 = x{%k}(-{!xOLSlags})
group gtmp y{%k} x{%k}(-{!xOLSlags})
for !boot = 1 to !bootrepsum
gtmp.resample(suffix=_{%k}_b)
'   Y.resample y_{%k}_b
'   Z1.resample Z1_{%k}_b
   series DZ1_{%k}_b = @recode(Z1_{%k}_b>0,1,0)
      if {%k} = 003 then
      'hk
      ridgereg(L={tmp003SChk}) Y_003_b C Z1_003_b DZ1_003_b
      table temptable=results
      delete results
      tabplace(tabcoefs_hk_{%k},temptable,{!boot},1,11,2,11,2)
      delete temptable
etc...

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13318
Joined: Tue Sep 16, 2008 5:38 pm

Re: getting s.e. from ridgereg.prg

Postby EViews Gareth » Thu Oct 20, 2011 9:13 pm

The documentation on group.resample is pretty bad. You cannot supply a suffix (as far as I can tell). You must supply individual names for the series that will be resampled. This you need to do something like:

Code: Select all

group.resample name1 name2 name3
Follow us on Twitter @IHSEViews


Return to “Add-in Support”

Who is online

Users browsing this forum: No registered users and 68 guests