Setup initial file structure
This commit is contained in:
28
Modules/ado/plus/_/_eststo.ado
Normal file
28
Modules/ado/plus/_/_eststo.ado
Normal file
@ -0,0 +1,28 @@
|
||||
*! version 1.0.4 09nov2007 Ben Jann
|
||||
|
||||
program define _eststo, byable(onecall)
|
||||
local caller : di _caller()
|
||||
version 8.2
|
||||
if "`_byvars'"!="" local by "by `_byvars'`_byrc0' : "
|
||||
if inlist(`"`1'"',"clear","dir","drop") {
|
||||
version `caller': `by'eststo `0'
|
||||
}
|
||||
else {
|
||||
capt _on_colon_parse `0'
|
||||
if !_rc {
|
||||
local command `"`s(after)'"'
|
||||
if `"`command'"'!="" {
|
||||
local command `":`command'"'
|
||||
}
|
||||
local 0 `"`s(before)'"'
|
||||
}
|
||||
syntax [anything] [, Esample * ]
|
||||
if `"`esample'"'=="" {
|
||||
local options `"noesample `options'"'
|
||||
}
|
||||
if `"`options'"'!="" {
|
||||
local options `", `options'"'
|
||||
}
|
||||
version `caller': `by'eststo `anything'`options' `command'
|
||||
}
|
||||
end
|
1
Modules/ado/plus/_/_eststo.hlp
Normal file
1
Modules/ado/plus/_/_eststo.hlp
Normal file
@ -0,0 +1 @@
|
||||
.h eststo
|
86
Modules/ado/plus/_/_get_mlogit_bv.ado
Normal file
86
Modules/ado/plus/_/_get_mlogit_bv.ado
Normal file
@ -0,0 +1,86 @@
|
||||
*! version 1.0.2 07oct2009 Ben Jann
|
||||
* - name change; restructured; new reshape algorithm
|
||||
* version 1.0.1 15sep2009 Scott Long
|
||||
* - reshape b to Stata 5 format
|
||||
* version 1.0.0 15sep2009 Ben Jann
|
||||
|
||||
* get stata e(b) and e(V) from -mlogit- and reshape
|
||||
* to the format used by SPost based on Stata 5
|
||||
|
||||
capture program drop _get_mlogit_bv
|
||||
capture program drop _remove_baseeq
|
||||
program _get_mlogit_bv
|
||||
version 9
|
||||
args b v
|
||||
if `"`b'"'=="" | `"`v'"'=="" {
|
||||
di as err "_get_mlogit_bv: names for b and v must be specified"
|
||||
exit 198
|
||||
}
|
||||
if `"`e(cmd)'"'!="mlogit" {
|
||||
di as err "_get_mlogit_bv: model not mlogit"
|
||||
exit 498
|
||||
}
|
||||
|
||||
// get copy of e(b) and e(V)
|
||||
matrix `b' = e(b)
|
||||
matrix `v' = e(V)
|
||||
|
||||
// remove base eq if mlogit v11
|
||||
_remove_baseeq `b' `v'
|
||||
|
||||
// reshape b to (ncat-1) rows by (nvars + 1) columns
|
||||
// this is the stata 5 format used in SPost
|
||||
local eqs: coleq `b', quoted
|
||||
local eqs: list uniq eqs
|
||||
tempname tmp bnew
|
||||
local r 0
|
||||
foreach eq of local eqs {
|
||||
local ++r
|
||||
mat `tmp' = `b'[1, `"`eq':"']
|
||||
mat rown `tmp' = y`r'
|
||||
mat `bnew' = nullmat(`bnew') \ `tmp'
|
||||
}
|
||||
mat coleq `bnew' = :
|
||||
mat drop `b'
|
||||
mat rename `bnew' `b'
|
||||
end
|
||||
|
||||
program _remove_baseeq
|
||||
args b v
|
||||
if `"`b'"'=="" | `"`v'"'=="" {
|
||||
di as err "_remove_baseeq: b and v must be specified"
|
||||
exit 198
|
||||
}
|
||||
if c(stata_version) < 11 exit // Stata 11 (or newer) only
|
||||
if `"`e(cmd)'"'!="mlogit" exit // mlogit only
|
||||
local ibase = e(k_eq_base) // get base equation number
|
||||
capt confirm integer number `ibase' // check validity of ibase
|
||||
if _rc exit
|
||||
if `ibase'>=. | `ibase'<1 exit
|
||||
_ms_eq_info, matrix(`b') // get equations info
|
||||
local l = 0 // determine subscripts to
|
||||
forv i = 1/`r(k_eq)' { // remove base equation:
|
||||
if `i' == `ibase' continue, break // l = last element before
|
||||
local l = `l' + r(k`i') // base eq, or 0
|
||||
} // r = first element after
|
||||
local i = `l' // base eq, or .
|
||||
while (`++i' <= r(k`ibase')) { // make sure that base eq is,
|
||||
if `b'[1,`i']!=0 exit // in fact, a base eq (all 0)
|
||||
}
|
||||
local r = cond(`ibase' >= r(k_eq), ., `l' + r(k`ibase') + 1)
|
||||
if `l' > 0 & `r' < . { // base eq within
|
||||
mat `b' = `b'[1..., 1..`l'] , `b'[1..., `r'...]
|
||||
mat `v' = `v'[1..., 1..`l'] , `v'[1..., `r'...]
|
||||
mat `v' = `v'[1..`l', 1...] \ `v'[`r'..., 1...]
|
||||
}
|
||||
else if `r' < . { // base eq at beginning
|
||||
mat `b' = `b'[1..., `r'...]
|
||||
mat `v' = `v'[1..., `r'...]
|
||||
mat `v' = `v'[`r'..., 1...]
|
||||
}
|
||||
else if `l' > 0 { // base eq at end
|
||||
mat `b' = `b'[1..., 1..`l']
|
||||
mat `v' = `v'[1..., 1..`l']
|
||||
mat `v' = `v'[1..`l', 1...]
|
||||
}
|
||||
end
|
85
Modules/ado/plus/_/_get_mlogit_bvecv.ado
Normal file
85
Modules/ado/plus/_/_get_mlogit_bvecv.ado
Normal file
@ -0,0 +1,85 @@
|
||||
*! version 1.0.0 2009-10-21 jsl
|
||||
* - based on _get_mlogit_bv but leave b as vector
|
||||
|
||||
* get stata e(b) and e(V) from -mlogit- and reshape
|
||||
* to the format used by SPost based on Stata 9
|
||||
|
||||
capture program drop _get_mlogit_bvecv
|
||||
capture program drop _remove_baseeq
|
||||
program _get_mlogit_bvecv
|
||||
version 9
|
||||
args b v
|
||||
if `"`b'"'=="" | `"`v'"'=="" {
|
||||
di as err "_get_mlogit_bv: names for b and v must be specified"
|
||||
exit 198
|
||||
}
|
||||
if `"`e(cmd)'"'!="mlogit" {
|
||||
di as err "_get_mlogit_bv: model not mlogit"
|
||||
exit 498
|
||||
}
|
||||
|
||||
// get copy of e(b) and e(V)
|
||||
matrix `b' = e(b)
|
||||
matrix `v' = e(V)
|
||||
|
||||
// remove base eq if mlogit v11
|
||||
_remove_baseeq `b' `v'
|
||||
|
||||
/*
|
||||
// reshape b to (ncat-1) rows by (nvars + 1) columns
|
||||
// this is the stata 5 format used in SPost
|
||||
local eqs: coleq `b', quoted
|
||||
local eqs: list uniq eqs
|
||||
tempname tmp bnew
|
||||
local r 0
|
||||
foreach eq of local eqs {
|
||||
local ++r
|
||||
mat `tmp' = `b'[1, `"`eq':"']
|
||||
mat rown `tmp' = y`r'
|
||||
mat `bnew' = nullmat(`bnew') \ `tmp'
|
||||
}
|
||||
mat coleq `bnew' = :
|
||||
mat drop `b'
|
||||
mat rename `bnew' `b'
|
||||
*/
|
||||
end
|
||||
|
||||
program _remove_baseeq
|
||||
args b v
|
||||
if `"`b'"'=="" | `"`v'"'=="" {
|
||||
di as err "_remove_baseeq: b and v must be specified"
|
||||
exit 198
|
||||
}
|
||||
if c(stata_version) < 11 exit // Stata 11 (or newer) only
|
||||
if `"`e(cmd)'"'!="mlogit" exit // mlogit only
|
||||
local ibase = e(k_eq_base) // get base equation number
|
||||
capt confirm integer number `ibase' // check validity of ibase
|
||||
if _rc exit
|
||||
if `ibase'>=. | `ibase'<1 exit
|
||||
_ms_eq_info, matrix(`b') // get equations info
|
||||
local l = 0 // determine subscripts to
|
||||
forv i = 1/`r(k_eq)' { // remove base equation:
|
||||
if `i' == `ibase' continue, break // l = last element before
|
||||
local l = `l' + r(k`i') // base eq, or 0
|
||||
} // r = first element after
|
||||
local i = `l' // base eq, or .
|
||||
while (`++i' <= r(k`ibase')) { // make sure that base eq is,
|
||||
if `b'[1,`i']!=0 exit // in fact, a base eq (all 0)
|
||||
}
|
||||
local r = cond(`ibase' >= r(k_eq), ., `l' + r(k`ibase') + 1)
|
||||
if `l' > 0 & `r' < . { // base eq within
|
||||
mat `b' = `b'[1..., 1..`l'] , `b'[1..., `r'...]
|
||||
mat `v' = `v'[1..., 1..`l'] , `v'[1..., `r'...]
|
||||
mat `v' = `v'[1..`l', 1...] \ `v'[`r'..., 1...]
|
||||
}
|
||||
else if `r' < . { // base eq at beginning
|
||||
mat `b' = `b'[1..., `r'...]
|
||||
mat `v' = `v'[1..., `r'...]
|
||||
mat `v' = `v'[`r'..., 1...]
|
||||
}
|
||||
else if `l' > 0 { // base eq at end
|
||||
mat `b' = `b'[1..., 1..`l']
|
||||
mat `v' = `v'[1..., 1..`l']
|
||||
mat `v' = `v'[1..`l', 1...]
|
||||
}
|
||||
end
|
17
Modules/ado/plus/_/_peabbv.ado
Normal file
17
Modules/ado/plus/_/_peabbv.ado
Normal file
@ -0,0 +1,17 @@
|
||||
*! version 1.6.0 3/29/01
|
||||
|
||||
capture program drop _peabbv
|
||||
program define _peabbv
|
||||
cap version 7
|
||||
if _rc == 0 {
|
||||
local matnm "`1'"
|
||||
local nms : colnames `matnm'
|
||||
tokenize `nms'
|
||||
while "`1'"!="" {
|
||||
local x = abbrev("`1'", 12)
|
||||
local newnms "`newnms'`x' "
|
||||
macro shift
|
||||
}
|
||||
mat colnames `matnm' = `newnms'
|
||||
}
|
||||
end
|
342
Modules/ado/plus/_/_pebase.ado
Normal file
342
Modules/ado/plus/_/_pebase.ado
Normal file
@ -0,0 +1,342 @@
|
||||
*! version 1.6.5 2007-02-08 - rest(zero)
|
||||
|
||||
capture program drop _pebase
|
||||
program define _pebase, rclass
|
||||
version 6.0
|
||||
tempvar tmp input tmp2 peb2 xmin xmax chtest mark
|
||||
tempname tobase tobase2 b min max mean median min2 max2 mean2 median2 prev prev2
|
||||
|
||||
if "`e(cmd)'"=="ztp" { local flags "none" }
|
||||
if "`e(cmd)'"=="ztnb" { local flags "none" }
|
||||
if "`e(cmd)'"=="logit" { local flags "none" }
|
||||
if "`e(cmd)'"=="logistic" { local flags "none" }
|
||||
if "`e(cmd)'"=="probit" { local flags "none" }
|
||||
if "`e(cmd)'"=="cloglog" { local flags "none" }
|
||||
if "`e(cmd)'"=="ologit" { local flags "none" }
|
||||
if "`e(cmd)'"=="oprobit" { local flags "none" }
|
||||
if "`e(cmd)'"=="gologit" { local flags "noupper" }
|
||||
if "`e(cmd)'"=="mlogit" { local flags "noupper" }
|
||||
if "`e(cmd)'"=="mprobit" { local flags "noupper" }
|
||||
if "`e(cmd)'"=="slogit" { local flags "noupper" }
|
||||
if "`e(cmd)'"=="clogit" { local flags "noupper" }
|
||||
if "`e(cmd)'"=="poisson" { local flags "none" }
|
||||
if "`e(cmd)'"=="nbreg" { local flags "none" }
|
||||
if "`e(cmd)'"=="zip" { local flags "twoeq noupper" }
|
||||
if "`e(cmd)'"=="zinb" { local flags "twoeq noupper" }
|
||||
if "`e(cmd)'"=="tobit" { local flags "none" }
|
||||
if "`e(cmd)'"=="intreg" { local flags "none" }
|
||||
if "`e(cmd)'"=="cnreg" { local flags "none" }
|
||||
if "`e(cmd)'"=="fit" { local flags "none" }
|
||||
if "`e(cmd)'"=="regress" { local flags "none" }
|
||||
if "`flags'"=="" {
|
||||
di in r "_pebase does not work with `e(cmd)'"
|
||||
exit 198
|
||||
}
|
||||
|
||||
*-> unpack flags: define relevant special features of different models
|
||||
|
||||
*flag twoeq -- 2 equation model like zip or zinb
|
||||
if index("`flags'", "twoeq") == 0 { local twoeq "no" }
|
||||
else { local twoeq "yes" }
|
||||
*flag noupper -- do not allow upper and lower for rest()
|
||||
if index("`flags'", "noupper") == 0 { local noupper "no" }
|
||||
else { local noupper "yes" }
|
||||
|
||||
* options:
|
||||
* x: specified x variable values
|
||||
* rest: what to set remaining values to
|
||||
* choices: choices after clogit
|
||||
* all
|
||||
|
||||
syntax [if] [in] [, x(passthru) rest(string) choices(varlist) all]
|
||||
|
||||
*set flag because so many if zip | zinb statements
|
||||
local twoeq "no"
|
||||
if "`e(cmd)'"=="zip" | "`e(cmd)'"=="zinb" {
|
||||
local twoeq "yes"
|
||||
}
|
||||
|
||||
* get names of rhs variables in models
|
||||
_perhs
|
||||
local rhsnms "`r(rhsnms)'"
|
||||
local nrhs = `r(nrhs)'
|
||||
if "`twoeq'"=="yes" {
|
||||
local rhsnms2 "`r(rhsnms2)'"
|
||||
local nrhs2 = `r(nrhs2)'
|
||||
}
|
||||
|
||||
*go to _peife to see if you need to restrict the sample
|
||||
_peife `if', `all'
|
||||
local if "`r(if)'"
|
||||
|
||||
* get summary statistics for models (both if zip/zinb)
|
||||
quietly _pesum `if' `in', median
|
||||
mat `min' = r(Smin)
|
||||
mat `min' = `min'[1, 2...]
|
||||
mat `max' = r(Smax)
|
||||
mat `max' = `max'[1, 2...]
|
||||
mat `mean' = r(Smean)
|
||||
mat `mean' = `mean'[1, 2...]
|
||||
mat `median' = r(Smedian)
|
||||
mat `median' = `median'[1, 2...]
|
||||
*! version 1.6.5 2007-02-08 - rest(zero)
|
||||
tempname zero
|
||||
mat `zero' = 0*`mean'
|
||||
if "`twoeq'"=="yes" {
|
||||
quietly _pesum `if' `in', median two
|
||||
mat `min2' = r(Smin)
|
||||
mat `min2' = `min2'[1, 2...]
|
||||
mat `max2' = r(Smax)
|
||||
mat `max2' = `max2'[1, 2...]
|
||||
mat `mean2' = r(Smean)
|
||||
mat `mean2' = `mean2'[1, 2...]
|
||||
*! version 1.6.5 2007-02-08 - rest(zero)
|
||||
tempname zero2
|
||||
mat `zero2' = 0*`mean2'
|
||||
mat `median2' = r(Smedian)
|
||||
mat `median2' = `median2'[1, 2...]
|
||||
}
|
||||
|
||||
* get matrix of previous values if it exists
|
||||
local oldpe = "yes"
|
||||
local pematch = "yes"
|
||||
capture mat `prev' = PE_base
|
||||
if _rc != 0 { local oldpe "no" }
|
||||
else {
|
||||
local test1 : colnames(`prev')
|
||||
local test2 : colnames(`mean')
|
||||
if "`test1'" != "`test2'" {
|
||||
local pematch "no"
|
||||
}
|
||||
if "`twoeq'"=="yes" {
|
||||
capture mat `prev2' = PE_base2
|
||||
if _rc != 0 { local oldpe "no" }
|
||||
else {
|
||||
local test1 : colnames(`prev2')
|
||||
local test2 : colnames(`mean2')
|
||||
if "`test1'" != "`test2'" {
|
||||
local pematch "no"
|
||||
}
|
||||
} /* else */
|
||||
} /* if "`twoeq'"=="yes" */
|
||||
} /* else */
|
||||
if "`oldpe'"=="no" { local pematch = "no" }
|
||||
|
||||
*=> decode x()
|
||||
* tokenize `x', parse(" =")
|
||||
tokenize `x', parse("()")
|
||||
local x "`3'"
|
||||
tokenize `x', parse(" =")
|
||||
|
||||
local allnums = "yes"
|
||||
local xchngs = 0 /* number of x changes proposed */
|
||||
while "`1'" != "" {
|
||||
while "`2'"=="=" | "`2'"=="==" {
|
||||
local temp1 "`1'"
|
||||
macro shift
|
||||
local 1 "`temp1'"
|
||||
}
|
||||
if "`2'"=="" {
|
||||
di _newline in red "Odd number of arguments in x()"
|
||||
error 198
|
||||
}
|
||||
local xchngs = `xchngs' + 1
|
||||
local cvar`xchngs' "`1'"
|
||||
*make sure variable is rhs variable
|
||||
local found "no"
|
||||
local i2 = 1
|
||||
while `i2' <= `nrhs' {
|
||||
local rhschk : word `i2' of `rhsnms'
|
||||
unab 1 : `1', max(1)
|
||||
if "`1'"=="`rhschk'" {
|
||||
local found "yes"
|
||||
local cvno`xchngs' `i2'
|
||||
local i2 = `nrhs'
|
||||
}
|
||||
local i2 = `i2' + 1
|
||||
}
|
||||
*check in binary equation if zip/zinb
|
||||
if "`twoeq'"=="yes" {
|
||||
local i3 = 1
|
||||
while `i3' <= `nrhs2' {
|
||||
local rhschk : word `i3' of `rhsnms2'
|
||||
if "`1'"=="`rhschk'" {
|
||||
local found "yes"
|
||||
local cvn2`xchngs' `i3'
|
||||
local i3 = `nrhs2'
|
||||
}
|
||||
local i3 = `i3' + 1
|
||||
}
|
||||
}
|
||||
if "`found'"=="no" {
|
||||
di in r "`1' not rhs variable"
|
||||
error 198
|
||||
}
|
||||
*make sure value is legal
|
||||
local cval`xchngs' "`2'"
|
||||
if "`2'"=="mean" | "`2'"=="median" | "`2'"=="min" | /*
|
||||
*/ "`2'"=="max" | "`2'"=="grmean" | "`2'"=="grmedian" | /*
|
||||
*/ "`2'"=="grmin" | "`2'"=="grmax" | "`2'"=="upper" | /*
|
||||
*/ "`2'"=="lower" | "`2'"=="previous" | "`2'"=="old" {
|
||||
local allnums = "no"
|
||||
}
|
||||
else {
|
||||
confirm number `2'
|
||||
local cexp`xchngs' "`cvar`xchngs'' == `cval`xchngs''"
|
||||
}
|
||||
macro shift 2
|
||||
} /* while `1' != "" { */
|
||||
|
||||
*set matrix to 'rest' values
|
||||
*rest default is mean
|
||||
if "`rest'" == "" {
|
||||
local rest = "mean"
|
||||
}
|
||||
|
||||
if "`rest'"=="previous" | "`rest'"=="old" {
|
||||
mat `tobase' = `prev'
|
||||
if "`twoeq'"=="yes" {
|
||||
mat `tobase2' = `prev'
|
||||
}
|
||||
}
|
||||
|
||||
*! version 1.6.5 2007-02-08 - rest(zero)
|
||||
else if "`rest'"=="mean" | "`rest'"=="max" | "`rest'"=="min" | ///
|
||||
"`rest'"=="median" | "`rest'"=="zero" {
|
||||
mat `tobase' = ``rest''
|
||||
if "`twoeq'"=="yes" {
|
||||
mat `tobase2' = ``rest'2'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
else if "`rest'"=="grmean" | "`rest'"=="grmax" | "`rest'"=="grmin" | "`rest'"=="grmedian" {
|
||||
if "`allnums'"!="yes" {
|
||||
di in r "`rest' not allowed if x() values not all real numbers"
|
||||
exit 198
|
||||
} /* if "`allnums'"!="yes" */
|
||||
qui gen `mark' = 1 `if'
|
||||
local i = 1
|
||||
while `i' <= `xchngs' {
|
||||
qui replace `mark' = . if ~(`cexp`i'')
|
||||
local i = `i' + 1
|
||||
} /* while i <= `xchngs' */
|
||||
|
||||
_pesum if `mark' == 1, median
|
||||
if "`rest'"=="grmean" { mat `tobase' = r(Smean) }
|
||||
if "`rest'"=="grmax" { mat `tobase' = r(Smax) }
|
||||
if "`rest'"=="grmin" { mat `tobase' = r(Smin) }
|
||||
if "`rest'"=="grmedian" { mat `tobase' = r(Smedian) }
|
||||
mat `tobase' = `tobase'[1, 2...]
|
||||
if "`twoeq'"=="yes" {
|
||||
_pesum if `mark' == 1, median two
|
||||
if "`rest'"=="grmean" { mat `tobase2' = r(Smean) }
|
||||
if "`rest'"=="grmax" { mat `tobase2' = r(Smax) }
|
||||
if "`rest'"=="grmin" { mat `tobase2' = r(Smin) }
|
||||
if "`rest'"=="grmedian" { mat `tobase2' = r(Smedian) }
|
||||
mat `tobase2' = `tobase2'[1, 2...]
|
||||
} /* if "`twoeq'"=="yes" */
|
||||
} /* else if "`rest'"=="grmean"... */
|
||||
|
||||
else if "`rest'"=="upper" | "`rest'"=="lower" {
|
||||
if "`noupper'"=="yes" {
|
||||
di in r "rest(`rest') not permitted after `e(cmd)'"
|
||||
exit 198
|
||||
} /* if "`noupper'"=="yes" */
|
||||
capture matrix `b' = e(b)
|
||||
mat `tobase' = r(Smax)
|
||||
mat `tobase' = `tobase'[1, 2...]
|
||||
mat `xmin' = r(Smin)
|
||||
mat `xmin' = `xmin'[1, 2...]
|
||||
local nvars = colsof(`tobase')
|
||||
local i = 1
|
||||
while `i' <= `nvars' {
|
||||
if "`rest'"=="upper" & `b'[1,`i']<0 {
|
||||
mat `tobase'[1, `i'] == `xmin'[1, `i']
|
||||
}
|
||||
if "`rest'"=="lower" & `b'[1,`i']>0 {
|
||||
mat `tobase'[1, `i'] == `xmin'[1, `i']
|
||||
}
|
||||
local i = `i' + 1
|
||||
} /* while `i' <= `nvars' */
|
||||
|
||||
}
|
||||
else {
|
||||
di in red "rest(`rest') not allowed"
|
||||
error 999
|
||||
}
|
||||
|
||||
* set specific values of tobase and tobase2...
|
||||
local i = 1
|
||||
while `i' <= `xchngs' {
|
||||
if "`cvno`i''"!="" {
|
||||
if "`cval`i''"=="mean" | "`cval`i''"=="median" | /*
|
||||
*/ "`cval`i''"=="min" | "`cval`i''"=="max" {
|
||||
mat `tobase'[1, `cvno`i''] = ``cval`i'''[1, `cvno`i'']
|
||||
}
|
||||
else if "`cval`i''"=="previous" {
|
||||
mat `tobase'[1, `cvno`i''] = `prev'[1, `cvno`i'']
|
||||
}
|
||||
else if "`cval`i''"=="upper" | "`cval`i''"=="lower" {
|
||||
if "`noupper'"=="yes" {
|
||||
di in r "`rest' not permitted in x() after `e(cmd)'"
|
||||
exit 198
|
||||
} /* if "`noupper'"=="yes" */
|
||||
capture matrix `b' = e(b)
|
||||
if "`cval`i''"=="upper" {
|
||||
if `b'[1,`cvno`i'']<0 {
|
||||
mat `tobase'[1, `cvno`i''] == `min'[1, `cvno`i'']
|
||||
}
|
||||
else {
|
||||
mat `tobase'[1, `cvno`i''] == `max'[1, `cvno`i'']
|
||||
}
|
||||
}
|
||||
if "`cval`i''"=="lower" {
|
||||
if `b'[1,`cvno`i'']>0 {
|
||||
mat `tobase'[1, `cvno`i''] == `min'[1, `cvno`i'']
|
||||
}
|
||||
else {
|
||||
mat `tobase'[1, `cvno`i''] == `max'[1, `cvno`i'']
|
||||
}
|
||||
}
|
||||
} /* if "`cval`i''"=="upper" | "`cval`i''"=="lower" */
|
||||
else { mat `tobase'[1, `cvno`i''] = `cval`i'' }
|
||||
} /* if "`cvno`i'"!="" */
|
||||
|
||||
if "`cvn2`i''"!="" {
|
||||
if "`cval`i''"=="mean" | "`cval`i''"=="median" /*
|
||||
*/ | "`cval`i''"=="min" | "`cval`i''"=="max" {
|
||||
mat `tobase2'[1, `cvn2`i''] = ``cval`i'''[1, `cvn2`i'']
|
||||
}
|
||||
else if "`cval`i''"=="previous" {
|
||||
mat `tobase2'[1, `cvn2`i''] = `prev'[1, `cvn2`i'']
|
||||
}
|
||||
else { mat `tobase2'[1, `cvn2`i''] = `cval`i'' }
|
||||
} /* if "`cvn2`i'"!="" */
|
||||
|
||||
local i = `i' + 1
|
||||
} /* while i <= `xchngs' */
|
||||
|
||||
if "`choices'"!="" { return local choices `choices' }
|
||||
mat rownames `tobase' = x
|
||||
mat PE_base = `tobase'
|
||||
return matrix pebase `tobase'
|
||||
return local nrhs "`nrhs'"
|
||||
return local rhsnms "`rhsnms'"
|
||||
if "`twoeq'"=="yes" {
|
||||
mat rownames `tobase2' = x_eq2
|
||||
mat PE_base2 = `tobase2'
|
||||
return matrix pebase2 `tobase2'
|
||||
return local nrhs2 "`nrhs2'"
|
||||
return local rhsnms2 "`rhsnms2'"
|
||||
}
|
||||
end
|
||||
|
||||
exit
|
||||
|
||||
*History
|
||||
* version 1.6.4 13Apr2005
|
||||
* version 1.6.3 27Mar2005 slogit
|
||||
* version 1.6.2 28Feb2005 mprobit
|
||||
* version 1.6.1 18Feb2005 ztp & ztnb
|
||||
* version 1.6.0 3/29/01
|
54
Modules/ado/plus/_/_pebase.hlp
Normal file
54
Modules/ado/plus/_/_pebase.hlp
Normal file
@ -0,0 +1,54 @@
|
||||
.-
|
||||
help for ^_pebase^ - 1.6.5 2007-02-08
|
||||
.-
|
||||
|
||||
_pebase [if] [in] [, x(variables_and_values) rest(stat) all]
|
||||
|
||||
where
|
||||
|
||||
^variables_and_values^ is an alternating list of variables and either
|
||||
numeric values or mean, median, min, max, upper, lower, previous
|
||||
|
||||
^stat^ is either mean, median, min, max, upper, lower, previous, zero,
|
||||
grmean (group mean), grmedian, grmin, grmax
|
||||
|
||||
|
||||
^_pebase^ is a utility command for programming commands that require the user
|
||||
to set specific values for the independent variables in regression.
|
||||
|
||||
Values of the independent variables are set using the ^x()^, ^rest()^, and ^all^
|
||||
options. These can be passed directly from the user input for the
|
||||
programmer's command to ^_pebase^.
|
||||
|
||||
Options
|
||||
-------
|
||||
|
||||
^x()^ sets the values of independent variables for calculating predicted
|
||||
values. The list must alternate variable names and either numeric values
|
||||
or types of ^stat^.
|
||||
|
||||
^rest()^ sets the independent variables not specified in x() to one of the
|
||||
types of ^stat^. Type ^help prstar^ for more details about using ^x()^ and
|
||||
^rest^.
|
||||
|
||||
^all^ specifies that any calculations of means, medians, etc., should use
|
||||
the entire sample instead of the sample used to estimate the model.
|
||||
|
||||
Output of _pebase
|
||||
-----------------
|
||||
|
||||
PE_base: global matrix containing set values of independent variables
|
||||
|
||||
r(pebase): r() class matrix containing set values of independent variables
|
||||
|
||||
r(nrhs): r() class macro containing number of independent variables
|
||||
|
||||
r(rhsnms): r() class macro containing names of independent variables
|
||||
|
||||
PE_base2, r(pebase2), r(nrhs2), r(rhsnms): same as above but for the
|
||||
second equation of two-equation commands like ^zip^ and ^zinb^.
|
||||
|
||||
.-
|
||||
Authors: J. Scott Long and Jeremy Freese
|
||||
www.indiana.edu/~jslsoc/spost.htm
|
||||
spostsup@@indiana.edu
|
172
Modules/ado/plus/_/_pecats.ado
Normal file
172
Modules/ado/plus/_/_pecats.ado
Normal file
@ -0,0 +1,172 @@
|
||||
*! version 1.7.3 2007-06-29 Stata 10 fix for categories
|
||||
|
||||
capture program drop _pecats
|
||||
program define _pecats, rclass
|
||||
version 6.0
|
||||
tempname refval valnum rcount
|
||||
scalar `refval' = -999
|
||||
syntax [varlist(max=1 default=none)] [if] [in]
|
||||
|
||||
* only return values for models with categorical outcomes
|
||||
if "`varlist'" == "" & ( /*
|
||||
*/ "`e(cmd)'"!="logit" & /*
|
||||
*/ "`e(cmd)'"!="logistic" & /*
|
||||
*/ "`e(cmd)'"!="probit" & /*
|
||||
*/ "`e(cmd)'"!="cloglog" & /*
|
||||
*/ "`e(cmd)'"!="ologit" & /*
|
||||
*/ "`e(cmd)'"!="oprobit" & /*
|
||||
*/ "`e(cmd)'"!="mlogit" & /*
|
||||
*/ "`e(cmd)'"!="mprobit" & /*
|
||||
*/ "`e(cmd)'"!="gologit" & /*
|
||||
*/ "`e(cmd)'"!="clogit" & /*
|
||||
*/ "`e(cmd)'"!="slogit" & /*
|
||||
*/ ) {
|
||||
if "`e(cmd)'"=="tobit" /*
|
||||
*/ | "`e(cmd)'"=="intreg" /*
|
||||
*/ | "`e(cmd)'"=="cnreg" /*
|
||||
*/ | "`e(cmd)'"=="regress" /*
|
||||
*/ | "`e(cmd)'"!="poisson" /*
|
||||
*/ | "`e(cmd)'"!="nbreg" /*
|
||||
*/ | "`e(cmd)'"!="ztp" /*
|
||||
*/ | "`e(cmd)'"!="ztnb" /*
|
||||
*/ | "`e(cmd)'"!="zip" /*
|
||||
*/ | "`e(cmd)'"!="zinb" {
|
||||
return scalar numcats = 2
|
||||
}
|
||||
exit
|
||||
}
|
||||
|
||||
* numeric value of reference category of mlogit
|
||||
* 2007-06-29 stata 10 fix
|
||||
if c(stata_version) < 10 {
|
||||
|
||||
if "`e(cmd)'"=="mlogit" { scalar `refval' = e(basecat) }
|
||||
if "`e(cmd)'"=="mprobit" { scalar `refval' = e(i_base) }
|
||||
}
|
||||
else {
|
||||
if "`e(cmd)'"=="mlogit" { scalar `refval' = e(baseout) }
|
||||
if "`e(cmd)'"=="mprobit" { scalar `refval' = e(i_base) }
|
||||
}
|
||||
|
||||
* determine names and values of outcome categories
|
||||
local catnms ""
|
||||
if "`varlist'" != "" {
|
||||
local lhs `varlist'
|
||||
quietly tabulate `1' `if' `in', matrow(`valnum') matcell(`rcount')
|
||||
}
|
||||
if "`varlist'" == "" {
|
||||
local lhs "`e(depvar)'"
|
||||
quietly tabulate `e(depvar)' if e(sample)==1, matrow(`valnum') matcell(`rcount')
|
||||
}
|
||||
local nrows = rowsof(`valnum')
|
||||
|
||||
* grab value labels
|
||||
local vallbl : value label `lhs'
|
||||
local i = 1
|
||||
while `i' <= `nrows' {
|
||||
local vali = `valnum'[`i',1]
|
||||
|
||||
* if value labels have been declared
|
||||
if "`vallbl'" != "" {
|
||||
local valnm : label `vallbl' `vali'
|
||||
if "`valnm'" == "" { local valnm = `vali' }
|
||||
* change blanks to _'s
|
||||
local valnm = trim("`valnm'")
|
||||
local bloc = index("`valnm'"," ")
|
||||
while `bloc' != 0 {
|
||||
local bloc = `bloc' - 1
|
||||
local bloc2 = `bloc' + 2
|
||||
local valnm = trim(substr("`valnm'",1,`bloc') /*
|
||||
*/ + "_" + substr("`valnm'",`bloc2',.))
|
||||
local bloc = index("`valnm'"," ")
|
||||
}
|
||||
* change :'s to _'s
|
||||
local bloc = index("`valnm'",":")
|
||||
while `bloc' != 0 {
|
||||
local bloc = `bloc' - 1
|
||||
local bloc2 = `bloc' + 2
|
||||
local valnm = trim(substr("`valnm'",1,`bloc') /*
|
||||
*/ + "_" + substr("`valnm'",`bloc2',.))
|
||||
local bloc = index("`valnm'",":")
|
||||
}
|
||||
* change {'s to _'s
|
||||
local bloc = index("`valnm'","{")
|
||||
while `bloc' != 0 {
|
||||
local bloc = `bloc' - 1
|
||||
local bloc2 = `bloc' + 2
|
||||
local valnm = trim(substr("`valnm'",1,`bloc') /*
|
||||
*/ + "_" + substr("`valnm'",`bloc2',.))
|
||||
local bloc = index("`valnm'","{")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
* if no value labels, then use value numbers
|
||||
else { local valnm `vali' }
|
||||
|
||||
* change .'s to _'s
|
||||
local bloc = index("`valnm'",".")
|
||||
while `bloc' != 0 {
|
||||
local bloc = `bloc' - 1
|
||||
local bloc2 = `bloc' + 2
|
||||
local valnm = trim(substr("`valnm'",1,`bloc') /*
|
||||
*/ + "_" + substr("`valnm'",`bloc2',.))
|
||||
local bloc = index("`valnm'",".")
|
||||
}
|
||||
|
||||
|
||||
* if current value is refernce value, store it
|
||||
if `vali'==`refval' {
|
||||
local refnm `valnm'
|
||||
local refval `vali'
|
||||
}
|
||||
else {
|
||||
local catnms `catnms' `valnm'
|
||||
local catvals `catvals' `vali'
|
||||
|
||||
*handle long label names for catnms8
|
||||
if length("`valnm'") > 8 { local valnm = substr("`valnm'", 1, 8) }
|
||||
local catnms8 `catnms8' `valnm'
|
||||
}
|
||||
local i = `i' + 1
|
||||
}
|
||||
|
||||
* place reference value at end for mlogit
|
||||
if `refval'!=-999 {
|
||||
local catnms `catnms' `refnm'
|
||||
local catvals `catvals' `refval'
|
||||
|
||||
*handle long label names for catnms8
|
||||
if length("`refnm'") > 8 { local refnm = substr("`refnm'", 1, 8) }
|
||||
local catnms8 `catnms8' `refnm'
|
||||
}
|
||||
|
||||
* logit probit clogit for case of 0 vs > 0
|
||||
if "`varlist'"=="" & /*
|
||||
*/ ("`e(cmd)'"=="logit" | "`e(cmd)'"=="probit" | "`e(cmd)'"== "clogit" | "`e(cmd)'"=="cloglog" ) /*
|
||||
*/ & `nrows'~=2 {
|
||||
local catnms 0 ~0
|
||||
local catvals 0 ~0
|
||||
local catnms8 0 ~0
|
||||
}
|
||||
|
||||
*number of categories as catnum
|
||||
local numcats : word count `catnms'
|
||||
|
||||
*return information about reference category if mlogit
|
||||
if "`varlist'"=="" & "`e(cmd)'" == "mlogit" {
|
||||
return scalar refval =`refval'
|
||||
return local refnm "`refnm'"
|
||||
}
|
||||
|
||||
return local catnms "`catnms'"
|
||||
return local catvals "`catvals'"
|
||||
return local catnms8 "`catnms8'"
|
||||
return scalar numcats = `numcats'
|
||||
|
||||
end
|
||||
exit
|
||||
* version 1.7.2 13Apr2005
|
||||
* version 1.7.1 27Mar2005 slogit
|
||||
* version 1.7.0 28Feb2005 mprobit
|
||||
* version 1.6.9 18Feb2005 ztp and ztnb
|
61
Modules/ado/plus/_/_pecats.hlp
Normal file
61
Modules/ado/plus/_/_pecats.hlp
Normal file
@ -0,0 +1,61 @@
|
||||
.-
|
||||
help for ^_pecats^ - 1.6.6 - 3/4/00
|
||||
.-
|
||||
|
||||
Utility to determine names and values of categories of dependent variable
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
^_pecats^ [varname] [^if^ exp] [^in^ range]
|
||||
|
||||
Description
|
||||
-----------
|
||||
|
||||
^_pecats^ returns the names and values of the categories for models with
|
||||
ordinal, nominal, or binary outcomes. For ^mlogit^ it indicates the value
|
||||
of the reference category.
|
||||
|
||||
NOTE: If no variable name is specified, _pecats will use the dependent
|
||||
variable retrieved from `e(depvar)' and will restrict the sample according
|
||||
to e(sample)
|
||||
|
||||
Returns
|
||||
-------
|
||||
|
||||
r(numcats): scalar value containing number of categories of depvar.
|
||||
|
||||
r(refval): scalar value of the reference category for ^mlogit^.
|
||||
|
||||
r(refnm): local macro with name of the reference category for ^mlogit^.
|
||||
|
||||
r(catnms): local macro with names of categories of the dependent variable.
|
||||
For ^mlogit^ the reference category is given last.
|
||||
|
||||
r(catnms): local macro with names of categories of the dependent variable.
|
||||
For ^mlogit^ the reference category is given last.
|
||||
|
||||
r(catnms8): local macro with names of categories of the dependent variable.
|
||||
Value labels longer than 8 characters are truncated to 8
|
||||
characters so they can be used as matrix row/column names. For
|
||||
^mlogit^ the reference category is given last.
|
||||
|
||||
r(catvals): local macro with values of categories of the dependent variable.
|
||||
For ^mlogit^ the reference value is given last.
|
||||
|
||||
Examples within a program
|
||||
-------------------------
|
||||
|
||||
...
|
||||
* `if' is the curret if condition
|
||||
_peife `if'
|
||||
* the new condition includes "& e(sample)
|
||||
local eif "`r(if)'"
|
||||
_pecats `e(depvar)' `eif' `in'
|
||||
local cnames r(catnms)
|
||||
local cvals r(catvals)
|
||||
local refval r(refval)
|
||||
|
||||
.-
|
||||
Authors: J. Scott Long and Jeremy Freese
|
||||
www.indiana.edu/~jslsoc/spost.htm
|
||||
spostsup@@indiana.edu
|
||||
|
629
Modules/ado/plus/_/_peciboot.ado
Normal file
629
Modules/ado/plus/_/_peciboot.ado
Normal file
@ -0,0 +1,629 @@
|
||||
*! version 1.0.0 2009-10-28 jsl
|
||||
* - stata 11 fix
|
||||
|
||||
// bootstrap for prvalue
|
||||
capture program drop _peciboot
|
||||
program define _peciboot, rclass
|
||||
|
||||
version 8.0
|
||||
syntax [if] [in] [, x(passthru) rest(passthru) all ///
|
||||
save diff ///
|
||||
Reps(int 1000) SIze(int -9) dots ///
|
||||
ITERate(passthru) match SAving(string)]
|
||||
|
||||
tempname post_name lastest p_obsmat pdif_obsmat pci_mat pdifci_mat ///
|
||||
probs1 probs2 probsdif ///
|
||||
tempbase tempbase2
|
||||
tempname mu_obs mudif_obs mu mudif ///
|
||||
all0_obs all0dif_obs all0 all0dif
|
||||
tempname p_obs p_avg p_std p_normlo p_normup ///
|
||||
pdif_obs pdif_avg pdif_std pdif_normlo pdif_normup ///
|
||||
p_pctlo p_pctup pdif_pctlo pdif_pctup z t tdf ///
|
||||
totobs zpr zdc p_bcup p_bclo pdif_bcup pdif_bclo
|
||||
tempname orig_pred orig_info orig_type orig_base orig_base2 ///
|
||||
orig_upper orig_lower orig_upnorm orig_lonorm ///
|
||||
orig_upbias orig_lobias
|
||||
tempvar touse
|
||||
tempfile original post_file
|
||||
|
||||
* store information to restore when bootstrapping is done
|
||||
mat `orig_pred' = pepred
|
||||
mat `orig_info' = peinfo
|
||||
mat `orig_base' = pebase
|
||||
mat `orig_upper' = peupper
|
||||
mat `orig_lower' = pelower
|
||||
|
||||
* get information on model that has been estimated
|
||||
local io $petype
|
||||
local maxcnt = peinfo[1,11] // get max # of counts compute
|
||||
if "`maxcnt'"=="." {
|
||||
local maxcnt = 9
|
||||
}
|
||||
local cmd : word 1 of $petype // specific model command
|
||||
local input : word 2 of $petype // typical or twoeq
|
||||
local output : word 3 of $petype // output type
|
||||
local depvar "`e(depvar)'" // dep var
|
||||
local numcats = peinfo[1,2]
|
||||
forval i = 1/`numcats' { // construct string with outcome categories
|
||||
local curval = pepred[1, `i']
|
||||
local catvals "`catvals'`cutval' "
|
||||
}
|
||||
local rhsnms : colnames(pebase)
|
||||
if "`input'"=="twoeq" {
|
||||
local rhsnms2 : colnames(pebase2)
|
||||
}
|
||||
local wtype "`e(wtype)'" // weight type
|
||||
local wexp "`e(wexp)'" // weight expression
|
||||
local nocon = "" // constant in model
|
||||
if peinfo[1,6]==1 {
|
||||
local nocon "nocon"
|
||||
}
|
||||
local inout "`input' `output'"
|
||||
if "`input'"=="twoeq" {
|
||||
mat `orig_base2' = pebase2
|
||||
}
|
||||
local nobs = e(N) // observations in original estimation sample
|
||||
local level = peinfo[1,3]
|
||||
scalar `z' = peinfo[1,4]
|
||||
|
||||
* trap improper use of match option
|
||||
if ("`output'"!="binary" & "`output'"!="ordered" ///
|
||||
& "`output'"!="mlogit") & "`match'"!="" {
|
||||
di as error ///
|
||||
"Error: match works only with binary, ordered, and mlogit routines."
|
||||
exit
|
||||
}
|
||||
|
||||
* create touse indicating cases in the original estimatin sample
|
||||
mark `touse' if e(sample)
|
||||
if "`size'"=="-9" | "`size'"=="" { // -9 is default for size
|
||||
local size = e(N)
|
||||
}
|
||||
if "`size'"!="" & `size'>e(N) {
|
||||
di as error ///
|
||||
"Error: resample size is larger than the number of observations"
|
||||
exit
|
||||
}
|
||||
|
||||
* create list of variables and expressions used by post commands
|
||||
forval i = 1/`numcats' {
|
||||
tempname p`i'
|
||||
local post_var "`post_var'`p`i'' "
|
||||
local post_exp "`post_exp'(`p`i'') "
|
||||
if "`diff'"!="" {
|
||||
tempname p`i'dif
|
||||
local post_var "`post_var'`p`i'dif' "
|
||||
local post_exp "`post_exp'(`p`i'dif') "
|
||||
}
|
||||
}
|
||||
|
||||
* count # of statistics to bootstrap
|
||||
local nstats = `numcats' // # of statistics being bootstrapped
|
||||
if "`output'"=="count" {
|
||||
local nstats = `nstats' + 1 // add mu
|
||||
local post_exp "`post_exp'(`mu') "
|
||||
local post_var "`post_var'`mu' "
|
||||
if "`diff'"!="" {
|
||||
local post_exp "`post_exp'(`mudif') "
|
||||
local post_var "`post_var'`mudif' "
|
||||
}
|
||||
}
|
||||
if "`input'"=="twoeq" {
|
||||
local nstats = `nstats' + 1 // add pr always 0
|
||||
local post_exp "`post_exp'(`all0') "
|
||||
local post_var "`post_var'`all0' "
|
||||
if "`diff'"!="" {
|
||||
local post_exp "`post_exp'(`all0dif') "
|
||||
local post_var "`post_var'`all0dif' "
|
||||
}
|
||||
}
|
||||
|
||||
local dots = cond("`dots'"=="", "*", "noisily")
|
||||
|
||||
// STORE OBSERVED ESTIMATES
|
||||
|
||||
* get predictions for prob and discrete change
|
||||
mat `p_obsmat' = (pepred[2, 1...])'
|
||||
if "`diff'"!="" {
|
||||
mat `pdif_obsmat' = (pepred[6,1...])'
|
||||
}
|
||||
|
||||
* get rate and pr always 0
|
||||
if "`output'"=="count" {
|
||||
scalar `mu_obs' = pepred[3,2]
|
||||
if "`diff'"!="" {
|
||||
scalar `mudif_obs' = pepred[7,2]
|
||||
}
|
||||
}
|
||||
if "`input'"=="twoeq" {
|
||||
scalar `all0_obs' = pepred[3,4]
|
||||
if "`diff'"!="" {
|
||||
scalar `all0dif_obs' = pepred[7,4]
|
||||
}
|
||||
}
|
||||
|
||||
* hold non-bootstrap estimation results; restore later
|
||||
_estimates hold `lastest', restore
|
||||
|
||||
if "`match'"!="" {
|
||||
preserve // #1
|
||||
quietly keep if `touse'
|
||||
quietly save `original'
|
||||
restore // #1
|
||||
}
|
||||
|
||||
postfile `post_name' `post_var' using `post_file'
|
||||
|
||||
// BEGIN SIMULATIONS
|
||||
|
||||
quietly {
|
||||
|
||||
* # of replications missed due to nonconvergence
|
||||
local nmissed = 0
|
||||
|
||||
forval i = 1/`reps' {
|
||||
|
||||
`dots' dodot `reps' `i'
|
||||
preserve // #2
|
||||
|
||||
* if match, resample within outcome categories
|
||||
if "`match'"!="" {
|
||||
tempname samppct catsize
|
||||
scalar `samppct' = `size' / `nobs'
|
||||
|
||||
forval i = 1/`numcats' {
|
||||
tempfile cat`i'file
|
||||
use `original', clear
|
||||
local cur_val: word `i' of `catvals'
|
||||
local depval`i' "`cur_depval'"
|
||||
keep if `depvar'==`cur_val'
|
||||
count
|
||||
scalar `catsize' = `samppct'*r(N)
|
||||
local resamp = round(`catsize',1)
|
||||
if `catsize'==0 {
|
||||
local resamp = 1
|
||||
}
|
||||
bsample `resamp'
|
||||
save `cat`i'file'
|
||||
}
|
||||
|
||||
* stack data from all categories
|
||||
use `cat1file', clear
|
||||
forval i = 2/`numcats' {
|
||||
append using `cat`i'file'
|
||||
}
|
||||
} // matched
|
||||
|
||||
* if match option not specified
|
||||
else {
|
||||
|
||||
keep if `touse'
|
||||
bsample `size'
|
||||
|
||||
* check if boot sample has all outcome categories
|
||||
if "`output'"!="count" {
|
||||
_pecats `depvar'
|
||||
local catschk = r(numcats)
|
||||
* if category missed, count it, and take another sample
|
||||
if `catschk' != `numcats' {
|
||||
local ++nmissed // count missed replication
|
||||
local errlog "`errlog'`i', "
|
||||
restore
|
||||
continue
|
||||
}
|
||||
}
|
||||
} // no matched
|
||||
|
||||
// ESTIMATE MODEL WITH BOOTSTRAP SAMPLE
|
||||
|
||||
capture { // trap errors in estimation
|
||||
|
||||
if "`input'" == "typical" {
|
||||
`cmd' `depvar' `rhsnms' ///
|
||||
if `touse' [`wtype'`wexp'], `iterate' `nocon'
|
||||
}
|
||||
else if "`input'" == "twoeq" {
|
||||
`cmd' `depvar' `rhsnms' ///
|
||||
if `touse' [`wtype'`wexp'], ///
|
||||
inflate(`rhsnms2') `iterate' `nocon'
|
||||
}
|
||||
|
||||
* get base values for bootstrap sample
|
||||
_pebase `if' `in', `x' `rest' `all'
|
||||
mat `tempbase' = r(pebase)
|
||||
mat PE_in = `tempbase'
|
||||
if "`input'"=="twoeq" {
|
||||
mat `tempbase2' = r(pebase2)
|
||||
mat PE_in2 = `tempbase2'
|
||||
}
|
||||
|
||||
* get predictions
|
||||
_pepred, level(`level') maxcnt(`maxcnt')
|
||||
local tmp2 = r(maxcount)
|
||||
local tmp = r(level)
|
||||
capture _return drop pepred
|
||||
_return hold pepred
|
||||
_return restore pepred, hold
|
||||
* put results from bootstrap estimate into global matrices
|
||||
_pecollect, inout(`inout') level(`tmp') ///
|
||||
maxcount(`tmp2') `diff'
|
||||
|
||||
} // capture
|
||||
|
||||
* if error in estimation, count it as missed
|
||||
if _rc!=0 {
|
||||
local ++nmissed
|
||||
local errlog "`errlog'`i', "
|
||||
restore
|
||||
continue
|
||||
}
|
||||
|
||||
* get predicted probabilities
|
||||
mat `probs1'= (pepred[2, 1...])'
|
||||
* get mu and pr(always 0) from count models
|
||||
if "`output'"=="count" {
|
||||
scalar `mu' = pepred[3,2]
|
||||
}
|
||||
if "`input'"=="twoeq" {
|
||||
scalar `all0' = pepred[3,4]
|
||||
}
|
||||
|
||||
// DISCRETE CHANGES
|
||||
|
||||
if "`diff'"!="" {
|
||||
|
||||
capture {
|
||||
|
||||
* $pexsetup hold x() rest() all from prior prvalue
|
||||
_pebase `if' `in', $pexsetup
|
||||
mat `tempbase' = r(pebase)
|
||||
mat `tempbase2' = r(pebase2)
|
||||
mat PE_in = `tempbase'
|
||||
if "`input'"=="twoeq" {
|
||||
mat PE_in2 = `tempbase2'
|
||||
}
|
||||
_pepred, level(`level') maxcnt(`maxcnt')
|
||||
local tmp2 = r(maxcount)
|
||||
local tmp = r(level)
|
||||
_return drop _all
|
||||
_return hold pepred
|
||||
_return restore pepred, hold
|
||||
_pecollect, inout(`inout') level(`tmp') ///
|
||||
maxcount(`tmp2') `diff'
|
||||
|
||||
} // end of capture
|
||||
|
||||
if _rc !=0 { // if error in estimation
|
||||
local ++nmissed // count missed replication
|
||||
local errlog "`errlog'`i', "
|
||||
restore
|
||||
continue
|
||||
}
|
||||
|
||||
mat `probs2' = (pepred[2, 1...])'
|
||||
mat `probsdif' = `probs1' - `probs2'
|
||||
|
||||
* get results from count models
|
||||
if "`output'"=="count" {
|
||||
scalar `mudif' = -1 * pepred[7,2]
|
||||
}
|
||||
if "`input'"=="twoeq" {
|
||||
scalar `all0dif' = -1 * pepred[7,4]
|
||||
}
|
||||
|
||||
} // end of diff loop
|
||||
|
||||
// POST RESULTS
|
||||
|
||||
* move probs from matrices to scalars for posting
|
||||
forval j = 1/`numcats' {
|
||||
scalar `p`j'' = `probs1'[`j', 1]
|
||||
if "`diff'"!="" {
|
||||
scalar `p`j'dif' = `probsdif'[`j', 1]
|
||||
}
|
||||
}
|
||||
post `post_name' `post_exp'
|
||||
|
||||
restore // #2
|
||||
|
||||
} // end of replications loop
|
||||
|
||||
postclose `post_name' // close postfile
|
||||
|
||||
// CONSTRUCT CI
|
||||
|
||||
preserve // #3
|
||||
|
||||
use `post_file', clear
|
||||
qui count
|
||||
scalar `totobs' = r(N)
|
||||
scalar `tdf' = `totobs' -1
|
||||
scalar `t' = invttail(`tdf',((1-(`level')/100)/2))
|
||||
|
||||
* rename mu and all0 so loop for p_i can be used later
|
||||
local inew = `numcats'
|
||||
if "`output'"=="count" {
|
||||
local inew = `inew' + 1
|
||||
tempname p`inew'
|
||||
rename `mu' `p`inew''
|
||||
matrix `p_obsmat' = `p_obsmat' \ `mu_obs'
|
||||
if "`diff'"!="" {
|
||||
tempname p`inew'dif
|
||||
rename `mudif' `p`inew'dif'
|
||||
matrix `pdif_obsmat' = `pdif_obsmat' \ `mudif_obs'
|
||||
}
|
||||
}
|
||||
if "`input'"=="twoeq" {
|
||||
local inew = `inew' + 1
|
||||
tempname p`inew'
|
||||
rename `all0' `p`inew''
|
||||
matrix `p_obsmat' = `p_obsmat' \ `all0_obs'
|
||||
if "`diff'"!="" {
|
||||
tempname p`inew'dif
|
||||
rename `all0dif' `p`inew'dif'
|
||||
matrix `pdif_obsmat' = `pdif_obsmat' \ `all0dif_obs'
|
||||
}
|
||||
} // twoeq
|
||||
|
||||
* loop through each statistics
|
||||
forval i = 1/`nstats' {
|
||||
|
||||
sum `p`i'', d
|
||||
scalar `p_obs' = `p_obsmat'[`i',1]
|
||||
scalar `p_avg' = r(mean)
|
||||
scalar `p_std' = r(sd)
|
||||
|
||||
* bias correction method
|
||||
qui count if `p`i''<=`p_obs'
|
||||
* zpr will be missing if r(N)=0
|
||||
scalar `zpr' = invnorm(r(N)/`totobs')
|
||||
|
||||
* use t for normal
|
||||
scalar `p_normup' = `p_obs' + `t'*`p_std'
|
||||
scalar `p_normlo' = `p_obs' - `t'*`p_std'
|
||||
|
||||
* percentile method
|
||||
qui _pctile `p`i'', nq(1000)
|
||||
local upperpctile = 500 - 5*-`level'
|
||||
local lowerpctile = 1000 - `upperpctile'
|
||||
scalar `p_pctup' = r(r`upperpctile')
|
||||
scalar `p_pctlo' = r(r`lowerpctile')
|
||||
|
||||
* percentile for the bias-correction.
|
||||
local upnum = round((norm(2*`zpr' + `z')) * 1000, 1)
|
||||
local lonum = round((norm(2*`zpr' - `z')) * 1000, 1)
|
||||
if `zpr'==. { // if missing, upper & lower limits are missing
|
||||
scalar `p_bcup' = .
|
||||
scalar `p_bclo' = .
|
||||
}
|
||||
else {
|
||||
scalar `p_bcup' = r(r`upnum')
|
||||
scalar `p_bclo' = r(r`lonum')
|
||||
}
|
||||
|
||||
* stack results from 3 methods
|
||||
mat `pci_mat' = nullmat(`pci_mat') \ ///
|
||||
`p_pctlo', `p_pctup', ///
|
||||
`p_normlo', `p_normup', ///
|
||||
`p_bclo', `p_bcup'
|
||||
|
||||
// CI FOR DISCRETE CHANGE
|
||||
|
||||
if "`diff'"!="" {
|
||||
|
||||
sum `p`i'dif', d
|
||||
scalar `pdif_obs' = `pdif_obsmat'[`i',1]
|
||||
scalar `pdif_avg' = r(mean)
|
||||
scalar `pdif_std' = r(sd)
|
||||
|
||||
* bias corrected method
|
||||
qui count if `p`i'dif'<=`pdif_obs'
|
||||
scalar `zdc' = invnorm(r(N)/`totobs')
|
||||
local upnum = round((norm(2*`zdc' + `z'))*1000, 1)
|
||||
local lonum = round((norm(2*`zdc' - `z'))*1000, 1)
|
||||
|
||||
* use t for normal
|
||||
scalar `pdif_normup' = `pdif_obs' + `t'*`pdif_std'
|
||||
scalar `pdif_normlo' = `pdif_obs' - `t'*`pdif_std'
|
||||
|
||||
* percentile method
|
||||
_pctile `p`i'dif', nq(1000)
|
||||
scalar `pdif_pctup' = r(r`upperpctile')
|
||||
scalar `pdif_pctlo' = r(r`lowerpctile')
|
||||
|
||||
* percentile for bias corrected
|
||||
if `zdc'==. {
|
||||
scalar `pdif_bcup' = .
|
||||
scalar `pdif_bclo' = .
|
||||
}
|
||||
else {
|
||||
scalar `pdif_bcup' = r(r`upnum')
|
||||
scalar `pdif_bclo' = r(r`lonum')
|
||||
}
|
||||
|
||||
* stack results from 3 methods
|
||||
mat `pdifci_mat' = nullmat(`pdifci_mat') \ ///
|
||||
`pdif_pctlo', `pdif_pctup', ///
|
||||
`pdif_normlo', `pdif_normup', ///
|
||||
`pdif_bclo', `pdif_bcup'
|
||||
}
|
||||
}
|
||||
|
||||
} // end of quietly
|
||||
|
||||
* grab the mu and all0 ci's
|
||||
tempname muci mudifci all0ci all0difci
|
||||
local inew = `numcats'
|
||||
if "`output'"=="count" {
|
||||
local inew = `inew' + 1
|
||||
matrix `muci' = `pci_mat'[`inew',1...]
|
||||
if "`diff'"!="" {
|
||||
matrix `mudifci' = `pdifci_mat'[`inew',1...]
|
||||
}
|
||||
}
|
||||
if "`input'"=="twoeq" {
|
||||
local inew = `inew' + 1
|
||||
matrix `all0ci' = `pci_mat'[`inew',1...]
|
||||
if "`diff'"!="" {
|
||||
matrix `all0difci' = `pci_mat'[`inew',1...]
|
||||
}
|
||||
}
|
||||
* get rid of mu and all 0 info leaving only probabilities
|
||||
mat `pci_mat' = `pci_mat'[1..`numcats',1...]
|
||||
if "`diff'"!="" {
|
||||
mat `pdifci_mat' = `pdifci_mat'[1..`numcats',1...]
|
||||
}
|
||||
|
||||
// RESTORE DATA FROM ORIGINAL ESTIMATION
|
||||
|
||||
mat pepred = `orig_pred'
|
||||
mat peinfo = `orig_info'
|
||||
mat pebase = `orig_base'
|
||||
if "`input'"=="twoeq" {
|
||||
mat pebase2 = `orig_base2'
|
||||
}
|
||||
mat peupper = `orig_upper'
|
||||
mat pelower = `orig_lower'
|
||||
mat peupnorm = peupper
|
||||
mat pelonorm = pelower
|
||||
mat peupbias = peupper
|
||||
mat pelobias = pelower
|
||||
mat peuppct = peupper
|
||||
mat pelopct = pelower
|
||||
|
||||
global petype "`io'"
|
||||
|
||||
// ADD CIs TO GLOBAL
|
||||
|
||||
* get list of rownames to use in return matrices
|
||||
forval i = 1/`numcats' {
|
||||
local curval = pepred[1, `i'] // current val
|
||||
local plist "`plist' p`curval' " // predicted probabilities
|
||||
local dlist "`dlist' pdif`curval' " // discrete changes
|
||||
}
|
||||
|
||||
* save x() rest() all setup to be used for prvalue, dif
|
||||
if "`save'"!="" {
|
||||
global pexsetup "`x' `rest' `all'"
|
||||
}
|
||||
|
||||
if "`diff'"!="" { // if discrete change
|
||||
mat colnames `pdifci_mat' = pctlo pctup nrmlo nrmup bclo bcup
|
||||
mat rownames `pdifci_mat' = `dlist'
|
||||
mat pelower[6,1] = (`pdifci_mat'[1...,1])'
|
||||
mat peupper[6,1] = (`pdifci_mat'[1...,2])'
|
||||
mat pelonorm[6,1] = (`pdifci_mat'[1...,3])'
|
||||
mat peupnorm[6,1] = (`pdifci_mat'[1...,4])'
|
||||
mat pelobias[6,1] = (`pdifci_mat'[1...,5])'
|
||||
mat peupbias[6,1] = (`pdifci_mat'[1...,6])'
|
||||
return mat bootcidifp = `pdifci_mat'
|
||||
} // difference
|
||||
|
||||
mat colnames `pci_mat' = pctlo pctup nrmlo nrmup bclo bcup
|
||||
mat rownames `pci_mat' = `plist'
|
||||
mat pelower[2,1] = (`pci_mat'[1..., 1])'
|
||||
mat peupper[2,1] = (`pci_mat'[1..., 2])'
|
||||
mat pelonorm[2,1] = (`pci_mat'[1..., 3])'
|
||||
mat peupnorm[2,1] = (`pci_mat'[1..., 4])'
|
||||
mat pelobias[2,1] = (`pci_mat'[1..., 5])'
|
||||
mat peupbias[2,1] = (`pci_mat'[1..., 6])'
|
||||
return mat bootcip = `pci_mat'
|
||||
local repsnomis = `reps' - `nmissed'
|
||||
return scalar Nrepsnomis = `repsnomis'
|
||||
|
||||
if "`output'"=="count" {
|
||||
mat pelower[3,2] = `muci'[1,1]
|
||||
mat peupper[3,2] = `muci'[1,2]
|
||||
mat pelonorm[3,2] = `muci'[1,3]
|
||||
mat peupnorm[3,2] = `muci'[1,4]
|
||||
mat pelobias[3,2] = `muci'[1,5]
|
||||
mat peupbias[3,2] = `muci'[1,6]
|
||||
if "`diff'"!="" {
|
||||
mat pelower[7,2] = `mudifci'[1,1]
|
||||
mat peupper[7,2] = `mudifci'[1,2]
|
||||
mat pelonorm[7,2] = `mudifci'[1,3]
|
||||
mat peupnorm[7,2] = `mudifci'[1,4]
|
||||
mat pelobias[7,2] = `mudifci'[1,5]
|
||||
mat peupbias[7,2] = `mudifci'[1,6]
|
||||
}
|
||||
}
|
||||
if "`input'"=="twoeq" {
|
||||
mat pelower[3,4] = `all0ci'[1,1]
|
||||
mat peupper[3,4] = `all0ci'[1,2]
|
||||
mat pelonorm[3,4] = `all0ci'[1,3]
|
||||
mat peupnorm[3,4] = `all0ci'[1,4]
|
||||
mat pelobias[3,4] = `all0ci'[1,5]
|
||||
mat peupbias[3,4] = `all0ci'[1,6]
|
||||
if "`diff'"!="" {
|
||||
mat pelower[7,4] = `all0difci'[1,1]
|
||||
mat peupper[7,4] = `all0difci'[1,2]
|
||||
mat pelonorm[7,4] = `all0difci'[1,3]
|
||||
mat peupnorm[7,4] = `all0difci'[1,4]
|
||||
mat pelobias[7,4] = `all0difci'[1,5]
|
||||
mat peupbias[7,4] = `all0difci'[1,6]
|
||||
}
|
||||
}
|
||||
|
||||
if "`saving'"!="" {
|
||||
forval i = 1/`numcats' {
|
||||
local varnm: word `i' of `plist'
|
||||
rename `p`i'' b_`varnm'
|
||||
if "`diff'"!="" {
|
||||
local varnm: word `i' of `dlist'
|
||||
rename `p`i'dif' b_`varnm'
|
||||
}
|
||||
}
|
||||
if "`output'"=="count" {
|
||||
local iadd = `numcats'+1
|
||||
rename `p`iadd'' b_mu
|
||||
if "`diff'"!="" {
|
||||
rename `p`iadd'dif' b_mudif
|
||||
}
|
||||
}
|
||||
if "`input'"=="twoeq" {
|
||||
local iadd = `iadd' + 1
|
||||
rename `p`iadd'' b_alw0
|
||||
if "`diff'"!="" {
|
||||
rename `p`iadd'dif' b_alw0dif
|
||||
}
|
||||
}
|
||||
save `saving'
|
||||
}
|
||||
|
||||
restore // #3
|
||||
|
||||
// RESTORE ERETURNS
|
||||
|
||||
mat peuppct = peupper // just duplicate the default method
|
||||
mat pelopct = pelower // just duplicate the default method
|
||||
_estimates unhold `lastest'
|
||||
|
||||
end // _peciboot
|
||||
|
||||
* produce dots
|
||||
capture program drop dodot
|
||||
program define dodot
|
||||
version 8
|
||||
args N n
|
||||
local dot "."
|
||||
* don't bother with %'s if few than 20 reps
|
||||
if `N'>19 {
|
||||
scalar s = `N'/10
|
||||
forvalues c = 0/10 {
|
||||
local c`c' = floor(`c'*s)
|
||||
if `n'==`c`c'' {
|
||||
local pct = `c'*10
|
||||
di in g `pct' "%" _c
|
||||
local dot ""
|
||||
* new line when iterations are done
|
||||
if `pct'==100 {
|
||||
di
|
||||
}
|
||||
}
|
||||
} //forvalues
|
||||
} // if > 19
|
||||
di in g as txt "`dot'" _c
|
||||
end
|
||||
exit
|
||||
* version 0.2.0 2005-02-03 (13Apr2005)
|
||||
* version 0.2.1 13Apr2005
|
154
Modules/ado/plus/_/_peciboot.hlp
Normal file
154
Modules/ado/plus/_/_peciboot.hlp
Normal file
@ -0,0 +1,154 @@
|
||||
{smcl}
|
||||
{* 2005-02-06}{...}
|
||||
{hline}
|
||||
help for {hi:_peciboot}{right:2/6/2005}
|
||||
{hline}
|
||||
|
||||
{title: Utility to implement the bootstrap method for calculating confidence intervals}
|
||||
|
||||
{p 8 15 2}{cmd:_peciboot} [if] [in] [{cmd:,}
|
||||
{cmd:x(}{it:variables_and_values}{cmd:)}
|
||||
{cmd:rest(}{it:stat}{cmd:)}
|
||||
{cmdab:r:eps(}{it:#}{cmd:)}
|
||||
{cmdab:si:ze(}{it:#}{cmd:)}
|
||||
{cmd:save}
|
||||
{cmd:diff}
|
||||
{cmd:all match dots}
|
||||
{cmdab:sa:ving(}{it:filename, save_options}{cmd:)}]
|
||||
|
||||
{p 4 4 2}
|
||||
where {it:variables_and_values} is an alternating list of variables
|
||||
and either numeric values or mean, median, min, max, upper, lower,
|
||||
previous and {it:stat} is either mean, median, min, max, upper, lower,
|
||||
previous, grmean(group mean), mrmedian, grmin, grmax.
|
||||
|
||||
{title: Description}
|
||||
|
||||
{p 4 4 2}
|
||||
{cmd:_peciboot} is a utility command that returns prediction, discrete
|
||||
changes and their confidence intervals using {help bootstrap} method
|
||||
with resampling technique. It can calculate boostrapped confidence intervals
|
||||
using the normal approximation, percentile, and bias-corrected methods.
|
||||
It applies to {help logit}, {help probit}, {help cloglog}, {help ologit},
|
||||
{help oprobit}, {help gologit}, {help mlogit}, {help poisson}, {help nbreg},
|
||||
{help zip}, and {help zinb} models.
|
||||
|
||||
{title: Options}
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd:x(}{it:variables_and_values}{cmd:)} sets the values of independent
|
||||
variables for calculating predicted values. The list must alternate variable
|
||||
names and either numeric values or types of {cmd:stat}.
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd:rest(}{it:stat}{cmd:)} sets the independent variables not specified
|
||||
in {cmd:x(}{it:variables_and_values}{cmd:)} to one of the types of {cmd:stat}.
|
||||
Check into {help prstar} for more details about using
|
||||
{cmd:x(}{it:variables_and_values}{cmd:)} and {cmd:rest(}{it:stat}{cmd:)}
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd:reps(}{it:#}{cmd:)} specifies the number of bootstrap replications
|
||||
to be performed. The default is 1000.
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd: size(}{it:#}{cmd:)} specifies the size of the samples to be drawn.
|
||||
The default is _N, meaning to draw samples of the same size as the data.
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd:save} saves current values of indepenent variables and predictions
|
||||
for computing changes using the diff option.
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd:diff} computes difference between current predictions and those that
|
||||
were saved.
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd:all} specifies that any calculation of means, medians, etc., should
|
||||
use the entire sample instead of the sample used to estimate the model.
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd:match} requests {cmd:_peciboot} to resample from each category group
|
||||
of the dependent variable in proportion of the resample size to the original
|
||||
sample size.
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd:dots} requests a dot be placed on the screen at the beginning of each
|
||||
replication, thus providing entertainment when a large number of reps() are
|
||||
requested. It also prints out the percent replications finished.
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd:saving(}{it:filename, save_options}{cmd:)} creates a Stata data file
|
||||
(.dta file) containing the bootstrap distribution for predictions and discrete
|
||||
changes.
|
||||
|
||||
|
||||
{title: Global Matrices}
|
||||
|
||||
{p 4 4 2}
|
||||
The second row of the following matrices contains predicted probabilities and
|
||||
the sixth row contains differences for predicted probabilities.
|
||||
|
||||
{p 4 8 2}
|
||||
peupper: is a global matrix containing upper end of confidence intervals.
|
||||
Default produces the percentile confidence intervals.
|
||||
|
||||
{p 4 8 2}
|
||||
pelower: is a global matrix containing lower end of confidence intervals.
|
||||
Default produces the percentile confidence intervals.
|
||||
|
||||
{p 4 8 2}
|
||||
peuppct: is a global matrix containing upper end of confidence intervals
|
||||
using the percentile method.
|
||||
|
||||
{p 4 8 2}
|
||||
pelopct: is a global matrix containing lower end of confidence intervals
|
||||
using the percentile method.
|
||||
|
||||
{p 4 8 2}
|
||||
peupnorm: is a global matrix containing lower end of confidence intervals
|
||||
using the normal approximation method.
|
||||
|
||||
{p 4 8 2}
|
||||
pelonorm: is a global matrix containing lower end of confidence intervals
|
||||
using normal approximation method.
|
||||
|
||||
{p 4 8 2}
|
||||
peupbias: is a global matrix containing upper end of confidence intervals
|
||||
using the bias-corrected method.
|
||||
|
||||
{p 4 8 2}
|
||||
pelobias: is a global matrix containing lower end of confidence intervals
|
||||
using the bias-corrected method.
|
||||
|
||||
{title: Returns}
|
||||
|
||||
{p 4 8 2}
|
||||
r(Nrepsnomis): is a scalar return containing the factual number of replications
|
||||
used for calculating bootstraped confidence intervals, which might be smaller than
|
||||
the one requested by users.
|
||||
|
||||
|
||||
{title: Examples}
|
||||
|
||||
{p 4 8 2}{cmd:._pebase `if' `in' , `x' `rest' `choices' `all'}{p_end}
|
||||
{p 4 8 2}{cmd:.mat `tobase' = r(pebase)}{p_end}
|
||||
{p 4 8 2}{cmd:._pepred, `level' `maxcnt'}{p_end}
|
||||
{p 4 8 2}{cmd:.local maxc = r(maxcount)}{p_end}
|
||||
{p 4 8 2}{cmd:.local lvl = r(level)}{p_end}
|
||||
{p 4 8 2}{cmd:.capture _return drop pepred}{p_end}
|
||||
{p 4 8 2}{cmd:._return hold pepred}{p_end}
|
||||
{p 4 8 2}{cmd:._return restore pepred, hold}{p_end}
|
||||
{p 4 8 2}{cmd:...}{p_end}
|
||||
{p 4 8 2}{cmd:...}{p_end}
|
||||
{p 4 8 2}{cmd:...}{p_end}
|
||||
{p 4 8 2}{cmd:._pecollect, inout("`io'") level(`lvl') /// }{p_end}
|
||||
{p 4 8 2}{cmd:. maxcount(`maxc') `diff' `reps'}{p_end}
|
||||
|
||||
{p 4 8 2}{cmd:._peciboot, `x' `rest' `all' `save' /// }{p_end}
|
||||
{p 4 8 2}{cmd:. `diff' `reps' `size' `dots' `match' `saving'}{p_end}
|
||||
|
||||
{hline}
|
||||
|
||||
{p 2 4 2}Authors: J. Scott Long & Jun Xu{p_end}
|
||||
{p 11 4 2}{browse www.indiana.edu/~jslsoc/spost.htm}{p_end}
|
||||
{p 11 4 2}spostsup@indiana.edu{p_end}
|
552
Modules/ado/plus/_/_pecidelta.ado
Normal file
552
Modules/ado/plus/_/_pecidelta.ado
Normal file
@ -0,0 +1,552 @@
|
||||
*! version 2.5.0 2009-10-28 jsl
|
||||
* - stata 11 update for returns from -mlogit-
|
||||
|
||||
// delta method for prvalue
|
||||
|
||||
capture program drop _pecidelta
|
||||
program define _pecidelta, rclass
|
||||
|
||||
version 8
|
||||
* _G==gradient matrix; _V=var/cov matrix; _up=upper bound; _lo=lower
|
||||
tempname x_base x_base1 x_baseneg xb b b_j b_nocuts designmat cur_eq_sav
|
||||
tempname pdf z G V temp yval eV v
|
||||
tempname mu mu_G mu_V mudif mudif_V
|
||||
tempname pr_cur pr_G pr_V pr_se pr_lo pr_up pr_sav
|
||||
tempname prdif prdif_se prdif_lo prdif_up prdif_V
|
||||
|
||||
/* Matrix definitions:
|
||||
|
||||
pr_cur : predicted probabilities
|
||||
pr_lo : lower bound for pred prob
|
||||
pr_up : upper bound for pred prob
|
||||
pr_G : gradient matrix for pred prob
|
||||
: [dp/db'*e(V)*dp/db]
|
||||
pr_V : variance (only) matrix for pred prob
|
||||
pr_se : standards error for pred prob
|
||||
|
||||
prdif : discrete change for pred prob
|
||||
prdif_lo : lower bound for discrete change
|
||||
prdif_up : upper bound for discrete change
|
||||
prdif_V : covariance matrix for discrete change
|
||||
: [d(p2-p1)/db'*e(V)*d(p2-p1)/db]
|
||||
prdif_se : standards error for discrete change
|
||||
|
||||
yval : y values
|
||||
|
||||
G = [dF(XB)/dB]'
|
||||
= [dF(XB)/dXB]*[dXB/dB]' = f(XB)*X
|
||||
V = Var(F(XB))
|
||||
= [dF(XB)/dB]' V(B) [dF(XB)/dB]
|
||||
*/
|
||||
|
||||
// DECODE SYNTAX
|
||||
|
||||
* syntax [, Save Diff] // 0.2.3
|
||||
* 0.2.5 - caller indicates version of stata calling _pecidelta;
|
||||
* needed since other commands change versions.
|
||||
|
||||
syntax [, Save Diff caller(real 5.0)]
|
||||
|
||||
// GET INFORMATION STORED IN GLOBALS
|
||||
|
||||
loc io $petype
|
||||
loc cmd : word 1 of $petype // model command
|
||||
loc input : word 2 of $petype // typical or twoeqn
|
||||
loc output : word 3 of $petype // output type
|
||||
|
||||
loc nrhs = peinfo[1,1] // # of rhs
|
||||
loc nrhsp1 = peinfo[1,1] + 1 // adding 1 for constant
|
||||
scalar `z' = peinfo[1,4]
|
||||
loc numcats = peinfo[1,2]
|
||||
loc numcatsm1 = peinfo[1,2] - 1
|
||||
loc max_count = `numcats' - 1 // count model max counts
|
||||
|
||||
* mat `b' = e(b) // 0.2.2
|
||||
* 0.2.5 get b and v for all estimation commands
|
||||
mat `b' = e(b)
|
||||
local nbeta = colsof(`b')
|
||||
mat `eV' = e(V)
|
||||
|
||||
* 0.2.6 get_mlogit_bv to get b and V under Stata 11
|
||||
if "`e(cmd)'"=="mlogit" { // if mlogit, special treatment
|
||||
nobreak {
|
||||
_get_mlogit_bvecv `b' `eV'
|
||||
}
|
||||
}
|
||||
|
||||
* base values
|
||||
mat `x_base'= pebase[1, 1...] // base values for predictions
|
||||
mat `x_base1' = `x_base', 1 // adding 1 to end for constant
|
||||
|
||||
// GET PREDICTED PROBABILITIES
|
||||
|
||||
if "`output'"=="count" | "`output'"=="binary" {
|
||||
forval i = 0/`numcats' {
|
||||
tempname p`i'
|
||||
scalar `p`i'' = pepred[2,`i'+1]
|
||||
}
|
||||
}
|
||||
else {
|
||||
forval i = 1/`numcats' {
|
||||
tempname p`i'
|
||||
scalar `p`i'' = pepred[2,`i']
|
||||
}
|
||||
}
|
||||
mat `pr_cur'= pepred[2,1...]'
|
||||
|
||||
// BINARY MODELS - predicted probability
|
||||
|
||||
if "`output'"=="binary" {
|
||||
|
||||
scalar `xb' = pepred[3,1]
|
||||
|
||||
// dF(XB)/dXB = pdf
|
||||
if "`e(cmd)'"=="cloglog" {
|
||||
scalar `pdf' = (`p1'-1) * exp(`xb')
|
||||
}
|
||||
if "`e(cmd)'"=="logit" | "`e(cmd)'"=="logistic" {
|
||||
scalar `pdf' = `p1' * (1-`p1')
|
||||
}
|
||||
if "`e(cmd)'"=="probit"{
|
||||
scalar `pdf' = normden(`xb')
|
||||
}
|
||||
|
||||
// dF(XB)/dB = f(XB)*X
|
||||
mat `G' = `pdf' * `x_base1'
|
||||
// noncon fix 2004-12-22
|
||||
mat `G' = `G'[1,1..`nbeta']
|
||||
mat `pr_G' = `G' \ `G'
|
||||
// Var(F(XB)) = dF(XB)/dB' V(B) dF(XB)/dB
|
||||
* 0.2.2 mat `V' = `G' * e(V) * `G''
|
||||
mat `V' = `G' * `eV' * `G'' // 0.2.5
|
||||
// Since V(p1)=V(p0):
|
||||
mat `pr_V' = `V'[1,1] \ `V'[1,1]
|
||||
}
|
||||
|
||||
// ORDERED MODELS
|
||||
|
||||
if "`output'"=="ordered" {
|
||||
|
||||
* Ordered logit/probit involves computing:
|
||||
*
|
||||
* Pr(y=m) = F(t_m - xb) - F(t_m-1 * xb)
|
||||
*
|
||||
* The e(b) matrix is a vector of b's associated with the
|
||||
* x's and then the tau's. To compute t_m - xb by vector
|
||||
* multiplicaton of e(b), we need to compute vectors like:
|
||||
*
|
||||
* x_tau1 = -x1 -x2 -x3 1 0 0 0
|
||||
* x_tau2 = -x1 -x2 -x3 0 1 0 0
|
||||
* x_tau3 = -x1 -x2 -x3 0 0 1 0
|
||||
* x_tau4 = -x1 -x2 -x3 0 0 0 1
|
||||
*
|
||||
* Then x_taum*e(b) = t_m - xb
|
||||
|
||||
tempname I tau_xb tau_xbm1 pdf1 pdflast
|
||||
mat `I' = I(`numcatsm1')
|
||||
|
||||
* loop through categories
|
||||
forval i = 1/`numcatsm1' {
|
||||
tempname x_tau`i'
|
||||
mat `x_tau`i'' = -(`x_base'), `I'[`i',1...]
|
||||
}
|
||||
|
||||
* get b's with tau's at end
|
||||
mat `b' = e(b)
|
||||
* compute tau_m - xb
|
||||
mat `tau_xb' = `x_tau1'*`b''
|
||||
scalar `tau_xb' = `tau_xb'[1,1]
|
||||
* for category 1: d cdf/d xb = pdf
|
||||
if "`e(cmd)'"=="ologit" {
|
||||
lgtpdf 0 `tau_xb'
|
||||
scalar `pdf1' = r(pdf)
|
||||
}
|
||||
if "`e(cmd)'"=="oprobit" {
|
||||
scalar `pdf1' = normden(`tau_xb')
|
||||
}
|
||||
* for the first outcome
|
||||
mat `G' = `pdf1' * `x_tau1'
|
||||
mat `pr_G' = `G'
|
||||
* 0.2.2 mat `V' = `G' * e(V) * (`G')'
|
||||
mat `V' = `G' * `eV' * (`G')' // 0.2.5
|
||||
mat `pr_V' = `V'[1,1]
|
||||
|
||||
* cateories 2 through next to last add to matrices for category 1
|
||||
forval i = 2/`numcatsm1' {
|
||||
|
||||
local im1 = `i' - 1 // prior cutpoint
|
||||
tempname pdf`i'
|
||||
mat `tau_xb' = `x_tau`i''*`b''
|
||||
scalar `tau_xb' = `tau_xb'[1,1]
|
||||
mat `tau_xbm1' = `x_tau`im1''*`b''
|
||||
scalar `tau_xbm1' = `tau_xbm1'[1,1]
|
||||
if "`e(cmd)'"=="ologit" {
|
||||
* cutpoint i
|
||||
lgtpdf 0 `tau_xb'
|
||||
scalar `pdf`i'' = r(pdf)
|
||||
* cutpoint i-1
|
||||
lgtpdf 0 `tau_xbm1'
|
||||
scalar `pdf`im1'' = r(pdf)
|
||||
}
|
||||
if "`e(cmd)'"=="oprobit" {
|
||||
scalar `pdf`i'' = normden(`tau_xb')
|
||||
scalar `pdf`im1'' = normden(`tau_xb')
|
||||
}
|
||||
|
||||
mat `G' = (`pdf`i'')*(`x_tau`i'') ///
|
||||
- (`pdf`im1'') * (`x_tau`im1'')
|
||||
mat `pr_G' = `pr_G' \ `G'
|
||||
* 0.2.2 mat `V' = `G' * e(V) * (`G')'
|
||||
mat `V' = `G' * `eV' * (`G')' // 0.2.5
|
||||
mat `pr_V' = `pr_V' \ `V'[1,1]
|
||||
|
||||
} // if given category
|
||||
|
||||
* last category
|
||||
local im1 = `numcats' - 1
|
||||
mat `tau_xb' = `x_tau`im1''*`b''
|
||||
scalar `tau_xb' = `tau_xb'[1,1]
|
||||
if "`e(cmd)'"=="ologit" {
|
||||
lgtpdf 0 `tau_xb'
|
||||
scalar `pdflast' = - r(pdf)
|
||||
}
|
||||
if "`e(cmd)'"=="oprobit" {
|
||||
scalar `pdflast' = -normden(`tau_xb')
|
||||
}
|
||||
mat `G' = `pdflast' * (`x_tau`im1'')
|
||||
mat `pr_G' = `pr_G' \ `G'
|
||||
* 0.2.2 mat `V' = `G' * e(V) * (`G')'
|
||||
mat `V' = `G' * `eV' * (`G')' // 0.2.5
|
||||
mat `pr_V' = `pr_V' \ `V'[1,1]
|
||||
|
||||
} // ordered
|
||||
|
||||
// GOLOGIT
|
||||
|
||||
if "`e(cmd)'"=="gologit" {
|
||||
|
||||
tempname nextrow pdfmat fmxb_mx
|
||||
|
||||
forval j = 1/`numcatsm1' {
|
||||
|
||||
* select betas for outcome j
|
||||
local start = (`j'-1) * `nrhsp1' + 1
|
||||
local end = `start' + `nrhs' // including constant
|
||||
mat `b_j' = `b'[1...,`start'..`end']
|
||||
|
||||
* pdf at -xb
|
||||
mat `xb' = `x_base1'*(`b_j')'
|
||||
lgtpdf 0 -`xb'[1,1]
|
||||
scalar `pdf' = r(pdf)
|
||||
|
||||
* collect column vector with pdfs for each outcome
|
||||
mat `pdfmat' = nullmat(`pdfmat') \ `pdf'
|
||||
}
|
||||
|
||||
* dF(-xb)/dB = dFxbdB contains vec of:
|
||||
*
|
||||
* -x1*f(xb1) ... -x1*f(xbJ-1)
|
||||
* :::
|
||||
* -xK*f(xb1) ... -xK*f(xbJ-1)
|
||||
* -1 *f(xb1) ... -1 *f(xbJ-1)
|
||||
|
||||
mat `fmxb_mx' = vec((-1 * `pdfmat' * `x_base1')')
|
||||
|
||||
* designmat: let J = (1 1 ... 1) for # rhs + 1
|
||||
* let M = -J
|
||||
* let 0 = 0 matrix of same size
|
||||
*
|
||||
* designmat = J 0 0 0 0 ... 0 0 ==> -xf(-xb_1)
|
||||
* M J 0 0 0 ... 0 0 ==> xf(-xb_1)-xf(-xb_2)
|
||||
* 0 M J 0 0 ... 0 0
|
||||
* : : : : :::::::::
|
||||
* 0 0 0 0 0 ... 0 M==> xf(-xb_ncats)-xf(-xb_2)
|
||||
|
||||
tempname J M
|
||||
mat `J' = J(1,`nrhsp1',1)
|
||||
mat `M' = J(1,`nrhsp1',-1)
|
||||
|
||||
* outcome 1
|
||||
mat `designmat' = J(1,`nrhsp1',1), J(1,`nbeta'-`nrhsp1',0)
|
||||
|
||||
* outcomes 2 - J-1
|
||||
forval i = 2/`numcatsm1' {
|
||||
mat `nextrow' = J(1,`nbeta',0)
|
||||
local startneg = (`i'-2) * `nrhsp1' + 1 // start -1
|
||||
local startpos = (`i'-1) * `nrhsp1' + 1 // start 1
|
||||
mat `nextrow'[1,`startneg'] = `M'
|
||||
mat `nextrow'[1,`startpos'] = `J'
|
||||
mat `designmat' = nullmat(`designmat') \ `nextrow'
|
||||
}
|
||||
|
||||
* outcome J
|
||||
mat `nextrow' = J(1,`nbeta'-`nrhsp1',0), J(1,`nrhsp1',-1)
|
||||
mat `designmat' = nullmat(`designmat') \ `nextrow'
|
||||
* compute gradient and variance
|
||||
mat `pr_G' = `designmat' * diag(`fmxb_mx')
|
||||
* 0.2.2 mat `pr_V' = ( vecdiag(`pr_G' * e(V) * (`pr_G')') )'
|
||||
mat `pr_V' = ( vecdiag(`pr_G' * `eV' * (`pr_G')') )' // 0.2.5
|
||||
}
|
||||
|
||||
// NOMINAL
|
||||
|
||||
if "`e(cmd)'"=="mlogit" {
|
||||
local nbeta = colsof(`b')
|
||||
local nrhspluscon = `nrhsp1' // if constant
|
||||
local iscon "1"
|
||||
if `nrhs'*`numcatsm1'==`nbeta' { // if true, noconstant
|
||||
local nrhspluscon = `nrhsp1' - 1 // if noconstant
|
||||
local iscon ""
|
||||
}
|
||||
|
||||
tempname pj_xk_mat pj_xk_vec
|
||||
forval i = 1/`numcats' {
|
||||
tempname p`i'fst p`i'snd Var`i'
|
||||
}
|
||||
|
||||
* X = base values with 1 for contant
|
||||
* 0 = 0 row vector of same size.
|
||||
* Create:
|
||||
* X 0 0 0 ...
|
||||
* 0 X 0 0 ...
|
||||
* 0 0 X 0 ...
|
||||
mat `designmat' = J(`numcatsm1',`nbeta',0)
|
||||
forval i = 1/`numcatsm1' {
|
||||
local st = (`i'-1) * `nrhspluscon'
|
||||
forval j = 1/`nrhspluscon' {
|
||||
local k = `st' + `j'
|
||||
mat `designmat'[`i',`k'] = `x_base`iscon''[1,`j']
|
||||
}
|
||||
}
|
||||
* vector p1x1 p1x2 p1x3...p2x1 p2x2 p2x3...p3x1 p3x2 p3x3...
|
||||
forval i = 1/`numcatsm1' {
|
||||
mat `pj_xk_vec' = nullmat(`pj_xk_vec'),(`x_base`iscon'' * `p`i'')
|
||||
}
|
||||
* compute the variances
|
||||
forval i = 1/`numcats' {
|
||||
if "`i'"<"`numcats'" {
|
||||
mat `G' = (`p`i'') * (`designmat'[`i',1..`nbeta']) ///
|
||||
- (`p`i'') * (`pj_xk_vec')
|
||||
mat `pr_G' = nullmat(`pr_G') \ `G'
|
||||
}
|
||||
if "`i'"=="`numcats'" {
|
||||
mat `G' = -(`p`i'') * (`pj_xk_vec')
|
||||
mat `pr_G' = `pr_G' \ `G'
|
||||
}
|
||||
|
||||
// noncon fix 2004-12-22
|
||||
mat `G' = `G'[1,1..`nbeta']
|
||||
* 0.2.2 mat `V' = `G' * e(V) * (`G')'
|
||||
mat `V' = `G' * `eV' * (`G')' // 0.2.5
|
||||
mat `pr_V' = nullmat(`pr_V') \ `V'[1,1]
|
||||
}
|
||||
}
|
||||
|
||||
// NBREG AND POISSON
|
||||
|
||||
if "`output'"=="count" & "`input'"=="typical" {
|
||||
|
||||
// POISSON
|
||||
|
||||
if "`e(cmd)'"=="poisson" {
|
||||
scalar `mu' = pepred[3,2]
|
||||
mat `mu_G' = `mu'*`x_base1' // d mu/dB
|
||||
mat `mu_G' = `mu_G'[1,1..`nbeta'] // noncon fix 2004-12-22
|
||||
forval i = 0/`max_count' {
|
||||
matrix `G' = `x_base1' * ///
|
||||
( `i' * `mu'^`i' - `mu'^(`i'+1) ) / ///
|
||||
( exp(lngamma(`i'+1)) * exp(`mu') )
|
||||
mat `G' = `G'[1,1..`nbeta'] // noncon fix 2004-12-22
|
||||
mat `pr_G' = nullmat(`pr_G') \ `G'
|
||||
* 0.2.2 mat `V' = `G' * e(V) * (`G')'
|
||||
mat `V' = `G' * `eV' * (`G')' // 0.2.5
|
||||
mat `pr_V' = nullmat(`pr_V') \ `V'[1,1]
|
||||
* 0.2.2 mat `eV' = e(V)
|
||||
}
|
||||
}
|
||||
|
||||
// NBREG
|
||||
|
||||
if "`e(cmd)'"=="nbreg" {
|
||||
|
||||
tempname alpha alphainv inv1alpmu
|
||||
tempname scorexb gradb scorelnalpha gradlnalpha
|
||||
scalar `mu' = pepred[3,2]
|
||||
mat `mu_G' = (`mu' * `x_base1'), 0
|
||||
|
||||
// noncon fix 2005-01-08
|
||||
* strip off the alpha
|
||||
local nbeta = `nbeta' - 1 // drop alpha
|
||||
* strip off the alpha
|
||||
mat `mu_G' = `mu_G'[1,1..`nbeta']
|
||||
|
||||
scalar `alpha' = e(alpha)
|
||||
scalar `inv1alpmu' = 1 / (1+`alpha'*`mu')
|
||||
scalar `alphainv' = 1 / `alpha'
|
||||
|
||||
forval y = 0/`max_count' { // loop through counts
|
||||
|
||||
scalar `scorexb' = `inv1alpmu' * (`y'-`mu')
|
||||
mat `gradb' = (`scorexb' * `x_base1') * `p`y''
|
||||
scalar `scorelnalpha' = `alphainv' * ///
|
||||
( digamma(`alphainv') ///
|
||||
- digamma(`y' + `alphainv') ///
|
||||
- ln(`inv1alpmu') ///
|
||||
) ///
|
||||
+ `inv1alpmu'*(`y'-`mu')
|
||||
scalar `gradlnalpha' = `scorelnalpha' * `p`y''
|
||||
mat `G' = `gradb', `gradlnalpha'
|
||||
mat `G' = `G'[1,1..`nbeta'] // noncon fix 2004-12-22
|
||||
mat `pr_G' = nullmat(`pr_G') \ `G'
|
||||
|
||||
// noncon fix 2005-01-08
|
||||
*drop mat `V' = `G' * e(V) * (`G')'
|
||||
* 0.2.2 mat `eV' = e(V)
|
||||
mat `eV' = `eV'[1..`nbeta',1..`nbeta']
|
||||
mat `V' = `G' * `eV' * (`G')'
|
||||
mat `pr_V' = nullmat(`pr_V') \ `V'[1,1]
|
||||
|
||||
} // loop through counts
|
||||
|
||||
}
|
||||
|
||||
// BOTH MODELS
|
||||
|
||||
// noncon fix 2005-01-08
|
||||
mat `mu_V' = `mu_G' * `eV' * (`mu_G')' // variance for expected rate
|
||||
|
||||
} // negbin and poisson
|
||||
|
||||
// GET CATEGORY VALUES FOR PROBABILITIES - used to label matrices
|
||||
|
||||
forval i = 1/`numcats' {
|
||||
local category = pepred[1,`i'] // value of category
|
||||
* used for column names later
|
||||
local catnames "`catnames' Cat=`category'"
|
||||
}
|
||||
|
||||
// VARIANCE FOR DISCRETE CHANGE
|
||||
|
||||
if "`diff'"=="diff" {
|
||||
|
||||
/* 0.2.2
|
||||
if "`e(cmd)'"!="nbreg" {
|
||||
mat `eV' = e(V)
|
||||
} */
|
||||
|
||||
mat def `pr_sav' = pepred[4,1...]'
|
||||
mat `prdif' = `pr_cur' - `pr_sav'
|
||||
|
||||
* is there no change in x? 0 if no difference.
|
||||
scalar `cur_eq_sav' = mreldif(`pr_sav',`pr_cur')
|
||||
|
||||
* variance for change in prob
|
||||
mat `prdif_V' = pegrad_pr * `eV' * pegrad_pr' ///
|
||||
+ `pr_G' * `eV' * (`pr_G')' ///
|
||||
- pegrad_pr * `eV' * (`pr_G')' ///
|
||||
- `pr_G' * `eV' * pegrad_pr'
|
||||
mat `prdif_V' = (vecdiag(`prdif_V'))'
|
||||
|
||||
if "`e(cmd)'"=="poisson" | "`e(cmd)'"=="nbreg" {
|
||||
mat `mudif' = `mu' - pepred[5,2] // minus save mu
|
||||
* variance for change in mu
|
||||
mat `mudif_V' = pegrad_mu * `eV' * pegrad_mu' ///
|
||||
+ `mu_G' * `eV' * (`mu_G')' ///
|
||||
- pegrad_mu * `eV' * (`mu_G')' ///
|
||||
- `mu_G' * `eV' * pegrad_mu'
|
||||
}
|
||||
}
|
||||
|
||||
// COMPUTE UPPER AND LOWER BOUNDS
|
||||
|
||||
mat rownames `pr_V' = `catnames'
|
||||
mat rownames `pr_G' = `catnames'
|
||||
|
||||
* std errors = square root of variances
|
||||
mat `pr_se' = vecdiag(cholesky(diag(`pr_V')))'
|
||||
|
||||
* 2008-07-09
|
||||
matrix pedifsep = `pr_se'
|
||||
|
||||
if "`diff'"=="diff" {
|
||||
|
||||
if `cur_eq_sav'==0 { // no change from x_dif==0
|
||||
* se's are 0
|
||||
mat `prdif_se' = J(rowsof(`prdif_V'),1,0)
|
||||
}
|
||||
else {
|
||||
mat `prdif_se' = vecdiag(cholesky(diag(`prdif_V')))'
|
||||
}
|
||||
|
||||
* 2008-07-09
|
||||
matrix pedifsep = `prdif_se'
|
||||
|
||||
}
|
||||
|
||||
* construct bounds for pred prob
|
||||
mat `pr_lo' = `pr_cur' - `z'*`pr_se'
|
||||
mat `pr_up' = `pr_cur' + `z'*`pr_se'
|
||||
mat peupper[2,1] = (`pr_up')'
|
||||
mat pelower[2,1] = (`pr_lo')'
|
||||
|
||||
if "`e(cmd)'"=="poisson" | "`e(cmd)'"=="nbreg" {
|
||||
* 2008-07-09
|
||||
global pesemu = sqrt(`mu_V'[1,1])
|
||||
mat peupper[3,2] = `mu' + `z'*sqrt(`mu_V'[1,1])
|
||||
mat pelower[3,2] = `mu' - `z'*sqrt(`mu_V'[1,1])
|
||||
}
|
||||
|
||||
// CREATE BOUNDS FOR DISCRETE CHANGE
|
||||
|
||||
if "`diff'"=="diff" {
|
||||
mat `prdif_lo' = `prdif' - `z'*`prdif_se'
|
||||
mat `prdif_up' = `prdif' + `z'*`prdif_se'
|
||||
mat peupper[6,1] = (`prdif_up')'
|
||||
mat pelower[6,1] = (`prdif_lo')'
|
||||
if "`e(cmd)'"=="poisson" | "`e(cmd)'"=="nbreg" {
|
||||
* 2008-07-09
|
||||
global pedifsemu = sqrt(`mudif_V'[1,1])
|
||||
mat peupper[7,2] = (`mudif' + `z'*sqrt(`mudif_V'[1,1]))
|
||||
mat pelower[7,2] = (`mudif' - `z'*sqrt(`mudif_V'[1,1]))
|
||||
}
|
||||
}
|
||||
|
||||
// SAVE GRADIENTS TO COMPUTE CI FOR DCs
|
||||
|
||||
if "`save'"=="save" {
|
||||
mat pegrad_pr = `pr_G'
|
||||
if "`e(cmd)'"=="poisson" | "`e(cmd)'"=="nbreg" {
|
||||
mat pegrad_mu = `mu_G'
|
||||
}
|
||||
}
|
||||
|
||||
end
|
||||
|
||||
capture program drop lgtpdf
|
||||
// lgtpdf 0 0
|
||||
// scalar a = r(pdf) // pdf of logit
|
||||
// scalar b = r(cdf) // cdf of logit
|
||||
// di "pdf: " r(pdf)
|
||||
// di "cdf: " r(cdf)
|
||||
|
||||
program define lgtpdf, rclass
|
||||
version 8
|
||||
tempname expab pdf cdf
|
||||
args a bx
|
||||
scalar `expab' = exp(`a' + `bx')
|
||||
scalar `pdf' = `expab' / (1 + `expab')^2
|
||||
scalar `cdf' = `expab' / (1 + `expab')
|
||||
return scalar pdf = `pdf'
|
||||
return scalar cdf = `cdf'
|
||||
end
|
||||
|
||||
exit
|
||||
|
||||
version 0.2.0 2005-02-03
|
||||
version 0.2.1 13Apr2005
|
||||
version 0.2.2 2008-07-09
|
||||
- return global pedifsep
|
||||
version 0.2.5 2009-09-18
|
||||
- update for stata 11 - drop for _get_mlogit_bv.ado
|
||||
version 0.2.6 2009-10-21
|
||||
- use _get_mlogit_bv.ado
|
67
Modules/ado/plus/_/_pecidelta.hlp
Normal file
67
Modules/ado/plus/_/_pecidelta.hlp
Normal file
@ -0,0 +1,67 @@
|
||||
{smcl}
|
||||
{* 2005-02-06}{...}
|
||||
{hline}
|
||||
help for {hi:_pecidelta}{right:2/6/2005}
|
||||
{hline}
|
||||
|
||||
{title: Utility to implement the delta method for calculating confidence intervals}
|
||||
|
||||
{p 8 15 2}{cmd:_pecidelta} [{cmd:,}
|
||||
{cmdab:s:ave}
|
||||
{cmdab:d:iff}]
|
||||
|
||||
{title: Description}
|
||||
|
||||
{p 4 4 2}
|
||||
{cmd:_pecidelta} is a utility command that calculates confidence
|
||||
intervals for prediction and discrete changes by {search delta}
|
||||
method using analytical derivatives. It applies to {help logit},
|
||||
{help probit}, {help cloglog}, {help ologit}, {help oprobit},
|
||||
{help gologit}, {help mlogit}, {help poisson}, and {help nbreg}.
|
||||
|
||||
{title: Options}
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd:save} saves current values of indepenent variables and predictions
|
||||
for computing changes using the diff option.
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd:diff} computes difference between current predictions and those that
|
||||
were saved.
|
||||
|
||||
{title: Global Matrices}
|
||||
|
||||
{p 4 4 2}
|
||||
The second row of the following matrices contains predicted probabilities and
|
||||
the sixth row contains differences for predicted probabilities.
|
||||
|
||||
{p 4 8 2}
|
||||
peupper: _pecidelta calculates confidence intervals for prediction
|
||||
and place upper end into the global matrix peupper.
|
||||
|
||||
{p 4 8 2}
|
||||
pelower: _pecidelta calculates confidence intervals for prediction
|
||||
and place lower end into the global matrix pelower.
|
||||
|
||||
{title: Examples}
|
||||
|
||||
{p 4 8 2}{cmd:._pebase `if' `in' , `x' `rest' `choices' `all'}{p_end}
|
||||
{p 4 8 2}{cmd:.mat `tobase' = r(pebase)}{p_end}
|
||||
{p 4 8 2}{cmd:._pepred, `level' `maxcnt'}{p_end}
|
||||
{p 4 8 2}{cmd:.local maxc = r(maxcount)}{p_end}
|
||||
{p 4 8 2}{cmd:.local lvl = r(level)}{p_end}
|
||||
{p 4 8 2}{cmd:.capture _return drop pepred}{p_end}
|
||||
{p 4 8 2}{cmd:._return hold pepred}{p_end}
|
||||
{p 4 8 2}{cmd:._return restore pepred, hold}{p_end}
|
||||
{p 4 8 2}{cmd:...}{p_end}
|
||||
{p 4 8 2}{cmd:...}{p_end}
|
||||
{p 4 8 2}{cmd:...}{p_end}
|
||||
{p 4 8 2}{cmd:._pecollect, inout("`io'") level(`lvl') /// }{p_end}
|
||||
{p 4 8 2}{cmd:. maxcount(`maxc') `diff' `reps'}{p_end}
|
||||
{p 4 8 2}{cmd:._pecidelta, `save' `diff'}{p_end}
|
||||
|
||||
{hline}
|
||||
|
||||
{p 2 4 2}Authors: J. Scott Long & Jun Xu{p_end}
|
||||
{p 11 4 2}{browse www.indiana.edu/~jslsoc/spost.htm}{p_end}
|
||||
{p 11 4 2}spostsup@indiana.edu{p_end}
|
70
Modules/ado/plus/_/_peciml.ado
Normal file
70
Modules/ado/plus/_/_peciml.ado
Normal file
@ -0,0 +1,70 @@
|
||||
*! version 0.2.2 2008-07-09
|
||||
* - global pedifsey std error of dif in y
|
||||
|
||||
// ci for difference in y or y* based on ML Wald computation
|
||||
|
||||
capture program drop _peciml
|
||||
program define _peciml, rclass
|
||||
|
||||
version 8
|
||||
tempname xb_dif xb_dif_var xb_dif_sd xb_dif_lo xb_dif_up
|
||||
tempname x_cur x_sav x_dif z b b_var
|
||||
|
||||
// retrieve global data
|
||||
|
||||
local nocon = peinfo[1,6] // 1 if no constant
|
||||
local output : word 3 of $petype // what is the output type?
|
||||
scalar `z' = peinfo[1,4] // width of confidence interval
|
||||
local nrhs = peinfo[1,1] // # of RHS variables
|
||||
sca `xb_dif'= pepred[7,1] // difference in xb from saved to current
|
||||
matrix `x_cur' = pebase[1,1...] // current base values
|
||||
matrix `x_sav' = pebase[2,1...] // saved base values
|
||||
mat `b_var' = e(V)
|
||||
mat `b' = e(b)
|
||||
|
||||
// select and augment matrices to match model type
|
||||
|
||||
* ologit, oprobit // nocon option not allowed
|
||||
if "`output'"=="ordered" {
|
||||
mat `b_var' = `b_var'[1..`nrhs',1..`nrhs']
|
||||
}
|
||||
|
||||
* tobit, cnreg, intreg
|
||||
if "`output'"=="tobit" {
|
||||
if `nocon' != 1 {
|
||||
mat `x_cur' = `x_cur', 1, 0
|
||||
mat `x_sav' = `x_sav', 1, 0
|
||||
}
|
||||
else {
|
||||
mat `x_cur' = `x_cur', 0
|
||||
mat `x_sav' = `x_sav', 0
|
||||
}
|
||||
}
|
||||
|
||||
* regress, fit, logit, probit
|
||||
if "`output'"=="regress" | "`output'"=="binary" {
|
||||
if `nocon' != 1 {
|
||||
mat `x_cur' = `x_cur', 1
|
||||
mat `x_sav' = `x_sav', 1
|
||||
}
|
||||
}
|
||||
|
||||
mat `x_dif' = `x_cur' - `x_sav'
|
||||
* variance of difference
|
||||
mat `xb_dif_var' = (`x_dif')*`b_var'*(`x_dif')'
|
||||
sca `xb_dif_sd' = sqrt(`xb_dif_var'[1,1])
|
||||
|
||||
* 2008-07-09
|
||||
global pedifsey = `xb_dif_sd'
|
||||
|
||||
* compute and store upper and lower limits
|
||||
sca `xb_dif_lo' = `xb_dif' - `z'*`xb_dif_sd'
|
||||
sca `xb_dif_up' = `xb_dif' + `z'*`xb_dif_sd'
|
||||
|
||||
mat peupper[7,1] = `xb_dif_up'
|
||||
mat pelower[7,1] = `xb_dif_lo'
|
||||
|
||||
end
|
||||
|
||||
* version 0.2.1 13Apr2005
|
||||
* version 0.2.0 2005-02-03
|
77
Modules/ado/plus/_/_pecmdcheck.ado
Normal file
77
Modules/ado/plus/_/_pecmdcheck.ado
Normal file
@ -0,0 +1,77 @@
|
||||
*! version 0.2.3 13Apr2005
|
||||
* version 0.2.2 03Mar2005 cloglog
|
||||
* version 0.2.1 19Feb2005 zt
|
||||
* version 0.2.0 03Feb2005
|
||||
|
||||
// simply check if last command was valid for spost
|
||||
|
||||
capture program drop _pecmdcheck
|
||||
program define _pecmdcheck, rclass
|
||||
version 8
|
||||
args spostcmd
|
||||
local io ""
|
||||
* 03Mar2005 zt models added
|
||||
if "`e(cmd)'"=="ztp" {
|
||||
local io "typical count"
|
||||
}
|
||||
if "`e(cmd)'"=="ztnb" {
|
||||
local io "typical count"
|
||||
}
|
||||
if "`e(cmd)'"=="cloglog" {
|
||||
local io "typical binary"
|
||||
}
|
||||
if "`e(cmd)'"=="cnreg" {
|
||||
local io "typical tobit"
|
||||
}
|
||||
if "`e(cmd)'"=="fit" {
|
||||
local io "typical regress"
|
||||
}
|
||||
if "`e(cmd)'"=="gologit" {
|
||||
local io "typical ordered"
|
||||
}
|
||||
if "`e(cmd)'"=="intreg" {
|
||||
local io "typical tobit"
|
||||
}
|
||||
if "`e(cmd)'"=="logistic" {
|
||||
local io "typical binary"
|
||||
}
|
||||
if "`e(cmd)'"=="logit" {
|
||||
local io "typical binary"
|
||||
}
|
||||
if "`e(cmd)'"=="mlogit" {
|
||||
local io "typical nominal"
|
||||
}
|
||||
if "`e(cmd)'"=="nbreg" {
|
||||
local io "typical count"
|
||||
}
|
||||
if "`e(cmd)'"=="ologit" {
|
||||
local io "typical ordered"
|
||||
}
|
||||
if "`e(cmd)'"=="oprobit" {
|
||||
local io "typical ordered"
|
||||
}
|
||||
if "`e(cmd)'"=="poisson" {
|
||||
local io "typical count"
|
||||
}
|
||||
if "`e(cmd)'"=="probit" {
|
||||
local io "typical binary"
|
||||
}
|
||||
if "`e(cmd)'"=="regress" {
|
||||
local io "typical regress"
|
||||
}
|
||||
if "`e(cmd)'"=="tobit" {
|
||||
local io "typical tobit"
|
||||
}
|
||||
if "`e(cmd)'"=="zinb" {
|
||||
local io "twoeq count"
|
||||
}
|
||||
if "`e(cmd)'"=="zip" {
|
||||
local io "twoeq count"
|
||||
}
|
||||
if "`io'"=="" {
|
||||
di as error _new ///
|
||||
"`spostcmd' does not work for the last type of model estimated."
|
||||
}
|
||||
return local io = "`io'"
|
||||
|
||||
end
|
349
Modules/ado/plus/_/_pecollect.ado
Normal file
349
Modules/ado/plus/_/_pecollect.ado
Normal file
@ -0,0 +1,349 @@
|
||||
*! version 2.0.1 2008-07-09 jsl
|
||||
* - peinfo[3,8] to missing since it isn't the sd of the difference
|
||||
|
||||
// Utility to collect information from the current model and
|
||||
// saves it to global matrices for use by other programs (e.g.,
|
||||
// for simulations or constructing plots unavailable with prgen).
|
||||
// For details on these matrices, -help prvalue_collect-
|
||||
|
||||
capture program drop _pecollect
|
||||
program define _pecollect, rclass
|
||||
version 8
|
||||
tempname temp values mu xb prall0 nrhs2 muC ey mucount
|
||||
tempname dif difp difm r1 r2 r3
|
||||
|
||||
syntax , level(real) inout(string) maxcount(string) [Diff reps(int 1000)]
|
||||
|
||||
// get information about current model
|
||||
|
||||
* is it zero truncated?
|
||||
local iszt = 0
|
||||
if ("`e(cmd)'"=="ztp" | "`e(cmd)'"=="ztnb") local iszt = 1
|
||||
|
||||
* type of model
|
||||
global petype "`e(cmd)' `inout'"
|
||||
local input : word 2 of $petype // is it a typical or twoeq model?
|
||||
local output : word 3 of $petype // what is the output type?
|
||||
|
||||
local level = r(level) // CI level for current model
|
||||
|
||||
* nrhs: # of rhs variables
|
||||
local colnms: colnames(PE_in)
|
||||
local nrhs: word count `colnms'
|
||||
local nrhs = `nrhs' // no _cons included
|
||||
|
||||
* ncat: number of outcome categories
|
||||
if "`output'"=="count" {
|
||||
local ncat = `maxcount'+1
|
||||
}
|
||||
else if "`output'"=="regress" | "`output'"=="tobit" {
|
||||
local ncat = 1
|
||||
}
|
||||
else {
|
||||
_pecats
|
||||
local catvals = r(catvals)
|
||||
local ncat = r(numcats)
|
||||
_return restore pepred, hold // restore returns from _pepred
|
||||
}
|
||||
|
||||
* nrhs2: # of rhs if zip or zinb
|
||||
local nrhs2 = .
|
||||
if "`input'"=="twoeq" {
|
||||
local colnms2: colnames(PE_in2)
|
||||
local nrhs2: word count `colnms2'
|
||||
local nrhs2 = `nrhs2' // no _cons included
|
||||
}
|
||||
|
||||
* basecat: mlogit base category
|
||||
local basecat = .
|
||||
* if "`e(cmd)'"=="mlogit" { local basecat = e(basecat) }
|
||||
if "`e(cmd)'"=="mlogit" {
|
||||
local basecat = e(i_base)
|
||||
}
|
||||
|
||||
// peinfo - global matrix with numeric information about:
|
||||
//
|
||||
// Row 1: the current model
|
||||
// Row 2: the saved model used to compute the difference
|
||||
// Row 3: Row 1 - Row 2
|
||||
|
||||
mat def peinfo = J(3,12,.)
|
||||
matrix peinfo[1,1] = `nrhs' // nrhs - columns for pebase
|
||||
matrix peinfo[1,2] = `ncat' // numcats from _pecats
|
||||
matrix peinfo[1,3] = `level'*100 // ci level as 95 not .95
|
||||
matrix peinfo[1,4] = -invnorm((1-`level')/2) // z @ level for ci
|
||||
matrix peinfo[1,5] = `nrhs2' // nrhs2
|
||||
matrix peinfo[1,6] = . // nocon
|
||||
matrix peinfo[1,7] = `basecat' // base category for mlogit
|
||||
matrix peinfo[1,8] = . // stdp for binary model
|
||||
matrix peinfo[1,9] = `reps' // requested # of replications for bootstrap
|
||||
matrix peinfo[1,10] = . // completed # of replications for bootstrap
|
||||
// this will be added after _peciboot is called
|
||||
matrix peinfo[1,11] = `maxcount'
|
||||
|
||||
* if diff, add saved and compute current-saved
|
||||
if "`diff'" == "diff" {
|
||||
|
||||
mat `r1' = peinfo[1,1...] // current
|
||||
mat `r2' = PRVinfo // saved
|
||||
mat `dif' = `r1' - `r2'
|
||||
mat peinfo = `r1' \ `r2' \ `dif'
|
||||
|
||||
* 2008-07-09
|
||||
mat peinfo[3,8] = . // since this is not a valid stdp
|
||||
|
||||
} // "`diff'" == "diff"
|
||||
|
||||
// pebase and pebase2 - global matrices with base values
|
||||
|
||||
* start with current base and two blank rows
|
||||
mat pebase = PE_in \ J(1,`nrhs',.) \ J(1,`nrhs',.)
|
||||
if "`input'" == "twoeq" {
|
||||
mat pebase2 = PE_in2 \ J(1,`nrhs2',.) \ J(1,`nrhs2',.)
|
||||
matrix rownames pebase2 = Current Saved Cur-Saved
|
||||
}
|
||||
|
||||
* if diff, get previous base values
|
||||
if "`diff'" == "diff" {
|
||||
mat `dif' = pebase[1,1...] - PRVbase
|
||||
mat pebase = PE_in \ PRVbase \ `dif'
|
||||
if "`input'" == "twoeq" {
|
||||
mat `dif' = pebase2[1,1...] - PRVbase2
|
||||
mat pebase2 = PE_in2 \ PRVbase2 \ `dif'
|
||||
matrix rownames pebase2 = Current Saved Cur-Saved
|
||||
}
|
||||
}
|
||||
|
||||
// gather information about current model
|
||||
|
||||
* missing by default
|
||||
scalar `xb' = .
|
||||
scalar `mu' = . // rate in count; e(y) in zip/zinb
|
||||
scalar `mucount' = . // mu in count portion of zip/zinb
|
||||
scalar `prall0' = .
|
||||
|
||||
* info on mu for count models
|
||||
if "`output'"=="count" {
|
||||
mat `temp' = r(mu)
|
||||
scalar `mu' = `temp'[1,1]
|
||||
}
|
||||
if "`e(cmd)'"=="zip" | "`e(cmd)'"=="zinb" {
|
||||
mat `temp' = r(mucount) // grab rate from count portion of model
|
||||
scalar `mucount' = `temp'[1,1]
|
||||
}
|
||||
if `iszt' {
|
||||
mat `temp' = r(xb)
|
||||
scalar `xb' = `temp'[1,1]
|
||||
mat `temp' = r(muC) // E(y|y<0)
|
||||
scalar `muC' = `temp'[1,1]
|
||||
}
|
||||
if "`e(cmd)'"=="poisson" ///
|
||||
| "`e(cmd)'"=="nbreg" {
|
||||
mat `temp' = r(xb)
|
||||
scalar `xb' = `temp'[1,1]
|
||||
}
|
||||
if "`input'"=="twoeq" { // zip and zinb
|
||||
scalar `xb' = log(`mu')
|
||||
mat `temp' = r(always0)
|
||||
scalar `prall0' = `temp'[1,1]
|
||||
}
|
||||
|
||||
if "`output'"=="binary" ///
|
||||
| "`output'"=="regress" ///
|
||||
| "`output'"=="tobit" ///
|
||||
| "`output'"=="ordered" {
|
||||
mat `temp' = r(xb)
|
||||
scalar `xb' = `temp'[1,1]
|
||||
}
|
||||
|
||||
// start with empty pepred & peCpred matrices
|
||||
|
||||
mat def pepred = J(7,`ncat',.)
|
||||
matrix rownames pepred = ///
|
||||
1values 2prob 3misc 4sav_prob 5sav_misc 6dif_prob 7dif_misc
|
||||
mat def peupper = pepred // holds upper ci
|
||||
mat def pelower = pepred // holds lower ci
|
||||
local method = word("$pecimethod",1)
|
||||
if "`method'"=="bootstrap" { // bootstrap computes 3 types of CIs
|
||||
mat def peupnorm = pepred // holds upper by normal appox
|
||||
mat def pelonorm = pepred // holds lower by normal appox
|
||||
mat def peupbias = pepred // holds upper with bias adjustment
|
||||
mat def pelobias = pepred // holds lower with bias adjustment
|
||||
}
|
||||
if `iszt' {
|
||||
mat def peCpred = pepred // for conditional results
|
||||
}
|
||||
|
||||
// pepred & peinfo: add info about current model
|
||||
|
||||
if "`output'"=="binary" {
|
||||
|
||||
mat pepred[1,1] = 0 // outcome values
|
||||
mat pepred[1,2] = 1
|
||||
mat `temp' = r(p0) // predictions
|
||||
mat pepred[2,1] = `temp'[1,1]
|
||||
mat `temp' = r(p1)
|
||||
mat pepred[2,2] = `temp'[1,1]
|
||||
mat pepred[3,1] = `xb'
|
||||
mat `temp' = r(stdp)
|
||||
mat peinfo[1,8] = `temp'[1,1]
|
||||
mat `temp' = r(p0_lo) // upper and lower limits
|
||||
* due to error in _pepred, r(p0_lo) is really upper limit
|
||||
mat peupper[2,1] = `temp'[1,1]
|
||||
mat `temp' = r(p0_hi)
|
||||
mat pelower[2,1] = `temp'[1,1]
|
||||
mat `temp' = r(p1_hi)
|
||||
mat peupper[2,2] = `temp'[1,1]
|
||||
mat `temp' = r(p1_lo)
|
||||
mat pelower[2,2] = `temp'[1,1]
|
||||
mat `temp' = r(xb_hi)
|
||||
mat peupper[3,1] = `temp'[1,1]
|
||||
mat `temp' = r(xb_lo)
|
||||
mat pelower[3,1] = `temp'[1,1]
|
||||
}
|
||||
|
||||
if "`output'"=="tobit" ///
|
||||
| "`output'"=="regress" {
|
||||
|
||||
mat pepred[1,1] = . // value
|
||||
mat pepred[2,1] = . // predicted probability
|
||||
mat pepred[3,1] = `xb'
|
||||
mat `temp' = r(xb_lo)
|
||||
mat pelower[3,1] = `temp'[1,1]
|
||||
mat `temp' = r(xb_hi)
|
||||
mat peupper[3,1] = `temp'[1,1]
|
||||
mat `temp' = r(stdp)
|
||||
mat peinfo[1,8] = `temp'[1,1]
|
||||
}
|
||||
|
||||
if "`output'"=="ordered" { // also works for mlogit
|
||||
|
||||
mat `temp' = r(stdp)
|
||||
mat peinfo[1,8] = `temp'[1,1]
|
||||
mat `temp' = r(xb_hi)
|
||||
mat peupper[3,1] = `temp'[1,1]
|
||||
mat `temp' = r(xb_lo)
|
||||
mat pelower[3,1] = `temp'[1,1]
|
||||
forval i=1/`ncat' {
|
||||
local v : word `i' of `catvals'
|
||||
mat pepred[1,`i'] = `v'
|
||||
mat `temp' = r(p`i')
|
||||
mat pepred[2,`i'] = `temp'[1,1]
|
||||
}
|
||||
mat pepred[3,1] = `xb'
|
||||
}
|
||||
|
||||
if "`e(cmd)'"=="gologit" | "`e(cmd)'"=="mlogit" ///
|
||||
| "`e(cmd)'"=="mprobit" | "`e(cmd)'"== "slogit" {
|
||||
forval i=1/`ncat' {
|
||||
local v : word `i' of `catvals'
|
||||
mat pepred[1,`i'] = `v'
|
||||
mat `temp' = r(p`i')
|
||||
mat pepred[2,`i'] = `temp'[1,1]
|
||||
mat pepred[2,`i'] = `temp'[1,1]
|
||||
if `i' != `ncat' {
|
||||
mat `temp' = r(xb`i')
|
||||
mat pepred[3,`i'] = `temp'[1,1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if "`e(cmd)'"=="poisson" | "`e(cmd)'"=="nbreg" ///
|
||||
| "`e(cmd)'"=="zip" | "`e(cmd)'"=="zinb" ///
|
||||
| `iszt' {
|
||||
forval i=1/`ncat' { // add labels to headers
|
||||
local im1 = `i' - 1
|
||||
mat pepred[1,`i'] = `im1' // numbers 0 to ncat-1
|
||||
mat `temp'=r(p`im1')
|
||||
mat pepred[2,`i'] = `temp'[1,1]
|
||||
if `iszt' { // if zt model, get pr(y|y>0)
|
||||
mat peCpred[1,`i'] = `im1' // numbers 0 to ncat-1
|
||||
mat `temp'=r(Cp`im1')
|
||||
mat peCpred[2,`i'] = `temp'[1,1]
|
||||
}
|
||||
}
|
||||
mat pepred[3,1] = `xb'
|
||||
mat pepred[3,2] = `mu' // overall rate E(y)
|
||||
mat pepred[3,3] = `mucount' // mu for count model E(y|~always0)
|
||||
mat `temp' = r(stdp)
|
||||
mat peinfo[1,8] = `temp'[1,1]
|
||||
mat `temp' = r(mu_hi)
|
||||
mat peupper[3,1] = `temp'[1,1]
|
||||
mat `temp' = r(mu_lo)
|
||||
mat pelower[3,1] = `temp'[1,1]
|
||||
if "`input'"=="twoeq" {
|
||||
mat pepred[3,4] = `prall0'
|
||||
}
|
||||
if `iszt' { // zt models
|
||||
mat `temp' = r(Cmu) // conditional mu
|
||||
mat peCpred[3,2] = `temp'[1,1]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
* Information on current model is now located in pepred, peCpred,
|
||||
* peinfo, peupper, pelower.
|
||||
|
||||
// if -diff-, add saved and difference to output matrix
|
||||
|
||||
if "`diff'" == "diff" {
|
||||
|
||||
* peinfo
|
||||
mat `dif' = peinfo[1,1...] - PRVinfo
|
||||
mat peinfo = peinfo[1,1...] \ PRVinfo \ `dif'
|
||||
|
||||
* pepred: row 1-values; 2-prob; 3-misc
|
||||
mat `difp' = pepred[2,1...] - PRVprob // dif in prob
|
||||
mat `difm' = pepred[3,1...] - PRVmisc // dif in other stats
|
||||
* Current SavedMode Difference
|
||||
mat pepred = pepred[1..3,1...] \ PRVprob \ PRVmisc \ `difp' \ `difm'
|
||||
|
||||
if `iszt' { // if zero trucated, also fix conditional matrix
|
||||
mat `difp' = pepred[2,1...] - PRVCprob
|
||||
mat `difm' = pepred[3,1...] - PRVCmisc
|
||||
mat peCpred = ///
|
||||
pepred[1..3,1...] \ PRVCprob \ PRVCmisc \ `difp' \ `difm'
|
||||
}
|
||||
|
||||
} // end if diff
|
||||
|
||||
|
||||
// ADD CATEGORY NUMBERS TO FIRST ROW; ADD ROW & COL NAMES
|
||||
|
||||
mat `r1' = pepred[1,1...]
|
||||
mat `r2' = peupper[2...,1...]
|
||||
mat `r3' = pelower[2...,1...]
|
||||
mat peupper = `r1' \ `r2'
|
||||
mat pelower = `r1' \ `r3'
|
||||
matrix rownames peupper = ///
|
||||
1values 2up_pr 3up_misc 4up_sav_pr ///
|
||||
5up_sav_misc 6up_dif_pr 7up_dif_misc
|
||||
matrix rownames pelower = ///
|
||||
1values 2lo_pr 3lo_misc 4lo_sav_pr ///
|
||||
5lo_sav_misc 6lo_dif_pr 7lo_dif_misc
|
||||
matrix rownames peinfo = Current Saved Cur-Saved
|
||||
matrix colnames peinfo = 1nrhs 2numcats 3level 4z_level ///
|
||||
5nrhs2 6nocon 7basecat 8stdp 9reps 10repsdone 11maxcount ///
|
||||
12blank
|
||||
matrix rownames pebase = Current Saved Cur-Saved
|
||||
|
||||
// INFORMATION ON WHETHER CONSTANT IS IN MODEL
|
||||
|
||||
_penocon
|
||||
local temp = r(nocon)
|
||||
matrix peinfo[1,6] = `temp'
|
||||
|
||||
end
|
||||
|
||||
exit
|
||||
|
||||
version 1.0.0 15Apr2005 fix rate used for zip/zinb (see notes at end)
|
||||
|
||||
15Apr2005 - correct error for zip and zinb (see changes in _pepred, _pecollect, _peciboot
|
||||
E(y) was used incorrectly rather than E(y|~always0).
|
||||
_pepred[3|5|7, 2] used to be mu defined as rate in count portion of model E(y|not always 0)
|
||||
_pepred[3|5|7, 2] now is the overall rate E(y); listed as simply mu.
|
||||
_pepred[3|5|7, 3] rate in count portion of model E(y|not always 0); listed as mucount.
|
||||
To simplify changes in _peciboot, E(y) is referred to as mu; E(y|~always0) is mucount.
|
||||
|
||||
version 2.0.0 2007-03-04 jsl - revised for prvalue repeated dif calls
|
163
Modules/ado/plus/_/_pecollect.hlp
Normal file
163
Modules/ado/plus/_/_pecollect.hlp
Normal file
@ -0,0 +1,163 @@
|
||||
{smcl}
|
||||
{* 2005-02-06}{...}
|
||||
{hline}
|
||||
help for {hi:_pecollect}{right:2/6/2005}
|
||||
{hline}
|
||||
|
||||
{title: Utility program to collect information used by prvalue2}
|
||||
|
||||
{p 8 15 2}{cmd:_pecollect}
|
||||
{cmd:level(}{it:real}{cmd:)}
|
||||
{cmd:inout(}{it:string}{cmd:)}
|
||||
{cmd:maxcnt(}{it:string}{cmd:)}
|
||||
[{cmdab:d:iff}]
|
||||
|
||||
|
||||
{title: Description}
|
||||
|
||||
{p 4 4 2}
|
||||
{cmd:_pecollect} collects results from _pepred, _perhs, _pecats and other
|
||||
low level utility SPost programsto pass to _pecidelta and _peciboot.
|
||||
|
||||
{title: Options}
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd:level()} sets the level of the confidence interval for predicted values
|
||||
or probabilities for the commands for which these are provided.
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd:inout()} specifies model type, such as typical one equation versus two
|
||||
equation
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd:maxcount()} is the maximum count value for which the probability is computed
|
||||
in count models. Default is 9.
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd:diff} computes difference between current predictions and those that were
|
||||
saved.
|
||||
|
||||
|
||||
{title:Details on Globals}
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd:petype} - global string with type of model
|
||||
|
||||
{p 8 8 2}
|
||||
1. Contents - string with three words
|
||||
|
||||
{p 8 8 2}
|
||||
Word 1. cmd - e(cmd){break}
|
||||
Word 2. input from io - typical vs twoeq{break}
|
||||
Word 3. output from io - binary count mlogit ordered regress tobit{break}
|
||||
|
||||
{p 8 8 2}
|
||||
2. To retrieve information
|
||||
|
||||
{p 8 8 2}
|
||||
local cmd : word 1 of $petype // what model was estimated {break}
|
||||
local input : word 2 of $petype // is it a typical or twoeq model? {break}
|
||||
local output : word 3 of $petype // what is the output type? {break}
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd:pecimethod} - global string with type of ci
|
||||
|
||||
{p 8 8 2}
|
||||
1. Contents - string with two words
|
||||
|
||||
{p 8 8 2}
|
||||
Word 1. method for ci computation - ml, delta, ept, bootstrap {break}
|
||||
Word 2. type of bootstrap ci - normal, percentile, biascorrected {break}
|
||||
|
||||
{p 8 8 2}
|
||||
2. To retrieve information
|
||||
|
||||
{p 8 8 2}
|
||||
local cimethod : word 1 of $pecitype{break}
|
||||
local boottype : word 2 of $pecitype{break}
|
||||
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd:peinfo} - global matrix (3x11) with information about the model
|
||||
|
||||
{p 8 8 2}
|
||||
Row 1: current model {break}
|
||||
Row 2: saved model when diff option used{break}
|
||||
Row 3: difference current value - saved value{break}
|
||||
|
||||
{p 8 8 2}
|
||||
Column 1: # of rhs variables (i.e., # of columns inh PEbase) peinfo[1,1]{break}
|
||||
Column 2: # of categories in outcome (from _pecats){break}
|
||||
Column 3: level for confidence interval (e.g., 95 not .95) peinfo[1,3]{break}
|
||||
Column 4: z value for confidence interval at given level peinfo[1,4]{break}
|
||||
Column 5: # of rhs variables for inflation in zip and zinb{break}
|
||||
Column 6: 1 if model with no constant, else 0 peinfo[1,6]{break}
|
||||
Column 7: base category for mlogit{break}
|
||||
Column 8: stdp for binary models{break}
|
||||
Column 9: # of requested reps for bootstrap (# specified by rep() option){break}
|
||||
Column 10: # of completed replications for bootstrap{break}
|
||||
Column 11: maximum # of values in predicted probs in count models.{break}
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd:pebase} and {cmd:pebase2} - base values for the x's
|
||||
|
||||
{p 8 8 2}
|
||||
matrix pebase = PE_in{break}
|
||||
matrix pebase2 = PE_in2{break}
|
||||
|
||||
{p 8 8 2}
|
||||
The jth column of pebase is the jth right hand size variable in the
|
||||
model. The jth column of pebase2 is the jsth right hand side inflation
|
||||
variable in zip or zinb. If save and dif, three rows are in the matrix:{break}
|
||||
|
||||
{p 8 8 2}
|
||||
Row 1: current model {break}
|
||||
Row 2: saved model when diff option used{break}
|
||||
Row 3: difference current value - saved value{break}
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd:pepred} - a global matrix (7 by # of outcome) containing predicted values
|
||||
|
||||
{p 8 8 2}
|
||||
Row 1: values of the outcome category
|
||||
|
||||
{p 8 8 2}
|
||||
{result:Current model}{break}
|
||||
Row 2: predicted probabilities for the value in row 1{break}
|
||||
Row 3: Column 1: xb from first part of model{break}
|
||||
Column 2: mu from count model{break}
|
||||
Column 3: xb from inflation part of zip and zinb{break}
|
||||
Column 4: pr(always 0) from zip and zinb{break}
|
||||
|
||||
{p 8 8 2}
|
||||
{result:Saved model}{break}
|
||||
Row 4: predicted probabilities for the value in row 1{break}
|
||||
Row 5: Column 1: xb from first part of model{break}
|
||||
Column 2: mu from count model{break}
|
||||
Column 3: xb from inflation part of zip and zinb{break}
|
||||
Column 4: pr(always 0) from zip and zinb{break}
|
||||
|
||||
{p 8 8 2}
|
||||
{result:Difference for saved - current}{break}
|
||||
Row 6: predicted probabilities for the value in row 1{break}
|
||||
Row 7: Column 1: xb from first part of model{break}
|
||||
Column 2: mu from count model{break}
|
||||
Column 3: xb from inflation part of zip and zinb{break}
|
||||
Column 4: pr(always 0) from zip and zinb{break}
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd:peupper} - upper bound of ci
|
||||
|
||||
{p 4 8 2}
|
||||
{cmd:pelower} - lower bound of ci
|
||||
|
||||
{p 8 8 2}
|
||||
Row 1: is identical to pepred.{break}
|
||||
Rows 2-7: the upper or lower bounds from the corresponding quantity in pepred{break}
|
||||
|
||||
*** ADD MATRICES FROM BOOT FOR VARIOUS TYPES OF CIs
|
||||
|
||||
{hline}
|
||||
{p 2 4 2}Authors: J. Scott Long & Jun Xu{p_end}
|
||||
{p 11 4 2}{browse www.indiana.edu/~jslsoc/spost.htm}{p_end}
|
||||
{p 11 4 2}spostsup@indiana.edu{p_end}
|
9
Modules/ado/plus/_/_pedum.ado
Normal file
9
Modules/ado/plus/_/_pedum.ado
Normal file
@ -0,0 +1,9 @@
|
||||
*! version 1.6.0 2/27/99
|
||||
|
||||
capture program drop _pedum
|
||||
program define _pedum, rclass
|
||||
version 6
|
||||
syntax varlist(max=1) [if] [in]
|
||||
capture assert `varlist' == 0 | `varlist' == 1 | `varlist' == . `if' `in'
|
||||
return scalar dummy = _rc==0
|
||||
end
|
35
Modules/ado/plus/_/_pedum.hlp
Normal file
35
Modules/ado/plus/_/_pedum.hlp
Normal file
@ -0,0 +1,35 @@
|
||||
.-
|
||||
help for ^_pedum^ - 1.6.0 - 2/27/1999
|
||||
.-
|
||||
|
||||
Utility to determine if a variable is 0, 1 or .
|
||||
-----------------------------------------------
|
||||
|
||||
^_pedum^ varname [^if^ exp] [^in^ range]
|
||||
|
||||
Description
|
||||
-----------
|
||||
|
||||
^_pedum^ returns the scalar ^r(dummy)^=1 if the variable is binary, else 0.
|
||||
Binary variables have all values equal to 0, 1, or missing.
|
||||
|
||||
Examples within a program
|
||||
-------------------------
|
||||
|
||||
local i = 1
|
||||
while `i'<=`nvars' {
|
||||
local nmtoget : word `i' of `varnms'
|
||||
_pedum `nmtoget' `if' `in'
|
||||
mat `Sdummy'[1,`i'] = r(dummy)
|
||||
local i=`i'+1
|
||||
}
|
||||
|
||||
Note
|
||||
----
|
||||
|
||||
You must use ^_peife^ prior to calling ^_pedum^.
|
||||
|
||||
.-
|
||||
Authors: J. Scott Long and Jeremy Freese
|
||||
www.indiana.edu/~jslsoc/spost.htm
|
||||
spostsup@@indiana.edu
|
25
Modules/ado/plus/_/_peife.ado
Normal file
25
Modules/ado/plus/_/_peife.ado
Normal file
@ -0,0 +1,25 @@
|
||||
*! version 1.6.0 2/7/99
|
||||
|
||||
capture program drop _peife
|
||||
program define _peife, rclass
|
||||
version 6.0
|
||||
syntax [if/] [,All]
|
||||
|
||||
* Case 1: no if and no all
|
||||
if "`if'"=="" & "`all'"!="all" { local ifis "if e(sample)" }
|
||||
else {
|
||||
|
||||
* Case 2: if and no all
|
||||
if "`all'"!="all" { local ifis "if e(sample) & `if'" }
|
||||
|
||||
if "`all'"=="all" {
|
||||
|
||||
* Case 3: if and all
|
||||
if "`if'"!="" { local ifis "if `if'" }
|
||||
|
||||
* Case 4: no if and all
|
||||
if "`if'"=="" { local ifis "" }
|
||||
}
|
||||
}
|
||||
return local if "`ifis'"
|
||||
end
|
54
Modules/ado/plus/_/_peife.hlp
Normal file
54
Modules/ado/plus/_/_peife.hlp
Normal file
@ -0,0 +1,54 @@
|
||||
.-
|
||||
help for ^_peife^ - 1.6.0 - 2/7/1999
|
||||
.-
|
||||
|
||||
Utility to decide if estimation sample is used
|
||||
----------------------------------------------
|
||||
|
||||
^_peife^ [^if^ exp] [, ^a^ll]
|
||||
|
||||
Description
|
||||
-----------
|
||||
|
||||
^_peife^ modifies the given ^if^ expression to add restrictions to the last
|
||||
estimation sample ^e(sample)^. Whem the option ^all^ is specified, the
|
||||
^if^ statement is not modified to add ^e(sample)^.
|
||||
|
||||
|
||||
Output
|
||||
------
|
||||
|
||||
You can retrieve a string with the resulting ^if^ statement by using ^r(if)^.
|
||||
For example: local if "`r(if)'" Note that the resulting string includes
|
||||
the work ^if^.
|
||||
|
||||
Options
|
||||
-------
|
||||
|
||||
^all^ By default, the ^if^ statement passed to ^_peife^ is modified to add
|
||||
^& e(sample)^. With option ^all^ the ^if^ condition is not changed.
|
||||
Consequently, all that is done is the word "if" is placed in front of the
|
||||
current ^if^ condition before the string is returned.
|
||||
|
||||
Examples within a program
|
||||
-------------------------
|
||||
...
|
||||
* `if' is the curret if condition
|
||||
_peife `if',`all'
|
||||
* the new condition includes & e(sample)
|
||||
local if "`r(if)'"
|
||||
* note that `if' has the word if in it
|
||||
sum `varlist' `if'
|
||||
...
|
||||
|
||||
Note
|
||||
----
|
||||
|
||||
_pesum, _pedum, _pecats do not call _peife. You need to pass the correct if
|
||||
condition to them.
|
||||
|
||||
.-
|
||||
Authors: J. Scott Long and Jeremy Freese
|
||||
www.indiana.edu/~jslsoc/spost.htm
|
||||
spostsup@@indiana.edu
|
||||
|
232
Modules/ado/plus/_/_pemarg.ado
Normal file
232
Modules/ado/plus/_/_pemarg.ado
Normal file
@ -0,0 +1,232 @@
|
||||
*! version 2.5.0 2009-10-28 jsl
|
||||
* - stata 11 update for returns from -mlogit-
|
||||
|
||||
* compute marginal effects
|
||||
|
||||
capture program drop _pemarg
|
||||
program define _pemarg, rclass
|
||||
|
||||
* 11Apr2005; 13Jun2005
|
||||
if c(stata_version) >= 9 {
|
||||
tempname tmpb
|
||||
mat `tmpb' = e(b)
|
||||
local tmpcut : colnames `tmpb'
|
||||
if index("`tmpcut'", "_cut1") != 0 {
|
||||
local cut "_"
|
||||
}
|
||||
else {
|
||||
local cut "/"
|
||||
}
|
||||
}
|
||||
else {
|
||||
local cut "_"
|
||||
}
|
||||
|
||||
syntax [, caller(real 5.0)] // 1.7.0 stata version
|
||||
|
||||
version 6.0
|
||||
tempname b x xb sxb prob marg tmp bxb bxbm1 difpdf tmp2
|
||||
tempname b x o xo xb sxb prob marg tmp bxb bxbm1 difpdf tmp2 bi alpha
|
||||
tempname sumxb sp PredVal PredPr sump
|
||||
tempname pdiag sumpb o
|
||||
|
||||
if "`e(cmd)'"=="cloglog" { local hasmarg = "not yet" }
|
||||
if "`e(cmd)'"=="cnreg" { local hasmarg = "not applicable" }
|
||||
if "`e(cmd)'"=="fit" { local hasmarg = "not applicable" }
|
||||
if "`e(cmd)'"=="gologit" { local hasmarg = "not yet" }
|
||||
if "`e(cmd)'"=="intreg" { local hasmarg = "not applicable" }
|
||||
if "`e(cmd)'"=="logistic" { local hasmarg = "yes" }
|
||||
if "`e(cmd)'"=="logit" { local hasmarg = "yes" }
|
||||
if "`e(cmd)'"=="mlogit" { local hasmarg = "yes" }
|
||||
if "`e(cmd)'"=="nbreg" { local hasmarg = "yes" }
|
||||
if "`e(cmd)'"=="ologit" { local hasmarg = "yes" }
|
||||
if "`e(cmd)'"=="oprobit" { local hasmarg = "yes" }
|
||||
if "`e(cmd)'"=="poisson" { local hasmarg = "yes" }
|
||||
if "`e(cmd)'"=="probit" { local hasmarg = "yes" }
|
||||
if "`e(cmd)'"=="regress" { local hasmarg = "not applicable" }
|
||||
if "`e(cmd)'"=="tobit" { local hasmarg = "not applicable" }
|
||||
if "`e(cmd)'"=="zinb" { local hasmarg = "not yet" }
|
||||
if "`e(cmd)'"=="zip" { local hasmarg = "not yet" }
|
||||
return local hasmarg `hasmarg'
|
||||
if "`hasmarg'"!="yes" { exit }
|
||||
|
||||
/* 1.6.4
|
||||
matrix `b' = e(b)
|
||||
if "`e(cmd)'"=="mlogit" {
|
||||
version 5.0
|
||||
matrix `b' = get(_b)
|
||||
version 6.0
|
||||
}
|
||||
*/
|
||||
|
||||
tempname eV
|
||||
* 0.2.5 - get b and v for all estimation commands
|
||||
mat `b' = e(b)
|
||||
* drop local nbeta = colsof(`b')
|
||||
if "`e(cmd)'"=="mlogit" {
|
||||
version 5.0
|
||||
matrix `b' = get(_b)
|
||||
version 6.0
|
||||
}
|
||||
mat `eV' = e(V)
|
||||
* 2009-10-28 get b and V under Stata 11
|
||||
if "`e(cmd)'"=="mlogit" { // if mlogit, special treatment
|
||||
nobreak {
|
||||
_get_mlogit_bv `b' `eV'
|
||||
}
|
||||
}
|
||||
|
||||
if "`e(cmd)'"=="nbreg" {
|
||||
local nb = colsof(`b') - 1 /* -1 for alpha */
|
||||
matrix `b' = `b'[1,1..`nb']
|
||||
}
|
||||
matrix `x' = PE_base
|
||||
|
||||
if "`e(cmd)'"=="logit" | "`e(cmd)'"=="probit" | /*
|
||||
*/ "`e(cmd)'"=="logistic" | /*
|
||||
*/"`e(cmd)'"=="poisson" | "`e(cmd)'"=="nbreg" {
|
||||
matrix `x' = `x',J(1,1,1)
|
||||
matrix `xb' = `x' * `b''
|
||||
local nb = colsof(`b') - 1
|
||||
matrix `b' = `b'[1,1..`nb'] /* get rid of _con */
|
||||
scalar `sxb' = `xb'[1,1]
|
||||
if "`e(cmd)'"=="logit" | "`e(cmd)'"=="logistic" {
|
||||
scalar `prob' = exp(`sxb')/(1+exp(`sxb'))
|
||||
scalar `tmp' = `prob'*(1-`prob')
|
||||
matrix `marg' = `tmp'*`b'
|
||||
}
|
||||
else if "`e(cmd)'"=="probit" {
|
||||
scalar `prob' = normprob(`sxb')
|
||||
scalar `tmp' = exp(-`sxb'*`sxb'/2)/sqrt(2*_pi)
|
||||
matrix `marg' = `tmp'*`b'
|
||||
}
|
||||
else if "`e(cmd)'"=="poisson" | "`e(cmd)'"=="nbreg" {
|
||||
scalar `prob' = exp(`sxb')
|
||||
matrix `marg' = `prob'*`b'
|
||||
scalar `prob' = -exp(`sxb') /* to undo change below */
|
||||
}
|
||||
matrix `prob' = -1 * `marg'
|
||||
matrix `marg' = `prob' \ `marg'
|
||||
}
|
||||
|
||||
if "`e(cmd)'"=="oprobit" | "`e(cmd)'"=="ologit" {
|
||||
local ncats = e(k_cat)
|
||||
* xb without intercept
|
||||
local nb = colsof(`b') - `ncats' + 1
|
||||
matrix `b' = `b'[1,1..`nb'] /* get rid of _con's */
|
||||
matrix `xb' = `x' * `b''
|
||||
scalar `sxb' = `xb'[1,1]
|
||||
matrix `difpdf' = J(1,`ncats',1)
|
||||
matrix `marg' = J(`nb',`ncats',1)
|
||||
* compute probabilities
|
||||
if "`e(cmd)'"=="oprobit" {
|
||||
matrix `prob' = J(1,`ncats',1)
|
||||
scalar `bxb' = _b[`cut'cut1]-`sxb'
|
||||
matrix `prob'[1,1] = normprob(`bxb') /* prob for cat 1 */
|
||||
matrix `difpdf'[1,1] = exp(-`bxb'*`bxb'/2)/sqrt(2*_pi)
|
||||
local i 2
|
||||
while `i'<`ncats' {
|
||||
local im1 = `i' - 1
|
||||
scalar `bxb' = _b[`cut'cut`i'] - `sxb'
|
||||
scalar `bxbm1' = _b[`cut'cut`im1'] - `sxb'
|
||||
matrix `prob'[1,`i'] = normprob(`bxb') - normprob(`bxbm1')
|
||||
matrix `difpdf'[1,`i'] = exp(-`bxb'*`bxb'/2)/sqrt(2*_pi) /*
|
||||
*/ - exp(-`bxbm1'*`bxbm1'/2)/sqrt(2*_pi)
|
||||
local i = `i' + 1
|
||||
}
|
||||
local im1 = `i' - 1
|
||||
scalar `bxb' = `sxb'-_b[`cut'cut`im1']
|
||||
matrix `prob'[1,`ncats'] = normprob(`bxb')
|
||||
* 12/6/00
|
||||
matrix `difpdf'[1,`ncats'] = -1*exp(-`bxb'*`bxb'/2)/sqrt(2*_pi)
|
||||
}
|
||||
if "`e(cmd)'"=="ologit" {
|
||||
matrix `prob' = J(1,`ncats',1)
|
||||
scalar `tmp' = 1/(1+exp(`sxb'-_b[`cut'cut1]))
|
||||
matrix `prob'[1,1] = `tmp'
|
||||
matrix `difpdf'[1,1] = `tmp'*(1-`tmp')
|
||||
local i 2
|
||||
while `i'<`ncats' {
|
||||
local im1=`i'-1
|
||||
scalar `tmp' = 1/(1+exp(`sxb'-_b[`cut'cut`i']))
|
||||
scalar `tmp2' = 1/(1+exp(`sxb'-_b[`cut'cut`im1']))
|
||||
matrix `prob'[1,`i'] = `tmp' - `tmp2'
|
||||
matrix `difpdf'[1,`i'] = (`tmp'*(1-`tmp')) - (`tmp2'*(1-`tmp2'))
|
||||
local i=`i'+1
|
||||
}
|
||||
local im1 = `i'-1
|
||||
scalar `tmp' = 1/(1+exp(`sxb'-_b[`cut'cut`im1']))
|
||||
matrix `prob'[1,`ncats'] = 1-`tmp'
|
||||
matrix `difpdf'[1,`i'] = - (`tmp'*(1-`tmp'))
|
||||
}
|
||||
local i 1
|
||||
while `i'<=`nb' {
|
||||
local j 1
|
||||
while `j'<=`ncats' {
|
||||
matrix `marg'[`i',`j'] = -1 * `difpdf'[1,`j'] * `b'[1,`i']
|
||||
local j = `j' + 1
|
||||
}
|
||||
local i = `i' + 1
|
||||
}
|
||||
}
|
||||
|
||||
if "`e(cmd)'"=="mlogit" {
|
||||
matrix `x' = PE_base
|
||||
matrix `xo' = `x',J(1,1,1)
|
||||
matrix `PredVal' = J(1,1,1)
|
||||
matrix colnames `PredVal' = xb
|
||||
/* 1.6.4 - does not work with Stat 11
|
||||
version 5.0
|
||||
matrix `b' = get(_b)
|
||||
version 6.0
|
||||
*/
|
||||
* 2007-06-29 stata 10
|
||||
if c(stata_version) < 10 {
|
||||
local ncats = e(k_cat)
|
||||
}
|
||||
else {
|
||||
local ncats = e(k_out)
|
||||
}
|
||||
matrix `prob' = J(1,`ncats',-1)
|
||||
matrix `xb' = `b'*`xo'' /* one row for each set of b's */
|
||||
matrix `PredVal' = `xb'
|
||||
scalar `sumxb' = 1
|
||||
local i = 1
|
||||
while `i' < `ncats' {
|
||||
scalar `sxb' = exp(`xb'[`i',1])
|
||||
scalar `sumxb' = `sumxb' + `sxb' /* sum of exp(xb) */
|
||||
matrix `prob'[1,`i'] = `sxb'
|
||||
local i = `i' + 1
|
||||
}
|
||||
scalar `sumxb' = 1/`sumxb'
|
||||
matrix `prob'[1,`ncats'] = 1
|
||||
matrix `prob' = `sumxb'*`prob'
|
||||
matrix `PredPr' = `prob'
|
||||
matrix `pdiag' = `PredPr'
|
||||
matrix `pdiag' = diag(`pdiag')
|
||||
* 2007-06-29 stata 10
|
||||
if c(stata_version) < 10 {
|
||||
local ncats = e(k_cat)
|
||||
}
|
||||
else {
|
||||
local ncats = e(k_out)
|
||||
}
|
||||
local nb = colsof(`b')
|
||||
matrix `b' = `b' \ J(1,`nb',0) /* add 0's for last outcome */
|
||||
matrix `marg' = `pdiag' * (`b' - (J(`ncats',`ncats',1)*`pdiag'*`b'))
|
||||
local nb = colsof(`b') - 1
|
||||
matrix `marg' = `marg'[.,1..`nb']
|
||||
matrix `marg' = `marg''
|
||||
}
|
||||
return matrix marginal `marg'
|
||||
|
||||
end
|
||||
exit
|
||||
|
||||
* version 1.6.0 3/29/01
|
||||
* version 1.6.1 11Apr2005 fix _cut for stata 9
|
||||
* version 1.6.2 13Apr2005
|
||||
* version 1.6.3 13Jun2005 - fix ologit version 8/9 bug with _cut
|
||||
* version 1.6.4 2007-06-29 stata 10
|
||||
* version 1.7.0 2009-09-19 jsl
|
||||
* - fix for e(b) for mlogit in stata 11
|
15
Modules/ado/plus/_/_pemarg.hlp
Normal file
15
Modules/ado/plus/_/_pemarg.hlp
Normal file
@ -0,0 +1,15 @@
|
||||
.-
|
||||
help for ^_pemarg^
|
||||
.-
|
||||
|
||||
^_pemarg^
|
||||
|
||||
^_pemarg^ computes marginal effects for some regression models. ^_pemarg^
|
||||
takes as its input the contents of the PE_base matrix created by ^_pebase^.
|
||||
^_pemarg^ returns the marginal effects in r() class matrix r(marginal)
|
||||
|
||||
.-
|
||||
Authors: J. Scott Long and Jeremy Freese
|
||||
www.indiana.edu/~jslsoc/spost.htm
|
||||
spostsup@@indiana.edu
|
||||
|
94
Modules/ado/plus/_/_penocon.ado
Normal file
94
Modules/ado/plus/_/_penocon.ado
Normal file
@ -0,0 +1,94 @@
|
||||
*! version 0.2.3 13Apr2005
|
||||
* version 0.2.2 2005-03-29 stata 9 bug for aux parameters
|
||||
* version 0.2.1 2005-03-25 fixed long varlist bug
|
||||
* version 0.2.0 2005-02-03
|
||||
|
||||
* determine if model has a constant in it
|
||||
|
||||
capture program drop _penocon
|
||||
program define _penocon, rclass
|
||||
version 9.0
|
||||
tempname beta
|
||||
matrix `beta' = e(b)
|
||||
local nbeta = colsof(`beta')
|
||||
local names : colnames `beta'
|
||||
local tmpnms : subinstr local names `"_cons"' `"dog"', all count(local count)
|
||||
local isnocon = 1 - `count'
|
||||
|
||||
// binary
|
||||
|
||||
if "`e(cmd)'"=="cloglog" {
|
||||
local isnocon = `isnocon'
|
||||
}
|
||||
if "`e(cmd)'"=="logistic" {
|
||||
local isnocon = `isnocon'
|
||||
}
|
||||
if "`e(cmd)'"=="logit" {
|
||||
local isnocon = `isnocon'
|
||||
}
|
||||
if "`e(cmd)'"=="probit" {
|
||||
local isnocon = `isnocon'
|
||||
}
|
||||
|
||||
// ordered : nocon not allowed
|
||||
|
||||
if "`e(cmd)'"=="ologit" {
|
||||
local isnocon = 0
|
||||
}
|
||||
if "`e(cmd)'"=="oprobit" {
|
||||
local isnocon = 0
|
||||
}
|
||||
if "`e(cmd)'"=="gologit" {
|
||||
local isnocon = 0
|
||||
}
|
||||
|
||||
// count
|
||||
|
||||
if "`e(cmd)'"=="poisson" {
|
||||
local isnocon = `isnocon'
|
||||
}
|
||||
if "`e(cmd)'"=="nbreg" {
|
||||
local nrhs = e(df_m)
|
||||
local nbeta = `nbeta' - 1 // subtract alpha
|
||||
local isnocon = (`nrhs'==`nbeta')
|
||||
}
|
||||
|
||||
if "`e(cmd)'"=="zinb" {
|
||||
local tmpnms : subinstr local names `"_cons"' `"dog"', all count(local count)
|
||||
local isnocon = (`count'<3)
|
||||
}
|
||||
if "`e(cmd)'"=="zip" {
|
||||
local tmpnms : subinstr local names `"_cons"' `"dog"', all count(local count)
|
||||
local isnocon = (`count'<2)
|
||||
}
|
||||
|
||||
// regression models
|
||||
|
||||
if "`e(cmd)'"=="regress" | "`e(cmd)'"=="fit" {
|
||||
local isnocon = `isnocon'
|
||||
}
|
||||
if "`e(cmd)'"=="tobit" {
|
||||
* stata 9 includes auxillary parameters as a second equation
|
||||
local isnocon = (`count'==1)
|
||||
}
|
||||
if "`e(cmd)'"=="cnreg" {
|
||||
local isnocon = (`count'==1)
|
||||
}
|
||||
if "`e(cmd)'"=="intreg" {
|
||||
local isnocon = (`count'==1)
|
||||
}
|
||||
|
||||
// nominal
|
||||
|
||||
if "`e(cmd)'"=="mlogit" {
|
||||
_perhs
|
||||
local nrhs = r(nrhs)
|
||||
local ncatm1 = e(k_cat) - 1
|
||||
local isnocon = (`nrhs'*`ncatm1'==`nbeta')
|
||||
}
|
||||
|
||||
// return results
|
||||
|
||||
return local nocon "`isnocon'"
|
||||
|
||||
end
|
41
Modules/ado/plus/_/_penocon.hlp
Normal file
41
Modules/ado/plus/_/_penocon.hlp
Normal file
@ -0,0 +1,41 @@
|
||||
{smcl}
|
||||
{* 01/13/05}{...}
|
||||
{hline}
|
||||
help for {hi:_penocon}{right:1/13/2005}
|
||||
{hline}
|
||||
|
||||
{title: Utility to determine if model was run with nocon option}
|
||||
|
||||
{p 8 15 2}{cmd:_penocon}
|
||||
|
||||
|
||||
{title: Description}
|
||||
|
||||
{p 4 4 2}
|
||||
{cmd:_penocon} returns 1 if model used nocon option, else 0. Works
|
||||
with {help cloglog}, {help logistic}, {help logit}, {help probit},
|
||||
{help ologit}, {help oprobit}, {help gologit}, {help poisson},
|
||||
{help nbreg}, {help zinb}, {help zip}, {help regress}, {help tobit},
|
||||
{help cnreg}, {help fit}, {help intreg}, and {help mlogit}.
|
||||
|
||||
{title: Returns}
|
||||
|
||||
{p 4 8 2}
|
||||
r(nocon) : local with 1 if nocon option, else 0.
|
||||
|
||||
{title: Examples}
|
||||
|
||||
...
|
||||
_penocon
|
||||
local isnocon = r(nocon)
|
||||
if `isnocon'==1 {
|
||||
::: case without constant :::
|
||||
}
|
||||
else {
|
||||
:::
|
||||
}
|
||||
|
||||
{hline}
|
||||
{p 2 4 2}Authors: J. Scott Long & Jun Xu{p_end}
|
||||
{p 11 4 2}{browse www.indiana.edu/~jslsoc/spost.htm}{p_end}
|
||||
{p 11 4 2}spostsup@indiana.edu{p_end}
|
651
Modules/ado/plus/_/_pepred.ado
Normal file
651
Modules/ado/plus/_/_pepred.ado
Normal file
@ -0,0 +1,651 @@
|
||||
*! version 1.6.9 2013-08-05 increase maxcnt to 40
|
||||
* version 1.6.8 2008-07-10
|
||||
* - make stdp global
|
||||
|
||||
* _pepred takes as input the matrix PE_in; each row is an observation and
|
||||
* the columns are values for the independent variables in the regression
|
||||
* model. _pepred temporarily adds these observations to the dataset and
|
||||
* generates predicted values. _pepred puts the predicted values in
|
||||
* return matrices that can then be used by the calling program.
|
||||
|
||||
capture program drop _pepred
|
||||
program define _pepred, rclass
|
||||
version 6
|
||||
tempvar added stdp stdf xb xb_hi xb_lo p p1 p1_hi p1_lo p0 p0_hi p0_lo mucount mu
|
||||
tempvar mu_hi mu_lo tempvar p1_inf p0_inf always0
|
||||
tempname b alpha ai gai b_inf xb_inf infby replval zwidth
|
||||
syntax [, level(integer $S_level) maxcnt(integer 9) choices(varlist)]
|
||||
*handle 'level' option
|
||||
if `level' < 10 | `level' > 99 {
|
||||
di in r "level() invalid"
|
||||
error 198
|
||||
}
|
||||
local level = `level'/100
|
||||
*`zwidth' = SD width of confidence interval for `level'% ci
|
||||
sca `zwidth' = invnorm(`level'+((1-`level')/2))
|
||||
|
||||
*check if `maxcnt' specified to acceptable value
|
||||
local max_i = `maxcnt' /* how should this be set */
|
||||
if `max_i' < 0 | `max_i' > 41 {
|
||||
di in r "maxcnt() value not allowed"
|
||||
exit 198
|
||||
}
|
||||
|
||||
*preserve needed because data altered
|
||||
preserve
|
||||
|
||||
*add observations to end of dataset
|
||||
qui gen `added' = 0
|
||||
local newobs = rowsof(PE_in)
|
||||
local oldn = _N
|
||||
local newn = `oldn'+`newobs'
|
||||
qui set obs `newn'
|
||||
*`added'==1 for observations created by _pepred
|
||||
qui replace `added' = 1 if `added' == .
|
||||
|
||||
*use _perhs to get information about rhs variables
|
||||
_perhs
|
||||
local nrhs `r(nrhs)'
|
||||
local nrhs2 `r(nrhs2)'
|
||||
local rhsnms `r(rhsnms)'
|
||||
local rhsnms2 `r(rhsnms2)'
|
||||
|
||||
*fill in added observations with rows of PE_in
|
||||
*cycle through all rhs variables
|
||||
local i = 1
|
||||
while `i' <= `nrhs' {
|
||||
local varname : word `i' of `rhsnms'
|
||||
*for each rhs variable, cycle through all added observations
|
||||
local i2 = 1
|
||||
while `i2' <= `newobs' {
|
||||
*to_rep is the row number of the observation to insert PE_in values
|
||||
local to_rep = `oldn' + `i2'
|
||||
*`replval' value to move from PE_in to dataset
|
||||
sca `replval' = PE_in[`i2',`i']
|
||||
qui replace `varname' = `replval' in `to_rep'
|
||||
local i2 = `i2' + 1
|
||||
}
|
||||
local i = `i' + 1
|
||||
}
|
||||
|
||||
*fill in values for variables in second equation of ZIP/ZINB model
|
||||
if "`nrhs2'"!="" {
|
||||
local i = 1
|
||||
while `i' <= `nrhs2' {
|
||||
local varname : word `i' of `rhsnms2'
|
||||
local i2 = 1
|
||||
while `i2' <= `newobs' {
|
||||
local to_rep = `oldn' + `i2'
|
||||
sca `replval' = PE_in2[`i2',`i']
|
||||
qui replace `varname' = `replval' in `to_rep'
|
||||
local i2 = `i2' + 1
|
||||
}
|
||||
local i = `i' + 1
|
||||
}
|
||||
} /* if "`nrhs2'"!="" */
|
||||
|
||||
*list `rhsnms' in -`newobs'/-1
|
||||
*if "`nrhs2'"!="" { list `rhsnms2' in -`newobs'/-1 }
|
||||
|
||||
*specify routine below that estimation command should call
|
||||
|
||||
if "`e(cmd)'"=="slogit" { local routine "slogit" } // 26Mar2005
|
||||
if "`e(cmd)'"=="mprobit" { local routine "mprobit" } // 28Feb2005
|
||||
if "`e(cmd)'"=="ztp" { local routine "zt" } // 050218
|
||||
if "`e(cmd)'"=="ztnb" { local routine "zt" }
|
||||
if "`e(cmd)'"=="clogit" { local routine "clogit" }
|
||||
if "`e(cmd)'"=="cloglog" { local routine "binary" }
|
||||
if "`e(cmd)'"=="cnreg" { local routine "tobit" }
|
||||
if "`e(cmd)'"=="fit" { local routine "regress" }
|
||||
if "`e(cmd)'"=="gologit" { local routine "gologit" }
|
||||
if "`e(cmd)'"=="intreg" { local routine "tobit" }
|
||||
if "`e(cmd)'"=="logistic" { local routine "binary" }
|
||||
if "`e(cmd)'"=="logit" { local routine "binary" }
|
||||
if "`e(cmd)'"=="mlogit" { local routine "mlogit" }
|
||||
if "`e(cmd)'"=="nbreg" { local routine "count" }
|
||||
if "`e(cmd)'"=="ologit" { local routine "ordered" }
|
||||
if "`e(cmd)'"=="oprobit" { local routine "ordered" }
|
||||
if "`e(cmd)'"=="poisson" { local routine "count" }
|
||||
if "`e(cmd)'"=="probit" { local routine "binary" }
|
||||
if "`e(cmd)'"=="regress" { local routine "regress" }
|
||||
if "`e(cmd)'"=="tobit" { local routine "tobit" }
|
||||
if "`e(cmd)'"=="zinb" { local routine "zeroinf" }
|
||||
if "`e(cmd)'"=="zip" { local routine "zeroinf" }
|
||||
|
||||
*Note: these routines define a local macro `newvars', which is a list of
|
||||
* all matrices that _pepred will return to the calling program.
|
||||
|
||||
*NB!?: predictions are done for all observations because you can't use temporary
|
||||
*variables as an if condition after predict (if `added' == 1)
|
||||
|
||||
*BINARY ROUTINE
|
||||
|
||||
if "`routine'" == "binary" {
|
||||
local newvars "xb stdp p1 p0 xb_hi p1_hi p0_hi xb_lo p1_lo p0_lo"
|
||||
|
||||
quietly {
|
||||
|
||||
*use predict to get xb and std err of prediction
|
||||
predict `xb', xb
|
||||
predict `stdp', stdp
|
||||
*2008-07-09
|
||||
global stdp = `stdp'[1]
|
||||
|
||||
*calculate upper and lower ci for xb
|
||||
gen `xb_hi' = `xb' + (`zwidth'*`stdp')
|
||||
gen `xb_lo' = `xb' - (`zwidth'*`stdp')
|
||||
|
||||
*convert ci bounds into probabilities
|
||||
if "`e(cmd)'"=="logit" | "`e(cmd)'"=="logistic" {
|
||||
gen `p1' = exp(`xb')/(1+exp(`xb'))
|
||||
gen `p1_hi' = exp(`xb_hi')/(1+exp(`xb_hi'))
|
||||
gen `p1_lo' = exp(`xb_lo')/(1+exp(`xb_lo'))
|
||||
}
|
||||
if "`e(cmd)'"=="probit" {
|
||||
gen `p1' = normprob(`xb')
|
||||
gen `p1_hi' = normprob(`xb_hi')
|
||||
gen `p1_lo' = normprob(`xb_lo')
|
||||
}
|
||||
if "`e(cmd)'"=="cloglog" {
|
||||
gen `p1' = 1 - exp(-exp(`xb'))
|
||||
gen `p1_hi' = 1 - exp(-exp(`xb_hi'))
|
||||
gen `p1_lo' = 1 - exp(-exp(`xb_lo'))
|
||||
}
|
||||
|
||||
*use prob(1) values to calculate corresponding prob(0) values
|
||||
gen `p0' = 1 - `p1'
|
||||
gen `p0_hi' = 1 - `p1_hi'
|
||||
gen `p0_lo' = 1 - `p1_lo'
|
||||
|
||||
} /* quietly */
|
||||
}
|
||||
|
||||
* ORDERED ROUTINE
|
||||
|
||||
if "`routine'" == "ordered" {
|
||||
quietly {
|
||||
|
||||
*get information about categories of dependent variables
|
||||
_pecats
|
||||
local ncats = r(numcats)
|
||||
local catvals "`r(catvals)'"
|
||||
|
||||
*use predict to get probabilities for each outcome
|
||||
*cycle through each category
|
||||
local i = 1
|
||||
while `i' <= `ncats' {
|
||||
tempvar p`i'
|
||||
local newvars "`newvars'p`i' "
|
||||
local catval : word `i' of `catvals'
|
||||
*_PEtemp has to be used because temporary variable causes error
|
||||
capture drop _PEtemp
|
||||
predict _PEtemp, p outcome(`catval')
|
||||
gen `p`i'' = _PEtemp
|
||||
local i = `i' + 1
|
||||
}
|
||||
|
||||
*use predict to get probability of xb and std err of prediction
|
||||
local newvars "`newvars'xb stdp xb_hi xb_lo"
|
||||
capture drop _PEtemp
|
||||
predict _PEtemp, xb
|
||||
qui gen `xb' = _PEtemp
|
||||
capture drop _PEtemp
|
||||
predict _PEtemp, stdp
|
||||
qui gen `stdp' = _PEtemp
|
||||
*2008-07-09
|
||||
global stdp = `stdp'[1]
|
||||
|
||||
*calculate upper and lower ci's for xb
|
||||
gen `xb_hi' = `xb' + (`zwidth'*`stdp')
|
||||
gen `xb_lo' = `xb' - (`zwidth'*`stdp')
|
||||
|
||||
} /* quietly { */
|
||||
} /* if "`routine'" == "ordered" */
|
||||
|
||||
* MLOGIT ROUTINE
|
||||
|
||||
if "`routine'" == "mlogit" {
|
||||
|
||||
*get information on categories of dependent variable
|
||||
_pecats
|
||||
local ncats = r(numcats)
|
||||
local catvals "`r(catvals)'"
|
||||
local refval "`r(refval)'"
|
||||
|
||||
local i = 1
|
||||
quietly {
|
||||
while `i' <= `ncats' {
|
||||
tempvar p`i' xb`i' stdp`i' sdp`i' xb_hi`i' xb_lo`i'
|
||||
local newvars "`newvars'p`i' "
|
||||
|
||||
*use predict to get probabilities for each outcome
|
||||
local catval : word `i' of `catvals'
|
||||
capture drop _PEtemp
|
||||
predict _PEtemp, p outcome(`catval')
|
||||
gen `p`i'' = _PEtemp
|
||||
|
||||
*if `i' != `ncats', then outcome is not base category
|
||||
if `i' != `ncats' {
|
||||
local newvars "`newvars'xb`i' stdp`i' sdp`i' "
|
||||
capture drop _PEtemp
|
||||
*use predict to get standard error of prediction
|
||||
predict _PEtemp, stdp outcome(`catval')
|
||||
qui gen `stdp`i'' = _PEtemp
|
||||
capture drop _PEtemp
|
||||
*use predict to get standard error of difference in prediction
|
||||
predict _PEtemp, stddp outcome(`catval', `refval')
|
||||
qui gen `sdp`i'' = _PEtemp
|
||||
capture drop _PEtemp
|
||||
*use predict to get xb
|
||||
predict _PEtemp, xb outcome(`catval')
|
||||
qui gen `xb`i'' = _PEtemp
|
||||
*calculate upper and lower bounds of ci
|
||||
qui gen `xb_hi`i'' = `xb`i'' + (`zwidth'*`stdp`i'')
|
||||
qui gen `xb_lo`i'' = `xb`i'' - (`zwidth'*`stdp`i'')
|
||||
}
|
||||
local i = `i' + 1
|
||||
} /* while `i' <= `ncats' */
|
||||
}
|
||||
}
|
||||
|
||||
* MPROBIT 28Feb2005
|
||||
|
||||
if "`routine'" == "mprobit" {
|
||||
|
||||
*get information on categories of dependent variable
|
||||
_pecats
|
||||
local ncats = r(numcats)
|
||||
local catvals "`r(catvals)'"
|
||||
local refval "`r(refval)'"
|
||||
|
||||
local i = 1
|
||||
quietly {
|
||||
while `i' <= `ncats' {
|
||||
tempvar p`i' xb`i' /*stdp`i' sdp`i' xb_hi`i' xb_lo`i'*/
|
||||
local newvars "`newvars'p`i' "
|
||||
*use predict to get probabilities for each outcome
|
||||
local catval : word `i' of `catvals'
|
||||
capture drop _PEtemp
|
||||
predict _PEtemp, p outcome(`catval')
|
||||
gen `p`i'' = _PEtemp
|
||||
|
||||
*if `i' != `ncats', then outcome is not base category
|
||||
if `i' != `ncats' {
|
||||
local newvars "`newvars'xb`i' " /*stdp`i' sdp`i' "*/
|
||||
capture drop _PEtemp
|
||||
*use predict to get standard error of prediction
|
||||
capture drop _PEtemp
|
||||
*use predict to get standard error of difference in prediction
|
||||
capture drop _PEtemp
|
||||
*use predict to get xb
|
||||
predict _PEtemp, xb outcome(`catval')
|
||||
qui gen `xb`i'' = _PEtemp
|
||||
}
|
||||
local i = `i' + 1
|
||||
} /* while `i' <= `ncats' */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
* SLOGIT 26Mar2005
|
||||
|
||||
if "`routine'" == "slogit" {
|
||||
*get information on categories of dependent variable
|
||||
_pecats
|
||||
local ncats = r(numcats)
|
||||
local catvals "`r(catvals)'"
|
||||
local refval "`r(refval)'"
|
||||
|
||||
local i = 1
|
||||
quietly {
|
||||
while `i' <= `ncats' {
|
||||
tempvar p`i' xb`i' /*stdp`i' sdp`i' xb_hi`i' xb_lo`i'*/
|
||||
local newvars "`newvars'p`i' "
|
||||
*use predict to get probabilities for each outcome
|
||||
local catval : word `i' of `catvals'
|
||||
capture drop _PEtemp
|
||||
predict _PEtemp, p outcome(`catval')
|
||||
gen `p`i'' = _PEtemp
|
||||
local i = `i' + 1
|
||||
} /* while `i' <= `ncats' */
|
||||
} /* quietly */
|
||||
} /* if "`routine'" == "slogit" */
|
||||
|
||||
if "`routine'" == "gologit" {
|
||||
|
||||
*get information about number of categories
|
||||
_pecats
|
||||
local ncats = r(numcats)
|
||||
local catvals "`r(catvals)'"
|
||||
local numeqs = `ncats'-1 /* number of equations */
|
||||
quietly {
|
||||
|
||||
*cycle through each equation
|
||||
local i = 1
|
||||
while `i' <= `numeqs' {
|
||||
tempvar xb`i' pcut`i'
|
||||
*use predict to get xb for each equation
|
||||
predict `xb`i'', eq(mleq`i')
|
||||
local newvars "`newvars'xb`i' "
|
||||
*convert xb into prob(y<=`i')
|
||||
gen `pcut`i'' = exp(`xb`i'')/(1+exp(`xb`i''))
|
||||
local i = `i' + 1
|
||||
}
|
||||
|
||||
*setting variables to indicate that prob(y<=0)=0 and prob(y<=`ncats)=1
|
||||
tempvar pcut`ncats' pcut0
|
||||
gen `pcut`ncats''=0
|
||||
gen `pcut0'=1
|
||||
|
||||
*cycle through categories
|
||||
local i = 1
|
||||
while `i' <= `ncats' {
|
||||
tempvar p`i'
|
||||
local newvars "`newvars'p`i' "
|
||||
local j = `i' - 1
|
||||
*calculate prob(y=i) as prob(y<=i) - prob(y<=[i-1])
|
||||
gen `p`i'' = `pcut`j''-`pcut`i''
|
||||
local i = `i' + 1
|
||||
} /* while `i' <= `ncats' */
|
||||
} /* quietly */
|
||||
} /* if "`routine'" == "gologit" */
|
||||
|
||||
* COUNT MODEL ROUTINE
|
||||
|
||||
if "`routine'"=="count" | "`routine'" == "zt" { // 050218
|
||||
quietly {
|
||||
|
||||
*get alpha if nbreg
|
||||
*zt 18Feb2005
|
||||
if "`e(cmd)'"=="nbreg" | "`e(cmd)'"=="ztnb" {
|
||||
sca `alpha' = e(alpha)
|
||||
sca `ai' = 1/`alpha'
|
||||
*`gai' used to calculate probabilities
|
||||
sca `gai' = exp(lngamma(`ai'))
|
||||
if `gai'==. {
|
||||
di in r "problem with alpha from nbreg prohibits " /*
|
||||
*/ "estimation of predicted probabilities"
|
||||
exit 198
|
||||
}
|
||||
}
|
||||
|
||||
*use predict to get mu, xb, and std err of prediction
|
||||
*zt add Cmu for conditional mu 050218
|
||||
tempname Cmu
|
||||
local newvars "mu xb stdp "
|
||||
capture drop _PEtemp
|
||||
predict double _PEtemp, ir /* does not handle offset or exposure */
|
||||
gen `mu' = _PEtemp
|
||||
capture drop _PEtemp
|
||||
predict double _PEtemp, xb
|
||||
gen `xb' = _PEtemp
|
||||
capture drop _PEtemp
|
||||
predict double _PEtemp, stdp
|
||||
gen `stdp' = _PEtemp
|
||||
*zt Cmu 18Feb2005
|
||||
* compute conditional rate
|
||||
if "`e(cmd)'"=="ztnb" | "`e(cmd)'"=="ztp" {
|
||||
capture drop _PEtemp
|
||||
predict double _PEtemp, cm
|
||||
gen `Cmu' = _PEtemp
|
||||
local newvars "mu xb stdp Cmu "
|
||||
}
|
||||
|
||||
*ci's for poisson (doesn't work for nbreg because of alpha)
|
||||
*zt and compute upper and lower 18Feb2005
|
||||
if "`e(cmd)'"=="poisson" | "`e(cmd)'"=="ztp" {
|
||||
local newvars "`newvars'xb_hi xb_lo mu_hi mu_lo "
|
||||
gen `xb_hi' = `xb' + (`zwidth'*`stdp')
|
||||
gen `xb_lo' = `xb' - (`zwidth'*`stdp')
|
||||
gen `mu_hi' = exp(`xb_hi')
|
||||
gen `mu_lo' = exp(`xb_lo')
|
||||
}
|
||||
|
||||
*calculate prob of observing a given count [Prob(y=1)]
|
||||
*cycle from 0 to maximum count wanted
|
||||
local i = 0
|
||||
while `i' <= `max_i' {
|
||||
tempvar p`i'
|
||||
local newvars "`newvars'p`i' "
|
||||
*predicting a particular count from mu
|
||||
*zt 18Feb2005
|
||||
if "`e(cmd)'"=="poisson" | "`e(cmd)'"=="ztp" {
|
||||
* usual poisson formula
|
||||
qui gen double `p`i'' = ((exp(-`mu'))*(`mu'^`i')) / /*
|
||||
*/ (round(exp(lnfact(`i'))), 1)
|
||||
tempname p_hi`i' p_lo`i'
|
||||
local newvars "`newvars'p_hi`i' p_lo`i' "
|
||||
qui gen double `p_hi`i'' = /*
|
||||
*/ ((exp(-`mu_hi'))*(`mu_hi'^`i')) / /*
|
||||
*/ (round(exp(lnfact(`i'))), 1)
|
||||
qui gen double `p_lo`i'' /*
|
||||
*/ = ((exp(-`mu_lo'))*(`mu_lo'^`i')) /*
|
||||
*/ / (round(exp(lnfact(`i'))), 1)
|
||||
}
|
||||
|
||||
*zt 18Feb2005
|
||||
if "`e(cmd)'"=="nbreg" | "`e(cmd)'"=="ztnb" {
|
||||
capture drop _PEtemp
|
||||
qui gen double _PEtemp = ( exp(lngamma(`i'+`ai')) /*
|
||||
*/ / ( round(exp(lnfact(`i')),1) * exp(lngamma(`ai')) ) ) /*
|
||||
*/ * ((`ai'/(`ai'+`mu'))^`ai') * ((`mu'/(`ai'+`mu'))^`i')
|
||||
qui gen double `p`i'' = _PEtemp
|
||||
}
|
||||
local i = `i' + 1
|
||||
}
|
||||
return scalar maxcount = `max_i'
|
||||
} /* quietly */
|
||||
|
||||
*-> GENERATE CONDITIONAL PREDICTED PROBABILITIES
|
||||
*zt compute conditional predictions 18Feb2005
|
||||
if "`e(cmd)'"=="ztp" | "`e(cmd)'"=="ztnb" {
|
||||
local i 1
|
||||
while `i' <= `max_i' {
|
||||
tempvar Cp`i' // C for Conditional
|
||||
local newvars "`newvars'Cp`i' "
|
||||
* divide by prob not equal to 0
|
||||
quietly gen `Cp`i'' = `p`i''/(1-`p0')
|
||||
label variable `Cp`i'' "Pr(y=`i'|y>0) `modelis'"
|
||||
local i = `i' + 1
|
||||
}
|
||||
} // zt
|
||||
|
||||
} /* if "`routine'" == "count" */
|
||||
|
||||
|
||||
* ZERO-INFLATED COUNT MODEL ROUTINE
|
||||
|
||||
if "`routine'"=="zeroinf" {
|
||||
quietly {
|
||||
* mucount == mu from count portion of model - 15Apr2005
|
||||
* mu == expected y
|
||||
local newvars "mu mucount xb stdp p "
|
||||
capture drop _PEtemp
|
||||
* E(y)
|
||||
predict double _PEtemp, n /* does not handle offset or exposure */
|
||||
gen double `mu' = _PEtemp
|
||||
capture drop _PEtemp
|
||||
predict double _PEtemp, xb
|
||||
* xb from the count portion of the model
|
||||
gen double `xb' = _PEtemp
|
||||
capture drop _PEtemp
|
||||
predict double _PEtemp, stdp
|
||||
gen `stdp' = _PEtemp
|
||||
capture drop _PEtemp
|
||||
predict double _PEtemp, p
|
||||
gen `p' = _PEtemp
|
||||
* E(y | not always 0) - 15Apr2005
|
||||
quietly gen double `mucount' = `mu'/(1-`p')
|
||||
mat `b' = e(b)
|
||||
if "`e(cmd)'"=="zinb" {
|
||||
local temp = colsof(`b')
|
||||
sca `alpha' = `b'[1, `temp']
|
||||
sca `alpha' = exp(`alpha')
|
||||
sca `ai' = 1/`alpha'
|
||||
sca `gai' = exp(lngamma(`ai'))
|
||||
if `gai'==. {
|
||||
di in r "problem with alpha from zinb prohibits " /*
|
||||
*/ "estimation of predicted probabilities"
|
||||
exit 198
|
||||
}
|
||||
*take alpha off beta matrix
|
||||
local temp = `temp' - 1
|
||||
mat `b' = `b'[1, 1..`temp']
|
||||
}
|
||||
|
||||
*make beta matrix for inflate equation
|
||||
local temp = `nrhs' + 2
|
||||
local temp2 = colsof(`b')
|
||||
mat `b_inf' = `b'[1,`temp'..`temp2']
|
||||
|
||||
*calculate xb of the inflate model
|
||||
local newvars "`newvars'xb_inf "
|
||||
gen double `xb_inf' = 0 if `added' == 1
|
||||
local i = 1
|
||||
while `i' <= `nrhs2' {
|
||||
local infvar : word `i' of `rhsnms2'
|
||||
sca `infby' = `b_inf'[1,`i']
|
||||
replace `xb_inf' = `xb_inf' + (`infby'*`infvar')
|
||||
local i = `i' + 1
|
||||
}
|
||||
*add constant
|
||||
replace `xb_inf' = `xb_inf' + `b_inf'[1, `i']
|
||||
|
||||
*calculate prob(inflate==1)
|
||||
if "`e(inflate)'"=="logit" {
|
||||
gen `p1_inf' = exp(`xb_inf')/(1+exp(`xb_inf'))
|
||||
}
|
||||
if "`e(inflate)'"=="probit" {
|
||||
gen `p1_inf' = normprob(`xb_inf')
|
||||
}
|
||||
|
||||
*calculate prob(inflate==0)
|
||||
gen `p0_inf' = 1 - `p1_inf'
|
||||
|
||||
*return prob(inflate==1) as `always0'
|
||||
local newvars "`newvars'always0 "
|
||||
gen `always0' = `p1_inf'
|
||||
|
||||
*predicting a particular count from mucount
|
||||
local i = 0
|
||||
while `i' <= `max_i' {
|
||||
tempvar p`i'
|
||||
local newvars "`newvars'p`i' "
|
||||
* use mucount not mu! 15Apr2005
|
||||
if "`e(cmd)'"=="zip" {
|
||||
qui gen double `p`i'' = /*
|
||||
*/ ((exp(-`mucount'))*(`mucount'^`i'))/(round(exp(lnfact(`i'))), 1)
|
||||
}
|
||||
* use mucount not mu! 15Apr2005
|
||||
if "`e(cmd)'"=="zinb" {
|
||||
capture drop _PEtemp
|
||||
qui gen double _PEtemp = ( exp(lngamma(`i'+`ai')) /*
|
||||
*/ / (round(exp(lnfact(`i')),1) * exp(lngamma(`ai')))) /*
|
||||
*/ * ((`ai'/(`ai'+`mucount'))^`ai') * ((`mucount'/(`ai'+`mucount'))^`i')
|
||||
qui gen double `p`i'' = _PEtemp
|
||||
}
|
||||
|
||||
*adjust counts for always zeros
|
||||
qui replace `p`i'' = `p`i''*`p0_inf'
|
||||
|
||||
local i = `i' + 1
|
||||
}
|
||||
*adjust prob(y=0) for always zeros
|
||||
replace `p0' = `p0' + `always0'
|
||||
return scalar maxcount = `max_i'
|
||||
|
||||
} /* quietly */
|
||||
} /* if "`routine'" == "zeroinf" */
|
||||
|
||||
* TOBIT ROUTINE
|
||||
|
||||
if "`routine'" == "tobit" {
|
||||
* remove stdf 6/23/2006
|
||||
* local newvars "`newvars'xb xb_hi xb_lo stdp stdf "
|
||||
local newvars "`newvars'xb xb_hi xb_lo stdp "
|
||||
quietly {
|
||||
predict `xb', xb
|
||||
predict `stdp', stdp
|
||||
*2008-07-09
|
||||
global stdp = `stdp'[1]
|
||||
|
||||
* remove stdf 6/23/2006
|
||||
* predict `stdf', stdf
|
||||
gen `xb_hi' = `xb' + (`zwidth'*`stdp')
|
||||
gen `xb_lo' = `xb' - (`zwidth'*`stdp')
|
||||
}
|
||||
}
|
||||
|
||||
* REGRESS ROUTINE
|
||||
|
||||
if "`routine'" == "regress" {
|
||||
* remove stdf 6/23/2006
|
||||
* local newvars "`newvars'xb xb_hi xb_lo stdp stdf "
|
||||
local newvars "`newvars'xb xb_hi xb_lo stdp "
|
||||
quietly {
|
||||
predict `xb', xb
|
||||
predict `stdp', stdp
|
||||
*2008-07-09
|
||||
global stdp = `stdp'[1]
|
||||
|
||||
* remove stdf 6/23/2006
|
||||
* predict `stdf', stdf
|
||||
gen `xb_hi' = `xb' + (`zwidth'*`stdp')
|
||||
gen `xb_lo' = `xb' - (`zwidth'*`stdp')
|
||||
}
|
||||
}
|
||||
|
||||
** MAKE RETURN MATRICES
|
||||
|
||||
*return level
|
||||
return local level `level'
|
||||
tokenize "`newvars'"
|
||||
|
||||
*cycle through all the new variables created by routine above
|
||||
local i = 1
|
||||
while "``i''" != "" {
|
||||
|
||||
*make matrix tmatnam with all observations for a given new variable
|
||||
local tmatnam = "_``i''"
|
||||
if length("`tmatnam'") > 7 {
|
||||
local tmatnam = substr("`tmatnam'", 1, 7)
|
||||
}
|
||||
tempname `tmatnam'
|
||||
mat ``tmatnam'' = J(`newobs', 1, 0)
|
||||
local i2 = 1
|
||||
while `i2' <= `newobs' {
|
||||
local outob = `oldn' + `i2'
|
||||
mat ``tmatnam''[`i2',1] = ```i'''[`outob']
|
||||
local i2 = `i2' + 1
|
||||
}
|
||||
*return matrix so available to calling program
|
||||
return matrix ``i'' ``tmatnam''
|
||||
local i = `i' + 1
|
||||
}
|
||||
|
||||
end
|
||||
|
||||
|
||||
exit
|
||||
|
||||
15Apr2005 - correct error for zip and zinb (see changes in _pepred, _pecollect, _peciboot
|
||||
|
||||
E(y) was used incorrectly rather than E(y|~always0).
|
||||
|
||||
_pepred[3|5|7, 2] used to be mu defined as rate in count portion of model E(y|not always 0)
|
||||
|
||||
_pepred[3|5|7, 2] now is the overall rate E(y); listed as simply mu.
|
||||
|
||||
_pepred[3|5|7, 3] rate in count portion of model E(y|not always 0); listed as mucount.
|
||||
|
||||
To simplify changes in _peciboot, E(y) is referred to as mu; E(y|~always0) is mucount.
|
||||
|
||||
* version 1.6.7 2008-07-09
|
||||
* - save se of ystar predictions
|
||||
* version 1.6.6 23Jun2006 fix stdf bug for regress and tobit
|
||||
* version 1.6.5 15Apr2005 fix rate used for zip/zinb (see notes at end)
|
||||
* version 1.6.4 13Apr2005
|
||||
* version 1.6.3 27Mar2005 slogit
|
||||
* version 1.6.2 28Feb2005 mprobit
|
||||
* version 1.6.1 18Feb2005 zt models
|
||||
* version 1.6.0 3/29/01
|
54
Modules/ado/plus/_/_pepred.hlp
Normal file
54
Modules/ado/plus/_/_pepred.hlp
Normal file
@ -0,0 +1,54 @@
|
||||
.-
|
||||
help for ^_pepred^
|
||||
.-
|
||||
|
||||
_pepred [, level(integer) maxcnt(integer)]
|
||||
|
||||
^_pepred^ is a utility program designed to generate predicted values from a
|
||||
series of specified values of the independent variables. _pepred takes as
|
||||
input the matrix PE_in; each row is an observation and the columns are
|
||||
values for the independent variables in the regression model. ^_pepred^
|
||||
temporarily adds these observations to the dataset and generates predicted
|
||||
values. _pepred puts the predicted values in return matrices that can then
|
||||
be used by the calling program.
|
||||
|
||||
Options
|
||||
-------
|
||||
|
||||
^level()^ sets the level of the confidence interval for predicted values
|
||||
or probabilities for the estimation commands for which these are
|
||||
provided.
|
||||
|
||||
^maxcnt()^ is the maximum count value for which the probability is computed
|
||||
in count models. Default is 9.
|
||||
|
||||
Return Values
|
||||
-------------
|
||||
|
||||
Scalars
|
||||
r(always0) : pr(Always0) for zip and zinb
|
||||
r(xb): value of xb
|
||||
r(xb_lo): lower bound of confidence interval for xb
|
||||
r(xb_hi): upper bound of confidence interval for xb
|
||||
r(p0): predicted prob of 0 for binary model
|
||||
r(p0_lo): lower bound of confidence interval for predicted probability of 0
|
||||
r(p0_hi): upper bound of confidence interval for predicted probability of 0
|
||||
r(p1): predicted prob of 1 for binary model
|
||||
r(p1_lo): lower bound of confidence interval for predicted probability of 1
|
||||
r(p1_hi): upper bound of confidence interval for predicted probability of 1
|
||||
r(mu): predicted count or rate for count models
|
||||
|
||||
Macros:
|
||||
r(level) : confidence level for commands that have this
|
||||
|
||||
Matrices:
|
||||
r(probs): predicted probabilities for multiple categories or counts
|
||||
r(values): values of outcome categories for rows of r(probs)
|
||||
r(x): x values for independent variables
|
||||
r(x2): x values for independent variables in second equation (^zip^, ^zinb^)
|
||||
|
||||
.-
|
||||
Authors: J. Scott Long and Jeremy Freese
|
||||
www.indiana.edu/~jslsoc/spost.htm
|
||||
spostsup@@indiana.edu
|
||||
|
107
Modules/ado/plus/_/_perhs.ado
Normal file
107
Modules/ado/plus/_/_perhs.ado
Normal file
@ -0,0 +1,107 @@
|
||||
*! version 1.6.6 2010-06-30 parse out o. names
|
||||
* version 1.6.5 13Apr2005
|
||||
* version 1.6.4 2005-01-23 fix with zip and nocon
|
||||
|
||||
* determine rhs variables
|
||||
|
||||
capture program drop _perhs
|
||||
program define _perhs, rclass
|
||||
version 6.0
|
||||
* if name is passed as option it will parse these names
|
||||
local beta "`1'"
|
||||
if "`beta'" == "" {
|
||||
tempname beta
|
||||
matrix `beta' = e(b)
|
||||
}
|
||||
local varnms : colnames(`beta')
|
||||
|
||||
|
||||
* 1.6 6 - strip out omitted coefficients with names o.
|
||||
local no_o ""
|
||||
foreach v in `varnms' {
|
||||
local iso = substr("`v'",1,2)
|
||||
if "`iso'"!="o." {
|
||||
local no_o "`no_o' `v'"
|
||||
}
|
||||
}
|
||||
local varnms `no_o'
|
||||
|
||||
tokenize "`varnms'", parse(" ")
|
||||
|
||||
local iszi = 0 /* is it a zip model? */
|
||||
if "`e(cmd)'"=="zip" | "`e(cmd)'"=="zinb" {
|
||||
_penocon /* check if there is a constant */
|
||||
local ncon = 1 - `r(nocon)' /* 1 if constant, else 0 */
|
||||
local iszi = 1 /* it is a zip */
|
||||
}
|
||||
|
||||
* strip off _cons, _cut & _se
|
||||
local rhsnm ""
|
||||
|
||||
*050123 only for nonzip
|
||||
if `iszi'==0 {
|
||||
local hascon "no"
|
||||
local i 1
|
||||
while "``i''" != "" {
|
||||
* version 1.6.3 2004-12-22 - wrong rhs names if mlogit, nocon
|
||||
* When it finds the same variable a second time, program sets
|
||||
* local hascon to yes even though there is not a constant.
|
||||
* without this it would repeat all variables ncat-1 times.
|
||||
if "`i'"=="1" {
|
||||
local nm1 "``i''"
|
||||
}
|
||||
else if "`nm1'" == "``i''" {
|
||||
local hascon "yes"
|
||||
}
|
||||
|
||||
if "``i''" == "_cons" & "`hascon'"=="no" {
|
||||
local hascon "yes"
|
||||
local start2 = `i' + 1
|
||||
}
|
||||
if "``i''" != "_cons" /*
|
||||
*/ & "``i''" != "_se" /*
|
||||
*/ & substr("``i''",1,4) != "_cut" /*
|
||||
*/ & "`hascon'"=="no" {
|
||||
local rhsnm "`rhsnm' ``i''"
|
||||
}
|
||||
local i = `i' + 1
|
||||
}
|
||||
local nvar : word count `rhsnm'
|
||||
} /* 050123 not zip */
|
||||
|
||||
*050123 if zip, have to count differently for case with no constant
|
||||
if `iszi'==1 {
|
||||
local nvar = e(df_m) /* # vars in count model */
|
||||
local i = 1
|
||||
while `i'<=`nvar' {
|
||||
local rhsnm "`rhsnm' ``i''"
|
||||
local ++i
|
||||
}
|
||||
local i = `i' + `ncon' /* if constant, skip it */
|
||||
local rhsnm2 ""
|
||||
local nvar2 = e(df_c) - 1 /* # vars in inf model */
|
||||
while `i'<=`nvar'+`nvar2'+`ncon' {
|
||||
local rhsnm2 "`rhsnm2' ``i''"
|
||||
local ++i
|
||||
}
|
||||
} /* end of case for zip/zinb */
|
||||
|
||||
* specail case for mlogit
|
||||
if "`e(cmd)'"=="mlogit" {
|
||||
parse "`rhsnm'", parse(" ")
|
||||
local rhsnm2 ""
|
||||
local i 1
|
||||
while `i' <= `nvar' {
|
||||
local rhsnm2 "`rhsnm2' ``i''"
|
||||
local i = `i' + 1
|
||||
}
|
||||
local rhsnm "`rhsnm2'"
|
||||
local rhsnm2 ""
|
||||
}
|
||||
|
||||
return local rhsnms "`rhsnm'"
|
||||
return local nrhs "`nvar'"
|
||||
return local rhsnms2 "`rhsnm2'"
|
||||
return local nrhs2 "`nvar2'"
|
||||
|
||||
end
|
46
Modules/ado/plus/_/_perhs.hlp
Normal file
46
Modules/ado/plus/_/_perhs.hlp
Normal file
@ -0,0 +1,46 @@
|
||||
.-
|
||||
help for ^_perhs^ - 1.6.2 - 2/20/00
|
||||
.-
|
||||
|
||||
Utility to determine names and number of right hand size variables
|
||||
in regression models
|
||||
------------------------------------------------------------------
|
||||
|
||||
^_perhs^
|
||||
|
||||
Description
|
||||
-----------
|
||||
|
||||
^_perhs^ returns the number of right hand side variables and their names
|
||||
for regression models.
|
||||
|
||||
Works with regress, logit, probit, ologit, oprobit, mlogit, tobit,
|
||||
zip, zinb, poisson, nbreg.
|
||||
|
||||
Returns
|
||||
-------
|
||||
|
||||
r(nrhs) : local with number of rhs variables, excluding intercept.
|
||||
|
||||
r(rhsnms): local with names of rhs variables.
|
||||
|
||||
Examples within a program
|
||||
-------------------------
|
||||
|
||||
...
|
||||
_perhs
|
||||
local nvars = `r(nrhs)' + 1
|
||||
local varnms "`e(depvar)' `r(rhsnms)'"
|
||||
|
||||
local i = 1
|
||||
while `i'<=`nvars' {
|
||||
local nmtoget : word `i' of `varnms'
|
||||
:::
|
||||
local i=`i'+1
|
||||
}
|
||||
|
||||
.-
|
||||
Authors: J. Scott Long and Jeremy Freese
|
||||
www.indiana.edu/~jslsoc/spost.htm
|
||||
spostsup@@indiana.edu
|
||||
|
87
Modules/ado/plus/_/_pesum.ado
Normal file
87
Modules/ado/plus/_/_pesum.ado
Normal file
@ -0,0 +1,87 @@
|
||||
*! version 1.6.2 1/6/01
|
||||
|
||||
capture program drop _pesum
|
||||
program define _pesum, rclass
|
||||
version 6.0
|
||||
tempvar b tmp
|
||||
syntax [fweight aweight pweight] [if] [in][,Median Dummy Two]
|
||||
|
||||
* get weight info - if weight not specified in _pesum looks to see if estimation
|
||||
* was done with weights anyway - this way one can either have _pesum handle the
|
||||
* weights or let _pesum do it
|
||||
|
||||
local wtis "[`weight'`exp']"
|
||||
if "`wtis'"=="" & "`e(wtype)'"!="" {
|
||||
local weight "`e(wtype)'"
|
||||
local wtis "[`e(wtype)'`e(wexp)']"
|
||||
}
|
||||
if "`weight'"=="pweight" {
|
||||
local wtis "[aweight`e(wexp)']"
|
||||
di in r "Warning: " in g "pweights are being treated as aweights to compute SDs"
|
||||
}
|
||||
if "`weight'"=="iweight" {
|
||||
di in r "Error: command is incompatible with iweights."
|
||||
exit
|
||||
}
|
||||
|
||||
* get names of variables
|
||||
_perhs
|
||||
local nvars = `r(nrhs)' + 1
|
||||
local varnms "`e(depvar)' `r(rhsnms)'"
|
||||
* get variables in 2nd eq for zip and zinb
|
||||
if "`two'"=="two" {
|
||||
local nvars = `r(nrhs2)' + 1
|
||||
local varnms "`e(depvar)' `r(rhsnms2)'"
|
||||
}
|
||||
* intreg has two lhs vars; select only 1st one
|
||||
if "`e(cmd)'"=="intreg" {
|
||||
local nmtoget : word 1 of `varnms'
|
||||
local varnms "`nmtoget' `r(rhsnms)'"
|
||||
}
|
||||
|
||||
* Matrices for results
|
||||
tempname Smean Ssd Smin Smax Sdummy Smedian SN
|
||||
mat `Smean' = J(1,`nvars',-99999)
|
||||
mat colnames `Smean' = `varnms'
|
||||
mat `Ssd' = `Smean'
|
||||
mat `Smin' = `Smean'
|
||||
mat `Smax' = `Smean'
|
||||
mat `Sdummy' = `Smean'
|
||||
mat `Smedian' = `Smean'
|
||||
|
||||
* loop through variables
|
||||
local i = 1
|
||||
while `i'<=`nvars' {
|
||||
local nmtoget : word `i' of `varnms'
|
||||
quietly sum `nmtoget' `wtis' `if' `in'
|
||||
scalar `SN' = r(N)
|
||||
if `SN' == 0 {
|
||||
* selection criteria left no observations
|
||||
return scalar SN = `SN'
|
||||
exit
|
||||
}
|
||||
mat `Smean'[1,`i'] = r(mean)
|
||||
mat `Ssd'[1,`i'] = sqrt(r(Var))
|
||||
mat `Smin'[1,`i'] = r(min)
|
||||
mat `Smax'[1,`i'] = r(max)
|
||||
if "`dummy'"=="dummy" {
|
||||
* doesn't need weights. Won't change if is dummy
|
||||
_pedum `nmtoget' `if' `in'
|
||||
mat `Sdummy'[1,`i'] = r(dummy)
|
||||
}
|
||||
if "`median'"=="median" {
|
||||
quietly _pctile `nmtoget' `if' `in' `wtis'
|
||||
mat `Smedian'[1,`i'] = r(r1)
|
||||
}
|
||||
local i=`i'+1
|
||||
}
|
||||
|
||||
return matrix Smean `Smean'
|
||||
return matrix Ssd `Ssd'
|
||||
return matrix Smin `Smin'
|
||||
return matrix Smax `Smax'
|
||||
return matrix Sdummy `Sdummy'
|
||||
return matrix Smedian `Smedian'
|
||||
return scalar SN = `SN'
|
||||
|
||||
end
|
66
Modules/ado/plus/_/_pesum.hlp
Normal file
66
Modules/ado/plus/_/_pesum.hlp
Normal file
@ -0,0 +1,66 @@
|
||||
.-
|
||||
help for ^_pesum^ - 1.6.1 - 1/20/00
|
||||
.-
|
||||
|
||||
Utility to get descriptive statistics for variables in a regression model
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
^_pesum^ [^if^ exp] [^in^ range] [,^m^edian ^d^ummy ^t^wo]
|
||||
|
||||
|
||||
Description
|
||||
-----------
|
||||
|
||||
^_pesum^ gets the mean, standard deviation, minimum and maximum for the
|
||||
variables in a regression. Optionally, it determines the medians and
|
||||
whether the variables are 0|1|. . Matrices are returned with the first
|
||||
column containing statistics for the dependent variables, with the
|
||||
remaining columns containing information for the independent variables.
|
||||
|
||||
Note: ^_pesum^ assumes that ^_peife^ has been applied to the ^if^
|
||||
condition.
|
||||
|
||||
Options
|
||||
-------
|
||||
|
||||
^median^ Return ^r(Smedian)^ with medians for the variables.
|
||||
|
||||
^dummy^ Return ^r(Sdummy)^ with medians for the variables.
|
||||
|
||||
^two^ Return summary statistics for the second equation in for ^zip^ and
|
||||
^zinb^ models.
|
||||
|
||||
Returned Results
|
||||
----------------
|
||||
|
||||
Matrices: Colums are in the order dependent variable, independent variables.
|
||||
|
||||
^r(Smean)^ - means
|
||||
^r(Ssd)^ - standard deviations
|
||||
^r(Smin)^ & ^r(Smax)^ - minimums and maximums
|
||||
^r(Sdummy)^ - 1 if dummy variable, else 0; if ^dummy^ option
|
||||
is specified.
|
||||
^r(Smedian)^ - medians if ^median^ option is specified.
|
||||
|
||||
Scalars: ^r(SN) - sample size
|
||||
|
||||
Example with a Program
|
||||
-----------------------
|
||||
|
||||
if "`opt'"=="mean"|"`opt'"=="min"|"`opt'"=="max" {
|
||||
_pesum `if' `in',`all'
|
||||
mat pe_base = r(S`opt')
|
||||
}
|
||||
else if "`opt'"=="median" {
|
||||
_pesum `if' `in',median `all'
|
||||
mat pe_base = r(Smedian)
|
||||
}
|
||||
|
||||
Also see
|
||||
--------
|
||||
On-line: help for ^_peife^, ^_perhs^
|
||||
|
||||
.-
|
||||
Authors: J. Scott Long and Jeremy Freese
|
||||
www.indiana.edu/~jslsoc/spost.htm
|
||||
spostsup@@indiana.edu
|
79
Modules/ado/plus/_/_petrap.ado
Normal file
79
Modules/ado/plus/_/_petrap.ado
Normal file
@ -0,0 +1,79 @@
|
||||
*! 1.0.0 - 28 jun 2006
|
||||
|
||||
/*
|
||||
|
||||
cmd() - name of command that should appear in error message
|
||||
|
||||
force - ignore errors
|
||||
|
||||
robust - error if robust standard errors used
|
||||
cluster - error if cluster() specified
|
||||
weight - error if pweight, aweight, iweight specified
|
||||
pweight - error if pweight specified
|
||||
aweight - error if aweight specified
|
||||
iweight - error if iweight specified
|
||||
|
||||
*/
|
||||
|
||||
program define _petrap
|
||||
|
||||
version 9
|
||||
|
||||
syntax [, cmd(string) Robust Cluster Weight PWeight AWeight IWeight SVY FORCE]
|
||||
|
||||
if "`cmd'" == "" {
|
||||
local cmd "program"
|
||||
}
|
||||
|
||||
local vcetype = e(vcetype)
|
||||
local clustvar = e(clustvar)
|
||||
local wtype = e(wtype)
|
||||
local ecmd = e(cmd)
|
||||
local epredict = e(predict)
|
||||
|
||||
|
||||
* trap svy - possibly overkill for detecting if svy estimation performed but manuals obscure
|
||||
* about info provided under version control
|
||||
if ("`svyest'" == "svy_est" | substr("`ecmd'", 1, 3) == "svy" | substr("`epredict'", 1, 3) == "svy") ///
|
||||
& "`svy'" != "" & "`force'" == "" {
|
||||
di as err "`cmd' does not work with svy commands"
|
||||
}
|
||||
|
||||
* trap robust
|
||||
if "`vcetype'" == "Robust" & "`robust'" != "" & "`force'" == "" {
|
||||
di as err "`cmd' does not work with robust vcetype"
|
||||
exit 999
|
||||
}
|
||||
|
||||
* trap cluster
|
||||
if ("`clustvar'" != "." & "`clustvar'" != "") & "`cluster'" != "" & "`force'" == "" {
|
||||
di as err "`cmd' does not work if cluster() specified"
|
||||
exit 999
|
||||
}
|
||||
|
||||
* trap pweight, iweight, aweight
|
||||
if ("`wtype'" == "pweight" | "`wtype'" == "aweight" | "`wtype'" == "iweight") & "`weight'" != "" & "`force'" == "" {
|
||||
di as err "`cmd' does not work if `wtype' specified"
|
||||
exit 999
|
||||
}
|
||||
|
||||
* trap pweight
|
||||
if ("`wtype'" == "pweight") & "`pweight'" != "" & "`force'" == "" {
|
||||
di as err "`cmd' does not work if `wtype' specified"
|
||||
exit 999
|
||||
}
|
||||
|
||||
* trap aweight
|
||||
if ("`wtype'" == "aweight") & "`aweight'" != "" & "`force'" == "" {
|
||||
di as err "`cmd' does not work if `wtype' specified"
|
||||
exit 999
|
||||
}
|
||||
|
||||
* trap iweight
|
||||
if ("`wtype'" == "iweight") & "`iweight'" != "" & "`force'" == "" {
|
||||
di as err "`cmd' does not work if `wtype' specified"
|
||||
exit 999
|
||||
}
|
||||
|
||||
|
||||
end
|
42
Modules/ado/plus/_/_petrap.hlp
Normal file
42
Modules/ado/plus/_/_petrap.hlp
Normal file
@ -0,0 +1,42 @@
|
||||
.-
|
||||
help for ^_petrap^ - 28 Jun 2006
|
||||
.-
|
||||
|
||||
Check to see if model estimation incompatible with postestimation command
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
^_petrap^ [,^cmd^(string) ^svy^ ^r^obust ^c^luster ^w^eight ^pw^eight
|
||||
^iw^eight ^aw^eight ^force^ ]
|
||||
|
||||
|
||||
Description
|
||||
-----------
|
||||
|
||||
^_petrap^ produces an error message if the previous model estimated uses
|
||||
any of the specified options. It is intended to use for trapping
|
||||
errors in models incompatible with estimation command.
|
||||
|
||||
Options
|
||||
-------
|
||||
|
||||
^cmd()^ name of command that should appear in error message. if not
|
||||
specified, then "program" is used
|
||||
|
||||
^force^ will ignore conditions, and thus return with no errors
|
||||
|
||||
^robust^ exits with error if robust standard errors used in estimated model
|
||||
|
||||
^cluster^ exits with error if cluster() specified
|
||||
|
||||
^weight^ exits with error if pweight, aweight, or iweight specified
|
||||
|
||||
^pweight^ exits with error if pweight specified
|
||||
|
||||
^aweight^ exits with error if aweight specified
|
||||
|
||||
^iweight^ exits with error if iweight specified
|
||||
|
||||
.-
|
||||
Authors: J. Scott Long and Jeremy Freese
|
||||
www.indiana.edu/~jslsoc/spost.htm
|
||||
spostsup@@indiana.edu
|
22
Modules/ado/plus/_/_peunvec.ado
Normal file
22
Modules/ado/plus/_/_peunvec.ado
Normal file
@ -0,0 +1,22 @@
|
||||
*! version 1.6.1 1/18/03
|
||||
|
||||
capture program drop _peunvec
|
||||
program define _peunvec, rclass
|
||||
version 7
|
||||
tempname oldbeta newbeta newrow
|
||||
mat `oldbeta' = e(b)
|
||||
_perhs
|
||||
local cols = `r(nrhs)'+1
|
||||
local rows = colsof(`oldbeta')/`cols'
|
||||
local i = 1
|
||||
while `i' <= `rows' {
|
||||
local start = ((`i'-1) * `cols') + 1
|
||||
local end = `start' + `cols' - 1
|
||||
mat `newrow' = `oldbeta'[1, `start'..`end']
|
||||
mat `newbeta' = nullmat(`newbeta') \ `newrow'
|
||||
local i = `i' + 1
|
||||
}
|
||||
mat coleq `newbeta' = _
|
||||
mat list `newbeta'
|
||||
return matrix b `newbeta'
|
||||
end
|
17
Modules/ado/plus/_/_peunvec.hlp
Normal file
17
Modules/ado/plus/_/_peunvec.hlp
Normal file
@ -0,0 +1,17 @@
|
||||
.-
|
||||
help for ^_peunvec^
|
||||
.-
|
||||
|
||||
^_peunvec^
|
||||
|
||||
^_peunvec^ takes regression estimates that are kept as a vector (like ^mlogit^
|
||||
results in Stata 6) and transforms them to a matrix (like ^mlogit^ results in
|
||||
Stata 5).
|
||||
|
||||
The input is taken from e(b), and the new matrix is saved as r(b).
|
||||
|
||||
.-
|
||||
Authors: J. Scott Long and Jeremy Freese
|
||||
www.indiana.edu/~jslsoc/spost.htm
|
||||
spostsup@@indiana.edu
|
||||
|
18
Modules/ado/plus/_/_pexstring.ado
Normal file
18
Modules/ado/plus/_/_pexstring.ado
Normal file
@ -0,0 +1,18 @@
|
||||
*! version 0.2.1 2005Jun20 - fix for long string jf
|
||||
* version 0.2.0 2005-02-03
|
||||
|
||||
// change values in PE_in to string x(a=1 b=2..)
|
||||
|
||||
capture program drop _pexstring
|
||||
program _pexstring, rclass
|
||||
version 8
|
||||
local cols = colsof(PE_in)
|
||||
local xnames : colnames PE_in
|
||||
local xis ""
|
||||
foreach c of numlist 1/`cols' {
|
||||
local xnm : word `c' of `xnames'
|
||||
local xval = PE_in[1,`c']
|
||||
local xis "`xis' `xnm'=`xval'"
|
||||
}
|
||||
return local xis "`xis'"
|
||||
end
|
Reference in New Issue
Block a user