Robust Regression

For posting your own programs to share with others

Moderators: EViews Gareth, EViews Moderator

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

Robust Regression

Postby EViews Gareth » Mon Dec 07, 2009 12:27 pm

This program is taken from a post that Trubador made in the EViews 7 beta forum. I am reposting it here for him, but all credit goes to him for its creation.

Attached is a subroutine to carry out a robust regression analysis in EViews. I am sure the following code can be modified in a much more elegant way, especially with the new programming features of the version 7. However, it would be nice to see this type of analysis, or preferably a more comprehensive version of it, as an additional tool in EV7. Once implemented, robust regression procedures can later be combined with those of quantile regression under a more general name.

This program requires EViews 7 or later

Code: Select all

'Robust Regression 'Reference: Adapted from the MATLAB code written by James P. LeSage, Dept of Economics, University of Toledo. Original version of the code is available at: http://www.spatial-econometrics.com/regress/robust.m call robustreg subroutine robustreg 'The following objects will be created through the program. 'If necessary, assign different names to avoid overwriting. %coef = "beta" %eq1 = "ols" %eq2 = "itrw" %table = "output" %ser = "weight" %resid = "resw" !result = 0 'Get the names of variables directly from the user. @uidialog("edit",%depname,"Enter the name of the dependent variable","edit",%indepname,"Enter a list of independent variables (can include group names)") if !result=-1 then stop endif 'Select an appropriate method for the weighting function !choice = 1 %list = "Tukey(default) Huber Ramsay Andrew" !result = @uiradio(!choice, "Weighting Function Choice", %list) if !result=-1 then stop endif %choice = @word(%list,!choice) group robustindependents {%indepname} %indepname = "robustindependents" 'Determine the number of independent variables. !n = {%indepname}.@count 'Construct a table to store the results table(4+!n,5) {%table} {%table}.setwidth(1:5) 10 {%table}.setlines(a2:e2) +d {%table}.setlines(a4:e4) +d {%table}.setformat(D) f.5 {%table}(3,1)= "Variable" {%table}(3,2)= "Coefficient" {%table}(3,3)= "t-Statistic" {%table}(3,4)= "Prob." {%table}(3,5)= "R-squared" for !v = 1 to !n {%table}(4+!v,1) = {%indepname}.@seriesname(!v) next 'Find starting values equation {%eq1}.ls {%depname} {%indepname} vector {%coef} = @subextract(c,1,1,!n,1) series {%resid} = resid !wparm = 2*({%eq1}.@se)^2 'weighting parameter !scale = @median(@abs({%resid} - @median({%resid})))/.6745 'scale parameter 'Initialize series and control variables series {%ser} =1 group {%indepname}star vector(!n) {%coef}0 !tol=1 !count=0 'Start the loop while !tol >.00001 {%coef}0 = {%coef} {%resid} = {%resid}/!scale !count=!count+1 if !choice= 1 then {%table}(1,1)= "Tukey's biweight" {%ser}=(1-({%resid}/!wparm)^2)^2 {%ser}=@recode({%resid}=0,1,@recode(@abs({%resid})>!wparm,0,{%ser})) else if !choice = 2 then {%table}(1,1)= "Huber's t function" {%ser} = !wparm/@abs({%resid}) {%ser} = @recode(@abs({%resid})<=!wparm,1,{%ser}) else if !choice = 3 then {%table}(1,1)= "Ramsay's E function" {%ser} = @exp(-!wparm*@abs({%resid})) {%ser} = @recode({%resid}=0,1,{%ser}) else if !choice = 4 then {%table}(1,1)= "Andrew's wave function" {%ser} = @sin({%resid}/!wparm)/({%resid}/!wparm) {%ser} = @recode({%resid}=0,1,@recode({%resid}>@acos(-1)*!wparm,0,{%ser})) endif endif endif endif 'Weighted least squares series {%depname}star = {%depname}*@sqrt({%ser}) for !i=1 to !n series {%indepname}star!i = {%indepname}(!i)*@sqrt({%ser}) {%indepname}star.add {%indepname}star!i next series {%depname}hat=0 equation {%eq2}.ls {%depname}star {%indepname}star {%coef} = @subextract(c,1,1,!n,1) for !j=1 to !n {%depname}hat = {%depname}hat+{%coef}(!j)*{%indepname}(!j) next {%resid} = {%depname}-{%depname}hat 'Recalculate the tolerance level !tol = @max(@ediv(@abs({%coef}-{%coef}0),@abs({%coef}0))) wend 'Generate some basic equation output (can be extended further) stom({%indepname}star,{%indepname}m) !evar = @sumsq({%resid})/(@obs({%depname})-!n) vector {%coef}sig = @sqrt(!evar*@getmaindiagonal(@inverse(@transpose({%indepname}m)*{%indepname}m))) vector {%coef}t= @ediv({%coef},{%coef}sig) 'Store the generated output values into the table for !v=1 to !n {%table}(4+!v,2) = {%coef}(!v) {%table}(4+!v,3) = {%coef}t(!v) {%table}(4+!v,4) = @tdist({%coef}t(!v),@obs({%depname})-!n) next {%table}(5,5) = 1-(@sumsq({%resid})/@sumsq({%depname}-@mean({%depname}))) 'Final wrap-up delete {%coef}0 {%coef}t {%coef}sig {%eq2} {%indepname}m {%indepname}star* {%depname}hat {%depname}star robustindependents show {%table} endsub

Return to “Program Repository”

Who is online

Users browsing this forum: No registered users and 1 guest