***
*** Example 1.2. Estimating standard errors for average PCE in Pakistan
***
*** The program takes data from a file consdata.dta, which contains 
*** information on total expenditure, household size, and survey 
*** design variables, that were taken from the expenditure files of the 
*** PIHS. It then computes weighted and unweighted means, together with 
*** various correct and incorrect variances and standard errors. This 
*** program should not be used in preference to the complex survey 
*** design procedures that became available in Version 5.0 of STATA.
***
*** 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.
***
version 4.0
#delimit ;
cap log close;
log using table15, replace;
drop _all;
set more 1; 
use consdata;
*per capita household expenditure;
gen pce=totmexp/hhsize;
*stratum identifiers;
gen strat=int(hhid/1000000);
*cluster identifiers;
gen clust=int(hhid/1000);
*province identifiers;
gen prov=int(hhid/100000000);
tab prov;
keep pce strat clust weight prov;
save temp, replace; 
*defining the program to do the work;
cap program drop surv;
program def surv;
dis("first the ordinary unweighted mean");
*ci provides a standard error of the estimate, col 2;
ci pce;
dis("now the weighted mean");
summ pce [aweight=weight];
local xw=_result(3);
*xw is the correct, weighted mean;
qui summ weight;
local nhat=_result(1)*_result(3);
gen dx2=(pce-`xw')^2;
qui summ dx2 [aweight=weight];
*this is a weighted variance estimate;
local varx1=_result(3)/_result(1);
display("first incorrect variance");
dis `varx1';
display("standard error for col 4");
dis sqrt(`varx1');
dis("allowing weights but no stratification or clusters");
*this corresponds to (1.28);
gen vi2dx2=(weight/`nhat')^2*dx2;
qui summ vi2dx2;
local varx2=_result(1)*_result(3);
display("second incorrect variance");
dis `varx2';
display("standard error");
dis sqrt(`varx2');
dis("allowing weights and stratification, but no clusters");
*this uses (1.28) and adds over strata using (1.36);
egen svarx2=mean(vi2dx2), by(strat);
egen ns=count(pce), by(strat);
gen fac=ns/(ns-1);
replace svarx2=svarx2*fac;
qui summ svarx2;
local varx3=_result(1)*_result(3);
display("third incorrect variance");
dis `varx3';
display("standard error");
dis sqrt(`varx3');
dis("now allowing for everything");
*this is evaluated according to (1.63);
gen z=weight*pce;
egen zcs=sum(z), by(clust);
egen wcs=sum(weight), by(clust);
sort clust;
qui by clust: keep if _n==1;
egen zs=mean(zcs), by(strat);
egen ws=mean(wcs), by(strat);
gen dall=((zcs-zs)-`xw'*(wcs-ws))^2;
egen tm=sum(dall), by(strat);
sort strat;
qui by strat: keep if _n==1;
qui replace tm=tm*fac;
qui summ tm;
local varok=_result(1)*_result(3)/(`nhat'^2);
display("correct variance");
dis `varok';
display("standard error");
dis sqrt(`varok');
end; 
surv; 
**Now doing the provinces;
cap program drop cont;
program def cont;
local pno=1;
while `pno' <= 4 {;
drop _all;
use temp;
keep if prov==`pno';
surv;
local pno=`pno'+1;
};
end; 
cont; 
log close; 

 


[Return to Program List]