*! REGMSNG.ADO, by Mead Over, July 31, 1998 Version 1.0.4 STB-47 sg99 program define regmsng version 4.0 tempvar Nmiss hat misslt2 missimp misssum local options "NMiss(real 1) Inst(string) NOHist NOTEst FITted STeps CHange NOAux Level(integer $S_level)" if (substr("`1'",1,1)=="," | "`*'"=="") { if "$S_E_cmd"~="regmsng" { error 301 } parse "`*'" if `level'<10 | `level'>99 { local level 95 } regress, level(`level') } else { local varlist "required existing min(3)" local if "optional" local weight "aweight fweight iweight pweight" parse "`*'" if `nmiss' < 0 { di in red "The option -nmiss- cannot be negative." error 198 /* This line is not reached.*/ } if "`inst'"~="" { unabbrev `inst' local inst "$S_1" if "`fitted'"=="" { di in red "The option -inst- requires the option -fitted-." error 198 /* This line is not reached.*/ } } if "`steps'"=="" { local shhh quietly } if "`change'" ~="" { label data "Data changed by REGMSMG" } else { preserve if "`weight'"!="" { tempvar wt gen double `wt' `exp' local exp "=`wt'" } keep `varlist' `inst' `wt' /* Drop unused variables only if data is preserved. */ } capture keep `if' /* Drop unused observations regardless of whether data is preserved. */ parse "`varlist'", parse(" ") local dvar `1' macro shift local ivar "`*'" * How many missing variables are present on each observation? egen `Nmiss' = rmiss(`ivar') label variable `Nmiss' "No. of RHS variables missing" if "`nohist'"=="" { di " " di in gr "Number of observations with 0, 1, 2, ... RHS variables missing" tab `Nmiss' if `dvar'~=., plot mi } quietly sum `Nmiss' if _result(6) < 1 { di in red "None of the RHS variables is missing when the LHS variable is present." di in red "Therefore, it was not necessary to use regmsng." local notests "No missing" } capture keep if `Nmiss' <= `nmiss' & `dvar'~=. * Compute & display summary statistics for data prior to zero-filling the missing values. di " " `shhh' di in green "Summary statistics prior to zero substitution." `shhh' summ `dvar' `ivar' `wt' * Store the mean of each ind. variable in a global macro called Mvarname. while "`1'"~="" { local summ_wt "`weight'" /* Convert pweights and iweights to aweights for the summarize command. */ if "`weight'" == "pweight" | "`weight'" == "iweight" { local summ_wt "aweight" } qui summ `1' [`summ_wt' `exp'] if length("`1'") > 7 { di in red "The names of independent variables can be no longer than 7 characters." error 198 /* This line is not reached. */ } global M`1' = _result(3) macro shift } * Create an auxiliary variable for every independent variable and zero-fill the variables. qui gen byte `misslt2' = . /* Temp. variable used below to check candidate predictor variables. */ qui gen byte `missimp' = . /* Temp. variable used below to check if any obs are missing.*/ local beenhre local i = 1 local k : word count `ivar' local m : word count `inst' while `i' <= `k' { /* Consider all the r.h.s. variables as candidates to predict -impvar-.*/ local impvar : word `i' of `ivar' qui replace `missimp' = (`impvar'==.) qui summ `missimp' if _result(6)>0 { /* Skip r.h.s variables that have no missing values. */ if "`fitted'"~="" { /* Generate imputed values by regression. */ `shhh' di _newline (2) `shhh' di in green "The regression for imputing the missing values of " in ye "`impvar':" local nextvar local othvars local j = 1 while `j' <= `k' { /*Reject candidate r.h.s vars. which are missing when -impvar- is missing.*/ if `j'~=`i' { local nextvar : word `j' of `ivar' qui replace `misslt2' = (`impvar'==.)&(`nextvar'==.) qui summ `misslt2' if _result(6)<1 { local othvars `othvars' `nextvar' } } local j = `j' + 1 } if "`inst'"~="" { local nextvar local instvrs local j = 1 while `j' <= `m' { /*Reject candidate instruments which are missing when -impvar- is missing.*/ if `j'~=`i' { local nextvar : word `j' of `inst' qui replace `misslt2' = (`impvar'==.)&(`nextvar'==.) qui summ `misslt2' if _result(6)<1 { local instvrs `instvrs' `nextvar' } } local j = `j' + 1 } } `shhh' regress `impvar' `othvars' `instvrs'[`weight'`exp'], level(`level') qui predict `hat' qui gen A`impvar' = 0 if `impvar'~=. if "`shhh'"=="" { di " " di in gr "On observations where " in ye "`impvar' " in gr "is missing, insert fitted values into " in ye "A`impvar'." } `shhh' replace A`impvar' = `hat' if `impvar'==. qui gen `misssum' = sum(A`impvar') local missbar = `misssum'[_N]/(_N - _result(1)) local mthis : word count `instvrs' drop `misssum' drop `hat' if "`shhh'"~="" { if "`beenhre'"=="" { di in gr _col(17) "Number of" _col(29) "Number of" di in gr "Independent" _col(17) " Proposed" _col(29) " Actual" _col(53) "Mean of" _col(65) "Mean of" di in gr "Variables" _col(12) "nobs" _col(17) "Instruments" _col(29) "Regressors" _col(42) "F-stat" _col(51) "Non-missing" _col(65) "Imputed" di in gr "---------" _col(12) "----" _col(17) "-----------" _col(29) "----------" _col(42) "------" _col(51) "-----------" _col(65) "-------" local beenhre "already" } di in ye "`impvar'" _col(13) _result(1) _col(22) `mthis' _col(32) _result(3) %6.2f _col(42) _result(6) _col(53) ${M`impvar'} _col(65) `missbar' } } else { /* Generate auxiliary values as the means. */ qui gen A`impvar' = ${M`impvar'} if `impvar'==. qui replace A`impvar' = 0 if `impvar'~=. } * Now replace the missing values. if "`noaux'"~= "" { /* Replace with imputed values if the -noaux- option is set. */ quietly replace `impvar' = A`impvar' if `impvar' == . } else { quietly replace `impvar' = 0 if `impvar' == . /* Otherwise zero-fill the missing values. */ local AuxVars `AuxVars' A`impvar' /* and assemble the list of auxiliary variables. */ } } local i = `i' + 1 } * Estimate regression. di " " di " " di _n in gr "Regression with Imputed Values of Missing R.H.S. Variables" * Either estimate regression with imputed values substituted for missing variables. if "`noaux'"~="" { if "`fitted'"~="" { di in green "Fitted values substituted for missing observations of RHS variables." } else { di in green "Means substituted for missing observations of RHS variables." } regress `dvar' `ivar' [`weight'`exp'] , level(`level') } * Or add "auxiliary" variables containing imputed variables. else { if "`fitted'"~="" { di in green "A-variables contain the fitted values of corresponding independent variables." } else { di in green "A-variables contain the mean values of corresponding independent variables." } regress `dvar' `ivar' `AuxVars' [`weight'`exp'] , level(`level') } if _result(1)==0 | _result(1)==. { error 2000 } local nobs=_result(1) local mdf=_result(3) local tdf=_result(5) local r2=_result(7) local ar2=_result(8) local rmse=_result(9) * Perform the joint tests of significance on each variable & its auxiliary. * Also test for equality between the coefficient of each variable and its auxiliary variable. if "`notest'"=="" & "`noaux'"=="" { parse "`ivar'", parse(" ") /* First, test hypotheses on each variable. */ di " " while "`1'"~="" { capture confirm variable A`1' if _rc==0 { qui count if A`1'~=0 & `Nmiss' <= `nmiss' di "A total of " _result(1) " observations were missing for variable `1'" test `1' A`1' test `1' == A`1' } macro shift } parse "`ivar'", parse(" ") /* Then test joint hypotheses. */ local beenhre di " " while "`1'"~="" { capture confirm variable A`1' if _rc==0 { if "`beenhre'"=="" { qui test `1' == A`1', notest local beenhre already } else { qui test `1' == A`1', accum notest } } macro shift } * And on all auxiliary variables. di " " if "`notests'" == "" { di in gr "A joint test of all equality constraints:" test di in gr "A joint test that the coefficients of all the A-variables are zero:" test `AuxVars' } } if "`change'"~="" { rename `Nmiss' _Nmiss di in red "Warning! Data has been changed." if "`noaux'"~="" { di in red "Missing values of independent variables have been replaced by imputed values." } else { di in red "Missing values of independent variables have been zero-filled." } } global S_E_grs "`grs'" global S_E_dv "`dvar'" global S_E_nobs `nobs' global S_E_tdf `tdf' global S_E_mdf `mdf' global S_E_r2 `r2' global S_E_ar2 `ar2' global S_E_rmse `rmse' global S_E_if "`if'" global S_E_wgt "`weight'" global S_E_exp "`exp'" global S_E_cmd "regmsng" } end