***
*** Example 4.1. Locally weighted regressions for calorie Engel curves
***
*** There are three sections of code which, for logical 
*** transparency, are presented in the reverse order in which 
*** they should be run. The first section calculates the estimated 
*** regression function and its derivatives and then picks up the 
*** bootstrap replications from the second and thirs sections of 
*** the code to construct Figures 4.4 and 4.5. 
*** 
*** 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 Section 1 of the code and calulates the main results
*/ 
version 4.0
# delimit ;
drop _all;
set more 1;
capture log close;
capture log using ex41a.log, replace;
/* setting up the data */
use ..\feb95\working;
gen lnpce=log(totexp/nmems);
gen lncal=log(tcal1/nmems);
drop if lncal == .;
drop if lnpce == .;
keep lnpce lncal; 
/* the locally-weighted regression code */
cap program drop lowrex;
program def lowrex;
local ic=1;
gen `3'=.;
gen `4'=.;
while `ic' <= 100 {;
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=3.5;
global xmax=6.0;
global st=($xmax-$xmin)/99;
global h=0.5;
gen xgrid=$xmin+(_n-1)*$st in 1/100;
lowrex lncal lnpce smth dsmth $h xgrid;
set more 0; 
/* graphing the estimated regression function */
graph smth xgrid, c(l) s(i) xlabel ylabel;
/* graphing the slope */
graph dsmth xgrid, c(l) s(i) xlabel ylabel;
keep smth dsmth xgrid;
sort xgrid;
save temp, replace;
/* bringing in the bootstrap results (w/o clustering) */
use ..\feb95\lowboot;
drop if replic==0;
egen sdsmth=sd(smth), by(xgrid);
egen sddsmth=sd(dsmth), by(xgrid);
sort xgrid;
qui by xgrid: drop if _n ~= 1;
keep xgrid sdsmth sddsmth;
merge xgrid using temp;
tab _merge;
drop _merge;
sort xgrid;
/* bands for the smooth and its slope */
gen smthu=smth+2*sdsmth;
gen smthd=smth-2*sdsmth;
gen dsmthu=dsmth+2*sddsmth;
gen dsmthd=dsmth-2*sddsmth;
save temp, replace;
/* repeating with clustered bootstrap */
use clusam; /* clustered version of lowboot */
drop if replic==0;
egen sdsmthc=sd(smth), by(xgrid);
egen sddsmthc=sd(dsmth), by(xgrid);
sort xgrid;
qui by xgrid: drop if _n ~= 1;
keep xgrid sdsmthc sddsmthc;
merge xgrid using temp;
tab _merge;
drop _merge;
sort xgrid;
gen smthuc=smth+2*sdsmthc;
gen smthdc=smth-2*sdsmthc;
gen dsmthuc=dsmth+2*sddsmthc;
gen dsmthdc=dsmth-2*sddsmthc;
graph smth* xgrid, c(lllll) pen(22222) s(iiiii)
xlabel(3.4,3.8,4.2,4.6,5.0,5.4,5.8,6.2)
ylabel(7,7.2,7.4,7.6,7.8,8.0,8.2)saving(lowregx1, replace);
graph dsmth* xgrid, c(lllll) pen(22222) s(iiiii)
xlabel(3.4,3.8,4.2,4.6,5.0,5.4,5.8,6.2)
ylabel(0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1)
saving(lowregx2, replace);
log close;

/* Section 2: Bootstraps the locally weighted regression
** Without any allowance for cluster design */ 
version 4.0
#delimit ;
drop _all;
set more 1;
cap log close;
log using ex41b, replace;
use ..\feb95\working;
gen lnpce=log(totexp/nmems);
gen lncal=log(tcal1/nmems);
drop if lncal == .;
drop if lnpce == .;
keep lnpce lncal; 
save temp, replace;

ren lncal smth;
ren lnpce xgrid;
gen dsmth=.;
gen replic=0;
save lowboot, replace;
/* this program controls the bootstrap */
cap program drop lowrxit;
program def lowrxit;
local jc=1;
while `jc' <= 50 {;
dis `jc';
drop _all;
use temp;
bsample _N;
local xmin=3.5;
local xmax=6.0;
local st=(`xmax'-`xmin')/99;
local h=0.5;
qui gen xgrid=`xmin'+(_n-1)*`st' in 1/100;
qui lowregx lncal lnpce smth dsmth `h' xgrid;
drop lncal lnpce;
keep in 1/100;
gen replic=`jc';
append using lowboot;
save lowboot, replace;
local jc=`jc'+1;
};
end; 
lowrxit;
/* plotting bunches of regression functions and derivatives */
set more 0;
gen lnc=smth if replic==0;
replace smth=. if replic==0;
sort replic xgrid;
graph lnc smth xgrid, c(.L) s(oi) xlabel ylabel;
graph smth xgrid if replic > 0, c(L) s(i) xlabel(3.5,4,4.5,5,5.5,6)
ylabel saving(lowboot1, replace);
graph dsmth xgrid if replic > 0, c(L) s(i) xlabel(3.5,4,4.5,5,5.5,6)
ylabel saving(lowboot2, replace);
log close;

/* Section 3: Bootstrapping the locally weighted regression
** With allowance for cluster design */ 
version 4.0
#delimit;
cap log close;
log using ex41c, replace;
set more 1;
drop _all; 
use ..\feb95\working;
gen lnpce=log(totexp/nmems);
gen lncal=log(tcal1/nmems);
drop if lncal == .;
drop if lnpce == .;
keep lnpce lncal hid;
gen long cluster=int(hid/100);
sort cluster;
save tempwork, replace;

keep cluster;
qui by cluster: drop if _n ~= 1;
save tempclus, replace; 
cap program drop csampl; 
program define csampl;
use tempclus;
bsample _N;
sort cluster;
qui by cluster: gen dum=_N;
qui by cluster: drop if _n ~= 1;
merge cluster using tempwork;
qui drop if _merge ~= 3;
drop _merge;
qui expand dum;
drop dum;
sort cluster;
end; 
use tempwork;
ren lncal smth;
ren lnpce xgrid;
gen dsmth=.;
gen replic=0;
save clusam, replace; 
cap program drop loxit;
program def loxit;
local jc=1;
while `jc' <= 50 {;
dis `jc';
drop _all;
csampl;
local xmin=3.5;
local xmax=6.0;
local st=(`xmax'-`xmin')/99;
local h=0.5;
qui gen xgrid=`xmin'+(_n-1)*`st' in 1/100;
qui lowregx lncal lnpce smth dsmth `h' xgrid;
drop lncal lnpce;
keep in 1/100;
gen replic=`jc';
append using clusam;
save clusam, replace;
local jc=`jc'+1;
};
end; 
loxit;
/* plotting bunches of regression functions and derivatives */
set more 0;
gen lnc=smth if replic==0;
replace smth=. if replic==0;
sort replic xgrid;
graph lnc smth xgrid, c(.L) s(.i) xlabel ylabel;
graph smth xgrid if replic > 0, c(L) s(i) xlabel(3.5,4,4.5,5,5.5,6)
ylabel saving(clusam1, replace);
graph dsmth xgrid if replic > 0, c(L) s(i) xlabel(3.5,4,4.5,5,5.5,6)
ylabel saving(clusam2, replace); 
log close; 

[Return to Program List]