***
*** Example 3.8. Locally-weighted regression (Figure 3.20)
***
*** Figure 3.20 shows plots of expected actual and potential 
*** pension receipts against the logarithm of household per 
*** capita income. These are calculated using locally-weighted 
*** regressions, the code for which is given below.
*** 
*** Note: The following provides the STATA code used to produce 
*** results used in "The Analysis of Household Surveys: 
*** A Microeconometric Approach to Development Policy", by Angus Deaton.
*** This book, published for the World Bank by The Johns Hopkins University
*** Press and scheduled for release in 1997.
*** 
/* this is for Africans, i.e. left panel of Figure 3.20 */ 
version 4.0
#delimit ;
cap log close;
log using fanpen, replace;
set more 1;
drop _all;
use socpenh;
keep if race==1;
/* prepinc is total income less pension income */
gen prepinc=totminc-socpen1;
drop if prepinc <= 0;
gen ppens=n60f+n65m;
gen lnpcy=log(prepinc/hhsizem);
gen incpc=prepinc/hhsizem;
global penamt=370;
gen psocpen=ppens*$penamt;
keep psocpen socpen1 lnpcy;
/*
** psocpen is potential pension receipts
** socpen1 is receipts of social pensions
** lnpcy is the log of nonpension income
*/
/*
** lowrex does the locally-weighted regression
** argument 1 is the regressand (input)
** argument 2 is the regressor (input)
** argument 3 is the estimated regression function (output)
** argument 4 is the derivative of the regression function (output)
** argument 5 is the bandwidth (input)
** argument 6 is the grid over the regressor for evaluation (input)
*/ 
cap program drop lowrex;
program def lowrex;
local ic=1;
gen `3'=.;
gen `4'=.;
while `ic' <= $gsize {;
dis `ic';
quietly {;
local xx=`6'[`ic'];
gen z=abs((`2'-`xx')/`5');
gen kz=(15/16)*(1-z^2)^2 if z <= 1;
reg `1' `2' =kz if kz ~= .;
replace `4'=_b[`2'] in `ic';
* replace `3'=_b[_cons]+_b[`2']*`xx' in `ic';
drop z kz;
};
local ic=`ic'+1;
};
end; 
global xmin=0;
global xmax=9;
global gsize=50;
global st=($xmax-$xmin)/($gsize-1);
global h=1.0;
gen xgrid=$xmin+(_n-1)*$st in 1/$gsize;
lowrex psocpen lnpcy smthp dsmth $h xgrid;
drop dsmth;
lowrex socpen1 lnpcy smtha dsmth $h xgrid;
sort xgrid;
keep in 1/$gsize;
graph smthp smtha xgrid, c(ll) s(ii) ylabel xlabel(0,2,4,6,8,10)
xline(4.654) saving(fanaf, replace);
log close; 

[Return to Program List]