|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| program define ivreg2_p
|
|
|
|
|
| version 8
|
|
|
|
|
|
|
|
|
| if "`e(ivreg2cmd)'"=="ivreg2" {
|
| ivreg211_p `0'
|
| }
|
| else if "`e(ivreg2cmd)'"=="ivreg210" {
|
| ivreg210_p `0'
|
| }
|
| else if "`e(ivreg2cmd)'"=="ivreg29" {
|
| ivreg29_p `0'
|
| }
|
| else if "`e(ivreg2cmd)'"=="ivreg28" {
|
| ivreg28_p `0'
|
| }
|
| else {
|
| di as err "Error - ivreg2 estimation missing e(ivreg2cmd) macro"
|
| exit 601
|
| }
|
|
|
| end
|
|
|
|
|
|
|
| program define ivreg211_p
|
| version 8.2
|
| syntax newvarname [if] [in] , [XB Residuals stdp]
|
| marksample touse, novarlist
|
|
|
|
|
|
|
| local vernum "`e(version)'"
|
| if ("`vernum'" < "03.0.00") | ("`vernum'" > "09.9.99") {
|
| di as err "Error: incompatible versions of ivreg2 and ivreg2_p."
|
| di as err "Currently installed version of ivreg2 is `vernum'"
|
| di as err "To update, from within Stata type " _c
|
| di in smcl "{stata ssc install ivreg2, replace :ssc install ivreg2, replace}"
|
| exit 601
|
| }
|
|
|
| local type "`xb'`residuals'`stdp'"
|
|
|
| if "`type'"=="" {
|
| local type "xb"
|
| di in gr "(option xb assumed; fitted values)"
|
| }
|
|
|
|
|
| if e(partial_ct) {
|
|
|
| if "`type'" == "residuals" {
|
|
|
| tempvar esample
|
| tempname ivres
|
| gen byte `esample' = e(sample)
|
|
|
|
|
| local lhs "`e(depvar)'"
|
| tsrevar `lhs', substitute
|
| local lhs_t "`r(varlist)'"
|
|
|
| local rhs : colnames(e(b))
|
| tsrevar `rhs', substitute
|
| local rhs_t "`r(varlist)'"
|
|
|
| if "`e(partial1)'" != "" {
|
| local partial "`e(partial1)'"
|
| }
|
| else {
|
| local partial "`e(partial)'"
|
| }
|
| tsrevar `partial', substitute
|
| local partial_t "`r(varlist)'"
|
|
|
| if ~e(partialcons) {
|
| local noconstant "noconstant"
|
| }
|
|
|
| local allvars "`lhs_t' `rhs_t'"
|
|
|
| _estimates hold `ivres', restore
|
| foreach var of local allvars {
|
| tempname `var'_partial
|
| qui regress `var' `partial' if `esample', `noconstant'
|
| qui predict double ``var'_partial' if `touse', resid
|
| local allvars_partial "`allvars_partial' ``var'_partial'"
|
| }
|
| _estimates unhold `ivres'
|
|
|
| tokenize `allvars_partial'
|
| local lhs_partial "`1'"
|
| mac shift
|
| local rhs_partial "`*'"
|
|
|
| tempname b
|
| mat `b'=e(b)
|
| mat colnames `b' = `rhs_partial'
|
|
|
| tempvar xb
|
| mat score double `xb' = `b' if `touse'
|
| gen `typlist' `varlist' = `lhs_partial' - `xb'
|
| label var `varlist' "Residuals"
|
| }
|
| else {
|
| di in red "Option `type' not supported with -partial- option"
|
| error 198
|
| }
|
| }
|
| else if "`type'" == "residuals" {
|
| tempname lhs lhs_t xb
|
| local lhs "`e(depvar)'"
|
| tsrevar `lhs', substitute
|
| local lhs_t "`r(varlist)'"
|
| qui _predict `typlist' `xb' if `touse'
|
| gen `typlist' `varlist'=`lhs_t'-`xb'
|
| label var `varlist' "Residuals"
|
| }
|
|
|
| else {
|
| _predict `typlist' `varlist' if `touse', `type'
|
| }
|
|
|
| end
|
|
|