Setup initial file structure

This commit is contained in:
2024-03-05 11:20:30 +01:00
parent 6183a6391b
commit 70e53e7760
708 changed files with 277486 additions and 0 deletions

View 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

View File

@ -0,0 +1 @@
.h eststo

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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}

View 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

View 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}

View 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

View 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

View 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

View 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}

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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}

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View File

@ -0,0 +1,77 @@
*! Dan Blanchette 1.1 dan.blanchette@duke.edu 08Feb2005
*! Center of Entrepreneurship and Innovation Duke University's Fuqua School of Business
** research computing, unc-ch
* Stata fixed Linux problem of not being able to edit a file in a directory starting with "~"
* Added "Caution" note when requesting to edit a Stata ado file.
** Dan Blanchette 1.0.3 06 Nov 2003 made it work in Linux
*! NJC 1.0.2 31 Dec 2002
** CFB 1.0.1 26Dec2002 cloned from adotype
* NJC 1.2.1 1 December 1999
** NJC 1.2.2 6 December 1999
* NJC 1.2.3 14 December 1999
** AJM 1.3 5 August 2009 (Eliminated file size check for versions 11 and greater.)
program def adoedit, rclass
version 8.0
if "`1'" == "" | "`2'" != "" {
di as err "incorrect syntax"
exit 198
}
args cmd
/* ends with .ado */
if substr(`"`cmd'"',length(`"`cmd'"')-3,length(`"`cmd'"'))==".ado" {
local filen `"`cmd'"'
}
else {
local filen `"`cmd'.ado"'
}
findfile `filen'
// here will exit 601 if `cmd' not found
local file `"`r(fn)'"'
local tfile : subinstr local file "\" "/" , all
if index(`"`tfile'"',"/ado/base") | index(`"`tfile'"',"/ado/updates") {
di " "
di as err "Caution, you are requesting to edit an ado file provided by Stata."
di as err "If this is really what you want, consider first copying the file to {input}`: sysdir PERSONAL' {error}."
di " "
more
di " "
}
capture hexdump `"`file'"', analyze
local size = r(filesize)
* Eliminates file size check for versions 11 and greater.
if c(stata_version) < 11 {
if `size' < 32000 & `size' > 0 {
doedit `"`file'"'
discard
return local adofile `"`file'"'
exit 0
}
else {
di as txt _n "Sorry, files larger than 32,000 bytes cannot be do-edited."
di as txt "You must use another text editor for this file."
error 603
}
}
else if c(stata_version) >= 11 {
doedit `"`file'"'
discard
return local adofile `"`file'"'
exit 0
}
end

View File

@ -0,0 +1,87 @@
{smcl}
{* 5Aug2009}{...}
{* 17Jan2008}{...}
{* 06Nov2003}{...}
{hline}
help for {hi:adoedit} {right:manual: {hi:[R] none}}
{right:dialog: {hi: none} }
{hline}
{title:Edit ado-file in Stata's do-file editor}
{p 8 17 2}{cmd:adoedit}
{it:cmdname}
{p_end}
{title:Description}
{p 4 4 2}
{cmd:adoedit} attempts to edit whichever ado-file named {cmd:cmdname.ado} is first on the
current {help adopath:adopath}. In Stata Version 10 and earlier, one cannot edit files
larger than 32,000 bytes in the do-file editor, nor can one edit built-in commands.
The file size limitation is removed for Stata Version 11 and later.
{p 4 4 2}
This routine is designed for use by those who are writing their own ado-files;
users are exhorted to make a copy of any official ado-files, and to save modified official
ado-files in the ado directory, rather than the Stata directory.{p_end}
{p 4 4 2}
{cmd:adoedit cmdname.ado}
is tolerated.{p_end}
{p 4 4 2}
After a successful edit, the {cmd:discard} command is issued so that Stata will load the
revised ado-file. This will also clear any estimates from the last estimation.{p_end}
{p 4 4 2}
If you wish to edit ado-files with a different text editor, please see
{stata findit fedit:fedit} and the {stata findit texteditors:texteditors} modules on the
{help ssc:SSC} archive.{p_end}
{title:Examples}
{p 4 8 2}{cmd:. adoedit tsspell}{p_end}
{title:Saved Results}
{p 4 8 2}
The {cmd:adoedit} command saves in {cmd:r()}:{p_end}
{synoptset 20 tabbed}{...}
{p2col 5 20 24 2: Macros}{p_end}
{synopt:{cmd:r(adofile)}}the ado-file name and full path{p_end}
{title:Author}
{p 4 4 2}
Dan Blanchette {break}
Center of Entrepreneurship and Innovation {break}
Duke University's Fuqua School of Business {break}
Dan.Blanchette@Duke.edu{p_end}
{title:Acknowledgements}
{p 4 4 2}
This program grew out of previous work by:{p_end}
{p 6 4 2}
Nicholas J. Cox, University of Durham, U.K.{p_end}
{p 4 4 2}
and{p_end}
{p 6 4 2}
Christopher F Baum, Boston College, USA{p_end}
{title:Also see}
{p 4 13 2}On-line: {help doedit:doedit} {help fildfile:findfile},
{help fedit:fedit} (if installed){p_end}

View File

@ -0,0 +1,23 @@
program def alphlist, rclass
*! NJC 1.1.1 28 June 2001
* NJC 1.1.0 6 June 2000
* NJC 1.0.0 27 Jan 2000
version 6.0
syntax , [ Capitals Underscore Global(str) Noisily ]
if length("`global'") > 8 {
di in r "global name must be <=8 characters"
exit 198
}
if "`capitals'" != "" {
local newlist "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
}
else local newlist "a b c d e f g h i j k l m n o p q r s t u v w x y z"
if "`underscore'" != "" { local newlist "`newlist' _" }
if "`noisily'" != "" { di "`newlist'" }
if "`global'" != "" { global `global' "`newlist'" }
return local list `newlist'
end

View File

@ -0,0 +1,2 @@
.h listutil

View File

@ -0,0 +1,48 @@
*! version 1 27may2007
*! Jean-Benoit Hardouin
*
************************************************************************************************************
* Stata program : anaoption
*
* Historic
* Version 1 (2007-05-27): Jean-Benoit Hardouin
*
* Jean-benoit Hardouin, phD, Assistant Professor
* Team of Biostatistics, Clinical Research and Subjective Measures in Health Sciences
* University of Nantes - Faculty of Pharmaceutical Sciences
* France
* jean-benoit.hardouin@anaqol.org
*
* News about this program :http://www.anaqol.org
* FreeIRT Project website : http://www.freeirt.org
*
* Copyright 2007 Jean-Benoit Hardouin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
************************************************************************************************************
program define anaoption ,rclas
version 7.0
syntax [, DETails minvi(real .03) siglevel(real 0.05) minsize(real 0)]
return scalar minvi=`minvi'
return scalar siglevel=`siglevel'
return scalar minsize=`minsize'
return local details `details'
end

View File

@ -0,0 +1,697 @@
*! 0.3.2 2009-03-14 | long freese | fix mean calculation
** predictions for asmprobit
capture program drop asprvalue
capture program drop _Prvasmp
program define asprvalue, rclass
version 9
if e(cmd) == "asmprobit" {
_Prvasmp `0' // local program just for asmprobit -- see below
return add
exit
}
preserve
syntax [, x(passthru) Cat(string) Base(string) Save Diff Rest(string) BRief Full]
* need to sort to calculate means below so accurate for unbalanced panels
local idvar "`e(group)'"
tempname recordnum
sort `idvar', stable
by `idvar' : gen `recordnum' = _n
tempname b
mat `b' = e(b)
local allnames : colnames `b'
* check if there are any interactions, so that otherwise warning message can be specified
local anyX "no"
* make list of case-specific variables
foreach var of local allnames {
local i = index("`var'", "X")
if `i' != 0 {
local anyX "yes"
* is it an alternativeXcase-specific-variable interaction?
local temppreX = substr("`var'", 1, `i'-1)
local temppostX = substr("`var'", `i'+1, .)
* assume that preX are all cats -- TO DO: insert check?
local catslist "`catslist' `temppreX'"
local isAxA "no"
foreach var2 of local allnames {
if "`temppostX'"=="`var2'" | "`temppostX'"=="`base'" {
local isAxA "yes"
local aXalist "`aXalist' `var'"
}
}
* is it an alternativeXalternative interaction?
if "`isAxA'" == "no" {
local aXclist "`aXclist' `var'"
local csvlist "`csvlist' `temppostX'"
}
}
}
* if cat is specified, that is the list of categories
if "`cat'"!= "" {
local catslist = "`cat'"
}
* make sure either cat() or interactions specified
if "`cat'"=="" & "`anyX'"=="no" {
di as err "cat() must be specified if no interactions in model"
error 999
}
local catslist : list uniq catslist
local ncatsmin1 : word count `catslist'
local numcats = `ncatsmin1' + 1
local csvlist : list uniq csvlist
local asvlist : list allnames - aXclist
local asvlist : list asvlist - catslist
local asvlist : list asvlist - aXalist
/*
di "altXasv interactions: `aXalist'"
di "altXcase interactions: `aXclist'"
di "alternatives: `catslist'"
di "altspec vars: `asvlist'"
di "casespec vars: `csvlist'"
di "number of alternatives `ncatsmin1'"
*/
* decode x() values
tokenize `x', parse("()")
local x "`3'"
local x : subinstr local x "=" " ", all
tokenize `x', parse(" ")
while "`1'" != "" & "`2'" != "" {
capture confirm number `3'
if _rc == 0 {
* TO DO: check that `1' is alternative-specific variables
forvalues i = 1(1)`numcats' {
local iplus1 = `i' + 1
confirm number ``iplus1''
local V`1'V`i' = ``iplus1''
}
macro shift `iplus1'
}
else {
local V`1' = `2'
macro shift 2
}
}
* HANDLE REST OPTION
* rest() = mean by default
if "`rest'" == "" {
local rest "mean"
}
*check that rest option includes an allowable option
if "`rest'" != "mean" & "`rest'" != "asmean" {
di as err "rest(`rest') not allowed"
error 999
}
foreach var of local csvlist {
if "`V`var''" == "" {
if "`rest'" == "mean" | "`rest'" == "asmean" {
* qui su `var' if e(sample) == 1 & `recordnum' == 1
qui su `var' if e(sample) == 1 // FIX TO MEAN CALCULATION 9/2012
local V`var' = r(mean)
}
}
}
foreach var of local asvlist {
if "`V`var''" == "" & "`V`var'V1'" == "" {
if "`rest'" == "mean" {
* qui su `var' if e(sample) == 1 & `recordnum' == 1
qui su `var' if e(sample) == 1 // FIX TO MEAN CALCULATION 9/2012
forvalues i = 1(1)`numcats' {
local V`var'V`i' = r(mean)
}
}
if "`rest'" == "asmean" {
tempname refdum
* this variable will equal 1 for only those cases that indicate base
qui gen `refdum' = 1 if e(sample) == 1
local i = 1
foreach catname of local catslist {
qui su `var' if `catname' == 1 & e(sample) == 1
local V`var'V`i' = r(mean)
* turn off refdum for variables indicating category
qui replace `refdum' = 0 if `catname' == 1 & e(sample) == 1
local i = `i' + 1
}
* use refdum to get mean for reference category
qui su `var' if `refdum' == 1 & e(sample) == 1
local V`var'V`i' = r(mean)
}
}
}
* add observations to bottom of dataset
local N = _N
local firstobs = `N' + 1 // firstobs is the reference number of the first added obs
local lastobs = `firstobs'+`ncatsmin1'
qui set obs `lastobs'
capture drop _addedobs
qui gen _addedobs = 1 if _n >= `firstobs'
* find unique value for new observations
local unique "no"
local i = 1234567
while "`unique'" == "no" {
qui count if `idvar' == `i'
if r(N) == 0 {
local unique "yes"
}
local i = `i' + 1234567
}
qui replace `idvar' = `i' in `firstobs'/`lastobs'
qui replace `idvar' = `i' in `firstobs'/`lastobs'
foreach cat of local catslist {
local obs = `firstobs'
foreach cat2 of local catslist {
* set dummy variables indicating which row is which alternative
qui replace `cat' = 1 in `obs' if "`cat'" == "`cat2'"
qui replace `cat' = 0 in `obs' if "`cat'" != "`cat2'"
* set values for aXc interactions
foreach csvar of local csvlist {
qui replace `cat'X`csvar' = `V`csvar'' in `obs' if "`cat'" == "`cat2'"
qui replace `cat'X`csvar' = 0 in `obs' if "`cat'" != "`cat2'"
}
local obs = `obs' + 1
}
* set all alternative dummies to zero for row indicating reference category
qui replace `cat' = 0 in `obs'
* set all aXc to zero for row corresponding to reference category
foreach csvar of local csvlist {
qui replace `cat'X`csvar' = 0 in `obs'
}
}
* set values for alternative-specific variables
foreach alt of local asvlist {
if "`V`alt''" != "" {
qui replace `alt' = `V`alt'' in `firstobs'/`lastobs'
}
else {
forvalues i = 1(1)`numcats' {
local obs = `firstobs' + `i' - 1
qui replace `alt' = `V`alt'V`i'' in `obs'
}
}
}
* set values for aXa interactions
foreach var of local aXalist {
local i = index("`var'", "X")
local temppreX = substr("`var'", 1, `i'-1)
local temppostX = substr("`var'", `i'+1, .)
qui replace `var' = `temppreX'*`temppostX' if _addedobs == 1
}
* generate predicted probabilities
tempname prob
* 5/26/06 WORKAROUND FOR STATA BUG (?!) WHERE PREDICT RESORTS DATA
tempname order
gen `order' = _n
qui predict `prob' if _addedobs == 1
sort `order'
* reference category name
if "`base'" == "" {
local base = "base"
}
* DISPLAY RESULTS
* heading
local ecmd "`e(cmd)'"
local edepvar "`e(depvar)'"
di _n as res "`ecmd'" as txt ": Predictions for " as res "`edepvar'"
* display predicted probabilities
local obs = `firstobs'
capture mat drop asprvres
tempname tmpprob
foreach cat of local catslist {
sca `tmpprob' = `prob'[`obs']
mat asprvres = (nullmat(asprvres) \ `tmpprob')
local obs = `obs' + 1
}
sca `tmpprob' = `prob'[`obs']
mat asprvres = (nullmat(asprvres) \ `tmpprob')
mat rownames asprvres = `catslist' `base'
mat colnames asprvres = prob
if "`diff'" != "" {
mat changesav = asprvres - _ASPRVsav
tempname display
mat `display' = (asprvres, _ASPRVsav, changesav)
mat colnames `display' = Current Saved Diff
mat list `display', noh
}
else {
mat list asprvres, noh
}
* display base values for case-specific variables
if "`csvlist'" != "" {
capture mat drop csvals
foreach var of local csvlist {
mat csvals = (nullmat(csvals) , `V`var'')
}
mat colnames csvals = `csvlist'
mat rownames csvals = x=
if "`brief'" == "" {
di _n as txt "case-specific variables"
if "`diff'" != "" {
mat changecsv = csvals - _ASPRVcsv
tempname displaycsv
mat `displaycsv' = (csvals \ _ASPRVcsv \ changecsv)
mat rownames `displaycsv' = Current Saved Diff
mat list `displaycsv', noh
}
else {
mat list csvals, noh
}
}
}
* display base values for alternative-specific variables
if "`asvlist'" != "" {
capture mat drop asvals
foreach alt of local asvlist {
capture mat drop _tmp
if "`V`alt''" != "" {
mat _tmp = J(1, `numcats', `V`alt'')
}
else {
forvalues i = 1(1)`numcats' {
mat _tmp = (nullmat(_tmp) , `V`alt'V`i'')
}
}
mat asvals = (nullmat(asvals) \ _tmp)
}
mat rownames asvals = `asvlist'
mat colnames asvals = `catslist' `base'
if "`brief'" == "" {
di _n as txt "alternative-specific variables"
if "`diff'" != "" {
tempname curasv
mat `curasv' = asvals
tempname savedasv
mat `savedasv' = _ASPRVasv
mat changeasv = asvals - `savedasv'
mat roweq `curasv' = Current
mat roweq `savedasv' = Saved
mat roweq changeasv = Dif
tempname displayasv
mat `displayasv' = (`curasv' \ `savedasv' \ changeasv)
mat list `displayasv', noh
}
else {
mat list asvals, noh
}
}
}
* display all added observations and values if desired
if "`full'" != "" {
list `allnames' if _addedobs == 1, noobs
}
if "`save'" != "" {
mat _ASPRVsav = asprvres
if "`csvlist'" != "" {
mat _ASPRVcsv = csvals
}
if "`asvlist'" != "" {
mat _ASPRVasv = asvals
}
}
* return results
if "`diff'" != "" { //! added bj 24jul2008
capture return matrix p = changesav, copy //!
capture return matrix csv = changecsv, copy //!
capture return matrix asv = changeasv, copy //!
} //!
else { //!
capture return matrix p = asprvres, copy
capture return matrix csv = csvals, copy
capture return matrix asv = asvals, copy
} //!
restore
end
program define _Prvasmp, rclass
version 9
preserve
syntax [, x(passthru) Cat(string) Base(string) Save Diff Rest(string) BRief Full]
if "`cat'" != "" {
di as err "(note: cat() ignored when using asprvalue with asmprobit)"
}
if "`base'" != "" {
di as err "(note: base() ignored when using asprvalue with asmprobit)"
}
local altvar = e(altvar)
local numcats = e(k_alt)
local asvlist "`e(indvars)'"
local csvlist "`e(ind2vars)'"
if "`csvlist'" == "" {
local csvlist "`e(casevars)'"
}
local idvar "`e(casevar)'"
if "`idvar'" == "" {
local idvar "`e(case)'"
}
* need to sort to calculate means below so accurate for unbalanced panels
tempname recordnum
sort `idvar', stable
by `idvar' : gen `recordnum' = _n
* add values to bottom of dataset
local N = _N
local firstobs = `N' + 1
local lastobs = `firstobs' + `numcats' - 1
qui set obs `lastobs'
capture drop _addedobs
qui gen _addedobs = 1 if _n >= `firstobs'
* find unique value for new observations
local unique "no"
local i = 1234567
while "`unique'" == "no" {
qui count if `idvar' == `i'
if r(N) == 0 {
local unique "yes"
}
local i = `i' + 1234567
}
qui replace `idvar' = `i' in `firstobs'/`lastobs'
* write values for alternative variable with values of alternatives
_pecats `altvar' if e(sample)
local catvals "`r(catvals)'"
forvalues i = 1(1)`numcats' {
local cat`i' : word `i' of `catvals'
local catslist "`catslist' `e(alt`i')'"
local obsnum = `firstobs' + `i' - 1
qui replace `altvar' = `cat`i'' in `obsnum'
}
* decode x() values
tokenize `x', parse("()")
local x "`3'"
local x : subinstr local x "=" " ", all
tokenize `x', parse(" ")
while "`1'" != "" & "`2'" != "" {
* if `3' exists and is a number, alternative-specific variables being specified
capture confirm number `3'
if _rc == 0 {
* TO DO: check that `1' is alternative-specific variable
forvalues i = 1(1)`numcats' {
local iplus1 = `i' + 1
confirm number ``iplus1''
local V`1'V`i' = ``iplus1''
}
macro shift `iplus1'
}
else {
local V`1' = `2'
macro shift 2
}
}
* HANDLE REST OPTION
* rest() = mean by default
if "`rest'" == "" {
local rest "mean"
}
*check that rest option includes an allowable option
if "`rest'" != "mean" & "`rest'" != "asmean" {
di as err "rest(`rest') not allowed"
error 999
}
foreach var of local csvlist {
if "`V`var''" == "" {
if "`rest'" == "mean" | "`rest'" == "asmean" {
* qui su `var' if e(sample) == 1 & `recordnum' == 1
qui su `var' if e(sample) == 1 // FIX TO MEAN CALCULATION 9/2012
local V`var' = r(mean)
}
}
}
foreach var of local asvlist {
if "`V`var''" == "" & "`V`var'V1'" == "" {
if "`rest'" == "mean" {
* qui su `var' if e(sample) == 1 & `recordnum' == 1
qui su `var' if e(sample) == 1 // FIX TO MEAN CALCULATION 9/2012
forvalues i = 1(1)`numcats' {
local V`var'V`i' = r(mean)
}
}
if "`rest'" == "asmean" {
forvalues i = 1(1)`numcats' {
qui su `var' if `altvar' == `cat`i'' & e(sample) == 1
local V`var'V`i' = r(mean)
}
}
}
}
* set values for alternative-specific variables
foreach alt of local asvlist {
if "`V`alt''" != "" {
qui replace `alt' = `V`alt'' in `firstobs'/`lastobs'
}
else {
forvalues i = 1(1)`numcats' {
local obs = `firstobs' + `i' - 1
qui replace `alt' = `V`alt'V`i'' in `obs'
}
}
}
* set values for case-specific variables
foreach var of local csvlist {
qui replace `var' = `V`var'' in `firstobs'/`lastobs'
}
* generate predicted probabilities
tempname prob
** 5/26/06 WORKAROUND FOR STATA BUG (?!) WHERE PREDICT RESORTS DATA
tempname order
gen `order' = _n
* 2008-06-15 list `order' if _addedobs == 1
qui predict `prob' if _addedobs == 1
sort `order'
* DISPLAY RESULTS -- whole routine almost the same as asprvalue but not quite
* heading
local ecmd "`e(cmd)'"
local edepvar "`e(depvar)'"
di _n as res "`ecmd'" as txt ": Predictions for " as res "`edepvar'"
* display predicted probabilities
local obs = `firstobs'
capture mat drop asprvres
tempname tmpprob
foreach cat of local catslist {
sca `tmpprob' = `prob'[`obs']
mat asprvres = (nullmat(asprvres) \ `tmpprob')
local obs = `obs' + 1
}
mat rownames asprvres = `catslist'
mat colnames asprvres = prob
if "`diff'" != "" {
mat changesav = asprvres - _ASPRVsav
tempname display
mat `display' = (asprvres, _ASPRVsav, changesav)
mat colnames `display' = Current Saved Diff
mat list `display', noh
}
else {
mat list asprvres, noh
}
* display base values for case-specific variables
if "`csvlist'" != "" {
capture mat drop csvals
foreach var of local csvlist {
mat csvals = (nullmat(csvals) , `V`var'')
}
mat colnames csvals = `csvlist'
mat rownames csvals = x=
if "`brief'" == "" {
di _n as txt "case-specific variables"
if "`diff'" != "" {
mat changecsv = csvals - _ASPRVcsv
tempname displaycsv
mat `displaycsv' = (csvals \ _ASPRVcsv \ changecsv)
mat rownames `displaycsv' = Current Saved Diff
mat list `displaycsv', noh
}
else {
mat list csvals, noh
}
}
}
* display base values for alternative-specific variables
if "`asvlist'" != "" {
capture mat drop asvals
foreach alt of local asvlist {
capture mat drop _tmp
if "`V`alt''" != "" {
mat _tmp = J(1, `numcats', `V`alt'')
}
else {
forvalues i = 1(1)`numcats' {
mat _tmp = (nullmat(_tmp) , `V`alt'V`i'')
}
}
mat asvals = (nullmat(asvals) \ _tmp)
}
mat rownames asvals = `asvlist'
mat colnames asvals = `catslist'
if "`brief'" == "" {
di _n as txt "alternative-specific variables"
if "`diff'" != "" {
tempname curasv
mat `curasv' = asvals
tempname savedasv
mat `savedasv' = _ASPRVasv
mat changeasv = asvals - `savedasv'
mat roweq `curasv' = Current
mat roweq `savedasv' = Saved
mat roweq changeasv = Dif
tempname displayasv
mat `displayasv' = (`curasv' \ `savedasv' \ changeasv)
mat list `displayasv', noh
}
else {
mat list asvals, noh
}
}
}
* display all added observations and values if desired
if "`full'" != "" {
list `allnames' if _addedobs == 1, noobs
}
if "`save'" != "" {
mat _ASPRVsav = asprvres
if "`csvlist'" != "" {
mat _ASPRVcsv = csvals
}
if "`asvlist'" != "" {
mat _ASPRVasv = asvals
}
}
* return results
capture return matrix p = asprvres, copy
capture return matrix csv = csvals, copy
capture return matrix asv = asvals, copy
restore
end
exit
* 0.2.0 - jf - 5/26/06 - workaround for bug in Stata where predict resorts
* 0.1.9 - jf - 12/19/05 - add returns
* 0.1.8 - jf - 9/8/05 - warning but not error with cat() or base() with asmprobit
* 0.1.7 - jf - 7/24/05 - fix asmean for asmprobit bug
* 0.1.6 - jf - 7/19/05 - add heading to output
* 0.1.5 - jf - 7/18/05 - bug fix
* 0.1.4 - jf - 7/15/05 - add asmprobit (kludgy) - jf
* 0.1.3 - jf - 7/15/05 - fix to allow = in x()
* 0.1.2 - jf - 7/11/05 - bug fix
* 0.1.1 - jf - 6/15/05 - change refcat() to base()
* 0.1.0 - jf - 6/11/05
* 0.2.1 - jf - 7/2/07 - fix for changes in stata 7 asmprobit routine
* 0.3.0a - bj 24jul2008 for work with estout
* 0.3.0 2008-06-15 jsl
* 0.3.1 2009-03-14

View File

@ -0,0 +1,122 @@
{smcl}
{* 03Nov2005}{...}
{hline}
help for {hi:asprvalue}{right:03Nov2005}
{hline}
{title:Predicted probabilities for models with alternative-specific variables}
{p 8 15 2}{cmd:asprvalue} [{cmd:,}
{cmd:x(}{it:variables_and_values}{cmd:)}
{cmdab:r:est(}{it:stat}{cmd:)}
{cmdab:b:ase(}{it:refcatname}{cmd:)}
{cmdab:c:at(}{it:catnames}{cmd:)}
{cmdab:s:ave}
{cmdab:d:iff}
{cmdab:br:ief}
{p 4 4 2}
where {it:variables_and_values} is an alternating list of variables
and numeric values
{p 4 4 2}
{it:stat} is either mean or asmean (alternative-specific means for alternative-specific variables)
{p 4 4 2}
{cmd:asprvalue} is intended to be used to compute predicted probabilities for logit or probit models
that can combine case- and alternative-specific variables. For these models, predicted probabilities
depend on the values of the independent variables, which may or may not vary over the alternatives for
a particular case. {cmd:asprvalue} allows you to specify the values of the independent variables and
presents predicted probabilities for the different alternatives. The command presently works after
{helpb clogit}, {helpb rologit}, or {helpb asmprobit}.
{p 4 4 2}
{cmd:IMPORTANT:} For {helpb clogit} and {helpb rologit} models, case-specific variables are specified
by a set of interactions with dummy variables for the alternatives. {cmd:asprvalue} can only be used
if these interaction variables are named {it:alternative_name}X{it:case_specific_varname}. In other
words, if the dummy variables for the alternatives are named "car" and "bus" and a case-specific
variable is "male", the interactions must be named "carXmale" and "busXmale". These names for the
interactions correspond with the names used if the data have been arranged for estimation using
the command {cmd:case2choice}. A capital "X" cannot be used in the names of any of the other
variables in the model.
{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.
{p 4 8 2}
{cmd:x()} sets the values of independent variables for calculating
predicted values. For case-specific variables, the list must alternate
variable names and values. For alternative-specific variables, the list
may either be followed by a single value to be assigned to all alternatives
or J values if there are J alternatives. For {helpb clogit} or {helpb rologit}
, when J values are specified, these
are assigned to the alternatives in the order they have been specified by
{cmd:cat()} or in the estimation command, with the value to be assigned to the
reference category being last. For {helpb asmprobit}, the different alternatives are specified
using a single variable rather than a series of dummy variables, and values for
alternative-specific variables should be ordered to correspond with the ascending
values of the variable.
{p 4 8 2}
{cmd:rest()} sets the values for variables unspecified in {cmd:x()}. The default
is {it:mean}, which holds all unspecified variables to their case-specific means.
One can also specific "asmean", which holds unspecified alternative-specific
variables to their alternative-specific means. For example, if "time" was an
alternative-specific variable, {it:mean} would assign all alternatives the
mean of "time" over all individuals and alternatives, while {it:asmean} would assign
each alternative the mean of "time" for that alternative.
{p 4 8 2}
{cmd:base()} specifies the name of the base (reference) category. If this is not
specified, "base" will be used to refer to this category in the output. This option
should not be used after {helpb asmprobit}.
{p 4 8 2}
{cmd:cat()} specifies the names of the dummy variables in the model used to
indicate different alternatives (the alternative-specific intercepts). {cmd:cat()} only
needs to be specified if the model includes no case-specific variables, as otherwise
this list is inferred from the names of the interaction terms for case-specific
variables. The name of the reference category should not be included in {cmd:cat()}. This option
should not be used after {helpb asmprobit}.
{p 4 8 2}
{cmd:brief} prints only limited output.
{title:Examples}
{p 4 4 2}
{cmd:. use "http://www.stata-press.com/data/lfr/nomocc2.dta", clear}{break}
{cmd:. gen busXhinc = bus*hinc}{break}
{cmd:. gen trainXhinc = train*hinc}{break}
{cmd:. gen busXpsize = bus*psize}{break}
{cmd:. gen trainXpsize = train*psize}{break}
{cmd:. clogit choice train* bus* time invc , group(id)}{break}
{cmd:. asprvalue, x(time 600 invc 30 hinc 40 psize 0) base(car)}{break}
{p 4 4 2}
{cmd:. asprvalue, x(psize 0) base(car) save}{break}
{cmd:. asprvalue, x(psize 1) base(car) dif}{break}
{p 4 4 2}
{cmd:. asprvalue, x(psize 0) base(car) rest(asmean) save}{break}
{cmd:. asprvalue, x(psize 1) base(car) rest(asmean) dif}{break}
{p 4 4 2}
{cmd:. asprvalue, x(time 600 hinc 40 psize 1) base(car) save}{break}
{cmd:. asprvalue, x(time 700 600 600 hinc 40 psize 1) base(car) dif}{break}
{cmd:. asprvalue, x(time 600 700 600 hinc 40 psize 1) base(car) dif}{break}
{cmd:. asprvalue, x(time 600 600 700 hinc 40 psize 1) base(car) dif}{break}
{title:Authors}
Jeremy Freese and J. Scott Long
{browse www.indiana.edu/~jslsoc/spost.htm}
spostsup@indiana.edu

View File

@ -0,0 +1,220 @@
*! bagroup.ado written 8/5/1997 by PTS version 1.2.2 (STB-55: sbe33)
*! modified Bland-Altman plots for more than two measures
*! one plot per figure
*!
*! Syntax: bagroup varlist if in, rows avlab difflab <graph options> obs_c
* modified 3/8/1998 to allow for larger symbols if there are repetitions
* modified 7/8/1998 to use all possible pairs, and to allow for ylines
* modified 11/11/1998 to give n, means, sd of vars in table
* modified 2/2/2000 to accommodate xlab & ylab options & warn against xlab, ylab without options
set trace off
cap prog drop bagroup
prog define bagroup
local varlist "req ex min(3)"
local if "opt"
local in "opt"
local options "format(str) rows(int 999) XLABel(str) YLABel(str) avlab(str) difflab(str) title(str) saving(str) obs(int 2) listwise text(real 100) *"
parse "`*'"
if index("`options'","xlab") ~= 0 | index("`options'","ylab") ~= 0 {
di in red "xlabel and ylabel without values not permitted"
exit 198
}
tempvar touse
mark `touse' `if' `in'
local nvars : word count `varlist'
if "`listwise'" ~= "" {local obs = `nvars'}
cap assert `obs' >= 2 & `obs' <= `nvars'
if _rc { di in red "obs must be between 2 and the number of variables: " in ye `nvars'
exit _rc}
tempvar obs_c
qui gen `obs_c' = 0
parse "`varlist'", parse(" ")
while "`1'" ~= "" {
qui replace `obs_c' = `obs_c' + (`1' ~= .)
mac shift
}
qui replace `touse' = 0 if `obs_c' < `obs'
if "`format'" == "" { local format "%5.2f" }
tempvar av diff
qui egen `av' = rmean(`varlist') `if' `in'
qui gen `diff' = .
_table `varlist', av(`av') diff(`diff') touse(`touse') format(`format') obs_c(`obs_c')
if "`xlabel'" == "" {
nicenum xlabel = $xmin $xmax
local xlabel "xlabel($xlabel)"
global xlabel
}
else local xlabel "xlabel(`xlabel')"
if "`ylabel'" == "" {
nicenum ylabel = $rrmin $rrmax
parse "$ylabel" , parse (",")
assert "`2'" == ","
if `1' > $rrmin {
local ymin = 2*`1' - `3'
global ylabel "`ymin',$ylabel"
}
local ylabel "ylabel($ylabel)"
global ylabel
}
else local ylabel "ylabel(`ylabel')"
if "`avlab'" ~= "" { local avlab avlab("`avlab'") }
if "`difflab'" ~= "" { local difflab difflab("`difflab'") }
if "`title'" ~= "" { local title title("`title'") }
if "`saving'" ~= "" { local saving saving(`saving') }
qui replace `diff' = .
cap noi _graph `varlist', av(`av') diff (`diff') touse(`touse') rows(`rows') /*
*/ `xlabel' `ylabel' `avlab' `difflab' `saving' `title' `options' obs_c(`obs_c') text(`text')
if _rc {di in red "error in graph options"
exit _rc}
cap gph close
end bagroup
prog define _table
local varlist "req ex "
local options "av(str) diff(str) touse(str) format(str) title(str) obs_c(str)"
parse "`*'"
di _n(2) in gr "Comparisons with the average of the other measures"
di _n in gr "Variable | Obs Mean SD Difference Reference Range "
di in gr "---------+----------------------------------------------------------"
parse "`varlist'", parse(" ")
while "`1'" ~ = "" {
qui replace `diff' = (`1' - `av')* `obs_c'/(`obs_c'-1) if `touse'
qui summ `diff' if `touse'
local mean = _result(3)
local lrr = _result(3) - 2*_result(4)^.5
local urr = _result(3) + 2*_result(4)^.5
if "`rrmin'" == "" { local rrmin = `lrr' }
else if `lrr' < `rrmin' { local rrmin = `lrr' }
if "`rrmax'" == "" { local rrmax = `urr' }
else if `urr' > `rrmax' { local rrmax = `urr' }
* set trace on
summ `av' , mean
if "`xmin'" == "" { local xmin = _result(5) }
else if _result(5) < `xmin' { local xmin = _result(5) }
if "`xmax'" == "" { local xmax = _result(6) }
else if _result(6) > `xmax' { local xmax = _result(6) }
qui corr `av' `diff' if `touse'
local r = _result(4)
local n = _result(1)
local sig = tprob(`n'-2, `r'*((`n'-2)/(1-`r'^2))^.5)
qui summ `1' if `touse'
#delim ;
di in gr "`1'" _col(10) "|"
_col(12) in ye %7.0f `n'
_col(22) `format' _result(3)
_col(32) `format' _result(4)^.5
_col(40) `format' `mean'
_col(54) `format' `lrr' in gr " to " in ye `format' `urr';
#delim cr
mac shift
}
global xmin = `xmin'
global xmax = `xmax'
global rrmin = `rrmin'
global rrmax = `rrmax'
end _table
prog define _graph
local varlist "req ex min(3)"
local options "av(str) diff(str) touse(str) rows(int 999) avlab(str) difflab(str) title(str) saving(str) obs_c(str) yline(str) text(real 100) *"
parse "`*'"
if "`yline'" ~= "" { local yline ",`yline'" }
if "`avlab'" == "" {local avlab " " }
if "`difflab'" == "" {local difflab " " }
if "`saving'" ~= "" { local saving saving(`saving') }
label var `av' "`avlab'"
label var `diff' "`difflab'"
local nvar: word count `varlist'
if "`rows'" == "999" { local rows = int(`nvar'^.5) }
local cols = int(`nvar' / `rows')
if `cols'*`rows' < `nvar' {
local cols = `cols' + 1
}
local rlow = 0
local rmax = 23063
if "`title'" ~= "" {local rmax = 20000 }
local clow = 0
local cmax = 32000
local dr = `rmax'/`rows' - 100
local dc = `cmax'/`cols' - 100
cap noi gph open, `saving'
if "title" ~= "" {
gph pen 1
gph font 1000 500
gph text 22000 16000 0 0 `title'
}
parse "`varlist'", parse(" ")
while "`1'" ~ = "" {
qui replace `diff' = (`1' - `av')* `obs_c'/(`obs_c'-1) if `touse'
qui summ `diff' if `touse'
local mean = _result(3)
local lrr = _result(3) - 2*_result(4)^.5
local urr = _result(3) + 2*_result(4)^.5
local rhigh = `rlow' + `dr'
local chigh = `clow' + `dc'
local lab1 : var label `1'
if "`lab1'" == "" { local lab1 "`1'" }
sort `diff' `av'
tempvar f n
qui by `diff' `av' : gen `f' = _N if `touse'
qui by `diff' `av' : gen `n' = _n if `touse'
local r_tx = int(400 * `text'/100)
local c_tx = int(200 * `text'/100)
#delim ;
cap noi graph `diff' `av' if `touse' & `n' == 1 [fw=`f'], s(o) `xlabel' `ylabel' yline(`lrr', `mean', `urr' `yline')
`options' title("`lab1'")
bbox(`rlow', `clow', `rhigh', `chigh', `r_tx',`c_tx', 0);
#delim cr
local clow = `clow' + `dc'
if `clow' + `dc' > `cmax' {
local clow = 0
local rlow = `rlow' + `dr'
}
mac shift
}
gph close
end _graph

View File

@ -0,0 +1,60 @@
.-
help for ^bagroup^ (STB-55: sbe33)
.-
Modified Bland-Altman plots
---------------------------
^bagroup^ varlist [^if^ exp] [^in^ range] [^,^ ^format(^str^)^ ^rows(^#^)^
^avlab(^str^)^ ^difflab(^str^)^ ^title(^str^)^ ^obs(^#^)^
^listwise^ graph_options ]
Description
-----------
^bagroup^ produces modified Bland-Altman plots.
Options
-------
^format(^str^)^ sets the format for display of results.
^rows(^#^)^ specifies the number of rows of graphs to be shown.
^avlab(^str^)^ gives a variable label to the average before plotting the graph.
^difflab(^str^)^ gives a variable label to the difference before plotting the
graph.
^title(^str^)^ adds a single title to the block of graphs.
^obs(^#^)^ specifies the minimum number of nonmissing values per observations
needed for a point to be plotted.
^listwise^ specifies listwise deletion of missing data. Default is pairwise.
Only observations with no missing values are used.
graph_options are any of the options allowed with ^graph, twoway^.
Example
-------
. ^use tan_part^
. ^bagroup pct_*^
Author
------
Paul Seed
GKT School of Medicine
King's London, UK
email: paul.seed@@kcl.ac.uk
Also see
--------
STB: STB-55 sbe33

View File

@ -0,0 +1,300 @@
*! bamat Version 1.2.2 written 11 June 1997 by P T Seed (STB-55: sbe33)
*!
*! A matrix of Bland-Altman plots for a series of variables
*! bamat varlist, graph_options
* Amended 7 August 1998 to allow for saving graphs
*
* Amended 3 May 2000 by arr@stata to suppress intermediary graphs and
* display only the final matrix of Bland-Altman plots
prog define bamat
version 5.0
local varlist "req ex min(3)"
local if "opt"
local in "opt"
local options "noTABle TItle(string) saving(string) data nograph markout FORmat(string) XLAbel(string) YLAbel(string) text(str) *"
parse "`*'"
if index("`options'","xlab") ~= 0 | index("`options'","ylab") ~= 0 {
di in red "xlabel and ylabel without values not permitted"
exit 198
}
parse "`varlist'", parse(" ")
if "`table'" == "notable" & "`graph'" == "nograph" & "`data'" == "" {
di in red "Either table data or graph options must be chosen"
exit 194 }
preserve
tempvar touse
mark `touse' `if'
if "`markout'" ~= "" {markout `touse' `varlist'}
if "`format'" == "" { local format "%6.3f" }
if "`saving'" ~= "" { local saving "saving(`saving')" }
if "`table'" ~= "notable" {
_table `varlist' if `touse', format(`format') `data'
}
if "`graph'" ~= "nograph" |"`data'" ~= "" {
_limits `varlist' if `touse'
if "`xlabel'" == "" | "`xlabel'" == "xlabel" {
_mynnum xlabel $xmin $xmax
local xlabel "xlabel($xlabel)"
}
else {local xlabel "xlabel(`xlabel')" }
if "`ylabel'" == "" {
_mynnum ylabel $ymin $ymax
local ylabel "ylabel($ylabel)"
}
else {local ylabel "ylabel(`ylabel')" }
#delim ;
di _n in gr "Range of x values is " in ye %6.0g $xmin in gr " to " in ye %6.0g $xmax
in gr ", range of y values is " in ye %6.0g $ymin in gr " to " in ye %6.0g $ymax;
#delim cr
if "`title'" == "" {_graph `varlist' if `touse', `graph' `saving' `xlabel' `ylabel' `data' `options' }
else {_graph `varlist' if `touse', title("`title'") `graph' `saving' `xlabel' `ylabel' `data' `options' }
}
end bamat
prog define _table
local varlist "req ex"
local if "opt"
local options "data format(str)"
parse "`*'"
tempvar touse
mark `touse' `if'
di in gr _new "Reference ranges for differences between two methods""
#delimit;
di in gr _new(2) "Method 1" _col(10) "Method 2" _col(20) "Mean"
_col(30) "[95% Reference Range]"
_col(54) "Minimum" _col(64) "Maximum" ;
#delimit cr
di in gr _dup(70) "-"
tempvar av diff
qui gen `av' = .
qui gen `diff' = .
local nvar : word count `varlist'
local i = 2
while `i' <= `nvar' {
local j = 1
while `j' < `i' {
* di "i = |`i'|, j = |`j'|"
qui replace `diff' = ``i'' - ``j'' `if'
label var `diff' " "
qui replace `av' = (``i'' + ``j'')/2 `if'
label var `av' " "
qui summ `diff' `if'
local mean = _result(3)
local lrr = `mean' - 2*_result(4)^.5
local urr = `mean' + 2*_result(4)^.5
local min = _result(5)
local max = _result(6)
di in gr "``i''" _col(10) "``j''" _col(20) in ye `format' `mean' /*
*/ _col(30) `format' `lrr' _col(40) `format' `urr' _col(52) `format' `min' _col(62) `format' `max'
local j = `j' +1
}
local i = `i' + 1
}
di in gr _dup(70) "-"
end _table
prog define _data
local av `1'
local diff `2'
local m1 `3'
local m2 `4'
di _n in gr _dup(40) "-"
di in gr "Data for graph comparing `m1' and `m2'"
di in gr "Difference" _col(20) "Average"
di in gr _dup(40) "-"
local k = 1
while `k' <= _N {
if `av'[`k'] ~= . & `diff'[`k'] ~= . { di `diff'[`k'] _col(20) `av'[`k'] }
local k = `k' + 1
}
di in gr _dup(40) "-" _n
end _data
prog define _graph
local varlist "req ex"
local if "req"
local options "title(str) saving(str) xlabel(str) ylabel(str) nograph data *"
parse "`*'",
if "`saving'" ~= "" { local saving "saving(`saving')" }
local nvar : word count `varlist'
tempvar diff av
qui gen `diff' = .
qui gen `av' = .
label var `diff' " "
label var `av' " "
local gonoff : set graphics
set graphics off
local i = 1
while `i' <= `nvar' {
local j = 1
while `j' <= `nvar' {
qui replace `diff' = ``i'' - ``j'' `if'
qui replace `av' = (``i'' + ``j'')/2 `if'
tempfile g`i'`j'
local graph2 "`graph2' `i'`j'"
local graphs "`graphs' `g`i'`j''"
if `i' == `j' & "`graph'" == "" { _namegr ``i'', saving(`g`i'`j'', replace) }
* set trace on
else if `i' ~= `j'{
qui summ `diff'
local mean = _result(3)
local lrr = _result(3) - 2*_result(4)^.5
local urr = _result(3) + 2*_result(4)^.5
if "`graph'" == "" {
sort `diff' `av'
* set trace off
tempvar f n
qui by `diff' `av' : gen `f' = _N if `diff' ~= . & `av' ~= .
qui by `diff' `av' : gen `n' = _n if `diff' ~= . & `av' ~= .
qui graph `diff' `av' `if' & `n' == 1 [fw=`f'], xlabel(`xlabel') /*
*/ylabel(`ylabel') `options' saving(`g`i'`j'', replace) yline(`lrr', `mean', `urr')
}
if "`data'" ~= "" { _data `av' `diff' ``i'' ``j''}
}
local j = `j' + 1
}
local i = `i' + 1
}
set graphics `gonoff'
if "`graph'" == "" { graph using `graphs', title("`title'") `saving' }
end _graph
prog define _namegr
local varlist "req ex"
local options "saving(string)"
parse "`*'"
local f1 = 3000
local f2 = 1400
parse "`varlist'", parse (" ")
local lab1 : var label `1'
if "`lab1'" == "" { local lab1 "`1'" }
qui gph open, saving(`saving')
qui gph font `f1' `f2'
qui gph text 11188 16000 0 0 `lab1'
qui gph close
end _namegr
prog define _limits
local varlist "req ex"
local if "opt"
parse "`*'"
tempvar av diff
qui gen `diff' = `2' - `1'
qui gen `av' = (`2' + `1')/2
qui summ `av' `if'
global xmin = _result(5)
global xmax = _result(6)
qui summ `diff' `if'
global ymin = _result(5)
global ymax = _result(6)
local nvar : word count `varlist'
local i = 3
while `i' <= `nvar' {
local j = 1
while `j' < `i' {
qui replace `diff' = ``i'' - ``j'' `if'
qui replace `av' = (``i'' + ``j'')/2 `if'
qui summ `av' `if'
if _result(5) < $xmin { global xmin = _result(5) }
if _result(6) > $xmax { global xmax = _result(6) }
qui summ `diff' `if'
if _result(5) < $ymin { global ymin = _result(5) }
if _result(6) > $ymax { global ymax = _result(6) }
local j = `j' + 1
}
local i = `i' + 1
}
if $ymax + $ymin > 0 { global ymin = -$ymax }
else { global ymax = -$ymin }
end _limits
prog define _mynnum
*! _mynnum Version 1.00 20/3/1997 PTS
*! a version of nicenum to handle numbers less than 1
*! usage: _mynnum global_macro minimum maximum mumber_of_ticks
version 5.0
local macro "`1'"
if `2' ~= `3' {
if `2' < `3' {
local min `2'
local max `3'
}
else {
local min `3'
local max `2'
}
}
else {
di in red "Numbers do not differ"
exit 198
}
local nticks `4'
if "`nticks'" == "" { local nticks = 5 }
else { cap confirm integer `nticks'
assert `nticks' > 2
if _rc ~= 0 {
di in red "Number of ticks requested must be at least 2"
exit 198
}
}
local gap = 10^(int(log10(`max' - `min'))-1)
local nt = int((`max' - `min' )/`gap') + 1
while `nt' < `nticks' {
local gap = `gap'/10
local nt = int((`max' - `min' )/`gap') + 1
}
if `nt' > `nticks' - 1 {
local gap = `gap' * 2
local nt = int((`max' - `min' )/`gap') + 1
}
if `nt' > `nticks' - 1 {
local gap = `gap' * 2.5
}
if `nt' > `nticks' - 1 {
local gap = `gap' * 2
}
local tick = `gap'*int(`min'/`gap')
while `tick' > `min' { local tick = `tick' - `gap' }
global `macro' "`tick'"
while `tick' < `max' {
local tick = `tick' + `gap'
global `macro' "$`macro', `tick'"
}
end _mynnum

View File

@ -0,0 +1,64 @@
.-
help for ^bamat^ (STB-55: sbe33)
.-
Multiple Bland-Altman plots
---------------------------
^bamat^ varlist [^if^ exp] [^in^ range] [^, for^mat^(^str^)^ ^notable^
^data^ ^avlab(^str^)^ ^difflab(^str^)^ ^obs(^#^)^
^listwise^ ^ti^tle^(^str^)^ graph_options ]
Description
-----------
^bamat^ produces a matrix of Bland-Altman plots for all possible pairs of
variables.
Options
-------
^format(^str^)^
sets the format for display of results.
^notable^ suppresses display of results data list data used in plotting
each graph.
^avlab(^str^)^ gives a variable label to the average before plotting the graph.
^difflab(^str^)^ gives a variable label to the difference before plotting
the graph.
^obs(^#^)^ specifies the minimum number of nonmissing values per observations
needed for a point to be plotted. The default value is 2.
^listwise^ specifies listwise deletion of missing data. Default is
pairwise. Only observations with no missing values are used.
^title(^str^)^ adds a single title to the block of graphs.
graph_options are any of the options allowed with ^graph, twoway^.
Example
-------
. ^use tan_part^
. ^bamat pct_*^
Author
------
Paul Seed
GKT School of Medicine
King's London, UK
email: paul.seed@@kcl.ac.uk
Also see
--------
STB: STB-55 sbe33

View File

@ -0,0 +1,218 @@
*! baplot.ado version 1.21 written by PTS (p.seed@umds.ac.uk) (STB-55: sbe33)
*! Produces Bland-Altman plots for two variables
*! See Bland & Altman Lancet Feb 8 1986, pp 307-310
*!
*! syntax: baplot var1 var2 if in, symbol(symbol) format(%6.3f) avlab("Average") difflab("Difference") yline(str) textsize(#) other graph options
* Now allows choice of symbol, and extra ylines Feb 8 1996
* larger plotting symbols for overlapping points
* Silly comparisons taken out
cap prog drop baplot
prog define baplot
version 6.0
* set trace off
syntax varlist(min=2 max=2 numeric) [if] [in], /*
*/ [Symbol(string) format(string) avlab(string) difflab(string) /*
*/ novars noGRaph ci zero mean yline(string) diag saving(string) /*
*/ text(real 100) ratio(real 1) *]"
parse "`varlist'", parse(" ")
local m1 "`1'"
local m2 "`2'"
if "`symbol'" == "" { local symbol "o" }
if "`format'" == "" {local format "%6.3f" }
if "`saving'" ~= "" { local saving "saving(`saving')"}
if "`ci'" ~= "" {local mean = "mean" }
preserve
tempvar touse
mark `touse' `if' `in'
markout `touse' `m1' `m2'
qui keep if `touse'
tempvar av diff
if "`avlab'" == "" { local avlab "Average"}
if lower("`avlab'") == "nolab" { local avlab " "}
if "`difflab'" == "" { local difflab "Difference" }
if lower("`difflab'") == "nolab" { local difflab " "}
qui gen `av' = (`m1' + `m2')/2
qui gen `diff' = `m1' - `m2'
label var `av' "`avlab'"
label var `diff' "`difflab'"
qui summ `diff' if `touse'
local xbar = _result(3)
local sd = _result(4)^.5
local n = _result(2)
local se = `sd'/`n'^.5
local t = invt(_result(2)-1, .95)
local lrr = `xbar' - 2*`sd'
local urr = `xbar' + 2*`sd'
local min = _result(5)
local max = _result(6)
local lcb = `xbar' - `t'*`se'
local ucb = `xbar' + `t'*`se'
summ `av', meanonly
local xmin = _result(5)
local xmax = _result(6)
local yline "`lrr', `urr'"
if "`ci'" ~= "" {
local yline "`yline', `lcb', `ucb'"
}
if "`mean'" ~= "" | "`zero'" == "" {local yline "`yline', `xbar'"}
if "`zero'" ~= "" {local yline "`yline', 0"}
qui corr `av' `diff'
local r = _result(4)
local n = _result(1)
local sig = tprob(`n'-2, `r'*((`n'-2)/(1-`r'^2))^.5)
#delim ;
di in gr _n "Bland-Altman comparison of `m1' and `m2'";
di in gr "Limits of agreement (Reference Range for difference): " in ye `format' `lrr'
in gr " to " in ye `format' `urr' ;
di in gr "Mean difference: " in ye `format' `xbar'
in gr " (CI " in ye `format' `lcb'
in gr " to " in ye `format' `ucb'
in gr ") ";
di in gr "Range : " in ye `format' `xmin'
in gr " to " in ye `format' `xmax';
di in gr "Pitman's Test of difference in variance: r = " in ye `format' `r'
in gr ", n = " in ye `n'
in gr ", p =" in ye `format' `sig' ;
#delim cr
if "`vars'" ~= "" {
qui corr `m1' `m2', cov
local tau = _result(4)
qui summ `m1'
local err1 = _result(4) - `tau'
qui summ `m2'
local err2 = _result(4) - `tau'
di in gr "Estimated variance of true measure: " in ye `format' `tau'
di in gr "Estimated error variance (`1'): " in ye `format' `err1'
di in gr "Estimated error variance (`2'): " in ye `format' `err2'
di in gr "Very low or negative error variances may indiate that modelling assumptions are violated."
}
global S_1 `xbar'
global S_2 `lrr'
global S_3 `urr'
if "`graph'" == "" & "`diag'" == "" {
sort `diff' `av'
tempvar f n
qui by `diff' `av': gen `f' = _N if `diff' ~= . & `av' ~= .
qui by `diff' `av': gen `n' = _n if `diff' ~= . & `av' ~= .
graph `diff' `av' if `touse' & `n' == 1 [fw=`f'], symbol(`symbol') `xlabel' `ylabel' yline(`yline') `saving' `options'
}
else if "`diag'" ~= "" {
local nobs = _N + 1
qui set obs `nobs'
qui replace `m2' = 0 if `m2' == .
* loa
tempvar lb ub
qui gen `ub' = `m2' + `xbar' + 2*`sd'
qui replace `ub' = . if `ub' < 0
summ `ub', mean
qui replace `ub' = . if `ub' > _result(5) & `ub' < _result(6)
qui gen `lb' = `m2' + `xbar' - 2*`sd'
summ `lb', mean
if _result(5) < 0 {
qui replace `lb' = . if `lb' < 0
local nobs = _N + 1
qui set obs `nobs'
qui replace `lb' = 0 if _n == _N
qui replace `m2' = 2*`sd' - `xbar' if _n == _N
}
summ `lb', mean
qui replace `lb' = . if `lb' > _result(5) & `lb' < _result(6)
* ci
if "`ci'" ~= "" {
tempvar lci uci
qui gen `lci' = `m2' + `xbar' - invt(`n'-1,0.95)*`sd'/`n'^.5
qui gen `uci' = `m2' + `xbar' + invt(`n'-1,0.95)*`sd'/`n'^.5
summ `lci', mean
if _result(5) < 0 {
qui replace `lci' = . if `lci' < 0
local nobs = _N + 1
qui set obs `nobs'
qui replace `lci' = 0 if _n == _N
qui replace `m2' = invt(`n'-1,0.95)*`sd'/`n'^.5 - `xbar' if _n == _N
}
summ `uci', mean
if _result(5) < 0 {
qui replace `uci' = . if `uci' < 0
local nobs = _N + 1
qui set obs `nobs'
qui replace `uci' = 0 if _n == _N
qui replace `m2' = - invt(`n'-1,0.95)*`sd'/`n'^.5 - `xbar' if _n == _N
}
summ `lci', mean
qui replace `lci' = . if `lci' > _result(5) & `lci' < _result(6)
summ `uci', mean
qui replace `uci' = . if `uci' > _result(5) & `lci' < _result(6)
}
summ `m2' if `m2' ~= ., mean
tempvar diag
* zero
if "`zero'" ~= "" | "`mean'" == "" {
qui gen `diag' = `m2' if `m2' == _result(5) | `m2' == _result(6)
}
* mean
else {
qui gen `diag' = `m2' + `xbar' if `m2' == _result(5) | `m2' == _result(6)
summ `diag', mean
if _result(5) < 0 {
qui replace `diag' = . if `diag' < 0
local nobs = _N + 1
qui set obs `nobs'
qui replace `diag' = 0 if _n == _N
qui replace `m2' = - `xbar' if _n == _N
}
}
sort `m1' `m2'
tempvar f n
qui by `m1' `m2': gen `f' = _N
qui by `m1' `m2': gen `n' = _n
if ("`xlabel'" == "" | "`ylabel'" == "") & index("`options'","xlab") == 0 & index("`options'","ylab") == 0 {
nicenum labels = 0 `m1' `m2'
local xlabel xlab($labels)
local ylabel ylab($labels)
}
local r_tx = int(923 * `text'/100)
local c_tx = int(444 * `text'/100)
local c_min = int(16000 - (23063*`ratio'/2))
local c_max = int(16000 + (23063*`ratio'/2))
di "bbox(0,`c_min',23063,`c_max',`r_tx',`c_tx',0) "
gph open, `saving'
graph `m1' `diag' `ub' `lb' `lci' `uci' `m2' if `n' == 1 [fw=`f'], /*
*/ `xlabel' `ylabel' s(oiiiii) c(.lllll) sort /*
*/ bbox(0,`c_min',23063,`c_max',`r_tx',`c_tx',0) `options'
gph close
}
end
exit

View File

@ -0,0 +1,51 @@
.-
help for ^baplot^ (STB-55: sbe33)
.-
Bland-Altman plots
------------------
^baplot^ varname1 varname2 [^if^ exp] [^in^ range] [^, format(^str^)^
^avlab(^str^) difflab(^str^)^ graph_options]
Description
-----------
^baplot^ produces Bland-Altman plots, that is, plots of the difference of
paired variables versus their average.
Options
-------
^format(^str^)^ sets the format for the results given.
^avlab(^str^)^ gives a variable label to the average before plotting the graph.
^difflab(^str^)^ gives a variable label to the difference before plotting the
graph.
graph_options are any of the options allowed with ^graph, twoway^.
Examples
--------
. ^use col_icp^
. ^baplot icp colorime, avlab("ICPOES vs Colorimetry)^
Author
------
Paul Seed
GKT School of Medicine
King's London, UK
email: paul.seed@@kcl.ac.uk
Also see
--------
STB: STB-55 sbe33

View File

@ -0,0 +1,269 @@
*! Date : 19 May 2009
*! Version : 1.12
*! Authors : Adrian Mander
*! Email : adrian.mander@mrc-bsu.cam.ac.uk
*! Description : Bland-Altman plots with trend adjustment
/*
17/5/06 v1.3 add-in the middle line
6/10/06 v1.4 Handle multiple data points at the same point by using frequency option
Also add Scatter option just to add options to the scatter part of the plot
2/9/07 v1.5 Changed version from 8 to 9.2
11/12/07 v1.6 Extended the shaded area
14/12/07 v1.7 Add limits of agreement into the info option
11/2/08 v1.8 BUGs fixed and extended the shading to the xlimits
17/4/08 v1.9 BUG fix.. the info limits were the wrong way round!
9/5/08 v1.10 Allowing shading to extend beyond data to any limit you like
11/5/08 v1.11 Added the range to be displayed for the averages
19/5/2009 v1.12 Changed email address
11/6/12 v1.13 Added extra option for titles and number of decimal places in display
*/
prog def batplot, rclass
version 9.2
syntax varlist (min=2 max=2) [if] [in] [,NOtrend INFO VALabel(varname) MOPTIONS(string asis) XLABel(numlist) SHADING(numlist min=2 max=2) SCatter(string asis) NOGraph DP(int 2) *]
local gopt "`options'"
if "`moptions'"~="" local mopt `"moptions(`moptions')"'
preserve
tempvar touse
mark `touse' `if' `in'
markout `touse' `m1' `m2'
qui keep if `touse'
if "`xlabel'"~="" {
local glab `"xlabel(`xlabel')"'
local i 0
foreach xv of local xlabel {
if `i++'==0 local sxmin "`xv'"
local sxmax "`xv'"
}
local shade "shade(`sxmin' `sxmax')"
}
/* To stop the shading going the whole length of the x-axis */
if "`shading'"~="" {
local i 0
foreach s of local shading {
if `i++'==0 local smin "`s'"
else local smax "`s'"
}
if `smin'<`smax' local shade "shade(`smin' `smax')"
else local shade "shade(`smax' `smin')"
}
/* NOW do the trend version */
if "`valabel'"~="" local add "val(`valabel')"
_calctrend `varlist', `gopt' `notrend' `info' `add' `mopt' sc(`scatter') `glab' `shade' `nograph' dp(`dp')
return local mean = "`r(mean)'"
return local b0 = "`r(b0)'"
return local b1 = "`r(b1)'"
return local c0 = "`r(c0)'"
return local c1 = "`r(c1)'"
return local eqn = "`r(eqn)'"
return local upper = "`r(upper)'"
return local lower = "`r(lower)'"
restore
end
/* calculate trend and do a BA plot with trend */
prog def _calctrend,rclass
syntax [varlist] [,NOTREND INFO VALabel(varname) MOPTIONS(string asis) SCatter(string asis) shade(numlist) NOGraph DP(int 2) *]
local xopt "`options'"
if "`shade'"~="" {
local i 0
foreach s of local shade {
if `i++'==0 local a "`s'"
local b "`s'"
}
local sxmin = `a'
local sxmax = `b'
}
local i 1
foreach var of varlist `varlist' {
local v`i++' "`var'"
}
tempvar av diff
qui gen `av' = (`v1' + `v2')/2
qui gen `diff' = `v1' - `v2'
local ytit "Difference (`v1'-`v2')"
local xtit "Average of `v1' and `v2'"
lab var `diff' "Diff"
lab var `av' "Mean"
if "`notrend'"~="" {
qui summ `diff'
local xbar = `r(mean)'
local sd = `r(Var)'^.5
local n = `r(N)'
local se = `sd'/`n'^.5
local t = invttail(`n'-1, .95)
local lrr = `xbar' - invnorm(0.975)*`sd' /* 95% lower limit of agreement */
local urr = `xbar' + invnorm(0.975)*`sd' /* 95% upper limit of agreement */
local mrr = `xbar' /* 95% mean agreement */
di "{text}Mean difference = {res}`mrr'"
di "{text}Limits of agreement = ({res}`lrr'{text},{res}`urr'{text})"
local min = `r(min)'
local max = `r(max)'
local lcb = `xbar' - `t'*`se'
local ucb = `xbar' + `t'*`se'
qui summ `av'
local xmin = `r(min)'
local xmax = `r(max)'
local a : di %5.3f `xmin'
local b : di %5.3f `xmax'
local range = "Averages lie between `a' and `b'"
di "{text}Averages lie between {res} `a' {text}and {res}`b'"
qui corr `av' `diff'
local r = `r(rho)'
local n = `r(N)'
local sig = ttail(`n'-2, `r'*((`n'-2)/(1-`r'^2))^.5)
tempvar uy ly my
/* The bit to extend the shade */
local obs =`c(N)'+2
qui set obs `obs'
if "`sxmin'"~="" qui replace `av'=`sxmin' in `obs--'
if "`sxmax'"~="" qui replace `av'=`sxmax' in `obs'
qui gen `uy' = `urr'
qui gen `ly' = `lrr'
qui gen `my' = `mrr'
sort `av'
if "`info'"~="" {
local te1:di %6.3f `mrr'
local te2:di %6.3f `lrr'
local te3:di %6.3f `urr'
qui count if (`diff'>`urr' | `diff'<`lrr' ) & `diff'~=.
local nout = `r(N)'
qui count if `diff'~=.
local n = `r(N)'
local pctout : di %6.2f `nout'/`n'*100
local xopt `"`xopt' subtitle("`nout'/`n' = `pctout'% outside the limits of agreement" "Mean difference `te1'" "95% limits of agreement (`te2',`te3')" "`range'") "'
}
if "`valabel'"~="" {
tempvar label
qui gen `label' = ""
cap confirm string variable `valabel'
if _rc~=0 qui replace `label' = string(`valabel') if `diff'>`urr' | `diff'<`lrr'
else qui replace `label' = `valabel' if `diff'>`urr' | `diff'<`lrr'
local scatteropt "mlabel(`label') note(Points outside limits labelled by `valabel')"
}
tempvar freq
qui bysort `diff' `av':gen `freq'=_N
local fopt "[fw=`freq']"
if index("`scatter'","jitter")~=0 local fopt ""
if "`nograph'"=="" twoway (rarea `uy' `ly' `av', bc(gs13) sort) (scatter `diff' `av' `fopt', m(o) `scatteropt' `scatter' `moptions' ) (line `my' `av',lp(dash) sort ) , ////
legend(off) ytitle(`ytit') xtitle(`xtit') xlabel(`xmin' `xmax') `xopt'
return local lower = `lrr'
return local upper = `urr'
return local mean = `mrr'
}
else {
qui reg `diff' `av'
local b1 = _b[`av']
local b0 = _b[_cons]
local sd = `e(rmse)'
qui predict resid , resid
qui gen absresid = abs(resid)
/* Analysis of the residuals */
qui reg absresid `av'
local c0 = _b[_cons]
local c1=_b[`av']
qui su `diff'
local max = `r(max)'
local min = `r(min)'
qui su `av'
local xmax = r(max)
local xmin = r(min)
local max: di %5.3f `xmax'
local min: di %5.3f `xmin'
tempvar y uy ly x
/* The bit to extend the shade */
local obs =`c(N)'+2
qui set obs `obs'
if "`sxmin'"~="" qui replace `av'=`sxmin' in `obs--'
if "`sxmax'"~="" qui replace `av'=`sxmax' in `obs'
sort `av'
qui gen `y' = `b0'+`b1'*`av'
qui gen `uy' = `b0'+`b1'*`av' + 2.46*(`c0'+`c1'*`av')
qui gen `ly' = `b0'+`b1'*`av' - 2.46*(`c0'+`c1'*`av')
if "`info'"~="" {
qui count if (`diff'>`uy' | `diff'<`ly') & `diff'~=.
local nout = `r(N)'
qui count if `diff'~=.
local n = `r(N)'
local mdp = `dp'+3
local mdp1 = `dp'+4
local pctout : di %`mdp1'.`dp'f `nout'/`n'*100
local te0 : di %`mdp'.`dp'f `b0'
local te1 : di %`mdp'.`dp'f `b1'
local te2 : di %`mdp'.`dp'f `c0'
local te3 : di %`mdp'.`dp'f `c1'
local xopt `"`xopt' subtitle("`nout'/`n' = `pctout'% outside the limits of agreement" "Mean Diff = `te0'+ `te1'*Average " "Limits +/- 2.46*(`te2' + `te3'*Average) ") "'
}
if "`valabel'"~="" {
tempvar label
qui gen `label' = ""
cap confirm string variable `valabel'
if _rc~=0 qui replace `label' = string(`valabel') if `diff'>`uy' | `diff'<`ly'
else qui replace `label' = `valabel' if `diff'>`uy' | `diff'<`ly'
local scatteropt "mlabel(`label') note(Points outside limits labelled by `valabel')"
}
tempvar freq
qui bysort `diff' `av':gen `freq'=_N
local fopt "[fw=`freq']"
if index("`scatter'","jitter")~=0 local fopt ""
if "`nograph'"=="" {
twoway (rarea `uy' `ly' `av', bc(gs13) sort)(scatter `diff' `av' `fopt', `scatteropt' `scatter') /*
*/(line `y' `av', lp(dash) sort) , legend(off) ytitle(`ytit') xtitle(`xtit') xlabel(`xmin' `xmax') `xopt'
}
return local b0 = `b0'
return local b1 = `b1'
return local c0 = `c0'
return local c1 = `c1'
return local eqn = "`b0'+`b1'*av"
return local upper = "`b0'+`b1'*Average + 2.46*(`c0'+`c1'*Average)"
return local lower = "`b0'+`b1'*Average - 2.46*(`c0'+`c1'*Average)"
}
end

View File

@ -0,0 +1,128 @@
{smcl}
{* 9May2008}{...}
{cmd:help batplot}
{hline}
{title:Title}
{hi: Produces a Bland-Altman plot when there is a relationship between paired differences and their average}
{title:Syntax}
{p 8 17 2}
{cmdab:batplot} {it:varname1 varname2} [if] [in]
[{cmd:,} {it:options}]
{synoptset 20 tabbed}{...}
{synopthdr}
{synoptline}
{syntab:Main}
{synopt:{opt info:}} specifies that the percentage of points outside the limits of agreement are displayed as a subtitle.{p_end}
{synopt:{opt val:abel}({varname})} specifies that the points outside the limits of agreement be labelled using the variable {it:varname}.{p_end}
{synopt:{opt shading:}(min max)} specifies the extent of shading beyond the range of the data.{p_end}
{synopt:{opt notrend:}} specifies that the original Bland-Altman plot (without a trend) be plotted.{p_end}
{synopt:{opt moptions:}} specifies options for the markers that lie outside the limits of agreement.{p_end}
{synopt:{opt sc:atter}} specifies options for the scatter part of the final plot, see {help scatter}.{p_end}
{synopt:{help twoway_options}} specifies options for example titles and labels.{p_end}
{synoptline}
{p2colreset}{...}
{title:Description}
{pstd}
{cmd:batplot} produces a Bland-Altman plot adjusted for trend.
{pstd}
The standard Bland-Altman plot is between the difference of paired variables versus the average, this is produced
using the {bf:notrend} option. The main addition that this command handles is when there is a linear relationship between
the the paired difference and the paired average. A regression model is used to adjust the limits of agreement accordingly.
This is particularly useful when the two variables might be measured on different scales and hence a straight conversion
factor would recalibrate the two variables.
{title:Options}
{dlgtab:Main}
{phang}
{opt info:} specifies that the percentage of points outside the limits of agreement are displayed as a subtitle. Additionally
when using the notrend option the limits of agreement and mean difference are included in the subtitle.
{phang}
{opt val:abel}({varname}) specifies that the points outside the limits of agreement be labelled using the variable {it:varname}.
{phang}
{opt shading:}(min max) specifies the extent of shading beyond the range of the data. The default is that the limits of
shading is determined by the values in the {hi:xlabel} option.
{phang}
{opt notrend:} specifies that the original Bland-Altman plot (without a trend) be plotted.
{phang}
{opt moptions:} specifies options for the markers that lie outside the limits of agreement, the options can be anything from
the scatter marker options {help scatter##marker_options}.
{phang}
{opt sc:atter} specifies options for the scatter part of the final plot.
{title:Examples}
{pstd}
Using the {hi:auto.dta} dataset supplied with STATA 8 this command can check whether there is agreement between
turning circle (ft) and miles per gallon, click the highlighted text in order,
{phang}{stata sysuse auto, clear}
{phang}{stata batplot mpg turn}
{pstd}
This is the most basic graphic and using twoway options the look can be improved by clicking below,
{phang}
{stata batplot mpg turn, title(Agreement between mpg and turn) xlab(26(4)38) }
{pstd}
By specifying extra options the outlying points can be labelled and identified by the car make,
{phang}
{stata batplot mpg turn, title(Agreement between mpg and turn) info valabel(make) xlab(26(4)38) }
{pstd}
To obtain the original Bland Altman plot use the notrend option,
{phang}
{stata batplot mpg turn, title(Agreement between mpg and turn) info valabel(make) notrend xlab(26(4)38) }
{pstd}
To improve the labelling of the point VW it may be preferable to change the clock position of the label i.e.
labels could appear to the left of the point. This is handled below with {hi: moptions()}.
{phang}
{stata batplot mpg turn, title(Agreement between mpg and turn) info valabel(make) notrend xlab(26(4)38) moptions(mlabp(9))}
{pstd}
Additionally in the case of multiple scatter points by using the {hi:scatter()} option the user can specify to "{hi:jitter}" datapoints
{phang}
{stata batplot mpg turn, notrend xlab(26(4)38) moptions(mlabp(9)) sc(jitter(4))}
{title:Author}
{p}
Adrian Mander, MRC Human Nutrition Research, Cambridge, UK.
Email {browse "mailto:adrian.mander@mrc-hnr.cam.ac.uk":adrian.mander@mrc-hnr.cam.ac.uk}
{title:Also see}
Related commands
HELP FILES
{help baplot} (if installed)

View File

@ -0,0 +1,22 @@
program def binolist, rclass
*! NJC 1.1.0 6 June 2000
* NJC 1.0.0 25 Jan 2000
version 6.0
syntax , K(numlist int max=1 >0) [ Global(str) Noisily ]
if length("`global'") > 8 {
di in r "global name must be <=8 characters"
exit 198
}
local i = 1
while `i' <= `k' {
local val = comb(`k'-1, `i'-1)
local newlist "`newlist' `val'"
local i = `i' + 1
}
if "`noisily'" != "" { di "`newlist'" }
if "`global'" != "" { global `global' "`newlist'" }
return local list `newlist'
end

View File

@ -0,0 +1,2 @@
.h listutil

View File

@ -0,0 +1,89 @@
***********************************************************************************************************************
**** Program blandaltman (one parameter against another parameter) startet ********************************************
***********************************************************************************************************************
* set more off
* capture program drop blandaltman
program blandaltman
syntax varlist(max=2)
// prepare for Bland Altman Interreader
tempvar diff_xy
tempvar avg_xy
tempvar lower
tempvar higher
tempvar MW
tempvar SE
tempvar CIhigher
tempvar CIlower
generate `diff_xy'=0
generate `avg_xy'=0
generate `lower'=0
generate `higher'=0
generate `MW'=0
generate `SE'=0
generate `CIhigher'=0
generate `CIlower'=0
// count the variable: how many variable are in the list?
local noofvars : word count `varlist'
display as text "The variable list of this program counts " `noofvars' " variables"
display as result " "
display as result " "
// Interreader
local x = 1
local y = 1
foreach varx of varlist `varlist' {
foreach vary of varlist `varlist'{
if `y' >`x'{
quietly replace `avg_xy'=(`varx'+`vary')/2
quietly replace `diff_xy'=`varx'-`vary'
display as result " Bland Altman Plot of `varx' and `vary'"
quietly sum `diff_xy'
quietly return list
quietly replace `MW'=r(mean)
quietly replace `lower'=r(mean)-2*r(sd)
quietly replace `higher'=r(mean)+2*r(sd)
quietly replace `SE'=(r(sd))/(sqrt(r(N)))
quietly replace `CIlower'=r(mean)-2*`SE'
quietly replace `CIhigher'=r(mean)+2*`SE'
display as result "- mean of difference between `varx' and `vary' is "r(mean)
display as result "- sd of difference between `varx' and `vary' is "r(sd)
display as result "- lower limit of difference between `varx' and `vary' is " `lower'
display as result "- higher limit of difference between `varx' and `vary' is " `higher'
display as result "- Limits of agreement (Reference Range for difference): " `lower' " to " `higher'
display as result "- Mean difference:" `MW' " (CI " `CIlower' " to " `CIhigher' ")"
display as result " "
display as result " "
label var `diff_xy' "Values"
label var `MW' "mean of difference"
label var `lower' "lower limit of agreement"
label var `higher' "higher limit of agreement"
twoway (scatter `diff_xy' `avg_xy', msymbol(smcircle_hollow) mcolor(ebblue)) (line `MW' `avg_xy', lcolor(red))(line `lower' `avg_xy', lcolor(black) ) (line `higher' `avg_xy', lcolor(black) ), title(Bland Altman Plot, size(8)) subtitle(,size(5)) xtitle(Average of `varx' and `vary') ytitle(Difference of `varx' and `vary') caption() note(NOTE) legend(off)
}
local y = `y'+1
}
local y = 1
local x =`x'+1
}
end
***********************************************************************************************************************
**** Program blandaltman (one parameter against another parameter) endet **********************************************
***********************************************************************************************************************
// EXAMPLE
* sysuse bpwide.dta
// to create a bland altman plot use the command "blandaltman" with variable1 and variable2
* blandaltman bp_before bp_after

View File

@ -0,0 +1,187 @@
*! Bollen-Stine bootstrap, v.1.3, Stas Kolenikov
program define bollenstine, eclass
syntax, [Reps(int 200) SAVing(str) notable noheader nolegend ///
SAFER CONFAOPTions(str) *]
* this is a post-estimation command following confa1
if "`e(cmd)'" ~= "confa1" & "`e(cmd)'" ~= "confa" error 301
* the low level preserve
preserve
tempfile pres
tempname confares
est store `confares'
qui save `pres'
qui keep if e(sample)
local T = e(lr_u)
local safer cap noi
if "`saving'" == "" {
tempfile bsres
local saving `bsres'
}
if "`e(cmd)'" == "confa1" {
local varlist = "`e(depvar)'"
local p : word count `varlist'
tempname Sigma bb
mat `Sigma' = e(Model)
mat `bb' = e(b)
mata: CONFA1_BSrotate("`Sigma'","`varlist'")
`safer' bootstrap _b (T: T = e(lr_u)) (reject: reject = (e(lr_u) > `T') ) , ///
reps(`reps') saving(`saving') notable noheader nolegend ///
reject( e(converged) == 0) `options' ///
: confa1 `varlist' , from(`bb', skip) `confaoptions'
* may need some other options, too!
nobreak if "`safer'"~="" & _rc {
* for whatever reason, the bootstrap broke down
qui use `pres' , clear
qui est restore `confares'
qui est drop `confares'
error _rc
}
* just to display the results
* the covariance matrix should have been reposted by the -bootstrap-!
* we still need to trick Stata back into confa1!
ereturn local cmd confa1
}
else if "`e(cmd)'" == "confa" {
local varlist = "`e(observed)'"
local p : word count `varlist'
tempname Sigma bb
mat `Sigma' = e(Sigma)
mat `bb' = e(b)
mata: CONFA1_BSrotate("`Sigma'","`varlist'")
* set up the call
local k = 1
while "`e(factor`k')'" ~= "" {
local call `call' (`e(factor`k')')
local ++k
}
* the first call and resetting the from vector
cap confa `call' , from(`bb') `confaoptions'
if _rc {
di as err "cannot execute confa with rotated data only"
restore
qui est restore `confares'
cap est drop `confares'
exit 309
}
mat `bb' = e(b)
if ~strpos("`confaoptions'", "from") local from from(`bb')
* correlated errors?
* unit variance identification?
`safer' bootstrap _b (T: T = e(lr_u)) (reject: reject = (e(lr_u) > `T') ) , ///
reps(`reps') saving(`saving') notable noheader nolegend ///
reject( e(converged) == 0) `options' ///
: confa `call' , `from' `confaoptions'
* may need some other options, too!
nobreak if "`safer'"~="" & _rc {
* for whatever reason, the bootstrap broke down
qui use `pres' , clear
qui est restore `confares'
cap est drop `confares'
error _rc
}
* the covariance matrix should have been reposted by the -bootstrap-!
* we still need to trick Stata back into confa!
ereturn local cmd confa
}
else {
* what on earth was that?
error 301
}
* the bootstrap test on T
gettoken bsres blah : saving , parse(",")
* to strip off replace option, if there is any
qui use `bsres', clear
sum reject_reject, mean
local pBS = r(mean)
local BBS = r(N)
qui sum T_T, det
local q05 = r(p5)
local q95 = r(p95)
qui use `pres', clear
qui est restore `confares'
qui est drop `confares'
ereturn scalar p_u_BS = `pBS'
ereturn scalar B_BS = `BBS'
* ereturn scalar lr_u = `T'
* ereturn scalar p_u = chi2tail(e(df_u),e(lr_u))
ereturn scalar T_BS_05 = `q05'
ereturn scalar T_BS_95 = `q95'
ereturn local vce BollenStine
ereturn local vcetype Bollen-Stine
`e(cmd)'
end
cap mata: mata drop CONFA1_BSrotate()
mata:
void CONFA1_BSrotate(
string SigmaName, // the parameter matrix name
string varnames // the variable names
) {
// declarations
real matrix data // views of the data
real matrix Sigma, SS, S2, SS2 // the covariance matrices and temp matrices
real matrix means // the means -- need modifications for weighted data!!!
real scalar p, n // dimension, no. obs
// get the data in
st_view(data=., ., tokens(varnames) )
n=rows(data)
p=cols(data)
Sigma = st_matrix(SigmaName)
// probability weights!!!
means = colsum(data)/n
SS = (cross(data,data)-n*means'*means)/(n-1)
S2 = cholesky(Sigma)
SS2 = cholesky(SS)
SS2 = solveupper(SS2',I(rows(SS)))
data[,] = data*SS2*S2'
}
end
exit
History:
v.1.1 -- Jan 9, 2007
v.1.2 -- Mar 26, 2008: confa1 options added; reject() added
v.1.3 -- July 12, 2008: upgraded to confa

View File

@ -0,0 +1,88 @@
{smcl}
{* *! version 1.3 28Oct2008}{...}
{cmd:help bollenstine} {right: ({browse "http://www.stata-journal.com/article.html?article=st0169":SJ9-3: st0169})}
{hline}
{title:Title}
{p2colset 5 20 22 2}{...}
{p2col :{hi:bollenstine} {hline 2}}Bollen-Stine bootstrap following confirmatory factor analysis
{p2colreset}{...}
{title:Syntax}
{p 8 19 2}
{cmd:bollenstine} [{cmd:,} {cmdab:r:eps(}{it:#}{cmd:) }
{cmdab:sav:ing(}{it:filename}{cmd:) }
{cmdab:confaopt:ions(}{it:string}{cmd:)}
{it:bootstrap_options}]
{p_end}
{title:Description}
{pstd}{cmd:bollenstine} performs the Bollen and Stine (1992) bootstrap
following structural equation models (confirmatory factor analysis) estimation.
The original data are rotated to conform to the fitted structure.
By default, {cmd:bollenstine} refits the model
with rotated data and uses the estimates as
starting values in each bootstrap iteration. It also rejects samples
where convergence was not achieved (implemented through the {cmd:reject(e(converged) == 0)} option supplied to
{helpb bootstrap}).
{title:Options}
{phang}{cmd:reps(}{it:#}{cmd:)} specifies the number of bootstrap replications.
The default is {cmd:reps(200)}.{p_end}
{phang}{cmd:saving(}{it:filename}{cmd:)} specifies the file
where the simulation results (the parameter estimates and the fit statistics)
are to be stored. The default is a temporary file that will
be deleted as soon as {cmd:bollenstine} finishes.{p_end}
{phang}{opt confaoptions(string)} allows the transfer of {cmd:confa}
options to {cmd:bollenstine}. If nondefault model options ({cmd:unitvar()} and
{cmd:correlated()}) were used, one would need to use them with
{cmd:bollenstine} as well.
{phang}All nonstandard model options, like {cmd:unitvar()} or {cmd:correlated()},
must be specified with {cmd:bollenstine} to produce correct results!
{phang}All other options are assumed to be {it:bootstrap_options}
and passed through to {helpb bootstrap}.
{title:Example}
{phang2}{cmd:. use hs-cfa}{p_end}
{phang2}{cmd:. confa (vis: x1 x2 x3) (text: x4 x5 x6) (math: x7 x8 x9), from(iv) correlated(x7:x8)}{p_end}
{phang2}{cmd:. set seed 10101}{p_end}
{phang2}{cmd:. bollenstine, reps(200) confaoptions(iter(20) corr(x7:x8))}
{title:Reference}
{phang}{bind:}Bollen, K., and R. Stine. 1992.
Bootstrapping goodness-of-fit measures in structural
equation models. {it:Sociological Methods and Research} 21: 205-229.
{p_end}
{title:Author}
{pstd}Stanislav Kolenikov{p_end}
{pstd}Department of Statistics{p_end}
{pstd}University of Missouri{p_end}
{pstd}Columbia, MO{p_end}
{pstd}kolenikovs@missouri.edu{p_end}
{title:Also see}
{psee}
Article: {it:Stata Journal}, volume 9, number 3: {browse "http://www.stata-journal.com/article.html?article=st0169":st0169}
{psee}Online: {helpb confa}, {helpb confa_estat:confa postestimation},
{helpb bootstrap} (if installed){p_end}

View File

@ -0,0 +1,89 @@
program def bothlist, rclass
*! NJC 1.3.0 6 June 2000
* NJC 1.2.0 31 Jan 2000
* NJC 1.1.0 22 Dec 1999
* NJC 1.0.0 21 Dec 1999
version 6.0
gettoken lists 0 : 0, parse(",")
if "`lists'" == "" | "`lists'" == "," { /* no \ */
di in r "incorrect syntax: no separator"
exit 198
}
tokenize "`lists'", parse("\")
if "`4'" != "" {
di in r "incorrect syntax: too much stuff"
exit 198
}
if "`1'" == "\" { /* list1 empty */
if "`2'" == "\" {
di in r "incorrect syntax: one \ only"
exit 198
}
local list2 "`2'" /* might be empty */
}
else if "`2'" == "\" {
local list1 "`1'"
local list2 "`3'" /* might be empty */
}
else {
di in r "incorrect syntax: what to compare?"
exit 198
}
syntax [ , Global(str) Noisily ]
if length("`global'") > 8 {
di in r "global name must be <=8 characters"
exit 198
}
* remove duplicates from `list1'
tokenize `list1'
local list1 "`1'"
mac shift
while "`1'" != "" {
local n1 : word count `list1'
local i = 1
local putin = 1
while `i' <= `n1' {
local word : word `i' of `list1'
if "`word'" == "`1'" {
local putin = 0
local i = `n1'
}
local i = `i' + 1
}
if `putin' { local list1 "`list1' `1'" }
mac shift
}
* what is in both lists?
local n1 : word count `list1'
tokenize `list1'
local n2 : word count `list2'
local i = 1
while `i' <= `n1' {
local j = 1
local putin = 0
while `j' <= `n2' {
local word : word `j' of `list2'
if "`word'" == "``i''" {
local putin = 1
local j = `n2'
}
local j = `j' + 1
}
if `putin' { local newlist "`newlist' ``i''" }
local i = `i' + 1
}
if "`noisily'" != "" { di "`newlist'" }
if "`global'" != "" { global `global' "`newlist'" }
return local list `newlist'
end

View File

@ -0,0 +1,2 @@
.h listutil

View File

@ -0,0 +1,297 @@
*! version 1.6.0 3/29/01
capture program drop brant
program define brant, rclass
version 6
tempvar touse
tempname bout d pvals ivchi ivout step1 step2 ologit
tempname XpWmmX iXpWmmX XpWmlX XpWllX iXpWllX DB DBp iDvBDp
syntax [, detail]
if "`e(cmd)'"!="ologit" {
di in r "brant can only be used after ologit"
exit
}
*to make output stata 6 or stata 7 compatible
cap version 7
if _rc!=0 {
local vers7 "no"
local smcl ""
local dash "-"
local vline "|"
local plussgn "+"
local topt "-"
local bottomt "-"
}
else { local vers7 "yes"
local smcl "in smcl "
local dash "{c -}"
local vline "{c |}"
local plussgn "{c +}"
local topt "{c TT}"
local bottomt "{c BT}"
}
version 6.0
local ocmd "`e(cmd)'"
if "`ocmd'"=="ologit" { local bcmd "logit" }
local depvar "`e(depvar)'"
gen `touse' = e(sample)
local wtis ""
if "`e(wtype)'"!="" {
di in r "-brant- does not work with ologit models with weights"
error 999
}
_perhs
local rhsnms "`r(rhsnms)'"
local nrhs "`r(nrhs)'"
_pecats
local numcats = `r(numcats)'
local catvals "`r(catvals)'"
local catnms "`r(catnms)'"
local catnms8 "`r(catnms8)'"
estimates hold `ologit'
*** estimate series of binary logits
local i = 1
while `i' <= `numcats'-1 {
local splitat : word `i' of `catvals'
tempvar dummy
quietly gen `dummy' = 0 if `depvar' <= `splitat' & `touse'==1
quietly replace `dummy' = 1 if `depvar' > `splitat' & `touse'==1
quietly `bcmd' `dummy' `rhsnms' `wtis' if `touse' == 1
_perhs
local binnrhs = "`r(nrhs)'"
if `nrhs' != `binnrhs' {
di in r "not all independent variables can be retained in all binary logits"
di in r "brant test cannot be computed"
exit 999
}
tempvar prob`i'
quietly predict `prob`i''
tempname b`i' V`i' bc`i'
mat `b`i'' = e(b)
mat `b`i'' = `b`i''[1, 1..`nrhs']
mat `V`i'' = e(V)
mat `V`i'' = `V`i''[1..`nrhs', 1..`nrhs']
mat `bc`i'' = e(b) /* with constant--for detail output only */
mat `bc`i'' = `bc`i'''
local outname "y>`splitat'"
local outname = substr("`outname'", 1, 8)
mat colnames `bc`i'' = "`outname'"
mat `bout' = nullmat(`bout'), `bc`i''
local i = `i' + 1
}
*** make variables for W(ml) matrices
local i = 1
while `i' <= `numcats'-1 {
local i2 = `i'
while `i2' <= `numcats'- 1 {
tempvar w`i'_`i2'
quietly gen `w`i'_`i2'' = `prob`i2'' - (`prob`i''*`prob`i2'')
local i2 = `i2' + 1
}
local i = `i' + 1
}
*** calculate variance Bm, Bl
local i = 1
while `i' <= `numcats'-1 {
local i2 = `i'
while `i2' <= `numcats'- 1 {
quietly {
* inverse(X'W(mm)X)
matrix accum `XpWmmX' = `rhsnms' [iw=`w`i'_`i''] if `touse'==1
matrix `iXpWmmX' = inv(`XpWmmX')
* X'W(ml)X
matrix accum `XpWmlX' = `rhsnms' [iw=`w`i'_`i2''] if `touse'==1
* inverse(X'W(ll)X)
matrix accum `XpWllX' = `rhsnms' [iw=`w`i2'_`i2''] if `touse'==1
matrix `iXpWllX' = inv(`XpWllX')
* product of three matrices
matrix `step1' = `iXpWmmX' * `XpWmlX'
tempname vb`i'_`i2'
matrix `vb`i'_`i2'' = `step1' * `iXpWllX'
}
mat `vb`i'_`i2''= `vb`i'_`i2''[1..`nrhs',1..`nrhs']
local i2 = `i2' + 1
}
local i = `i' + 1
}
* define var(B) matrix
local i = 1
while `i' <= `numcats'-1 {
tempname row`i'
local i2 = 1
while `i2' <= `numcats'- 1 {
quietly {
if `i'==`i2' { mat `row`i'' = nullmat(`row`i''), `V`i'' }
if `i'<`i2' { mat `row`i'' = nullmat(`row`i'') , `vb`i'_`i2'' }
if `i'>`i2' { mat `row`i'' = nullmat(`row`i'') , `vb`i2'_`i''' }
}
local i2 = `i2' + 1
}
local i = `i' + 1
}
* combine matrices
tempname varb
local i = 1
while `i' <= `numcats'-1 {
mat `varb' = nullmat(`varb') \ `row`i''
local i = `i' + 1
}
* make beta vector
tempname bstar
local i = 1
while `i' <= `numcats'-1 {
mat `bstar' = nullmat(`bstar') , `b`i''
local i = `i' + 1
}
mat `bstar' = `bstar''
* create design matrix for wald test; make I, -I, and 0 matrices
tempname id negid zero
local dim = `nrhs'
mat `id' = I(`dim')
mat rownames `id' = `rhsnms'
mat colnames `id' = `rhsnms'
mat `negid' = -1*`id'
mat rownames `negid' = `rhsnms'
mat colnames `negid' = `rhsnms'
mat `zero' = J(`dim', `dim', 0)
mat rownames `zero' = `rhsnms'
mat colnames `zero' = `rhsnms'
* dummy matrix
local i = 1
while `i' <= `numcats'-2 {
tempname drow`i'
local i2 = 1
while `i2' <= `numcats'- 1 {
quietly {
tempname feed
if `i2'==1 { mat `feed' = `id' }
else if `i2'-`i'==1 { mat `feed' = `negid' }
else { mat `feed' = `zero' }
mat `drow`i'' = nullmat(`drow`i'') , `feed'
}
local i2 = `i2' + 1
}
local i = `i' + 1
}
* combine matrices
local i = 1
while `i' <= `numcats'-2 {
mat `d' = nullmat(`d') \ `drow`i''
local i = `i' + 1
}
* terms of wald test
mat `DB' = `d' * `bstar'
mat `DBp' = `DB''
mat `step1' = `d'*`varb'
mat `step2' = `step1' * (`d'')
mat `iDvBDp' = inv(`step2')
*** calculate wald stat
tempname step1 wald waldout pout dfout
mat `step1' = `DBp' * `iDvBDp'
mat `wald' = `step1' * `DB'
sca `waldout' = `wald'[1,1]
sca `dfout' = `nrhs'*(`numcats'-2)
sca `pout' = chiprob(`dfout', `waldout')
tempname dtemp vbtemp bstemp
local i = 1
while `i' <= `nrhs' {
tempname d`i' vb`i' bstar`i'
local i2 = 1
while `i2' <= `numcats'-1 {
local row = ((`nrhs')*(`i2'-1)) + (`i')
tempname drow vbrow
local i3 = 1
while `i3' <= `numcats'-1 {
local column = ((`nrhs')*(`i3'-1)) + (`i')
if (`i2'<`numcats'-1) {
mat `dtemp' = `d'[`row',`column']
mat `drow' = nullmat(`drow') , `dtemp'
}
mat `vbtemp' = `varb'[`row',`column']
mat `vbrow' = nullmat(`vbrow') , `vbtemp'
local i3 = `i3' + 1
}
if (`i2'<`numcats'-1) { mat `d`i'' = nullmat(`d`i'') \ `drow' }
mat `vb`i'' = nullmat(`vb`i'') \ `vbrow'
mat `bstemp' = `bstar'[`row', 1]
mat `bstar`i'' = nullmat(`bstar`i'') \ `bstemp'
local i2 = `i2' + 1
}
local i = `i' + 1
}
*** wald test for each independent variable
tempname waldiv
local i = 1
while `i' <= `nrhs' {
tempname DB DBp iDvBDp step1 step2
mat `DB' = `d`i'' * `bstar`i''
mat `DBp' = `DB''
mat `step1' = `d`i''*`vb`i''
mat `step2' = `step1' * (`d`i''')
mat `iDvBDp' = inv(`step2')
tempname step1 wald`i'
mat `step1' = `DBp' * `iDvBDp'
mat `wald`i'' = `step1' * `DB'
mat `waldiv' = nullmat(`waldiv') \ `wald`i''
local i = `i' + 1
}
if "`detail'"!="" {
di _n in gr "Estimated coefficients from j-1 binary regressions"
mat list `bout', noheader
}
di _n in g "Brant Test of Parallel Regression Assumption"
di _n `smcl' in g " Variable `vline' chi2 p>chi2 df"
di `smcl' _dup(13) in g "`dash'" "`plussgn'" _dup(26) in g "`dash'"
di `smcl' in g " All `vline'" in y /*
*/ %10.2f `waldout' %9.3f `pout' %6.0f `dfout'
di `smcl' _dup(13) in g "`dash'" "`plussgn'" _dup(26) in g "`dash'"
* calculate p for individual wald tests
mat `pvals' = J(`nrhs', 1, 0)
local i = 1
local df = `numcats'-2
while `i' <= `nrhs' {
sca `ivchi' = `waldiv'[`i',1]
if `ivchi' >= 0 {
mat `pvals'[`i',1] = chiprob(`df',`ivchi')
}
if `ivchi' < 0 {
mat `pvals'[`i',1] = -999
}
local vnm : word `i' of `rhsnms'
*added for stata 7 compatibility
local printnm "`vnm'"
if "`vers7'"=="yes" { local printnm = abbrev("`printnm'", 12) }
di `smcl' in g %12s "`printnm'" _col(14) "`vline'" in y /*
*/ %10.2f `ivchi' %9.3f `pvals'[`i',1] %6.0f `df'
local i = `i' + 1
}
di `smcl' _dup(13) in g "`dash'" "`bottomt'" _dup(26) in g "`dash'"
di _n in g /*
*/ "A significant test statistic provides evidence that the parallel"
di in g "regression assumption has been violated."
mat `ivout' = `waldiv', `pvals'
mat rownames `ivout' = `rhsnms'
mat colnames `ivout' = chi2 p>chi2
estimates unhold `ologit'
return scalar chi2 = `waldout'
return scalar p = `pout'
return scalar df = `dfout'
return matrix ivtests `ivout'
end

View File

@ -0,0 +1,54 @@
.-
help for ^brant^ - 1.0.1 - 11/26/00
.-
Perform Brant test of parallel regression assumption after @ologit@
------------------------------------------------------------
^brant^ [^,^ ^detail^]
^brant^ is for use after ^ologit^; see help @ologit@.
Description
-----------
^brant^ performs a Brant test of the parallel regression assumption (also
called the proportional odds assumption) after ologit. The test compares
slope coefficients of the J-1 binary logits implied by the ordered regression
model. Stata reports both the results of an omnibus test for the entire
model and tests of the assumption for each of the independent variables in
the model.
The Brant test can only be computed if all of the independent variables in
the ordered model are retained in all of the implied binary models. This
is most likely not to be the case with models that have few observations in
the extreme categories and many independent variables.
Options
-------
^detail^ specifies that the coefficients for each of the estimated binary
logits should be presented.
Example
-------
. ^ologit warm yr89 male white age ed prst^
. ^brant^
. ^ologit warm yr89 male white age ed prst^
. ^brant, detail^
Also see
--------
Manual: ^[R] ologit^
.-
Authors: J. Scott Long and Jeremy Freese
www.indiana.edu/~jslsoc/spost.htm
spostsup@@indiana.edu

1402
Modules/ado/plus/backup.trk Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,235 @@
*! version 1.0.2 13oct2009 caplog by roywada@hotmail.com
*! captures a log file, possibly for use with logout or dataout
program define caplog
version 6
local logfile : log
version 7
* invisible to Stata 7
local Version7 ""
cap local Version7 `c(stata_version)'
if "`Version7'"=="" {
* it is version 7
local bind ""
*noi di in yel "limited functions under Stata 7"
}
else if `Version7'>=8.2 {
version 8.2
local bind "bind"
}
*qui log query
*if `"`r(status)'"'=="on" {
* qui log close
* local filename `"`r(filename)'"'
*}
if `"`logfile'"'~="" {
di ""
qui log close
local filename `"`logfile'"'
}
* embbed to avoid log being open
_caplog `0'
cap log close
*** c_locals coming back
* clickables
if "`tempfile'"~="tempfile" {
if "`smcl'"=="" {
local cl_text `"{browse `"`using1'"'}"'
noi di as txt `"`cl_text'"'
}
else {
local cl_text `"{stata `"view `using1'"':`using1'}"'
noi di as txt `"`cl_text'"'
}
}
cap log close
*if `"`filename'"'~="" {
* log using `"`filename'"', append
*}
end
********************************************************************************************
program define _caplog
version 7
local Version7 ""
cap local Version7 `c(stata_version)'
if "`Version7'"=="" {
* it is version 7
local bind ""
*noi di in yel "limited functions under Stata 7"
}
else if `Version7'>=8.2 {
version 8.2
local bind "bind"
}
* encase the colon in file name in quotes, avoiding string function length limits
local behind `"`0'"'
local 0 ""
gettoken front behind: behind, parse(" ,")
local 0 ""
local done 0
while `"`front'"'~="" & `done'==0 {
if `"`front'"'=="using" {
gettoken rest behind: behind, parse(" ,")
* strip off quotes
gettoken first second: rest, parse(" ")
cap local rest: list clean local(rest)
* take off colon at the end
local goldfish ""
if index(`"`rest'"',":")~=0 {
local end=substr(`"`rest'"',length(`"`rest'"'),length(`"`rest'"'))
if "`end'"==":" {
local rest=substr(`"`rest'"',1,`=length(`"`rest'"')-1')
local goldfish " : "
}
}
* colon reattached with a space at the end
* .txt attached here, SMCL TO BE FIXED LATER
local rabbit `"""'
if index(`"`rest'"', ".")==0 {
local using `"`rabbit'`rest'.txt`rabbit'`goldfish'"'
}
else {
local using `"`rabbit'`rest'`rabbit'`goldfish'"'
}
local 0 `"`0' using `using' `behind'"'
local done 1
}
else {
local 0 `"`0' `front'"'
gettoken front behind: behind, parse(" ,")
}
}
gettoken first second : 0, parse(":") `bind' match(par) quotes
local 0 `"`first'"'
while `"`first'"'~=":" & `"`first'"'~="" {
gettoken first second : second, parse(":") `bind' match(par) quotes
}
if `"`0'"'==":" {
* colon only when shorthand combined with prefix
local 0
}
else {
local _0 `"`0'"'
}
*** shorthand syntax if [using] is missing
syntax using/ [, replace append tempfile text smcl subspace]
if "`smcl'"=="smcl" {
if index(`"`using'"', ".txt")~=0 {
local temp=substr(`"`using'"',1,length(`"`using'"')-4)
local using `"`temp'.smcl"'
}
}
if "`text'"~="" & "`smcl'"~="" {
di "cannot choose both {opt text} and {opt smcl}"
exit 198
}
if "`text'"=="" & "`smcl'"=="" {
local text "text"
}
cap confirm file `"`using'"'
if !_rc & "`replace'"~="replace" & "`append'"~="append" {
* it exists
noi di in red `"`using' exists; specify {opt replace} or {opt append}"'
exit 198
}
* goes with `second'
if `"`second'"'~="" {
local _colon ":"
}
qui {
if "`subspace'"=="subspace" {
* fix the gaps in the value labels
ds8
foreach var in `r(varlist)'{
local temp : var label `var'
local temp = subinstr(`"`temp'"'," ","_",.)
label var `var' `"`temp'"'
}
}
}
* regular stuff
if `"`using'"'~="" {
* prefix use using file
qui log using `"`using'"', `replace' `append' `text' `smcl'
`second'
}
else {
* prefix use temp file
qui log using `"`using'"', `replace' `append' `text' `smcl'
`second'
}
* clickables
c_local smcl "`smcl'"
c_local using1 `"`using'"'
c_local tempfile `"`tempfile'"'
end
********************************************************************************************
*** ripped from outreg2 Mar 2009
program define ds8
* get you the list of variable like -ds- does for version 8
version 7.0
qui ds
if "`r(varlist)'"=="" {
local dsVarlist ""
foreach var of varlist _all {
local dsVarlist "`dsVarlist' `var'"
}
c_local dsVarlist `dsVarlist'
}
else {
c_local dsVarlist `r(varlist)'
}
end
/*
* version 1.0.1 May2009 caplog by roywada@hotmail.com
smcl accepted
version control fixed
1.0.2 close the log file at the end to avoid the possibility of it being left open

View File

@ -0,0 +1,64 @@
{smcl}
{* 13oct2009}{...}
{cmd:help caplog}
{hline}
{title:Title}
{p2colset 5 16 22 2}{...}
{p2col :{hi: caplog} {hline 2}}captures a ASCII log file (for use with {cmd:{help logout}}){p_end}
{marker s_Syntax}
{title:Syntax}
{p 4 4 6}
{cmdab:caplog} using filename [, {it:options}] : command
{marker s_Description}
{title:Description}
{p 4 4 6}
{cmd:caplog} provides a fast and easy way to capture text-based log file, possibly for use with
{cmd:{help logout}}. The default is text log files.
{title:Command}
{p 4 12 6}Any command accepted, i.e. { help estimation commands}, tabulation, summary, descrition, etc.
{marker s_Options}
{title:Options}
{dlgtab:Main}
{p 4 12 6}{opt replace} Replace pre-exiting log files.{p_end}
{p 4 12 6}{opt append} Append. {p_end}
{p 4 12 6}{opt smcl} Save smcl file instead of text file. {p_end}
{marker s_0}
{title:Examples}
{p 4 4 6}{cmd:* table}{p_end}
{p 4 4 6}{stata sysuse auto, clear}{p_end}
{p 4 4 6}{stata `"caplog using mystuff.txt, replace: table trunk rep78 , c(n mpg mean mpg sd mpg median mpg)"'}{p_end}
{p 4 4 6}{stata `"caplog using mystuff.txt, append: table trunk rep78 , c(n mpg mean mpg sd mpg median mpg)"'}{p_end}
{title:Remarks}
{p 4 12 6}PREVIOUSLY OPENED LOG FILES WILL BE CLOSED BY CAPLOG.{p_end}
{p 4 12 6}version 7: do not include colon characters (:) in the file path. Use -cd- instead.{p_end}
{title:Author}
{p 4 4 6}Roy Wada{p_end}
{p 4 4 6}roywada@hotmail.com{p_end}

View File

@ -0,0 +1,167 @@
*! 1.0.1 - allow either y() or choice(); yrank() or rank()
* 1.0.0 - revise option names: case->casevar; id->case
* 0.2.4 - small text formatting error
* 0.2.3 - change _optnum to altnum and add as option
* 0.2.2 - allow id() to specify new variable instead of using _id - 15Jul2005
* 0.2.1 - change specification of csv from varlist to case() - 11Jul2005
program define case2alt
version 9
syntax , [y(varname numeric) YRank(name) case(name) ///
Generate(name) REplace CASEVars(namelist) Alt(namelist) noNames altnum(name) ///
Choice(name) Rank(name)] // allow choice() and rank() to sub for y() and yrank()
** CHANGE 9/8/05: allow choice() instead of y() and allow rank() instead of yrank()
if "`choice'" != "" {
local y "`choice'"
local choice ""
}
if "`rank'" != "" {
local yrank "`rank'"
local rank ""
}
* either y() or yrank() must be specified
if "`y'" == "" & "`yrank'" == "" {
di as err "option y() or yrank() must be specified"
exit 198
}
* figure out if rankdata is being specified
local rankdata "yes"
if "`y'" != "" {
local rankdata "no"
}
if "`rankdata'" == "no" & "`generate'" == "" & "`replace'" == "" {
di as txt "(note: " as res "choice " as txt "used for outcome since no variable specified by gen())"
confirm new variable choice
local generate "choice"
}
local choice "`generate'"
if "`case'" == "" {
di as txt "(note: variable " as res "_id" as txt " used since case() not specified)"
confirm new variable _id
gen _id = _n
local case "_id"
}
if "`altnum'" == "" {
di as txt "(note: variable " as res "_altnum" as txt " used since altnum() not specified)"
confirm new variable _altnum
local altnum "_altnum"
}
capture confirm variable `case'
if _rc != 0 {
gen `case' = _n
}
if "`rankdata'" == "no" {
_pecats `y'
local catval = r(catvals)
local catnms = r(catnms)
local numcats = r(numcats)
forvalues i = 1(1)`numcats' {
local y`i' : word `i' of `catval'
local ynm`i' : word `i' of `catnms'
confirm integer number `y`i''
gen _ytemp`y`i'' = 1
}
local ytemp "_ytemp"
}
if "`rankdata'" == "yes" {
capture label drop `altnum'
foreach rvar of varlist `yrank'* {
local rvarnum = subinstr("`rvar'", "`yrank'", "", 1)
local rvarnumchk = real("`rvarnum'")
if "`rvarnum'" == "`rvarnumchk'" {
local rvarlabel : variable label `rvar'
label define `altnum' `rvarnum' "`rvarlabel'", modify
}
}
}
qui reshape long `ytemp' `yrank' `alt', i(`case') j(`altnum')
capture drop _ytemp
if "`rankdata'" == "no" {
tempvar ychosen
gen `ychosen' = `y'
if "`replace'" == "replace" {
drop `y'
local choice "`y'"
}
gen `choice' = (`altnum' == `ychosen') if `altnum' < .
}
if "`rankdata'" == "yes" {
label define `altnum', modify
label values `altnum' `altnum'
_pecats `altnum'
local catval = r(catvals)
local catnms = r(catnms)
local numcats = r(numcats)
forvalues i = 1(1)`numcats' {
local y`i' : word `i' of `catval'
local ynm`i' : word `i' of `catnms'
confirm integer number `y`i''
}
* if generate option specified, rename stub to that
if "`generate'" != "" {
rename `yrank' `generate'
local yrank "`generate'"
}
}
forvalues i = 1(1)`numcats' {
local name = "y`y`i''"
if "`names'" != "nonames" & "`ynm`i''" != "`y`i''" {
local name "`ynm`i''"
}
qui gen ytemp`y`i'' = (`altnum' == `y`i'') if `altnum' < .
label variable ytemp`y`i'' "`ynm`i''"
if "`casevars'" != "" {
foreach var of varlist `casevars' {
qui gen `name'X`var' = `var' * ytemp`y`i''
}
}
local ylist "`ylist' `name'*"
clonevar `name' = ytemp`y`i''
drop ytemp`y`i''
}
* output
if "`rankdata'" == "no" {
di _n as txt "choice indicated by:" as res " `choice'"
}
if "`rankdata'" == "yes" {
di _n as txt "ranks indicated by:" as res " `yrank'"
}
di as txt "case identifier:" as res " `case'"
di as txt "case-specific interactions:" as res "`ylist'"
if "`alt'" != "" {
di as txt "alternative-specific variables:" as res " `alt'"
}
end

View File

@ -0,0 +1,121 @@
{smcl}
{* 05Dec2005jf}
{hline}
help for {hi:case2alt}{right:03Nov2005}
{hline}
{p 4 4 2}
{title:Convert data from one observation per case to one observation per alternative per case}
{p 4 12 2}{cmd:case2alt} {cmd:,}
{{cmdab:choice(}{it:varname}{cmd:)} | {cmdab:rank(}{it:stubname}{cmd:)}}
[{cmdab:a:lt(}{it:stubnames}{cmd:)}
{cmdab:casev:ars(}{it:varlist}{cmd:)}
{cmdab:case(}{it:varname}{cmd:)}
{cmdab:g:enerate(}{it:newvar}{cmd:)}
{cmdab:rep:lace}
{cmdab:altnum(}{it:varname}{cmd:)}
{cmdab:non:ames}]
{title:Description}
{p 4 4 2}
{cmd:case2alt} is intended to be used to configure data for the estimation of estimation
of regression models for alternative-specific data, such as {helpb clogit},
{helpb rologit} or {helpb asmprobit}. {cmd:case2alt} presumes that you have data
where each observation corresponds to an individual case and that you want to convert
the data to the form in which each observation corresponds to an alternative for a specific case.
{p 4 4 2}
Imagine that you have data with an outcome that has four alternatives, with values 1, 2, 3 and 8.
{cmd:case2alt} will reshape the data so that there are n*4 observations. If you specify an
identifying variable with the {cmd:casevars()} option, this variable will continue to identify
unique cases; otherwise, new variable _id will identify cases.
{p 4 4 2}
A new variable, called either _altnum or the name specified in {cmd:altnum()},
will identify the alternatives within a case. Additionally, however, {cmd:case2alt}
will create dummy variables y{it:value} that also identify alternatives.
In our example the new dummy variables y1, y2, y3 and y8 will be created.
Interactions will also be created with these dummies and any variables
specified in {cmd:case()}. For the variable educ, {cmd:case2alt} will create
variables y1_educ, y2_educ, y3_educ, and y8_educ.
{p 4 4 2}
If we have simple choice variable, then {cmd:case2alt} will create an outcome
variable based on {cmd:y()} that contains 1 in the observation corresponding to
the selected alternative and 0 for other alternatives. We can specify the name of
this new outcome variable using the {cmd:gen()} option, or we can have it be the
same as {opt y()} using the {cmd:replace} option, or (by default) we
can have it be named choice.
{p 4 4 2}
After using {cmd:case2alt}, we would be able to estimate models like {helpb clogit} by typing, e.g.,:
{p 4 4 2}
{cmd:. clogit choice y1* y2* y3*, group(_id)}
{p 4 4 2}
Alternative-specific variables are specified using the {cmd:alt()} option.
The contents of {cmd:alt()} should be {it:stubnames}, corresponding to a series
of variables that contain the alternative-specific values.
Specifying {cmd:alt(time)}, in our example, would imply that there are
variables time1, time2, time3 and time8 in our case-level data.
{p 4 4 2}
Case-specific variables are specified using the {cmd:casevars()} option,
where the contents should be a {varlist}. Neither the outcome nor id variable
should be included in {cmd:casevars()}.
{p 4 4 2}
If we have ranked data, we can specify the ranked outcome with the
{cmd:rank()} outcome. The content of rank should again be a stubname.
Specifying {cmd:rank(}rank{cmd:)} in our example would assume there are
variables rank1, rank2, rank3, rank8 that contain the relevant information
on the ranks of each alternative.
{title:Options}
{p 4 8 2}{opth choice(varname)} or {opth rank(stubname)} is required. {varname} is the
variable that indicates the value of the selected alternative.
In the case of ranked outcome, {it:stubname} with {opt rank()} will contain
the stub for the names of variables that contain information about ranks of alternatives.
{p 4 8 2}{opth case(varname)} indicates the variable, either existing or to be
created, that identifies individual cases.
If {varname} is unspecified, a new variable named _id will be created.
{p 4 8 2}{cmd:alt(}{it:stubnames}{cmd:)} contains the {it:stubnames} for
alternative-specific variables. This requires that variables {it:stubname}# exist
for each value of an alternative.
{p 4 8 2}{opth casevars(varlist)} contains the names of the case-specific variables
(not including the id or outcome variable).
{p 4 8 2}{opth gen:erate(newvar)} and {opt replace} are used to name the variable that
contain 1 for the selected alternative and 0 for non-selected alternatives.
The variable will be named {newvar} if {newvar} is specified; the name of
the variable specified in {cmd: y()} if {opt replace} is specified; and will be named {it:choice} otherwise.
In the case of ranked data, the ranks will be contained in variable specified as the
stub in {opt yrank()} and {opt gen:erate()} or {opt replace} will be ignored.
{p 4 8 2}{opth altnum(varname)} indicates the name of the new variable used to indicate
the alternatives. _altnum will be used if altnum() is not specified.
{p 4 8 2}{opt non:ames} indicates that the case-specific interactions should be named
y# instead of using the value labels of the outcome variable.
{title:Example}
{p 4 4 2}{cmd:. use "http://www.stata-press.com/data/lfr/nomocc2.dta", clear}{break}
{cmd:. mlogit occ white ed exper}{break}
{cmd:. case2alt, choice(ed) casevars(white ed exper) replace nonames}{break}
{cmd:. clogit occ y1* y2* y3* y4*, group(_id)}{break}
{p 4 4 2}{cmd:. case2alt, rank(rank92) casevars(hntest) alt(rank04) case(id)}
{title:Authors}
Jeremy Freese and J. Scott Long
www.indiana.edu/~jslsoc/spost.htm
spostsup@indiana.edu

679
Modules/ado/plus/c/cfa1.ado Normal file
View File

@ -0,0 +1,679 @@
*! Confirmatory factor analysis with a single factor: v.2.2
*! Stas Kolenikov, skolenik-gmail-com
program define cfa1, eclass
version 9.1
if replay() {
if ("`e(cmd)'" != "cfa1") error 301
Replay `0'
}
else Estimate `0'
end
program Estimate `0', eclass
syntax varlist(numeric min=3) [if] [in] [aw pw / ] ///
, [unitvar FREE POSvar FROM(str) CONSTRaint(numlist) LEVel(int $S_level) ///
ROBust VCE(string) CLUster(passthru) SVY SEArch(passthru) * ]
* syntax: cfa1 <list of the effect indicators>
* untivar is for the identification condition of the unit variance of the latent variable
unab varlist : `varlist'
tokenize `varlist'
local q: word count `varlist'
marksample touse
preserve
qui keep if `touse'
* weights!
global CFA1N = _N
/*
* we'll estimate the means instead
qui foreach x of varlist `varlist' {
sum `x', meanonly
replace `x' = `x'-r(mean)
* deviations from the mean
}
*/
if "`weight'" != "" {
local mywgt [`weight'=`exp']
}
if "`robust'`cluster'`svy'`weight'"~="" {
local needed 1
}
else {
local needed 0
}
Parse `varlist' , `unitvar'
local toml `r(toml)'
if "`from'" == "" {
local from `r(tostart)', copy
}
* identification
constraint free
global CFA1constr `r(free)'
if "`unitvar'" ~= "" {
* identification by unit variance
constraint $CFA1constr [phi]_cons = 1
}
else if "`free'"=="" {
* identification by the first variable
constraint $CFA1constr [`1']_cons = 1
}
else {
* identification imposed by user
global CFA1constr
}
local nconstr : word count `constraint'
global CFA1PV = ("`posvar'" != "")
if "`posvar'" ~= "" {
di as text _n "Fitting the model without restrictions on error variances..."
}
* variance estimation
local vce = trim("`vce'")
if "`vce'" == "boot" local vce bootstrap
if "`vce'" == "sbentler" {
global CFA1SBV = 1
local vce
}
else {
if index("robustoimopg","`vce'") {
local vce vce(`vce')
}
else {
di as err "`vce' estimation is not supported"
if "`vce'" == "bootstrap" | "`vce'" == "boot" {
di as err "try {help bootstrap} command directly"
}
exit 198
}
}
tempname ilog1 ilog2
Tryit ml model lf cfa1_lf `toml' `mywgt', constraint($CFA1constr `constraint') ///
init (`from') maximize nooutput `options' `search' ///
`svy' `cluster' `robust' `vce'
/*
ml model lf cfa1_lf `toml' `mywgt', ///
constraint($CFA1constr `constraint') `svy' `robust' `cluster' ///
maximize
* ml check
* ml search, rep(5)
ml init `from', copy
cap noi ml maximize , `options'
*/
mat `ilog1' = e(ilog)
local nz = 0
if $CFA1PV {
* determine if refitting the model is needed
tempname ll_unr
scalar `ll_unr' = e(ll)
forvalues i=1/`q' {
if [``i''_v]_cons <0 {
constraint free
local ccc = `r(free)'
const define `ccc' [``i''_v]_cons = 0
local zerolist `zerolist' `ccc'
local ++nz
}
}
local zerolist = trim("`zerolist'")
global CFA1constr $CFA1constr `zerolist'
if "`zerolist'" ~= "" {
di as text _n "Fitting the model with some error variances set to zero..."
Tryit ml model lf cfa1_lf `toml' `mywgt' , constraint($CFA1constr `constraint') ///
init (`from') maximize nooutput `options' `search' ///
`svy' `robust' `cluster' `vce'
mat `ilog2' = e(ilog)
}
* adjust degrees of freedom!
}
* we better have this before Satorra-Bentler
if "`unitvar'" ~= "" {
ereturn local normalized Latent Variance
}
else if "`free'"=="" {
ereturn local normalized `1'
}
* work out Satorra-Bentler estimates
if "$CFA1SBV"!="" {
* repost Satorra-Bentler covariance matrix
tempname SBVar SBV Delta Gamma
cap SatorraBentler
if _rc {
di as err "Satorra-Bentler standard errors are not supported for this circumstance; revert to vce(oim)"
global CFA1SBV
}
else {
mat `SBVar' = r(SBVar)
mat `Delta' = r(Delta)
mat `Gamma' = r(Gamma)
mat `SBV' = r(SBV)
ereturn repost V = `SBVar'
ereturn matrix SBGamma = `Gamma', copy
ereturn matrix SBDelta = `Delta', copy
ereturn matrix SBV = `SBV', copy
ereturn local vce SatorraBentler
}
}
* get the covariance matrix and the number of observations!
***********************************************************
tempname lambda vars phi S Sindep Sigma trind eb
qui mat accum `S' = `varlist', dev nocons
mat `S' = `S' / $CFA1N
* implied matrix
mat `eb' = e(b)
mat `lambda' = `eb'[1,1..`q']
mat `vars' = `eb'[1,`q'+1..2*`q']
scalar `phi' = `eb'[1,3*`q'+1]
mat `Sigma' = `lambda''*`phi'*`lambda' + diag(`vars')
mat `Sindep' = diag(vecdiag(`S'))
* test against independence
mat `trind' = trace( syminv(`Sindep') * `S' )
local trind = `trind'[1,1]
ereturn scalar ll_indep = -0.5 * `q' * $CFA1N * ln(2*_pi) - 0.5 * $CFA1N * ln(det(`Sindep')) - 0.5 * $CFA1N * `trind'
ereturn scalar lr_indep = 2*(e(ll)-e(ll_indep))
ereturn scalar df_indep = `q'-`nz'-`nconstr'
ereturn scalar p_indep = chi2tail(e(df_indep),e(lr_indep))
* goodness of fit test
ereturn scalar ll_u = -0.5 * `q' * $CFA1N * ln(2*_pi) - 0.5 * $CFA1N * ln(det(`S')) - 0.5 * `q' * $CFA1N
ereturn scalar lr_u = -2*(e(ll)-e(ll_u))
ereturn scalar df_u = `q'*(`q'+1)*.5 - (2*`q' - `nz' - `nconstr')
* wrong if there are any extra constraints in -constraint- command!!!
ereturn scalar p_u = chi2tail(e(df_u),e(lr_u))
ereturn matrix ilog1 `ilog1'
cap ereturn matrix ilog2 `ilog2'
* Satorra-Bentler corrections
if "$CFA1SBV"!="" {
* compute the corrected tests, too
* Satorra-Bentler 1994
tempname U trUG2 Tdf
mat `U' = `SBV' - `SBV'*`Delta'*syminv(`Delta''*`SBV'*`Delta')*`Delta''*`SBV'
ereturn matrix SBU = `U'
mat `U' = trace( e(SBU)*`Gamma' )
ereturn scalar SBc = `U'[1,1]/e(df_u)
ereturn scalar Tscaled = e(lr_u)/e(SBc)
ereturn scalar p_Tscaled = chi2tail( e(df_u), e(Tscaled) )
mat `trUG2' = trace( e(SBU)*`Gamma'*e(SBU)*`Gamma')
ereturn scalar SBd = `U'[1,1]*`U'[1,1]/`trUG2'[1,1]
ereturn scalar Tadj = ( e(SBd)/`U'[1,1]) * e(lr_u)
ereturn scalar p_Tadj = chi2tail( e(SBd), e(Tadj) )
* Yuan-Bentler 1997
* weights!
ereturn scalar T2 = e(lr_u)/(1+e(lr_u)/e(N) )
ereturn scalar p_T2 = chi2tail( e(df_u), e(T2) )
}
if "`posvar'" ~= "" {
ereturn scalar lr_zerov = 2*(`ll_unr' - e(ll))
ereturn scalar df_zerov = `nz'
local replay_opt posvar llu(`ll_unr')
}
ereturn local cmd cfa1
Replay , `replay_opt' level(`level')
Finish
restore
ereturn repost, esample(`touse')
end
program define Tryit
cap noi `0'
local rc=_rc
if `rc' {
Finish
exit `rc'
}
end
program define Finish
* finishing off
constraint drop $CFA1constr
global CFA1S
global CFA1N
global CFA1PV
global CFA1theta
global CFA1arg
global CFA1data
global CFA1constr
global CFA1vars
global CFA1SBV
end
program define Replay
syntax, [posvar llu(str) level(passthru)]
di _n as text "Log likelihood = " as res e(ll) _col(59) as text "Number of obs = " as res e(N)
di as text "{hline 13}{c TT}{hline 64}"
di as text " {c |} Coef. Std. Err. z P>|z| [$S_level% Conf. Interval]"
di as text "{hline 13}{c +}{hline 64}"
tempname vce
mat `vce' = e(V)
local q = colsof(`vce')
local q = (`q'-1)/3
local a : colfullnames(`vce')
tokenize `a'
di as text "Lambda{col 14}{c |}"
forvalues i = 1/`q' {
gettoken v`i' : `i' , parse(":")
_diparm `v`i'' , label("`v`i''") prob `level'
}
di as text "Var[error]{col 14}{c |}"
forvalues i = 1/`q' {
_diparm `v`i''_v , label("`v`i''") prob `level'
}
di as text "Means{col 14}{c |}"
forvalues i = 1/`q' {
_diparm `v`i''_m , label("`v`i''") prob `level'
}
di as text "Var[latent]{col 14}{c |}"
_diparm phi , label("phi1") prob
di as text "{hline 13}{c +}{hline 64}"
di as text "R2{col 14}{c |}"
forvalues i = 1/`q' {
di as text %12s "`v`i''" "{col 14}{c |}{col 20}" ///
as res %6.4f (_b[`v`i'':_cons]^2*_b[phi:_cons]) / ///
(_b[`v`i'':_cons]^2*_b[phi:_cons] + _b[`v`i''_v:_cons])
}
di as text "{hline 13}{c BT}{hline 64}"
if e(df_u)>0 {
di as text _n "Goodness of fit test: LR = " as res %6.3f e(lr_u) ///
as text _col(40) "; Prob[chi2(" as res %2.0f e(df_u) as text ") > LR] = " as res %6.4f e(p_u)
}
else {
di as text "No degrees of freedom to perform the goodness of fit test"
}
di as text "Test vs independence: LR = " as res %6.3f e(lr_indep) ///
as text _col(40) "; Prob[chi2(" as res %2.0f e(df_indep) as text ") > LR] = " as res %6.4f e(p_indep)
if "`e(vce)'" == "SatorraBentler" & e(df_u)>0 {
* need to report all those corrected statistics
di as text _n "Satorra-Bentler Tbar" _col(26) "= " as res %6.3f e(Tscaled) ///
as text _col(40) "; Prob[chi2(" as res %2.0f e(df_u) as text ") > Tbar] = " as res %6.4f e(p_Tscaled)
di as text "Satorra-Bentler Tbarbar" _col(26) "= " as res %6.3f e(Tadj) ///
as text _col(40) "; Prob[chi2(" as res %4.1f e(SBd) as text ") > Tbarbar] = " as res %6.4f e(p_Tadj)
di as text "Yuan-Bentler T2" _col(26) "= " as res %6.3f e(T2) ///
as text _col(40) "; Prob[chi2(" as res %2.0f e(df_u) as text ") > T2] = " as res %6.4f e(p_T2)
}
if "`posvar'" ~= "" {
* just estimated?
if "`llu'" == "" {
di as err "cannot specify -posvar- option, need to refit the whole model"
}
else {
if e(df_zerov)>0 {
di as text "Likelihood ratio against negative variances: LR = " as res %6.3f e(lr_zerov)
di as text "Conservative Prob[chi2(" as res %2.0f e(df_zerov) as text ") > LR] = " ///
as res %6.4f chi2tail(e(df_zerov),e(lr_zerov))
}
else {
di as text "All variances are non-negative, no need to test against zero variances"
}
}
}
end
program define Parse , rclass
* takes the list of variables and returns the appropriate ml model statement
syntax varlist , [unitvar]
global CFA1arg
global CFA1theta
global CFA1vars
local q : word count `varlist'
* lambdas
forvalues i = 1/`q' {
local toml `toml' (``i'': ``i'' = )
local tostart `tostart' 1
global CFA1arg $CFA1arg g_``i''
global CFA1theta $CFA1theta l_`i'
global CFA1vars $CFA1vars ``i''
}
* variances
forvalues i = 1/`q' {
local toml `toml' (``i''_v: )
local tostart `tostart' 1
global CFA1arg $CFA1arg g_``i''_v
global CFA1theta $CFA1theta v_`i'
}
* means
forvalues i = 1/`q' {
local toml `toml' (``i''_m: )
qui sum ``i'', mean
local mean = r(mean)
local tostart `tostart' `mean'
global CFA1arg $CFA1arg g_``i''_m
global CFA1theta $CFA1theta m_`i'
}
* variance of the factor
local toml `toml' (phi: )
local tostart `tostart' 1
global CFA1arg $CFA1arg g_Phi
global CFA1theta $CFA1theta phi
* done!
return local toml `toml'
return local tostart `tostart'
end
**************************** Satorra-Bentler covariance matrix code
program SatorraBentler, rclass
version 9.1
syntax [, noisily]
* assume the maximization completed, the results are in memory as -ereturn data-
* we shall just return the resulting matrix
if "`e(normalized)'" == "" {
di as err "cannot compute Satorra-Bentler variance estimator with arbitrary identification... yet"
exit 198
}
* assume sample is restricted to e(sample)
* preserve
* keep if e(sample)
* get the variable names
tempname VV bb
mat `VV' = e(V)
local q = rowsof(`VV')
local p = (`q'-1)/3
local eqlist : coleq `VV'
tokenize `eqlist'
forvalues k=1/`p' {
local varlist `varlist' ``k''
}
* compute the implied covariance matrix
tempname Lambda Theta phi Sigma
mat `bb' = e(b)
mat `Lambda' = `bb'[1,1..`p']
mat `Theta' = `bb'[1,`p'+1..2*`p']
scalar `phi' = `bb'[1,`q']
mat `Sigma' = `Lambda''*`phi'*`Lambda' + diag(`Theta')
* compute the empirical cov matrix
tempname SampleCov
qui mat accum `SampleCov' = `varlist' , nocons dev
* weights!!!
mat `SampleCov' = `SampleCov' / (r(N)-1)
* compute the matrix Gamma
`noisily' di as text "Computing the Gamma matrix of fourth moments..."
tempname Gamma
SBGamma `varlist'
mat `Gamma' = r(Gamma)
return add
* compute the duplication matrix
* Dupl `p'
* let's call it from within SBV!
* compute the V matrix
`noisily' di as text "Computing the V matrix..."
SBV `SampleCov' `noisily'
tempname V
mat `V' = r(SBV)
return add
* compute the Delta matrix
`noisily' di as text "Computing the Delta matrix..."
tempname Delta
mata : SBDelta("`bb'","`Delta'")
*** put the pieces together now
tempname DeltaId
* enact the constraints!
SBconstr `bb'
mat `DeltaId' = `Delta' * diag( r(Fixed) )
* those should be in there, but it never hurts to fix!
if "`e(normalized)'" == "Latent Variance" {
* make the last column null
mat `DeltaId' = ( `DeltaId'[1...,1...3*`p'] , J(rowsof(`Delta'), 1, 0) )
}
else if "`e(normalized)'" ~= "" {
* normalization by first variable
local idvar `e(normalized)'
if "`idvar'" ~= "`1'" {
di as err "cannot figure out the identification variable"
exit 198
}
mat `DeltaId' = ( J(rowsof(`Delta'), 1, 0) , `DeltaId'[1...,2...] )
}
local dcnames : colfullnames `bb'
local drnames : rownames `Gamma'
mat colnames `DeltaId' = `dcnames'
mat rownames `DeltaId' = `drnames'
return matrix Delta = `DeltaId', copy
tempname VVV
mat `VVV' = ( `DeltaId'' * `V' * `DeltaId' )
mat `VVV' = syminv(`VVV')
mat `VVV' = `VVV' * ( `DeltaId'' * `V' * `Gamma' * `V' * `DeltaId' ) * `VVV'
* add the covariance matrix for the means, which is just Sigma/_N
* weights!
tempname CovM
mat `CovM' = ( J(2*`p',colsof(`bb'),0) \ J(`p',2*`p',0) , `Sigma', J(`p',1,0) \ J(1, colsof(`bb'), 0) )
mat `VVV' = (`VVV' + `CovM')/_N
return matrix SBVar = `VVV'
end
* of satorrabentler
program define SBGamma, rclass
syntax varlist
unab varlist : `varlist'
tokenize `varlist'
local p: word count `varlist'
forvalues k=1/`p' {
* make up the deviations
* weights!!!
qui sum ``k'', meanonly
tempvar d`k'
qui g double `d`k'' = ``k'' - r(mean)
local dlist `dlist' `d`k''
}
local pstar = `p'*(`p'+1)/2
forvalues k=1/`pstar' {
tempvar b`k'
qui g double `b`k'' = .
local blist `blist' `b`k''
}
* convert into vech (z_i-bar z)(z_i-bar z)'
mata : SBvechZZtoB("`dlist'","`blist'")
* blist now should contain the moments around the sample means
* we need to get their covariance matrix
tempname Gamma
qui mat accum `Gamma' = `blist', dev nocons
* weights!
mat `Gamma' = `Gamma'/(_N-1)
mata : Gamma = st_matrix( "`Gamma'" )
* make nice row and column names
forvalues i=1/`p' {
forvalues j=`i'/`p' {
local namelist `namelist' ``i''_X_``j''
}
}
mat colnames `Gamma' = `namelist'
mat rownames `Gamma' = `namelist'
return matrix Gamma = `Gamma'
end
* of computing Gamma
program define SBV, rclass
args A noisily
tempname D Ainv V
local p = rowsof(`A')
`noisily' di as text "Computing the duplication matrix..."
mata : Dupl(`p',"`D'")
mat `Ainv' = syminv(`A')
mat `V' = .5*`D''* (`Ainv' # `Ainv') * `D'
return matrix SBV = `V'
end
* of computing V
* need to figure out whether a constraint has the form parameter = value,
* and to nullify the corresponding column
program define SBconstr, rclass
args bb
tempname Iq
mat `Iq' = J(1,colsof(`bb'),1)
tokenize $CFA1constr
while "`1'" ~= "" {
constraint get `1'
local constr `r(contents)'
gettoken param value : constr, parse("=")
* is the RHS indeed a number?
local value = substr("`value'",2,.)
confirm number `value'
* parse the square brackets and turn them into colon
* replace the opening brackets with nothing, and closing brackets, with :
local param = subinstr("`param'","["," ",1)
local param = subinstr("`param'","]",":",1)
local param = trim("`param'")
local coln = colnumb(`bb',"`param'" )
mat `Iq'[1,`coln']=0
mac shift
}
return matrix Fixed = `Iq'
end
cap mata : mata drop SBvechZZtoB()
cap mata : mata drop Dupl()
cap mata : mata drop SBDelta()
mata:
void SBvechZZtoB(string dlist, string blist) {
// view the deviation variables
st_view(data=.,.,tokens(dlist))
// view the moment variables
// blist=st_local("blist")
st_view(moments=.,.,tokens(blist))
// vectorize!
for(i=1; i<=rows(data); i++) {
B = data[i,.]'*data[i,.]
moments[i,.] = vech(B)'
}
}
void Dupl(scalar p, string Dname) {
pstar = p*(p+1)/2
Ipstar = I(pstar)
D = J(p*p,0,.)
for(k=1;k<=pstar;k++) {
D = (D, vec(invvech(Ipstar[.,k])))
}
st_matrix(Dname,D)
}
void SBDelta(string bbname, string DeltaName) {
bb = st_matrix(bbname)
p = (cols(bb)-1)/3
Lambda = bb[1,1..p]
Theta = bb[1,p+1..2*p]
phi = bb[1,cols(bb)]
Delta = J(0,cols(bb),.)
for(i=1;i<=p;i++) {
for(j=i;j<=p;j++) {
DeltaRow = J(1,cols(Delta),0)
for(k=1;k<=p;k++) {
// derivative wrt lambda_k
DeltaRow[k] = (k==i)*Lambda[j]*phi + (j==k)*Lambda[i]*phi
// derivative wrt sigma^2_k
DeltaRow[p+k] = (i==k)*(j==k)
}
DeltaRow[cols(Delta)] = Lambda[i]*Lambda[j]
Delta = Delta \ DeltaRow
}
}
st_matrix(DeltaName,Delta)
}
end
* of mata piece
***************************** end of Satorra-Bentler covariance matrix code
exit
History:
v.1.0 -- May 19, 2004: basic operation, method d0
v.1.1 -- May 19, 2004: identification by -constraint-
common -cfa1_ll-
from()
v.1.2 -- May 21, 2004: method -lf-, robust
constraint free
v.1.3 -- unknown
v.1.4 -- Feb 15, 2005: pweights, arbitrary constraints
v.2.0 -- Feb 28, 2006: update to version 9 using Mata
v.2.1 -- Apr 11, 2006: whatever
v.2.2 -- Apr 13, 2006: Satorra-Bentler standard errors and test corrections
-vce- option
Apr 14, 2006: degrees of freedom corrected for # constraints
July 5, 2006: minor issue with -from(, copy)-

159
Modules/ado/plus/c/cfa1.hlp Normal file
View File

@ -0,0 +1,159 @@
{smcl}
{.-}
help for {cmd:cfa1} {right:author: {browse "http://stas.kolenikov.name/":Stas Kolenikov}}
{.-}
{title:Confirmatory factor analysis with a single factor}
{p 8 27}
{cmd:cfa1}
{it:varlist}
[{cmd:if} {it:exp}] [{cmd:in} {it:range}]
[{cmd:aw|pw =} {it:weight}]
[{cmd:,}
{cmd:unitvar}
{cmd:free}
{cmdab:pos:var}
{cmdab:constr:aint(}{it:numlist}{cmd:)}
{cmdab:lev:el(}{it:#}{cmd:)}
{cmdab:rob:ust}
{cmd:vce(robust|oim|opg|sbentler}{cmd:)}
{cmd:cluster(}{it:varname}{cmd:)}
{cmd:svy}
{cmdab:sea:rch(}{it:searchspec}{cmd:)}
{cmd:from(}{it:initspecs}{cmd:)}
{it:ml options}
]
{title:Description}
{p}{cmd:cfa1} estimates simple confirmatory factor
analysis model with a single factor. In this model,
each of the variables is assumed to be an indicator
of an underlying unobserved factor with a linear
dependence between them:
{center:{it:y_i = m_i + l_i xi + delta_i}}
{p}where {it:y_i} is the {it:i}-th variable
in the {it:varlist}, {it:m_i} is its mean,
{it:l_i} is the latent variable loading,
{it:xi} is the latent variable/factor,
and {it:delta_i} is the measurement error.
{p}The model is estimated by the maximum likelihood
procedure.
{p}As with all latent variable models, a number
of identifying assumptions need to be made about
the latent variable {it:xi}. It is assumed
to have mean zero, and its scale is determined
by the first variable in the {it:varlist}
(i.e., l_1 is set to equal 1). Alternatively,
identification can be achieved by setting the
variance of the latent variable to 1 (with option
{it:unitvar}). More sophisticated identification
conditions can be achieved by specifying option
{it:free} and then providing the necessary
{it:constraint}.
{title:Options}
{ul:Identification:}
{p 0 4}{cmd:unitvar} specifies identification by setting
the variance of the latent variable to 1.
{p 0 4}{cmd:free} requests to relax all identifying constraints.
In this case, the user is responsible for provision
of such constraints; otherwise, the estimation process
won't converge.
{p 0 4}{cmdab:pos:var} specifies that if one or more of the
measurement error variances were estimated to be
negative (known as Heywood cases), the model
needs to be automatically re-estimated by setting
those variances to zero. The likelihood ratio test
is then reported comparing the models with and without
constraints. If there is only one offending estimate,
the proper distribution to refer this likelihood
ratio to is a mixture of chi-squares; see
{help j_chibar:chi-bar test}. A conservative
test is provided by a reference to the chi-square
distribution with the largest degrees of freedom.
The p-value is then overstated.
{p 0 4}{cmdab:constr:aint(}{it:numlist}{cmd:)} can be used
to supply additional constraints. The degrees of freedom
of the model may be wrong, then.
{p 0 4}{cmdab:lev:el(}{it:#}{cmd:)} -- see
{help estimation_options##level():estimation options}
{ul:Standard error estimation:}
{p 0 4}{cmd:vce(oim|opg|robust|sbentler}
specifies the way to estimate the standard errors.
See {help vce_option}. {cmd:vce(sbentler)} is an
additional Satorra-Bentler estimator popular in
structural equation modeling literature that relaxes
the assumption of multivariate normality while
keeping the assumption of proper structural specification.
{p 0 4}{cmd:robust} is a synonum for {cmd:vce(robust)}.
{p 0 4}{cmd:cluster(}{it:varname}{cmd:)}
{p 0 4}{cmd:svy} instructs {cmd:cfa1} to respect the complex
survey design, if one is specified.
{ul:Maximization options: see {help maximize}}
{title:Returned values}
{p}Beside the standard {help estcom:estimation results}, {cmd:cfa1}
also performs the overall goodness of fit test with results
saved in {cmd:e(lr_u)}, {cmd:e(df_u)} and {cmd:e(p_u)}
for the test statistic, its goodness of fit, and the resulting
p-value. A test vs. the model with the independent data
is provided with the {help ereturn} results with {cmd:indep}
suffix. Here, under the null hypothesis,
the covariance matrix is assumed diagonal.
{p}When {cmd:sbentler} is specified, Satorra-Bentler
standard errors are computed and posted as {cmd:e(V)},
with intermediate matrices saved in {cmd:e(SBU)},
{cmd:e(SBV)}, {cmd:e(SBGamma)} and {cmd:e(SBDelta)}.
Also, a number of corrected overall fit test statistics
is reported and saved: T-scaled ({cmd:ereturn} results
with {cmd:Tscaled} suffix) and T-adjusted
({cmd:ereturn} resuls with {cmd:Tadj} suffix;
also, {cmd:e(SBc)} and {cmd:e(SBd)} are the
scaling constants, with the latter also
being the approximate degrees of freedom
of the chi-square test)
from Satorra and Bentler (1994), and T-double
bar from Yuan and Bentler (1997)
(with {cmd:T2} suffix).
{title:References}
{p 0 4}{bind:}Satorra, A. and Bentler, P. M. (1994)
Corrections to test statistics and standard errors in covariance structure analysis,
in: {it:Latent variables analysis}, SAGE.
{p 0 4}{bind:}
Yuan, K. H., and Bentler, P. M. (1997)
Mean and Covariance Structure Analysis: Theoretical and Practical Improvements.
{it:JASA}, {bf:92} (438), pp. 767--774.
{title:Also see}
{p 0 21}{bind:}Online: help for {help factor}
{title:Contact}
Stas Kolenikov, kolenikovs {it:at} missouri.edu

View File

@ -0,0 +1,69 @@
*! Log likelihood for cfa1: linear form; v.2.1
program define cfa1_lf
args lnf $CFA1theta
* $CFA1theta contains all the names needed:
* $CFA1theta == l_1 ... l_q v_1 ... v_q m_1 ... m_q phi
gettoken lnf allthenames : 0
tempvar lnl
qui g double `lnl' = .
nobreak mata: CFA1_NormalLKHDr( "`allthenames'", "$CFA1vars", "`lnl'")
qui replace `lnf' = `lnl'
end
*! NormalLKHDr: normal likelihood with normal deviates in variables
*! v.2.1 Stas Kolenikov skolenik@gmail.com
cap mata: mata drop CFA1_NormalLKHDr()
mata:
void CFA1_NormalLKHDr(
string parnames, // the parameter names
string varnames, // the variables
string loglkhd // where the stuff is to be returned
) {
// declarations
real matrix data, lnl, parms // views of the data
real matrix lambda, means, vars, phi // parameters
real matrix Sigma, WorkSigma, InvWorkSigma, SS // the covariance matrices and temp matrix
real scalar p, n // dimension, no. obs
// get the data in
st_view(data=., ., tokens(varnames) )
st_view(lnl=., ., tokens(loglkhd) )
st_view(parms=., 1, tokens(parnames) )
n=rows(data)
p=cols(data)
// get the parameters in
lambda= parms[1,1..p]
vars = parms[1,p+1..2*p]
means = parms[1,2*p+1..3*p]
phi = parms[1,3*p+1]
Sigma = lambda'*lambda*phi + diag(vars)
SS = cholesky(Sigma)
InvWorkSigma = solvelower(SS,I(rows(SS)))
InvWorkSigma = solveupper(SS',InvWorkSigma)
ldetWS = 2*ln(dettriangular(SS))
for( i=1; i<=n; i++ ) {
lnl[i,1] = -.5*(data[i,.]-means)*InvWorkSigma*(data[i,.]-means)' - .5*ldetWS - .5*p*ln(2*pi())
}
}
end
exit
History:
v.2.0 March 10, 2006 -- re-written for Stata 9 and Mata
v.2.1 March 10, 2006 -- everything is moved to Mata

View File

@ -0,0 +1,169 @@
*!Data management utility: check for existence of variables in a dataset.
*!Version 1.1 by Amadou B. DIALLO.
*!This version 1.2 . Updated by Amadou B. DIALLO and Jean-Benoit HARDOUIN.
*!Authors: Amadou Bassirou DIALLO (Poverty Division, World Bank) and Jean-Benoit HARDOUIN (Regional Health Observatory of Orl<72>ans).
program checkfor2 , rclass
version 8
syntax anything [if] [in] [, noList Tolerance(real 0) TAble noSUm GENMiss(namelist min=1 max=1) MISsing(string)]
marksample touse
tempname rat
local av
local unav
local manymissings
local avnum
quietly count if `touse'
local tot = r(N)
qui isvar `anything'
local badlist `r(badlist)'
local varlist `r(varlist)'
di _n
if "`table'"!="" {
if "`badlist'"!="" {
di _col(4) in green "{hline 39}"
di _col(4)in green "Unavailable variables: "
foreach i of local badlist {
di _col(4) in ye "`i'"
}
di _col(4) in green "{hline 39}"
di
}
di _col(4) in green "{hline 39}"
display _col(4) in gr "Existing" _col(15) in gr "Rate of"
display _col(4) in gr "Variable" _col(14) "missings" _col(26) "Type" _col(34) "Available"
di _col(4) in green "{hline 39}"
}
tokenize `varlist'
local nbvar : word count `varlist'
forvalues i=1/`nbvar' {
capture assert missing(``i'') if `touse'
local ty: type ``i''
local tty = substr("`ty'", 1, 3)
if !_rc {
if "`table'"=="" {
display in ye "``i''" in gr " is empty in the database." in ye " ``i''" in gr " is not added to the available list."
}
else {
display _col(4) in gr "`=abbrev("``i''",8)'" _col(15) in ye "100.00%" _col(26) "`ty'"
}
local manymissings `manymissings' ``i''
}
else {
if "`table'"=="" {
display in ye "``i''" in gr " exists and is not empty."
}
*Consider type
if "`tty'" == "str" {
qui count if (``i'' == ""|``i''=="`missing'") & `touse'
local num = r(N)
scalar `rat' = (`num'/`tot')*100
}
else {
local avnum `avnum' ``i''
capture confirm number `missing'
if _rc!=0 {
quietly count if ``i'' >= . & `touse'
}
else {
quietly count if (``i'' >= .|``i''==`missing') & `touse'
}
local num = r(N)
scalar `rat' = (`num'/`tot')*100
}
if "`table'"=="" {
display in ye "``i''" in gr " has " in ye r(N) in gr " missings."
display in gr "Ratio number of missings of" in ye " ``i''" in gr " to total number of observations: " in ye %6.2f `rat' "%"
}
if `rat' <= `tolerance' {
local av `av' ``i''
if "`table'"=="" {
display in ye "``i''" in gr " is added to the available list."
}
else {
display _col(4) in gr "`=abbrev("``i''",8)'" in ye _col(15) %6.2f `rat' "%" _col(26) "`ty'" _col(34) "X"
}
}
else {
local manymissings `manymissings' ``i''
if "`table'"=="" {
display in ye "``i''" in gr " has too many missings, compared to the tolerance level."
display in ye "``i''" in gr " is not added to the available list."
}
else {
display _col(4) in gr "`=abbrev("``i''",8)'" _col(15) in ye %6.2f `rat' "%" _col(26) "`ty'"
}
}
}
if "`table'"=="" {
di
}
}
if "`table'"!="" {
di _col(4) in green "{hline 39}"
}
return local available `av'
return local unavailable `badlist'
return local manymissings `manymissings'
if "`avnum'" ~= ""&"`sum'"=="" {
display _newline
display in ye _col(14) "Unweighted summary statistics for available variables:" _n
capture confirm number `missing'
if _rc!=0 {
summarize `avnum' if `touse'
}
else {
foreach i of local avnum {
summarize `i' if `touse'&`i'!=`missing'
}
}
}
if "`list'"== "" {
display _newline
display in ye _d(97) "_"
display _newline
if "`badlist'"~="" {
display in gr "Unavailable variables: " in ye _col(45) "`badlist'" _n
}
if "`av'"~="" {
display in gr "Available variables: " in ye _col(45) "`av'" _n
}
if "`manymissings'"~="" {
display in gr "Available variables but with too missings: " in ye _col(45) "`manymissings'" _n
}
display in ye _d(97) "_"
}
if "`genmiss'" !="" {
capture confirm variable `genmiss'
if _rc!=0 {
qui gen `genmiss' = 0
local nbav : word count `av'
tokenize `av'
forvalues i=1/`nbav' {
local ty: type ``i''
local tty = substr("`ty'", 1, 3)
if "`tty'" == "str" {
qui replace `genmiss'=`genmiss'+1 if ``i''=="."
}
else {
qui replace `genmiss'=`genmiss'+1 if ``i''>=.
}
}
}
else {
di in green "The variable" in ye " `genmiss' " in green "already exists".
}
}
end

View File

@ -0,0 +1,88 @@
{smcl}
{hline}
help for {cmd:checkfor2} {right:Amadou B. DIALLO}
{right:Jean-Benoit HARDOUIN}
{hline}
{title:Module to check whether a variable exists or not in a dataset.}
{p 4 8 2}{cmd:checkfor2} {it:anything} [{cmd:,}
{cmdab:t:olerance}({it:#}) {cmdab:ta:ble} {cmdab:nol:ist} {cmdab:nosu:m}
{cmdab:genm:iss}({it:newvarname}) {cmdab:mis:sing}({it:string})]
{title:Description}
{p 4 4 2}{cmd:checkfor2} is a data management routine to check for existence of variables
within a (usually big) data set.
{p 4 4 2}{cmd:checkfor2} searchs through the data whether each variable exists.
The variables are clustered between unavailable variables, available variables with
a little amount of missing values and available variables with too many missing values.
{p 4 4 2}{cmd:isvar} must be installed ({stata ssc install isvar:ssc install isvar}).
{title:Options}
{p 4 4 2}{it:anything} is composed of variable names or lists of variables,
{p 4 4 2}{cmd:tolerance} is the tolerance level (in percentage) to consider a variable as available, with default 0,
{p 4 4 2}{cmd:nolist} avoids displaying availability status at the end of the process,
{p 4 4 2}{cmd:nosum} avoids displaying summary statistics of available variables,
{p 4 4 2}{cmd:table} displays the results in a table (instead as text),
{p 4 4 2}{cmd:genmiss} creates a new variable containing the number of missing values among the available variables,
{p 4 4 2}{cmd:missing} defines a specific value or string considered as a missing value.
{title:Saved results}
{p 4 4 2} {cmd:r(unavailable)} names of unavailable variables.{p_end}
{p 4 4 2} {cmd:r(available)} names of available variables with a small amount of missing values.{p_end}
{p 4 4 2} {cmd:r(manymissings)} names of variables present but with too missings.{p_end}
{title:Examples}
{p 4 4 2}{cmd:. use mydata, clear }{p_end}
{p 4 4 2}{cmd:. checkfor2 x y z , mis(99) genmiss(countmiss) }{p_end}
{p 4 4 2}{cmd:. su `r(available)' }{p_end}
{p 4 4 2}{cmd:. tab countmiss }{p_end}
{p 4 4 2}{cmd:. u bigdataset in 1/100, clear // Big data set}{p_end}
{p 4 4 2}{cmd:. checkfor2 v1 v2 v3 xx yy , nosum tol(5) tab}{p_end}
{p 4 4 2}{cmd:. use `r(available)' using bigdataset, clear }{p_end}
{title:Remarks}
{p 4 4 2}{cmd:checkfor2} and its primary version ({cmd:checkfor}) have been primarily written for comparable surveys such as the Demography and
Health Surveys (DHS) or the Multiple Indicator Cluster Surveys (MICS). But this could easily applied
to any other survey.
{title:Authors}
{p 4 4 2}Amadou Bassirou DIALLO.
Poverty and Health Division, PREM, The World Bank.{p_end}
{p 4 4 2}Email: {browse "mailto:adiallo5@worldbank.org":adiallo5@worldbank.org}
{p 4 4 2}Jean-Benoit HARDOUIN.
Regional Health Observatory of Orl<72>ans, France.{p_end}
{p 4 4 2}Email: {browse "mailto:jean-benoit.hardouin@orscentre.org":jean-benoit.hardouin@orscentre.org}
{title:Aknowledgements}
{p 4 4 2}We would like to thank Christophe Rockmore and also Nick Cox
and Kit Baum for their comments.
{title:Also see}
{p 4 13 2}Online: help for {help checkfor}, {help isvar}, {help nmissing}, {help npresent}, {help missing} and {help dropmiss} if installed.{p_end}

View File

@ -0,0 +1,140 @@
program def choplist, rclass
*! NJC 1.4.0 13 December 2000
* NJC 1.3.0 29 June 2000
* NJC 1.2.0 7 June 2000
* NJC 1.1.0 22 Dec 1999
* NJC 1.0.0 20 Dec 1999 after discussion with Kit Baum
version 6.0
gettoken list 0 : 0, parse(",")
if "`list'" == "" | "`list'" == "," {
di in r "nothing in list"
exit 198
}
syntax , [ Pos(str) Value(str asis) Length(str) Char(int 0) /*
*/ Noisily Global(str) ]
if "`global'" != "" {
tokenize `global'
args global1 global2 global3
if "`global3'" != "" {
di in r "global( ) must contain at most 2 names"
exit 198
}
if (length("`global1'") > 8) | (length("`global2'") > 8) {
di in r "global name must be <=8 characters"
exit 198
}
}
local nopts = /*
*/ ("`pos'" != "") + (`"`value'"' != "") + /*
*/ ("`length'" != "") + (`char' != 0)
if `nopts' != 1 {
di in r "must specify pos( ), value( ), length( ) or char( )"
exit 198
}
* as string <= contains quote
local asstr = index(`"`value'"', `"""')
tokenize `list'
local n : word count `list'
if "`length'" != "" | `char' != 0 {
local i = 1
while `i' <= `n' {
local len = length("``i''")
if `len' > 80 {
di in r "cannot handle word length > 80"
exit 498
}
local i = `i' + 1
}
}
local i = 1
if "`pos'" != "" {
local negsign = index("`pos'", "-")
if `negsign' {
local pos1 = substr("`pos'",1,`negsign' - 1)
local pos2 = substr("`pos'",`negsign', .)
local pos2 = `n' + 1 + `pos2'
local pos "`pos1'`pos2'"
capture confirm integer number `pos'
if _rc == 0 { local pos ">= `pos'" }
}
else {
capture confirm integer number `pos'
if _rc == 0 { local pos "<= `pos'" }
}
while `i' <= `n' {
if `i' `pos' { local list1 "`list1' ``i''" }
else local list2 "`list2' ``i''"
local i = `i' + 1
}
}
else if "`value'" != "" {
capture confirm number `value'
if _rc == 0 { local value "<= `value'" }
if `asstr' {
while `i' <= `n' {
if "``i''" `value' {
local list1 `"`list1' ``i''"'
}
else local list2 `"`list2' ``i''"'
local i = `i' + 1
}
}
else {
while `i' <= `n' {
if ``i'' `value' {
local list1 "`list1' ``i''"
}
else local list2 "`list2' ``i''"
local i = `i' + 1
}
}
}
else if "`length'" != "" {
capture confirm number `length'
if _rc == 0 { local length "<= `length'" }
while `i' <= `n' {
if length("``i''") `length' {
local list1 "`list1' ``i''"
}
else local list2 "`list2' ``i''"
local i = `i' + 1
}
}
else {
if `char' >= 0 {
while `i' <= `n' {
local one = substr("``i''",1,`char')
local two = substr("``i''",`char'+1,.)
local list1 "`list1' `one'"
local list2 "`list2' `two'"
local i = `i' + 1
}
}
else if `char' < 0 {
while `i' <= `n' {
local one = substr("``i''",`char',.)
local ltwo = length("``i''") + `char'
local two = substr("``i''",1,`ltwo')
local list1 "`list1' `one'"
local list2 "`list2' `two'"
local i = `i' + 1
}
}
}
if "`noisily'" != "" {
di in g "list 1: " in y `"`list1'"'
di in g "list 2: " in y `"`list2'"'
}
if "`global1'" != "" { global `global1' `"`list1'"' }
if "`global2'" != "" { global `global2' `"`list2'"' }
return local list1 `"`list1'"'
return local list2 `"`list2'"'
end

View File

@ -0,0 +1,2 @@
.h listutil

View File

@ -0,0 +1,66 @@
*! version 1.0.4 PR 30sep2005
* Based on private version 6.1.4 of frac_chk, PR 25aug2004
program define cmdchk, sclass
version 7
local cmd `1'
mac shift
local cmds `*'
sret clear
if substr("`cmd'",1,3)=="reg" {
local cmd regress
}
if "`cmds'"=="" {
tokenize clogit cnreg cox ereg fit glm logistic logit poisson probit /*
*/ qreg regress rreg weibull xtgee streg stcox stpm stpmrs /*
*/ ologit oprobit mlogit nbreg
}
else tokenize `cmds'
sret local bad 0
local done 0
while "`1'"!="" & !`done' {
if "`1'"=="`cmd'" {
local done 1
}
mac shift
}
if !`done' {
sret local bad 1
*exit
}
/*
dist=0 (normal), 1 (binomial), 2 (poisson), 3 (cox), 4 (glm),
5 (xtgee), 6 (ereg/weibull), 7 (stcox, streg, stpm, stpmrs).
*/
if "`cmd'"=="logit" | "`cmd'"=="probit" /*
*/ |"`cmd'"=="clogit"| "`cmd'"=="logistic" /*
*/ |"`cmd'"=="mlogit"| "`cmd'"=="ologit" | "`cmd'"=="oprobit" {
sret local dist 1
}
else if "`cmd'"=="poisson" {
sret local dist 2
}
else if "`cmd'"=="cox" {
sret local dist 3
}
else if "`cmd'"=="glm" {
sret local dist 4
}
else if "`cmd'"=="xtgee" {
sret local dist 5
}
else if "`cmd'"=="cnreg" | "`cmd'"=="ereg" | "`cmd'"=="weibull" | "`cmd'"=="nbreg" {
sret local dist 6
}
else if "`cmd'"=="stcox" | "`cmd'"=="streg" | "`cmd'"=="stpm" | "`cmd'"=="stpmrs" {
sret local dist 7
}
else if substr("`cmd'",1,2)=="st" {
sret local dist 7
}
else sret local dist 0
sret local isglm = (`s(dist)'==4)
sret local isqreg = ("`cmd'"=="qreg")
sret local isxtgee= (`s(dist)'==5)
sret local isnorm = ("`cmd'"=="regress"|"`cmd'"=="fit"|"`cmd'"=="rreg")
end

View File

@ -0,0 +1,33 @@
program def collist
*! NJC 1.0.0 22 Dec 1999
version 6
gettoken list 0 : 0, parse(",")
if "`list'" == "" | "`list'" == "," {
di in r "nothing in list"
exit 198
}
syntax [ , Format(str) NUmber ]
tokenize `list'
if "`number'" != "" {
local n : word count `list'
local ndigits = length("`n'")
local number = 0
}
* asstr >0 string format
* asstr 0 numeric format
* asstr -1 default
local asstr = cond("`format'" != "",index("`format'", "s"), -1)
while "`1'" != "" {
if "`number'" != "" {
local number = `number' + 1
local num : di %`ndigits'.0f `number' ". "
}
if `asstr' { di in g "`num'" `format' in y `"`1'"' }
else di in g "`num'" `format' in y `1'
mac shift
}
end

View File

@ -0,0 +1,2 @@
.h listutil

View File

@ -0,0 +1,433 @@
*! 3.0.7 9aug2007 TJS & NJC fixed Stata 10 bug by adding missing -version-
* 3.0.6 14may2006 TJS & NJC fixed Stata 9 bug with -by()-
// 3.0.5 22aug2005 Stata 8 - fixed legend(off)
// 3.0.4 27jan2005 TJS & NJC rewrite for Stata 8 + more for graphs
// 3.0.3 6oct2004 TJS & NJC rewrite for Stata 8
// 3.0.2 6oct2004 TJS & NJC rewrite for Stata 8
// 3.0.1 4oct2004 TJS & NJC rewrite for Stata 8 + fixes + noref
// 3.0.0 27jul2004 TJS & NJC rewrite for Stata 8
// 3.0.0 19jul2004 TJS & NJC rewrite for Stata 8
// 2.2.9 14jan2003 TJS & NJC handling of [ ] within connect()
// 2.2.8 2jan2003 TJS & NJC handling of [ ] within symbol()
// 2.2.7 30jan2002 TJS & NJC rho_se corrected (SJ2-2: st0015)
// 2.2.6 10dec2001 TJS & NJC bug fixes (labels, diag line)
// 2.2.5 23apr2001 TJS & NJC sortpreserve
// 2.2.4 24jan2001 TJS & NJC l1title for loa
// 2.2.3 8sep2000 TJS & NJC bug fixes & mods (STB-58: sg84.3)
// 2.2.0 16dec1999 TJS & NJC version 6 changes (STB-54: sg84.2)
// 2.1.6 18jun1998 TJS & NJC STB-45 sg84.1
// 2.0.2 6mar1998 TJS & NJC STB-43 sg84
//
// syntax: concord vary varx [fw] [if] [in] [,
// BY(varname) Summary LEvel(real `c(level)')
// CCC(str) LOA(str) QNORMD(str) ]
program concord, rclass sortpreserve
// Syntax help
if "`1'" == "" {
di "{p}{txt}Syntax is: {inp:concord} " ///
"{it:vary varx} [{it:weight}] " ///
"[{inp:if} {it:exp}] [{inp:in} {it:range}] " ///
"[, {inp:by(}{it:byvar}{inp:)} " ///
"{inp:summary level(}{it:#}{inp:)} " ///
"{inp:ccc}[{inp:(noref} {it:ccc_options}{inp:)}] " ///
"{inp:loa}[{inp:(noref regline} {it:loa_options}{inp:)}] " ///
"{inp:qnormd}[{inp:(}{it:qnormd_options}{inp:)}] {p_end}"
exit 0
}
// Setup
version 8
syntax varlist(numeric min=2 max=2) ///
[fw] ///
[if] [in] ///
[ , BY(varname) ///
Summary ///
LEvel(real `c(level)') ///
ccc(str asis) ///
CCC2 ///
loa(str asis) ///
LOA2 ///
qnormd(str asis) ///
QNORMD2 * ]
marksample touse
qui count if `touse'
if r(N) == 0 error 2000
tokenize `varlist'
// Set temporary names
tempvar d d2 db dll dul m byg kk bylabel
tempname dsd zv k xb yb sx2 sy2 r rp sxy p u sep z zp
tempname t set ll ul llt ult rdm Fdm zt ztp sd1 sd2 sl
// Set up wgt
if "`weight'" != "" local wgt "[`weight'`exp']"
// Generate CI z-value and label from Level()
if `level' < 1 local level = `level' * 100
scalar `zv' = -1 * invnorm((1 - `level' / 100) / 2)
local rl = `level'
local level : di %7.0g `level'
local level = ltrim("`level'")
// Generate BY groups
qui {
bysort `touse' `by' : gen byte `byg' = _n == 1 if `touse'
if "`by'" != "" gen `kk' = _n if `byg' == 1
replace `byg' = sum(`byg')
local byn = `byg'[_N]
// Generate `by' labels -- if required
if "`by'" != "" {
capture decode `by', gen(`bylabel')
if _rc != 0 {
local type : type `by'
gen `type' `bylabel' = `by'
}
}
}
// Print title
di
di as txt "Concordance correlation coefficient (Lin, 1989, 2000):"
// Do calculations
forval j = 1/`byn' { /* start of loop for each `by' group */
di
if "`by'" != "" {
sort `kk'
di as txt "{hline}"
di as txt "-> `by' = " `bylabel'[`j'] _n
local byl : di "`by' = " `bylabel'[`j']
}
// LOA (Bland & Altman) calculations
qui {
gen `d' = `1' - `2'
gen `d2' = `d'^2
su `d' if `byg' == `j' `wgt'
gen `db' = r(mean)
scalar `dsd' = r(sd)
gen `dll' = `db' - `zv' * `dsd'
gen `dul' = `db' + `zv' * `dsd'
gen `m' = (`1' + `2') / 2
}
// Concordance calculations
qui su `1' if `byg' == `j' `wgt'
scalar `k' = r(N)
scalar `yb' = r(mean)
scalar `sy2' = r(Var) * (`k' - 1) / `k'
scalar `sd1' = r(sd)
qui su `2' if `byg' == `j' `wgt'
scalar `xb' = r(mean)
scalar `sx2' = r(Var) * (`k' - 1) / `k'
scalar `sd2' = r(sd)
qui corr `1' `2' if `byg' == `j' `wgt'
scalar `r' = r(rho)
scalar `sl' = sign(`r') * `sd1' / `sd2'
scalar `rp' = min(tprob(r(N) - 2, r(rho) * sqrt(r(N) - 2) ///
/ sqrt(1 - r(rho)^2)) ,1)
scalar `sxy' = `r' * sqrt(`sx2' * `sy2')
scalar `p' = 2 * `sxy' / (`sx2' + `sy2' + (`yb' - `xb')^2)
scalar `u' = (`yb' - `xb') / (`sx2' * `sy2')^.25
// --- variance, test, and CI for asymptotic normal approximation
// scalar `sep' = sqrt(((1 - ((`r')^2)) * (`p')^2 * (1 -
// ((`p')^2)) / (`r')^2 + (4 * (`p')^3 * (1 - `p') * (`u')^2
// / `r') - 2 * (`p')^4 * (`u')^4 / (`r')^2 ) / (`k' - 2))
// Corrected se: per Lin (March 2000) Biometrics 56:325-5.
#delimit ;
scalar `sep' = sqrt(((1 - ((`r')^2)) * (`p')^2 * (1 -
((`p')^2)) / (`r')^2 + (2 * (`p')^3 * (1 - `p') * (`u')^2
/ `r') - .5 * (`p')^4 * (`u')^4 / (`r')^2 ) / (`k' - 2));
#delimit cr
scalar `z' = `p' / `sep'
scalar `zp' = 2 * (1 - normprob(abs(`z')))
scalar `ll' = `p' - `zv' * `sep'
scalar `ul' = `p' + `zv' * `sep'
// --- statistic, variance, test, and CI for inverse hyperbolic
// tangent transform to improve asymptotic normality
scalar `t' = ln((1 + `p') / (1 - `p')) / 2
scalar `set' = `sep' / (1 - ((`p')^2))
scalar `zt' = `t' / `set'
scalar `ztp' = 2 * (1 - normprob(abs(`zt')))
scalar `llt' = `t' - `zv' * `set'
scalar `ult' = `t' + `zv' * `set'
scalar `llt' = (exp(2 * `llt') - 1) / (exp(2 * `llt') + 1)
scalar `ult' = (exp(2 * `ult') - 1) / (exp(2 * `ult') + 1)
// Print output
di as txt " rho_c SE(rho_c) Obs [" _c
if index("`level'",".") {
di as txt %6.1f `level' "% CI ] P CI type"
}
else di as txt " `level'% CI ] P CI type"
di as txt "{hline 63}"
di as res %6.3f `p' %10.3f `sep' %8.0f `k' %10.3f `ll' _c
di as res %7.3f `ul' %9.3f `zp' as txt " asymptotic"
di as res _dup(24) " " %10.3f `llt' %7.3f `ult' %9.3f `ztp' _c
di as txt " z-transform"
di _n as txt "Pearson's r =" as res %7.3f `r' _c
di as txt " Pr(r = 0) =" as res %6.3f `rp' _c
di as txt " C_b = rho_c/r =" as res %7.3f `p' / `r'
di as txt "Reduced major axis: Slope = " as res %9.3f `sl' _c
di as txt " Intercept = " as res %9.3f `yb'-`xb'*`sl'
di _n as txt "Difference = `1' - `2'"
di _n as txt " Difference" _c
if index("`level'", ".") {
di _col(33) as txt %6.1f `level' "% Limits Of Agreement"
}
else di _col(33) as txt " `level'% Limits Of Agreement"
di as txt " Average Std Dev. (Bland & Altman, 1986)"
di as txt "{hline 63}"
di as res %10.3f `db' %12.3f `dsd' _c
di as res " " %11.3f `dll' %11.3f `dul'
qui corr `d' `m' if `byg' == `j' `wgt'
scalar `rdm' = r(rho)
di _n as txt "Correlation between difference and mean =" _c
local fmt = cond(r(rho) < 0, "%7.3f", "%6.3f")
di as res `fmt' r(rho)
su `d2' if `byg' == `j' `wgt', meanonly
local sumd2 = r(sum)
qui reg `d' `m' if `byg' == `j' `wgt'
scalar `Fdm' = ((`sumd2' - e(rss)) / 2) / (e(rss) / e(df_r))
di _n as txt "Bradley-Blackwood F = " ///
as res %4.3f `Fdm' ///
as txt " (P = " %6.5f ///
as res 1 - F(2, e(df_r), `Fdm') ///
as txt ")"
if "`summary'" != "" su `1' `2' if `byg' == `j' `wgt'
// setup local options for passing to graph routines
if "`byl'" != "" local byls byl("`byl'")
if "`level'" != "" local levs level(`level')
// set more if needed
if (`"`loa'`loa2'"' != "" & `"`qnormd'`qnormd2'"' != "") | ///
(`"`loa'`loa2'"' != "" & `"`ccc'`ccc2'"' != "") | ///
(`"`ccc'`ccc2'"' != "" & `"`qnormd'`qnormd2'"' != "") {
local moreflag "more"
}
// loa graph
if `"`loa'`loa2'"' != "" {
gphloa `2' `1' `dll' `db' `dul' `d' `m' `byg' ///
`wgt', j(`j') byn(`byn') `byls' `levs' `loa' `options'
`moreflag'
}
// qnormd graph
if `"`qnormd'`qnormd2'"' != "" {
gphqnormd `2' `1' `d' `byg' ///
`wgt', j(`j') byn(`byn') `byls' `levs' `qnormd' `options'
`moreflag'
}
// ccc graph
if `"`ccc'`ccc2'"' != "" {
local sll = `sl'
local xbl = `xb'
local ybl = `yb'
gphccc `1' `2' `byg' `wgt', j(`j') ///
xb(`xbl') yb(`ybl') sl(`sll') byn(`byn') `byls' `ccc' `options'
}
if `byn' > 1 {
capture drop `d'
capture drop `d2'
capture drop `db'
capture drop `dll'
capture drop `dul'
capture drop `m'
}
} /* end of loop for each `by' group */
// save globals
if `byn' == 1 {
return scalar N = `k'
return scalar rho_c = `p'
return scalar se_rho_c = `sep'
return scalar asym_ll = `ll'
return scalar asym_ul = `ul'
return scalar z_tr_ll = `llt'
return scalar z_tr_ul = `ult'
return scalar C_b = `p' / `r'
return scalar diff = `db'
return scalar sd_diff = `dsd'
return scalar LOA_ll = `dll'
return scalar LOA_ul = `dul'
return scalar rdm = `rdm'
return scalar Fdm = `Fdm'
// double save globals
// now undocumented as of 3.0.0
global S_1 = `k'
global S_2 = `p'
global S_3 = `sep'
global S_4 = `ll'
global S_5 = `ul'
global S_6 = `llt'
global S_7 = `ult'
global S_8 = `p' / `r'
global S_9 = `db'
global S_10 = `dsd'
global S_11 = `dll'
global S_12 = `dul'
}
end
program gphloa
// loa graph
version 8
syntax varlist(numeric min=2) [fw] ///
[ , J(int 1) BYN(int 1) BYL(str) REGline LEvel(real `c(level)') ///
plot(str asis) noREF * ]
tokenize `varlist'
args two one dll db dul d m byg
if "`weight'" != "" local wgt "[`weight'`exp']"
if `"`byl'"' != "" local t2title `"t2title(`byl')"'
local name2 : variable label `2'
local name1 : variable label `1'
local lnth = length(`"`name2'"') + length(`"`name1'"')
if `"`name2'"' == `""' | `lnth' > 50 local name2 "`2'"
if `"`name1'"' == `""' | `lnth' > 50 local name1 "`1'"
qui if "`regline'" != "" {
tempvar fit
regress `d' `m' if `byg' == `j' `wgt'
predict `fit'
}
if "`ref'" == "" {
local ord 2 3
if "`regline'" != "" local ord 2 3 4
local zero yli(0, lstyle(refline)) yscale(range(0)) ylabel(0, add) ///
legend(on order(`ord') label(2 observed average agreement) ///
label(3 `"`level'% limits of agreement"') label(4 regression line)) ///
caption("y=0 is line of perfect average agreement")
}
graph twoway line `dll' `db' `dul' `fit' `m' if `byg' == `j', ///
clcolor(red purple red green) sort ///
|| scatter `d' `m' if `byg' == `j' ///
, ms(oh) `t2title' ///
yti(`"Difference of `name2' and `name1'"') ///
xti(`"Mean of `name2' and `name1'"') ///
caption("`level'% Limits Of Agreement") legend(off) `zero' ///
`options' ///
|| `plot'
if `byn' > 1 more
end
program gphqnormd, sort
// normal prob plot
// note: logic pilfered from qnorm
version 8
syntax varlist(numeric min=2) [fw] ///
[ , J(int 1) BYN(int 1) BYL(str) LEvel(real `c(level)') ///
plot(str asis) * ]
args two one d byg
if "`weight'" != "" local wgt "[`weight'`exp']"
else local exp 1
local name2 : variable label `2'
local name1 : variable label `1'
local lnth = length(`"`name2'"') + length(`"`name1'"')
if `"`name2'"' == `""' | `lnth' > 50 local name2 "`2'"
if `"`name1'"' == `""' | `lnth' > 50 local name1 "`1'"
tempvar Z Psubi touse2
mark `touse2' if `byg' == `j'
qui {
gsort -`touse2' `d'
gen `Psubi' = sum(`touse2' * `exp')
replace `Psubi' = cond(`touse2' == 0, ., `Psubi'/(`Psubi'[_N] + 1))
su `d' if `touse2' == 1 `wgt'
gen float `Z' = invnorm(`Psubi') * r(sd) + r(mean)
label var `Z' "Inverse Normal"
local xttl : var label `Z'
local yttl `"Difference of `name2' and `name1'"'
}
if `"`byl'"' != "" local t2title `"t2title(`byl')"'
graph twoway ///
(scatter `d' `Z', ///
sort ///
ytitle(`"`yttl'"') ///
xtitle(`"`xttl'"') ///
`t2title' ///
`options' ///
) ///
(function y=x, ///
range(`Z') ///
n(2) ///
clstyle(refline) ///
yvarlabel("Reference") ///
yvarformat(`fmt') ///
) ///
, legend(off) ///
|| `plot'
if `byn' > 1 more
end
program gphccc
version 8
//-----------------------------------------------------
// ccc graph
// ----------------------------------------------------
syntax varlist(numeric min=2) [fw] [ , J(int 1) XB(real 0) noREF ///
YB(real 0) SL(real 0) BYN(int 1) BYL(str) plot(str asis) LEGEND(str) * ]
tokenize `varlist'
tempvar byg rmaxis
local byg `3'
if "`weight'" != "" local wgt "[`weight'`exp']"
local yttl : variable label `1'
if `"`yttl'"' == "" local yttl "`1'"
local xttl : variable label `2'
if `"`xttl'"' == "" local xttl "`2'"
if "`ref'" == "" local lopc || function y = x, ra(`2') clstyle(refline) ///
legend(on order(2 "reduced major axis" 3 "line of perfect concordance"))
if "`legend'" != "" {
local legnd "legend(`legend')"
}
// Graph concordance plot
qui gen `rmaxis' = `sl' * (`2' - `xb') + `yb'
graph twoway scatter `1' `rmaxis' `2' ///
if `byg' == `j' `wgt', ///
sort connect(none line) ms(oh none) ///
yti(`"`yttl'"') xti(`"`xttl'"') legend(off) `lopc' ///
`options' ///
|| `plot'
if `byn' > 1 more
end

View File

@ -0,0 +1,183 @@
/* Dialog (Version 3.0.1) by: T. J. Steichen, steichen@triad.rr.com
concord VERSION 3.0.3 6oct2004
syntax: concord vary varx [fw] [if] [in] [,
BY(varname) Summary LEvel(real 95) ccc[(noREF ccc_graph_options)]
loa[(noREF REGline loa_graph_options)] qnormd[(qnormd_graph_options}] ]
Install in User Statistics menu via:
. window menu append item "stUserStatistics" "&concord (Concordance correlation coefficient)" "db concord3"
. window menu refresh
To permanently install, place the commands in your -profile.do- file.
*/
VERSION 8.0
INCLUDE _std_small
INCLUDE header
HELP hlp1, view("help concord")
RESET res1
DIALOG main, label("concord - Concordance correlation coefficient") tabtitle("Main")
BEGIN
TEXT tx_yvar _lft _top 150 ., ///
label("Y Variable:")
VARNAME vn_yvar @ _ss @ ., ///
label("Y Variable")
TEXT tx_xvar 180 _top 150 ., ///
label("X Variable:")
VARNAME vn_xvar @ _ss @ ., ///
label("X Variable")
CHECKBOX cb_by 10 70 50 ., ///
label("By:") ///
onclickon(main.vn_by.enable) ///
onclickoff(main.vn_by.disable)
VARNAME vn_by 60 70 270 ., ///
label("By Variable") ///
option("by")
CHECKBOX cb_summary 10 100 300 ., ///
label("Show Summary") ///
option("summary")
CHECKBOX cb_level 10 130 50 ., ///
label("Level:") ///
onclickon(main.ed_level.enable) ///
onclickoff(main.ed_level.disable)
EDIT ed_level 60 @ 40 ., ///
label("Level") ///
numonly default(global S_level) ///
option("level")
END
INCLUDE ifin
INCLUDE weights_f
DIALOG graph, tabtitle("Graph")
BEGIN
CHECKBOX cb_ccc 20 10 150 ., ///
label("Concordance") ///
onclickon(script ccc_on) ///
onclickoff(script ccc_off) ///
option("ccc")
CHECKBOX cb_ccc_ref 40 30 140 ., ///
label("No CCC Reference Line")
CHECKBOX cb_ccc_opt 40 50 90 ., ///
label("CCC Options:") ///
onclickon(graph.ed_ccc_opt.enable) ///
onclickoff(graph.ed_ccc_opt.disable)
EDIT ed_ccc_opt 130 50 200 ., ///
label("CCC Opts")
CHECKBOX cb_loa 20 70 150 ., ///
label("Limits of Agreement") ///
onclickon(script loa_on) ///
onclickoff(script loa_off) ///
option("loa")
CHECKBOX cb_loa_ref 40 90 140 ., ///
label("No LOA Reference Line")
CHECKBOX cb_loa_reg 40 110 140 ., ///
label("Regression Line")
CHECKBOX cb_loa_opt 40 130 90 ., ///
label("LOA Options:") ///
onclickon(graph.ed_loa_opt.enable) ///
onclickoff(graph.ed_loa_opt.disable)
EDIT ed_loa_opt 130 130 200 ., ///
label("LOA Opts")
CHECKBOX cb_qnormd 20 150 150 ., ///
label("Differences Normal plot") ///
onclickon(graph.cb_qnormd_opt.enable) ///
onclickoff(graph.cb_qnormd_opt.disable) ///
option("qnormd")
CHECKBOX cb_qnormd_opt 40 170 90 ., ///
label("QND Options:") ///
onclickon(graph.ed_qnormd_opt.enable) ///
onclickoff(graph.ed_qnormd_opt.disable)
EDIT ed_qnormd_opt 130 170 200 ., ///
label("QND Opts")
END
SCRIPT ccc_on
BEGIN
graph.cb_ccc_ref.enable
graph.cb_ccc_opt.enable
END
SCRIPT ccc_off
BEGIN
graph.cb_ccc_ref.disable
graph.cb_ccc_opt.disable
END
SCRIPT loa_on
BEGIN
graph.cb_loa_ref.enable
graph.cb_loa_reg.enable
graph.cb_loa_opt.enable
END
SCRIPT loa_off
BEGIN
graph.cb_loa_ref.disable
graph.cb_loa_reg.disable
graph.cb_loa_opt.disable
END
PROGRAM command
BEGIN
put "concord "
varlist main.vn_yvar main.vn_xvar
INCLUDE _weights_pr
INCLUDE _ifin_pr
beginoptions
optionarg main.vn_by
option main.cb_summary
optionarg main.ed_level
if graph.cb_ccc {
put "ccc"
if graph.cb_ccc_ref | graph.cb_ccc_opt {
put "("
}
if graph.cb_ccc_ref {
put "noref "
}
if graph.cb_ccc_opt {
put graph.ed_ccc_opt
}
if graph.cb_ccc_ref | graph.cb_ccc_opt {
put ") "
}
}
if graph.cb_loa {
put "loa"
if graph.cb_loa_ref | graph.cb_loa_opt | graph.cb_loa_reg {
put "("
}
if graph.cb_loa_ref {
put "noref "
}
if graph.cb_loa_reg {
put "regline "
}
if graph.cb_loa_opt {
put graph.ed_loa_opt
}
if graph.cb_loa_ref | graph.cb_loa_opt | graph.cb_loa_reg {
put ") "
}
}
if graph.cb_qnormd {
put "qnormd"
if graph.cb_qnormd_opt {
put "("
put graph.ed_qnormd_opt
put ") "
}
}
endoptions
END

View File

@ -0,0 +1,275 @@
{smcl}
{* 6oct2004/23aug2005/14may2006/9aug2007}
{hline}
{hi:help concord} {right:(SJ7-3: st0015_4)}
{hline}
{title:Title}
{p2colset 5 16 18 2}{...}
{p2col:{hi:concord} {hline 2}}Concordance correlation coefficient and associated measures, tests, and graphs{p_end}
{p2colreset}{...}
{title:Syntax}
{p 8 17 2}
{cmd:concord}
{it:vary} {it:varx}
{ifin}
{weight}
[{cmd:,}
{cmd:by(}{it:byvar}{cmd:)}
{cmdab:s:ummary}
{cmdab:le:vel(}{it:#}{cmd:)}
{cmd:ccc}[{cmd:(noref} {it:ccc_options}{cmd:)}]
{cmd:loa}[{cmd:(noref} {cmdab:reg:line} {it:loa_options}{cmd:)}]
{cmd:qnormd}[{cmd:(}{it:qnormd_options}{cmd:)}]]
{title:Description}
{p 4 4 2}
{cmd:concord} computes Lin's (1989, 2000) concordance correlation coefficient
for agreement on a continuous measure obtained by two persons or methods. (The
measure was introduced earlier by Krippendorff (1970).) The concordance
correlation coefficient combines measures of both precision and accuracy to
determine how far the observed data deviate from the line of perfect
concordance (i.e., the line at 45 degrees on a square scatter plot). Lin's
coefficient increases in value as a function of the nearness of the data's
reduced major axis to the line of perfect concordance (the accuracy of the
data) and of the tightness of the data about its reduced major axis (the
precision of the data). The Pearson correlation coefficient, r, the
bias-correction factor, C_b, and the equation of the reduced major axis are
reported to show these components. Note that the concordance correlation
coefficient, rho_c, can be expressed as the product of r, the measure of
precision, and C_b, the measure of accuracy.
{p 4 4 2}
{cmd:concord} also provides results for Bland and Altman's
limits-of-agreement, "loa", procedure (1986). The loa, a data-scale assessment
of the degree of agreement, is a complementary approach to the
relationship-scale approach of Lin.
{p 4 4 2}
Finally, two other results are reported:
{p 8 8 2}
1. The correlation between difference and mean. In one interpretation
this is a test statistic for a null hypothesis of equal variances given
bivariate normality (Pitman 1939; also see Snedecor and Cochran 1989, 192-193).
Alternatively, it is an exploratory diagnostic.
A value near zero implies concordance.
{p 8 8 2}
2. An F test of equality of means and variances. Note that this too
assumes bivariate normality (Bradley and Blackwood 1989).
See also Hsu (1940) and Reynolds and Gregoire (1991).
Nonsignificance implies concordance.
{p 4 4 2}
The user provides the pairs of measurements for a single property as
observations in variables {it:vary} and {it:varx}. Frequency weights may be
specified and used. Missing values (if any) are deleted in a casewise manner.
{p 4 4 2}
Various associated graphs may be obtained through options.
See below for explanations of the options {cmd:ccc}, {cmd:loa}, and
{cmd:qnormd}.
{title:Options}
{p 4 8 2}
{cmd:by(}{it:byvar}{cmd:)} produces separate results for groups of
observations defined by {it:byvar}.
{p 4 8 2}
{cmd:summary} requests summary statistics.
{p 4 8 2}
{cmd:level(}{it:#}{cmd:)} sets the confidence level % for the CI; default is
{cmd:c(level)}.
{p 4 8 2}
{cmd:ccc} requests a graphical display of the data and the reduced major axis
of the data. The reduced major axis or SD line goes through the intersection of
the means and has slope given by the sign of Pearson's r and the ratio of the
standard deviations. The SD line serves as a summary of the center of the data.
{p 8 8 2}
{cmd:ccc()} suboption {cmd:noref} suppresses the reference line of
perfect concordance, y=x.
{p 8 8 2}
{cmd:ccc()} may also be specified with other options, which should
be options of {helpb scatter}. For example, the scheme may be changed by a
call such as {cmd:ccc(scheme(lean1)}.
{p 4 8 2}
{cmd:loa} requests a graphical display of the loa, the mean difference, and
the data presented as paired differences plotted against pair-wise means.
{p 8 8 2}
{cmd:loa()} suboption {cmd:noref} suppresses the reference line of perfect
average agreement, y=0.
{p 8 8 2}
{cmd:loa()} suboption {cmd:regline} adds a regression line to the loa plot
fitting the paired differences to the pair-wise means.
{p 8 8 2}
{cmd:loa()} may also be specified with other options, which should normally
be options of {helpb scatter}. For example,
the reference line of perfect average agreement was generated as
{cmd:loa(yline(0, lstyle(refline)) yscale(range(0)) ylabel(0, add))}.
{p 4 8 2}
{cmd:qnormd} requests a normal plot of differences.
{p 8 8 2}
{cmd:qnormd()} may also be specified with options, which should
be options of {helpb scatter}. For example,
{cmd:qnormd(title(Normal plot of differences))} adds a title to the graph.
{title:Comments}
{p 4 4 2}
Lin (2000) reported typographical errors in his original 1989 paper
that affected calculation of the standard error of rho_c. These corrections
were included in {cmd:concord} in version 2.2.7 (January 2002) when the erratum
was brought to the attention of the program authors by Dr. Benjamin Littenberg.
We thank Dr. Littenberg.
{p 4 4 2}Kevan Polkinghorne pointed out a problem with {cmd:loa()}.
Mark Marshall pointed out a problem with {cmd:by()} under Stata 9.
{p 4 4 2}
Dunn (2004) contains a bibliography on related work.
Cox (2004) discusses other graphical approaches to this and related
problems. Cox (2006) discusses concordance correlation and
other numerical and graphical methods for assessing agreement
with various scientific examples.
{title:Saved results}
{p 4 4 2}
The following items are returned in {cmd:r()}, if the {cmd:by()} option was
not used:
{p 8 18}{cmd:r(N)}{space 9}number of observations compared{p_end}
{p 8 18}{cmd:r(rho_c)}{space 5}concordance correlation coefficient rho_c{p_end}
{p 8 18}{cmd:r(se_rho_c)}{space 2}standard error of rho_c{p_end}
{p 8 18}{cmd:r(asym_ll)}{space 3}lower CI limit (asymptotic){p_end}
{p 8 18}{cmd:r(asym_ul)}{space 3}upper CI limit (asymptotic){p_end}
{p 8 18}{cmd:r(z_tr_ll)}{space 3}lower CI limit (z-transform){p_end}
{p 8 18}{cmd:r(z_tr_ul)}{space 3}upper CI limit (z-transform){p_end}
{p 8 18}{cmd:r(C_b)}{space 7}bias-correction factor C_b{p_end}
{p 8 18}{cmd:r(diff)}{space 6}mean difference{p_end}
{p 8 18}{cmd:r(sd_diff)}{space 3}standard deviation of mean difference{p_end}
{p 8 18}{cmd:r(LOA_ll)}{space 4}lower loa CI limit{p_end}
{p 8 18}{cmd:r(LOA_ul)}{space 4}upper loa CI limit{p_end}
{p 8 18}{cmd:r(rdm)}{space 7}correlation between difference and mean{p_end}
{p 8 18}{cmd:r(Fdm)}{space 7}F from Bradley-Blackwood test{p_end}
{title:Examples}
{p 4 8 2}
{cmd:. concord rater1 rater2}
{p 4 8 2}
{cmd:. concord rater1 rater2 [fw=freq]}
{p 4 8 2}
{cmd:. concord rater1 rater2, summary ccc}
{p 4 8 2}
{cmd:. concord rater1 rater2, summary ccc(noref)}
{p 4 8 2}
{cmd:. concord rater1 rater2, level(90) by(grp)}
{p 4 8 2}
{cmd:. concord rater1 rater2, loa(regline noref)}
{p 4 8 2}
{cmd:. concord rater1 rater2, qnormd(title(Normal plot))}
{title:Authors}
{p 4 4 2}
Thomas J. Steichen, Winston-Salem, NC, USA, steichen@triad.rr.com
{p 4 4 2}
Nicholas J. Cox, Durham University, UK, n.j.cox@durham.ac.uk
{title:References}
{p 4 8 2}
Bland, J. M., and D. G. Altman. 1986. Statistical methods for assessing
agreement between two methods of clinical measurement. {it:Lancet} I:
307{c -}310.
{p 4 8 2}
Bradley, E. L., and L. G. Blackwood. 1989. Comparing paired data:
a simultaneous test for means and variances. {it:American Statistician}
43: 234{c -}235.
{p 4 8 2}
Cox, N. J. 2004. Graphing agreement and disagreement.
{it:Stata Journal} 4: 329{c -}349.
{p 4 8 2}
------. 2006.
Assessing agreement of measurements and predictions in geomorphology.
{it:Geomorphology} 76: 332{c -}346.
{p 4 8 2}
Dunn, G. 2004.
{it: Statistical Evaluation of Measurement Errors: Design and Analysis of Reliability Studies.}
London: Arnold.
{p 4 8 2}
Hsu, C. T. 1940. On samples from a normal bivariate population.
{it:Annals of Mathematical Statistics} 11: 410{c -}426.
{p 4 8 2}
Krippendorff, K. 1970. Bivariate agreement coefficients for reliability of data.
In Borgatta, E.F. and G.W. Bohrnstedt (eds)
{it:Sociological Methodology}. San Francisco: Jossey-Bass, 139{c -}150.
[a.k.a. {it:Sociological Methodology} 2: 139{c -}150]
{p 4 8 2}
Lin, L. I-K. 1989. A concordance correlation coefficient to evaluate
reproducibility. {it:Biometrics} 45: 255{c -}268.
{p 4 8 2}
------. 2000. A note on the concordance correlation coefficient.
{it:Biometrics} 56: 324{c -}325.
{p 4 8 2}
Pitman, E. J. G. 1939. A note on normal correlation.
{it:Biometrika} 31: 9{c -}12.
{p 4 8 2}
Reynolds, M., and T. G. Gregoire. 1991. Comment on Bradley and Blackwood.
{it:American Statistician} 45: 163{c -}164.
{p 4 8 2}
Snedecor, G. W., and W. G. Cochran. 1989. {it:Statistical Methods.}
Ames, IA: Iowa State University Press.
{title:Also see}
{p 4 13 2}
STB: STB-43 sg84; STB-45 sg84.1; STB-54 sg84.2; STB-58 sg84.3
{psee}
SJ:{space 3}SJ2-2 st0015; SJ4-4 st0015_1; SJ5-3: st0015_2; SJ6-2: st0015_3
{p_end}

1090
Modules/ado/plus/c/confa.ado Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,503 @@
*! Mata definitions for confa package; 3 March 2009; v.2.0
set matalnum on
mata:
mata set matastrict on
mata clear
//////////////////////////////////////////////
// needed by confa.ado
real CONFA_NF( string input ) {
real scalar nopenpar, nclosepar, ncolon;
// opening and closing parentheses
nopenpar = length(tokens(input, ")" ))
nclosepar = length(tokens(input, "(" ))
// n*par will be 2*nf
ncolon = length(tokens(input, ":" ))
// ncolon will be 2*nf + 1
if ( (nopenpar == nclosepar) & (nopenpar == ncolon-1 ) ) {
if (mod(nopenpar,2) == 0) {
return( nopenpar/2 )
}
}
// if everything was OK, should've exited by now
// if something's wrong, return zero
return(0)
}
matrix CONFA_StrucToSigma(real vector parms) {
real scalar CONFA_loglevel, nobsvar, nfactors, eqno;
real matrix Lambda, Phi, Theta, Sigma, CONFA_Struc;
// loglevel
CONFA_loglevel = strtoreal( st_global("CONFA_loglevel"))
CONFA_Struc = st_matrix("CONFA_Struc")
if (CONFA_loglevel>4) CONFA_Struc
if (CONFA_loglevel>4) {
printf("{txt}Current parameter values:\n")
parms
}
// the length should coinicde with the # pars from CONFA_Struc
if ( cols(parms) ~= rows(CONFA_Struc) ) {
// something's wrong, let's just drop out with an empty matrix
if (CONFA_loglevel>4) {
printf("{txt}Expected parameters: {res}%3.0f{txt}; received parameters: {res}%3.0f\n",
rows(CONFA_Struc),cols(parms))
}
return(J(0,0,0))
}
// # observed variables: max entry in the number of means
nobsvar = colmax( select(CONFA_Struc[,3], !(CONFA_Struc[,1]-J(rows(CONFA_Struc),1,1)) ) )
if (CONFA_loglevel>4) printf("{txt}No. of observed variables: {res}%3.0f\n",nobsvar)
// # observed factors: max entry in the phi indices
nfactors = colmax( select(CONFA_Struc[,3], !(CONFA_Struc[,1]-J(rows(CONFA_Struc),1,3)) ) )
if (CONFA_loglevel>4) printf("{txt}No. of latent factors: {res}%3.0f\n",nfactors)
// set up the matrices
Lambda = J(nobsvar,nfactors,0)
Phi = J(nfactors,nfactors,0)
Theta = J(nobsvar,nobsvar,0)
// fill the stuff in
for(eqno=nobsvar+1;eqno<=rows(CONFA_Struc);eqno++) {
if (CONFA_Struc[eqno,1] == 2) {
// a lambda-type entry
Lambda[ CONFA_Struc[eqno,3], CONFA_Struc[eqno,4] ] = parms[eqno]
}
if (CONFA_Struc[eqno,1] == 3) {
// a phi-type entry
Phi[ CONFA_Struc[eqno,3], CONFA_Struc[eqno,4] ] = parms[eqno]
Phi[ CONFA_Struc[eqno,4], CONFA_Struc[eqno,3] ] = parms[eqno]
}
if (CONFA_Struc[eqno,1] == 4) {
// a theta-type entry
Theta[ CONFA_Struc[eqno,3], CONFA_Struc[eqno,3] ] = parms[eqno]
}
if (CONFA_Struc[eqno,1] == 5) {
// a theta-type correlated errors entry
Theta[ CONFA_Struc[eqno,3], CONFA_Struc[eqno, 4] ] = parms[eqno]
Theta[ CONFA_Struc[eqno,4], CONFA_Struc[eqno, 3] ] = parms[eqno]
}
}
if (CONFA_loglevel > 4) {
printf("{txt}Loadings:\n")
Lambda
printf("{txt}Factor covariances:\n")
Phi
printf("{txt}Residual variances:\n")
Theta
}
Sigma = Lambda*Phi*Lambda' + Theta
if (CONFA_loglevel > 4) {
printf("{txt}Implied moments:\n")
Sigma
}
if (CONFA_loglevel == -1) {
// post matrices to Stata
st_matrix("CONFA_Lambda",Lambda)
st_matrix("CONFA_Phi",Phi)
st_matrix("CONFA_Theta",Theta)
st_matrix("CONFA_Sigma",Sigma)
}
// done with model structure, compute and return implied matrix
return( Sigma )
}
// vech covariance matrix, for Satorra-Bentler
void SBvechZZtoB(string dlist, string blist) {
real matrix data, moments, B;
real scalar i;
// view the deviation variables
st_view(data=.,.,tokens(dlist))
// view the moment variables
// blist=st_local("blist")
st_view(moments=.,.,tokens(blist))
// vectorize!
for(i=1; i<=rows(data); i++) {
B = data[i,.]'*data[i,.]
moments[i,.] = vech(B)'
}
}
// duplication matrix, for Satorra-Bentler
void Dupl(scalar p, string Dname) {
real scalar pstar, k;
real matrix Ipstar, D;
pstar = p*(p+1)/2
Ipstar = I(pstar)
D = J(p*p,0,.)
for(k=1;k<=pstar;k++) {
D = (D, vec(invvech(Ipstar[.,k])))
}
st_matrix(Dname,D)
}
// Satorra-Bentler Delta matrix
// Delta = \frac \partial{\partial \theta} vech \Sigma(\theta)
void SBStrucToDelta(string DeltaName) {
real scalar CONFA_loglevel, p, t, varno, facno, i, j, k, fac1, fac2, k1, k2;
// log level, # obs vars, # parameters, current var, current factor, cycle indices, temp indices
real matrix Lambda, Phi, Theta, Sigma, CONFA_Struc, Delta, DeltaRow;
// must be self-explanatory
real matrix U, E;
// identity matrices of the size #factors and #obs vars
// loglevel
CONFA_loglevel = strtoreal( st_global("CONFA_loglevel"))
// need the CONFA matrices
CONFA_Struc = st_matrix("CONFA_Struc")
Sigma = st_matrix("CONFA_Sigma")
Lambda = st_matrix("CONFA_Lambda")
Phi = st_matrix("CONFA_Phi")
// Theta = st_matrix("CONFA_Theta")
if (CONFA_loglevel>4) CONFA_Struc
// # parameters in the model
t = rows(CONFA_Struc)
// cols(Delta) = t = # pars
// rows(Delta) = pstar = p*(p+1)/2 = length( vech( Sigma ) )
// but that should be accumulated one by one...
Delta = J(0,t,.)
// sources of u and e vectors
p = rows( Sigma )
U = I( p )
E = I( rows(Phi) )
for(i=1;i<=p;i++) {
for(j=i;j<=p;j++) {
if (CONFA_loglevel > 4) printf("{txt}Working with pair ({res}%2.0f{txt},{res}%2.0f{txt})\n",i,j)
DeltaRow = J(1,t,0)
// parse Struc matrix and see how each parameter affects Cov(X_i,X_j)
for(k=1;k<=t;k++) {
if (CONFA_Struc[k,1] == 1) {
// a mean-type entry
// for the moment, assume it does not affect anything
}
if (CONFA_Struc[k,1] == 2) {
// a lambda-type entry
// CONFA_Struc[k,.] = (2, equation #, variable #, factor #)
varno = CONFA_Struc[k,3]
facno = CONFA_Struc[k,4]
DeltaRow[1,k] = U[i,.] * U[.,varno] * E[facno,.] * Phi * Lambda' * U[.,j] +
U[i,.] * Lambda * Phi * E[.,facno] * U[varno,.] * U[.,j]
}
if (CONFA_Struc[k,1] == 3) {
// a phi-type entry
// CONFA_Struc[k,.] = (3, equation #, `factor`kk'', `factor`k'')
fac1 = CONFA_Struc[k,3]
fac2 = CONFA_Struc[k,4]
DeltaRow[1,k] = U[i,.] * Lambda * E[.,fac1] * E[fac2,.] * Lambda' * U[.,j]
}
if (CONFA_Struc[k,1] == 4) {
// a theta-type entry
// CONFA_Struc[k,.] = (4, equation #, variable #, 0)
varno = CONFA_Struc[k,3]
DeltaRow[1,k] = (i==j) & (i==varno)
}
if (CONFA_Struc[k,1] == 5) {
// a theta_{jk}-type entry
// CONFA_Struc[k,.] = (5, equation #, variable k1, variable k2)
k1 = CONFA_Struc[k,3]
k2 = CONFA_Struc[k,4]
DeltaRow[1,k] = ((i==k1) & (j==k2) ) | ((i==k2) & (j==k1))
}
}
Delta = Delta \ DeltaRow
}
}
st_matrix(DeltaName,Delta)
}
///////////////////////////////////////////
// needed by confa_p.ado
void CONFA_P_EB(string Fnames, string ObsVarNames, string ToUseName) {
real matrix ff, xx;
// views
real matrix bb, Sigma, Lambda, Theta, Phi;
// substantive matrices
real scalar p
// view on the newly generated factors
st_view(ff=.,.,tokens(Fnames),ToUseName)
// view on the observed variables
st_view(xx=.,.,tokens(ObsVarNames),ToUseName)
// get the estimated matrices
bb = st_matrix("e(b)")
Sigma = st_matrix("e(Sigma)")
Theta = st_matrix("e(Theta)")
Lambda = st_matrix("e(Lambda)")
Phi = st_matrix("e(Phi)")
// # observed vars
p = rows(Sigma)
// prediction
ff[,] = (xx-J(rows(xx),1,1)*bb[1..p]) * invsym(Sigma) * Lambda * Phi
}
void CONFA_P_MLE(string Fnames, string ObsVarNames, string ToUseName) {
real matrix ff, xx;
// views
real matrix bb, Sigma, Lambda, Theta, Phi, ThetaInv;
// substantive matrices
real scalar p
// view on the newly generated factors
st_view(ff=.,.,tokens(Fnames),ToUseName)
// view on the observed variables
st_view(xx=.,.,tokens(ObsVarNames),ToUseName)
// get the estimated matrices
bb = st_matrix("e(b)")
Sigma = st_matrix("e(Sigma)")
Theta = st_matrix("e(Theta)")
Lambda = st_matrix("e(Lambda)")
Phi = st_matrix("e(Phi)")
// # observed vars
p = rows(Sigma)
// Theta is the vector of diagonal elements,
// so the inverse is easy!
ThetaInv = diag( 1:/Theta )
// prediction
ff[,] = (xx-J(rows(xx),1,1)*bb[1..p]) * ThetaInv * Lambda * invsym(Lambda' * ThetaInv * Lambda)
}
//////////////////////////////////
// needed by confa_lf.ado
void CONFA_NormalLKHDr( string ParsName, string lnfname) {
// ParsName are the parameters
// lnfname is the name of the likelihood variable
// the observed variables are in $CONFA_obsvar
real scalar CONFA_loglevel, nobsvar, ldetWS, i;
// log level, # obs vars, log determinant, cycle index
real matrix Sigma, means, SS, InvWorkSigma;
// intermediate computations
string scalar obsvar, touse;
// list of observed variables
real matrix data, lnl, parms;
// views
CONFA_loglevel = strtoreal( st_global("CONFA_loglevel"))
obsvar = st_global("CONFA_obsvar")
nobsvar = length(tokens(obsvar))
touse = st_global("CONFA_touse")
st_view(data=., ., tokens(obsvar), touse )
st_view(lnl=., ., tokens(lnfname), touse)
st_view(parms=., ., tokens(ParsName), touse)
// using the set up where the means are the first nobsvar entries of the parameter vector,
means = parms[1,1..nobsvar]
Sigma = CONFA_StrucToSigma(parms[1,.])
if (CONFA_loglevel > 2) {
parms[1,.]
means
Sigma
}
// do some equilibration??
SS = cholesky(Sigma)
InvWorkSigma = solvelower(SS,I(rows(SS)))
InvWorkSigma = solveupper(SS',InvWorkSigma)
ldetWS = 2*ln(dettriangular(SS))
for( i=1; i<=rows(data); i++ ) {
lnl[i,1] = -.5*(data[i,.]-means)*InvWorkSigma*(data[i,.]-means)' - .5*ldetWS - .5*nobsvar*ln(2*pi())
}
if (CONFA_loglevel>2) {
sum(lnl)
}
}
// normal likelihood with missing data
void CONFA_NormalLKHDrMiss( string ParsName, string lnfname) {
// ParsName are the parameters
// lnfname is the name of the likelihood variable
// the observed variables are in $CONFA_obsvar
real scalar CONFA_loglevel, nobsvar, thisldetWS, i, j;
// log level, # obs vars, log determinant, cycle index
real matrix Sigma, means, thisSigma, thisSS, thisInvSigma, thispattern, parms;
// intermediate computations
string scalar obsvar, misspat, touse;
// list of observed variables; the names of the missing patterns and touse tempvars
real matrix data, lnl, parmview, pattern, mdata, mlnl, info;
// views
CONFA_loglevel = strtoreal( st_global("CONFA_loglevel"))
obsvar = st_global("CONFA_obsvar")
nobsvar = length(tokens(obsvar))
misspat = st_global("CONFA_miss")
touse = st_global("CONFA_touse")
st_view(pattern=., ., misspat, touse )
st_view(data=., ., tokens(obsvar), touse )
st_view(lnl=., ., lnfname, touse )
// STILL USING THE FIRST OBSERVATIONS TO GET THE PARAMETERS!!!
st_view(parmview=., ., tokens(ParsName), touse )
parms = parmview[1,1..cols(parmview)]
if (CONFA_loglevel>2) {
obsvar
parms
}
// using the set up where the means are the first nobsvar entries of the parameter vector,
means = parms[1..nobsvar]
Sigma = CONFA_StrucToSigma(parms)
// utilize an existing set up of the missing data patterns
// data assumed to be sorted by the patterns of missing data
info = panelsetup( pattern, 1 )
for (i=1; i<=rows(info); i++) {
panelsubview(mdata=., data, i, info)
panelsubview(mlnl=., lnl, i, info)
// mdata should contain the portion of the data with the same missing data pattern
// mlnl will be conforming to mdata
// OK, now need to figure out that pattern
thispattern = J(1, cols(data), 1) - colmissing( mdata[1,] )
if (CONFA_loglevel > 2) {
printf("{txt}Pattern #{res}%5.0f{txt} :", i)
thispattern
};
// modify the matrices
thisSigma = select( select( Sigma, thispattern), thispattern' )
thisSS = cholesky(thisSigma)
thisInvSigma = solvelower(thisSS,I(rows(thisSS)))
thisInvSigma = solveupper(thisSS',thisInvSigma)
thisldetWS = 2*ln(dettriangular(thisSS))
if (CONFA_loglevel > 3) {
thisSigma
thisInvSigma
};
for( j=1; j<=rows(mdata); j++ ) {
// this is actually a single line broken by arithmetic operator signs
// that's bad style but it works
mlnl[j,1] = -.5*(select(data[j,.],thispattern)-select(means,thispattern)) *
thisInvSigma *
(select(data[j,.],thispattern)-select(means,thispattern))' -
.5*thisldetWS - .5*sum(thispattern)*ln(2*pi())
}
if (CONFA_loglevel>3) {
mlnl
};
}
}
// Bollen-Stine bootstrap rotation
void CONFA_BSrotate(
string SigmaName, // the parameter matrix name
string varnames // the variable names
) {
// declarations
real matrix data // views of the data
real matrix Sigma, SS, S2, SS2 // the covariance matrices and temp matrices
real matrix means // the means -- need modifications for weighted data!!!
real scalar n // dimension, no. obs
// get the data in
st_view(data=., ., tokens(varnames) )
n=rows(data)
Sigma = st_matrix(SigmaName)
// probability weights!!!
means = colsum(data)/n
SS = (cross(data,data)-n*means'*means)/(n-1)
S2 = cholesky(Sigma)
SS2 = cholesky(SS)
SS2 = solveupper(SS2',I(rows(SS)))
data[,] = data*SS2*S2'
}
// build a library
mata mlib create lconfa, replace
mata mlib add lconfa *()
mata mlib index
end
// of mata
exit
// don't need this:
string scalar CONFA_UL( string input ) {
string rowvector s;
real scalar i,j,n;
// tokenize input into a string vector
s = tokens( input )
n = cols( s )
for(i=1;i<=n;i++) {
// as I go over the elements, compare to the previous ones
for(j=1;j<i;j++) {
if ( s[i] == s[j] ) {
s[i] = ""
continue
}
}
}
// assemble back into a string scalar
return( stritrim(invtokens( s ) ) )
}

View File

@ -0,0 +1,299 @@
{smcl}
{* *! version 1.2.10 15may2007}{...}
{cmd:help confa} {right: ({browse "http://www.stata-journal.com/article.html?article=st0169":SJ9-3: st0169})}
{hline}
{title:Title}
{p2colset 5 14 16 2}{...}
{p2col :{hi:confa} {hline 2}}Confirmatory factor analysis{p_end}
{p2colreset}{...}
{title:Syntax}
{p 8 11 2}
{cmd:confa}
{it:factorspec} [{it:factorspec ...}]
{ifin}
{weight}
[{cmd:,} {it:options}]
{pstd}
{it:factorspec} is{p_end}
{p 8 27}
{cmd:(}{it:factorname}{cmd::} {it:varlist}{cmd:)}{p_end}
{synoptset 28 tabbed}{...}
{synopthdr}
{synoptline}
{syntab:Model}
{synopt :{cmdab:corr:elated(}{it:{help confa##corr:corrspec}} [...]{cmd:)}}correlated measurement errors{p_end}
{synopt :{cmd:unitvar(}{it:factorlist}|{cmd:_all}{cmd:)}}set variance of the factor(s) to 1{p_end}
{synopt :{opt free}}do not impose any constraints by default; seldom used{p_end}
{synopt :{opt constr:aint(numlist)}}user-supplied constraints;
must be used with {cmd:free}{p_end}
{synopt: {cmdab:miss:ing}}full-information maximum-likelihood
estimation with missing data{p_end}
{synopt: {cmdab:usen:ames}}alternative coefficient labeling{p_end}
{syntab:Variance estimation}
{synopt :{opth vce(vcetype)}}{it:vcetype} may be
{opt r:obust}, {opt cl:uster} {it:clustvar}, {cmd:oim}, {cmd:opg}, or
{opt sb:entler}{p_end}
{syntab:Reporting}
{synopt :{opt l:evel(#)}}set confidence level; default is {cmd:level(95)}{p_end}
{syntab:Other}
{synopt :{opt svy}}respect survey settings{p_end}
{synopt :{opth "from(confa##init_specs:init_specs)"}}control the starting values{p_end}
{synopt :{opt loglevel(#)}}specify the details of output; programmers only{p_end}
{synopt :{it:{help confa##maximize:ml_options}}}maximization options{p_end}
{synoptline}
{p2colreset}{...}
{title:Description}
{pstd}{cmd:confa} fits single-level confirmatory factor analysis (CFA) models.
In a CFA model, each of the variables is assumed to
be an indicator of underlying unobserved factor(s) with a linear dependence
between the factors and observed variables:
{center:{it:y_i} = {it:m_i} + {it:l_i1 f_1} + ... + {it:l_iK f_K} + {it:e_i}}
{pstd}where {it:y_i} is the {it:i}th variable
in the {it:varlist}, {it:m_i} is its mean,
{it:l_ik} are the latent variable loading(s),
{it:f_k} are the {it:k}th latent factor(s)
({it:k} = 1,...,{it:K}),
and {it:e_i} is the measurement error.
Thus the specification
{cmd:(}{it:factorname}{cmd::} {it:varlist}{cmd:)}
is interpreted as follows: the latent factor
{it:f_k} is given {it:factorname}
(for display purposes only);
the variables specified in
the {it:varlist} have their loadings, {it:l_ik}, estimated;
and all other observed variables in the model
have fixed loadings, {it:l_ik} = 0.
{pstd}The model is fitted by the maximum likelihood
procedure; see {helpb ml}.
{pstd}As with all latent variable models, a number
of identifying assumptions need to be made about
the latent variables {it:f_k}. They are assumed
to have mean zero, and their scales are determined
by the first variable in the {it:varlist}
(i.e., {it:l_1k} is set to equal 1 for all {it:k}).
Alternatively, identification can be achieved by setting the
variance of the latent variable to 1 (with option
{cmd:unitvar()}). More sophisticated identification
conditions can be achieved by specifying the
{cmd:free} option and then providing the necessary
constraints in the {cmd:constraint()} option.
{title:Options}
{dlgtab:Model}
{marker corr}{...}
{phang}{cmd:correlated(}{it:corrspec} [{it:corrspec} ...]{cmd:)}
specifies the correlated measurement errors {it:e_i} and {it:e_j}.
Here {it:corrspec} is of the form{p_end}
{pmore} [{cmd:(}]{it:varname_k}{cmd::}{it:varname_j}[{cmd:)}]{p_end}
{pmore}where {it:varname_k} and {it:varname_j} are some of
the observed variables in the model; that is, they must appear in at
least one
{it:factorspec} statement. If there
is only one correlation specified, the optional parentheses
shown above may be omitted. There should be no space between the colon and
{it:varname_j}.
{phang}{cmd:unitvar(}{it:factorlist}|{cmd:_all)} specifies the factors
(from those named in {it:factorspec}) that will be identified by setting
their variances to 1. The keyword {cmd:_all} can be used to specify that all
the factors have their variances set to 1 (and hence the matrix Phi can be
interpreted as a correlation matrix).
{phang}{cmd:free} frees up all the parameters in the model (making it
underidentified). It is then the user's responsibility to provide
identification constraints and adjust the degrees of freedom of the tests.
This option is seldom used.
{phang}{cmd:constraint(}{it:numlist}{cmd:)} can be used to supply additional
constraints. There are no checks implemented for
redundant or conflicting constraints, so in some rare cases, the degrees of
freedom may be incorrect. It might be wise to run the model with the
{cmd:free} and {cmd:iterate(0)} options and then look at the names in the output of
{cmd:matrix list e(b)} to find out the specific names of the parameters.
{phang}{cmd:missing} requests full-information maximum-likelihood estimation
with missing data. By default, estimation proceeds by listwise deletion.
{phang}{cmd:usenames} requests that the parameters be labeled with the names
of the variables and factors rather than with numeric values (indices of the
corresponding matrices). It is a technical detail that does not affect the
estimation procedure in any way, but it is helpful when working with several
models simultaneously, tabulating the estimation results, and transferring the
starting values between models.
{dlgtab:Variance estimation}
{phang}{cmd:vce(}{it:vcetype}{cmd:)} specifies different estimators of the
variance-covariance matrix. Common estimators ({cmd:vce(oim)},
observed information matrix, the default; {cmd:vce(robust)}, sandwich
information matrix; {cmd:vce(cluster }{it:clustvar}{cmd:)}, clustered
sandwich estimator with clustering on {it:clustvar}) are supported, along with
their aliases (the {cmd:robust} and {cmd:cluster(}{it:clustvar}{cmd:)}
options). See {manhelpi vce_option R}.
{pmore}An additional estimator specific to structural equation modeling is the Satorra-Bentler
estimator (Satorra and Bentler 1994). It is requested by
{cmd:vce(}{cmdab:sben:tler}{cmd:)} or {cmd:vce(}{cmdab:sat:orrabentler}{cmd:)}. When
this option is specified, additional Satorra-Bentler scaled and adjusted
goodness-of-fit statistics are computed and presented in the output.
{dlgtab:Reporting}
{phang}{cmd:level(}{it:#}{cmd:)} changes the confidence level for
confidence-interval reporting. See
{helpb estimation_options##level():[R] estimation options}.
{dlgtab:Other}
{phang}{cmd:svy} instructs {cmd:confa} to respect the complex
survey design, if one is specified. See {manhelp svyset SVY}.
{marker init_specs}{...}
{phang}{cmd:from(}{cmd:ones}|{cmd:2sls}|{cmd:ivreg}|{cmd:smart}|{it:ml_init_args}{cmd:)} provides the choice of starting values for the maximization
procedure. The {cmd:ml} command's internal default is to set all parameters to zero,
which leads to a noninvertible matrix, Sigma, and {cmd:ml} has to make many changes to those initial values to find anything feasible. Moreover, this
initial search procedure sometimes leads to a domain where the likelihood is
nonconcave, and optimization might fail there.
{phang2}{cmd:ones} sets all the parameters to values of one except
for covariance parameters (off-diagonal values of the Phi and Theta matrices),
which are set to 0.5. This might be a reasonable choice for data with
variances of observed variables close to 1 and positive covariances (no
inverted scales).
{phang2} {cmd:2sls} or {cmd:ivreg} requests that the initial parameters for the
freely estimated loadings be set to the two-stage least-squares
instrumental-variable estimates of Bollen (1996). This requires the model to
be identified by scaling indicators (i.e., setting one of the loadings to 1)
and to have at least three indicators for each latent variable. The instruments
used are all other indicators of the same factor. No checks for their validity
or search for other instruments is performed.
{phang2} {cmd:smart} provides an alternative set of starting values that
is often reasonable (e.g., assuming that the reliability of observed
variables is 0.5).
{pmore}Other specification of starting values, {it:ml_init_args}, should follow the format of
{cmd:ml init}. Those typically include the list of starting values of the form
{cmd:from(}{it:# #} ... {it:#}{cmd:, copy)} or a matrix of starting values
{cmd:from(}{it:matname}{cmd:,} [{cmd:copy}|{cmd:skip}]{cmd:)}. See {manhelp ml R}.
{phang}{cmd:loglevel(}{it:#}{cmd:)} specifies the details of output about
different stages of model setup and estimation,
and is likely of interest only to programmers. Higher numbers
imply more output.
{marker maximize}{...}
{phang}For other options, see {helpb maximize}.
{title:Remarks}
{pstd}{cmd:confa} relies on {cmd:listutil} for some parsing tasks. If
{cmd:listutil} is not installed in your Stata, {cmd:confa} will try to install
it from the SSC ({cmd:ssc install listutil}). If the installation is
unsuccessful, {cmd:confa} will issue an error message and stop.
{pstd}In large models, {cmd:confa} may be restricted by Stata's
{help limits:limit} of 244 characters in the string expression. You might
want to {helpb rename} your variables and give them shorter names.
{title:Examples}
{pstd}Holzinger-Swineford data{p_end}
{phang2}{cmd:. use hs-cfa}
{pstd}Basic model with different starting values{p_end}
{phang2}{cmd:. confa (vis: x1 x2 x3) (text: x4 x5 x6) (math: x7 x8 x9), from(ones)}{p_end}
{phang2}{cmd:. confa (vis: x1 x2 x3) (text: x4 x5 x6) (math: x7 x8 x9), from(iv)}{p_end}
{phang2}{cmd:. confa (vis: x1 x2 x3) (text: x4 x5 x6) (math: x7 x8 x9), from(smart)}
{pstd}Robust and Satorra-Bentler standard errors{p_end}
{phang2}{cmd:. confa (vis: x1 x2 x3) (text: x4 x5 x6) (math: x7 x8 x9), from(iv) vce(sbentler)}{p_end}
{phang2}{cmd:. confa (vis: x1 x2 x3) (text: x4 x5 x6) (math: x7 x8 x9), from(iv) robust}
{pstd}Correlated measurement errors{p_end}
{phang2}{cmd:. confa (vis: x1 x2 x3) (text: x4 x5 x6) (math: x7 x8 x9), from(iv) corr( x7:x8 )}
{pstd}An alternative identification{p_end}
{phang2}{cmd:. confa (vis: x1 x2 x3) (text: x4 x5 x6) (math: x7 x8 x9), from(ones) unitvar(_all) corr(x7:x8)}
{pstd}Missing data{p_end}
{phang2}{cmd:. forvalues k=1/9 {c -(}}{p_end}
{phang2}{cmd:. gen y`k' = cond( uniform()<0.0`k', ., x`k')}{p_end}
{phang2}{cmd:. {c )-}}{p_end}
{phang2}{cmd:. confa (vis: y1 y2 y3) (text: y4 y5 y6) (math: y7 y8 y9), from(iv)}{p_end}
{phang2}{cmd:. confa (vis: y1 y2 y3) (text: y4 y5 y6) (math: y7 y8 y9), from(iv) missing difficult}{p_end}
{title:Saved results}
{pstd}Aside from the standard {help estcom:estimation results}, {cmd:confa}
also performs the overall goodness-of-fit test with results
saved in {cmd:e(lr_u)}, {cmd:e(df_u)}, and {cmd:e(p_u)}
for the test statistic, its goodness of fit, and the resulting
p-value. A test versus the model with the independent data
is provided with the {helpb ereturn} results with the {cmd:indep}
suffix. Here, under the null hypothesis,
the covariance matrix is assumed diagonal.
{pstd}When {cmd:sbentler} is specified, Satorra-Bentler standard errors are
computed and posted as {cmd:e(V)}, with intermediate matrices saved in
{cmd:e(SBU)}, {cmd:e(SBV)}, {cmd:e(SBGamma)}, and {cmd:e(SBDelta)}. Also,
several corrected overall fit test statistics is reported and saved: T scaled
({cmd:ereturn} results with the {cmd:Tsc} suffix) and T adjusted ({cmd:ereturn}
results with the {cmd:Tadj} suffix). Scalars {cmd:e(SBc)} and {cmd:e(SBd)} are
the scaling constants, with the latter also being the approximate degrees of
freedom of the chi-squared test from Satorra and Bentler (1994), and T double
bar from Yuan and Bentler (1997) (with the {cmd:T2} suffix).
{title:References}
{phang}Bollen, K. A. 1996. An alternative two stage least squares (2SLS)
estimator for latent variable equations. {it:Psychometrika} 61: 109-121.
{phang}Satorra, A., and P. M. Bentler. 1994. Corrections to test statistics and standard errors in covariance structure analysis. In {it:Latent Variables Analysis}, ed. A. von Eye and C. C. Clogg, 399-419. Thousand Oaks, CA: Sage.
{phang} Yuan, K.-H., and P. M. Bentler. 1997. Mean and covariance structure analysis: Theoretical and practical improvements.
{it:Journal of the American Statistical Association} 92: 767-774.
{title:Author}
{pstd}Stanislav Kolenikov{p_end}
{pstd}Department of Statistics{p_end}
{pstd}University of Missouri{p_end}
{pstd}Columbia, MO{p_end}
{pstd}kolenikovs@missouri.edu{p_end}
{title:Also see}
{psee}
Article: {it:Stata Journal}, volume 9, number 3: {browse "http://www.stata-journal.com/article.html?article=st0169":st0169}
{psee}Online: {helpb factor}, {helpb bollenstine}, {helpb confa_estat:confa postestimation} (if installed)
{p_end}

View File

@ -0,0 +1,222 @@
*! version 2.0.2 08 Sep 2009; part of confa suite
program confa_estat, rclass
version 10
if "`e(cmd)'" != "confa" {
error 301
}
gettoken subcmd rest : 0, parse(" ,")
local lsubcmd= length("`subcmd'")
if `"`subcmd'"' == substr("fitindices", 1, max(3, `lsubcmd')) {
FitIndices `rest'
}
else if `"`subcmd'"' == substr("correlate", 1, max(4, `lsubcmd')) {
Correlate `rest'
}
else if `"`subcmd'"' == substr("aic",1, max(3, `lsubcmd')) {
FitIndices , aic
}
else if `"`subcmd'"' == substr("bic",1, max(3, `lsubcmd')) {
FitIndices , bic
}
else if `"`subcmd'"' == substr("ic",1,max(2, `lsubcmd')) {
FitIndices, aic bic
}
else if `"`subcmd'"' == substr("summarize", 1, max(2, `lsubcmd')) {
Summarize `rest'
}
else if `"`subcmd'"' == "vce" {
vce `rest'
}
else {
di as err "`subcmd' not allowed"
exit 198
}
return add
end
program Summarize
syntax , [noHEAder noWEIghts]
if "`e(wexp)'" ~= "" & "`weights'"~="noweights" local wgt [iw `e(wexp)']
sum `e(observed)' `wgt' if e(sample)
end
program Correlate, rclass
* correlation matrix of estimated factors
syntax, [level(passthru) bound]
di _n "{txt}Correlation equivalents of covariances"
if "`bound'" != "" local bound ci(atanh)
local q = rowsof( e(Phi) )
if `q'>1 {
* display the factor correlations
di as text "{hline 13}{c TT}{hline 64}"
if "`e(vcetype)'" ~= "" {
di as text " {c |} {center 15:`e(vcetype)'}"
}
di as text " {c |} Coef. Std. Err. z P>|z| [$S_level% Conf. Interval]"
di as text "{hline 13}{c +}{hline 64}"
* parse the factor names
local fnames : rownames e(Phi)
* parse the unitvar list to be used in -inlist-
local unitvarlist = `"""' + subinstr("`e(unitvar)'"," ",`"",""',.) + `"""'
_diparm __lab__ , label("Factors") eqlabel
forvalues i=1/`q' {
local i1 = `i'+1
forvalues j=`i1'/`q' {
if inlist("`: word `i' of `fnames''", `unitvarlist') & inlist("`: word `j' of `fnames''", `unitvarlist') {
* both factor variances are constrained at 1, display as is
_diparm phi_`i'_`j' , prob label("`: word `i' of `fnames''-`: word `j' of `fnames''") `level' `bound'
}
else if inlist("`: word `i' of `fnames''", `unitvarlist') & !inlist("`: word `j' of `fnames''", `unitvarlist') {
* `i' is restricted unit variance, `j' is not
_diparm phi_`i'_`j' phi_`j'_`j', ///
function( @1/sqrt(@2) ) d( 1/sqrt(@2) -0.5*@1/sqrt(@2*@2*@2) ) ///
prob label("`: word `i' of `fnames''-`: word `j' of `fnames''") `level' `bound'
}
else if !inlist("`: word `i' of `fnames''", `unitvarlist') & inlist("`: word `j' of `fnames''", `unitvarlist') {
* `j' is restricted unit variance, `i' is not
_diparm phi_`i'_`j' phi_`i'_`i', ///
function( @1/sqrt(@2) ) d( 1/sqrt(@2) -0.5*@1/sqrt(@2*@2*@2) ) ///
prob label("`: word `i' of `fnames''-`: word `j' of `fnames''") `level' `bound'
}
else {
* display correlation transform
_diparm phi_`i'_`j' phi_`i'_`i' phi_`j'_`j', ///
function( @1/sqrt(@2*@3) ) d( 1/sqrt(@2*@3) -0.5*@1/sqrt(@2*@2*@2*@3) -0.5*@1/sqrt(@2*@3*@3*@3) ) ///
prob label("`: word `i' of `fnames''-`: word `j' of `fnames''") `level' `bound'
}
}
}
}
if "`e(correlated)'" ~= "" {
if `q' < 2 {
* need to display the header
di as text "{hline 13}{c TT}{hline 64}"
if "`e(vcetype)'" ~= "" {
di as text " {c |} {center 15:`e(vcetype)'}"
}
di as text " {c |} Coef. Std. Err. z P>|z| [$S_level% Conf. Interval]"
di as text "{hline 13}{c +}{hline 64}"
}
* print out correlated measurement errors
_diparm __lab__ , label("Errors") eqlabel
local correlated `e(correlated)'
local obsvar `e(observed)'
while "`correlated'" != "" {
gettoken corrpair correlated : correlated , match(m)
gettoken corr1 corrpair : corrpair, parse(":")
unab corr1 : `corr1'
gettoken sc corr2 : corrpair, parse(":")
unab corr2 : `corr2'
poslist `obsvar' \ `corr1', global(CFA_temp)
local k1 = $CFA_temp
poslist `obsvar' \ `corr2', global(CFA_temp)
local k2 = $CFA_temp
_diparm theta_`k1'_`k2' theta_`k1' theta_`k2', ///
function( @1/sqrt(@2*@3) ) d( 1/sqrt(@2*@3) -0.5*@1/sqrt(@2*@2*@2*@3) -0.5*@1/sqrt(@2*@3*@3*@3) ) ///
prob label("`corr1'-`corr2'") `level' `bound'
}
}
else if `q'<2 {
di as text _n "Nothing to display" _n
}
di as text "{hline 13}{c BT}{hline 64}"
global CFA_temp
end
program FitIndices, rclass
syntax , [all tli rmsea rmsr aic bic]
di _n "{txt} Fit indices" _n
return add
* all, by default
if "`*'" == "" local all 1
* the fundamentals
local p = `: word count `e(obsvar)''
if "`rmsea'`all'"~="" {
return scalar RMSEA = sqrt( max( (e(lr_u)-e(df_u))/(e(N)-1), 0 )/e(df_u) )
tempname ll lu
scalar `ll' = cond(chi2(e(df_u),e(lr_u))>0.95,npnchi2(e(df_u),e(lr_u),0.95),0)
scalar `lu' = cond(chi2(e(df_u),e(lr_u))>0.05,npnchi2(e(df_u),e(lr_u),0.05),0)
return scalar RMSEA05 = sqrt( `ll'/( (e(N)-1)*e(df_u) ) )
return scalar RMSEA95 = sqrt( `lu'/( (e(N)-1)*e(df_u) ) )
di "{txt}RMSEA {col 8}= {res}" %6.4f return(RMSEA) _c
di "{txt}, 90% CI{col 8}= ({res}" %6.4f return(RMSEA05) "{txt}, {res}" %6.4f return(RMSEA95) "{txt})"
}
if "`rmsr'`all'"~="" {
cap mat li e(S)
if _rc {
* no matrix posted
return scalar RMSR = .
}
else {
tempname res
mata : st_numscalar("`res'",norm(vech(st_matrix("e(Sigma)") - st_matrix("e(S)"))) )
return scalar RMSR = `res'/sqrt(e(pstar) )
}
di "{txt}RMSR {col 8}= {res}" %6.4f return(RMSR)
}
if "`tli'`all'"~="" {
return scalar TLI = (e(lr_indep)/e(df_indep)-e(lr_u)/e(df_u))/(e(lr_indep)/e(df_indep)-1)
di "{txt}TLI {col 8}= {res}" %6.4f return(TLI)
}
if "`cfi'`all'"~="" {
return scalar CFI = 1 - max( e(lr_u)-e(df_u),0 )/max( e(lr_u)-e(df_u), e(lr_indep)-e(df_indep),0 )
di "{txt}CFI {col 8}= {res}" %6.4f return(CFI)
}
if "`aic'`all'"~="" {
if "`e(wexp)'" == "" & "`e(vcetype)'"~="Robust" return scalar AIC = -2*e(ll) + 2*e(df_m)
else return scalar AIC = .
di "{txt}AIC {col 8}= {res}" %8.3f return(AIC)
}
if "`bic'`all'"~="" {
if "`e(wexp)'" == "" & "`e(vcetype)'"~="Robust" return scalar BIC = -2*e(ll) + e(df_m)*ln( e(N) )
else return scalar BIC = .
di "{txt}BIC {col 8}= {res}" %8.3f return(BIC)
}
end
exit
Version history:
1.0.0 9 Jan 2008 -- Correlate
FitIndices
1.0.1 12 Sep 2008 -- AIC, BIC
1.0.2 Oct 2008 -- bound for Correlate CI
correlated measurement errors
CI for RMSEA

View File

@ -0,0 +1,205 @@
{smcl}
{* *! version 1.1.13 04jun2007}{...}
{cmd:help confa postestimation}{right: ({browse "http://www.stata-journal.com/article.html?article=st0169":SJ9-3: st0169})}
{hline}
{title:Title}
{p2colset 5 29 31 2}{...}
{p2col :{hi:confa postestimation} {hline 2}}Postestimation tools for confa{p_end}
{p2colreset}{...}
{title:Description}
{pstd}The following commands are available after {helpb confa}:{p_end}
{synoptset 17}{...}
{p2coldent :command}description{p_end}
{synoptline}
{synopt :{helpb confa_estat##fit:estat fitindices}}fit indices{p_end}
{synopt :{helpb confa_estat##ic:estat aic}}AIC{p_end}
{synopt :{helpb confa_estat##ic:estat bic}}BIC{p_end}
{synopt :{helpb confa_estat##corr:estat correlate}}correlations of factors and measurement errors{p_end}
{synopt :{helpb confa_estat##predict:predict}}factor scores{p_end}
{synopt :{helpb bollenstine}}Bollen-Stine bootstrap{p_end}
{synoptline}
{p2colreset}{...}
{marker fit}{...}
{title:The estat fitindices command}
{title:Syntax}
{p 8 15 2}
{cmd:estat} {cmdab:fit:indices}
[{cmd:,} {it:options}]
{p2colset 9 27 29 2}{...}
{p2col:{it:options}}fit index{p_end}
{p2line}
{p2col :{opt aic}}Akaike information criterion{p_end}
{p2col :{opt bic}}Schwarz Bayesian information criterion{p_end}
{p2col :{opt cfi}}comparative fit index{p_end}
{p2col :{opt rmsea}}root mean squared error of approximation{p_end}
{p2col :{opt rmsr}}root mean squared residual{p_end}
{p2col :{opt tli}}Tucker-Lewis index{p_end}
{p2col :{opt _all}}all the above indices, the default{p_end}
{p2line}
{p2colreset}{...}
{title:Description}
{pmore}{opt estat }{cmd:fitindices} computes, prints, and saves into
{cmd:r()} results several traditional fit indices.
{title:Options}
{phang2}
{opt aic} requests the Akaike information criterion (AIC).
{phang2}
{opt bic} requests the Schwarz Bayesian information criterion (BIC).
{phang2}
{opt cfi} requests the CFI (Bentler 1990b).
{phang2}
{opt rmsea} requests the RMSEA (Browne and Cudeck 1993).
{phang2}
{opt rmsr} requests the RMSR.
{phang2}
{opt tli} requests the TLI (Tucker and Lewis 1973).
{phang2}
{opt _all} requests all the above indices. This is the default
behavior if no option is specified.
{marker ic}{...}
{title:The estat aic and estat bic commands}
{title:Syntax}
{p 8 15 2}
{cmd:estat} {cmd:aic}
{p 8 15 2}
{cmd:estat} {cmd:aic}
{title:Description}
{pmore}{cmd:estat aic} and {cmd:estat bic} compute the Akaike and Schwarz
Bayesian information criteria, respectively.
{title:The estat correlate command}
{title:Syntax}
{p 8 15 2}
{cmd:estat} {cmdab:corr:elate}
[{cmd:,}
{opt level(#)}
{opt bound}]
{title:Description}
{marker corr}{...}
{pmore}{opt estat} {cmd:correlate} transforms the covariance parameters into
correlations for factor covariances and measurement-error covariances. The
delta method standard errors are given; for correlations close to plus or
minus 1, the confidence intervals may extend beyond the range of admissible
values.{p_end}
{title:Options}
{phang2}{opt level(#)} changes the confidence level for confidence-interval
reporting.{p_end}
{phang2}{cmd:bound} provides an alternative confidence interval based on
Fisher's z transform of the correlation coefficient. It guarantees
that the end points of the interval are in the (-1,1) range, provided the
estimate itself is in this range.
{marker predict}{...}
{title:The predict command}
{title:Syntax}
{p 8 19 2}
{cmd:predict} {dtype} {it:{help newvarlist}} {ifin} [{cmd:,} {it:scoring_method}]
{p2colset 9 27 29 2}{...}
{p2col:{it:scoring_method}}factor scoring method{p_end}
{p2line}
{p2col:{cmdab:reg:ression}}regression, or empirical Bayes, score{p_end}
{p2col:{cmdab:emp:iricalbayes}}alias for {cmd:regression}{p_end}
{p2col:{cmdab:eb:ayes}}alias for {cmd:regression}{p_end}
{p2col:{opt mle}}MLE, or Bartlett score{p_end}
{p2col:{cmdab:bart:lett}}alias for {cmd:mle}{p_end}
{p2line}
{p2colreset}{...}
{title:Description}
{pmore} {cmd:predict} can be used to create factor scores following {cmd:confa}.
The number of variables in {it:newvarlist} must be the same as the number of
factors in the model specification; all factors are predicted at once by the
relevant matrix formula.
{title:Options}
{phang2}
{opt regression}, {opt empiricalbayes}, or {opt ebayes}
requests regression, or empirical Bayes, factor scoring procedure.
{phang2}
{opt mle} or {opt bartlett} requests Bartlett scoring procedure.
{title:Example}
{phang}{cmd:. use hs-cfa}{p_end}
{phang}{cmd:. confa (vis: x1 x2 x3) (text: x4 x5 x6) (math: x7 x8 x9), from(iv) corr(x7:x8)}{p_end}
{phang}{cmd:. estat fit}{p_end}
{phang}{cmd:. estat corr}{p_end}
{phang}{cmd:. estat corr, bound}{p_end}
{phang}{cmd:. predict fa1-fa3, reg}{p_end}
{phang}{cmd:. predict fb1-fb3, bart}{p_end}
{title:References}
{phang}
Bentler, P. M. 1990. Comparative fit indexes in structural models.
{it:Psychological Bulletin} 107: 238-246.
{phang}
Browne, M. W., and R. Cudeck. 1993. Alternative ways of assessing model fit.
In {it:Testing Structural Equation Models}, ed. K. A. Bollen and J. S. Long,
136-162. Newbury Park, CA: Sage.
{phang}
Tucker, L. R., and C. Lewis. 1973. A reliability coefficient for maximum likelihood factor analysis. {it:Psychometrika} 38: 1-10.
{title:Author}
{pstd}Stanislav Kolenikov{p_end}
{pstd}Department of Statistics{p_end}
{pstd}University of Missouri{p_end}
{pstd}Columbia, MO{p_end}
{pstd}kolenikovs@missouri.edu{p_end}
{title:Also see}
{psee}
Article: {it:Stata Journal}, volume 9, number 3: {browse "http://www.stata-journal.com/article.html?article=st0169":st0169}
{psee}Online: {helpb confa}, {helpb bollenstine}{p_end}

View File

@ -0,0 +1,20 @@
*! Log likelihood for confa: linear form; part of confa suite; 16 Oct 2008
program define confa_lf
args lnf $CONFA_args
* $CONFA_args contains the names of the equations, but
* we need the variable names
gettoken lnf allthenames : 0
tempvar lnl
qui g double `lnl' = .
mata: CONFA_NormalLKHDr( "`allthenames'", "`lnl'")
qui replace `lnf' = `lnl'
if $CONFA_loglevel > 3 li `lnl'
end
exit

View File

@ -0,0 +1,20 @@
*! Log likelihood for confa: linear form; part of confa suite; 23 Apr 2009
program define confa_lfm
args lnf $CONFA_args
* $CONFA_args contains the names of the equations, but
* we need the variable names
gettoken lnf allthenames : 0
tempvar lnl
qui g double `lnl' = .
mata: CONFA_NormalLKHDrMiss( "`allthenames'", "`lnl'")
qui replace `lnf' = `lnl'
if $CONFA_loglevel > 3 li `lnl'
end
exit

View File

@ -0,0 +1,89 @@
*! v.1.0 -- prediction commands for confa suite; 16 Oct 2008
program define confa_p
version 10
* set trace on
* di as inp "`0'"
syntax anything [if] [in], [EBayes EMPiricalbayes REGression MLE BARTlett SCores]
* fork to equation scores or factor scores
if "`scores'"!="" EqScores `0'
else if "`ebayes'`empiricalbayes'`regression'`mle'`bartlett'" != "" FScores `0'
else {
di as err "cannot figure those options out"
exit 198
}
end
program define EqScores
* implicitly used by _robust and svy
* typical request: predict stub*, scores
* stub* is not parsed well by newvarlist, have to use anything
syntax anything [if] [in], scores
marksample touse, novarlist
ml score `anything' if `touse'
end
program define FScores
* user requested factor predictions
syntax newvarlist [if] [in], [EBayes EMPiricalbayes REGression MLE BARTlett]
marksample touse, novarlist
if "`ebayes'`empiricalbayes'`regression'`mle'`bartlett'" == "" | ///
( ("`ebayes'`empiricalbayes'`regression'"~="" ) & ("`mle'`bartlett'"~="" ) ) {
di as err "One and only one factor scoring option must be specified"
exit 198
}
else {
local nfactors = rowsof( e(Phi) )
if "`: word count `varlist''" ~= "`nfactors'" {
di as err "Must specify as many new variables as there were factors in confa model"
exit 198
}
* generate new variables
forvalues k=1/`nfactors' {
tempname f`k'
qui gen double `f`k'' = .
local flist `flist' `f`k''
}
if "`ebayes'`empiricalbayes'`regression`" ~= "" {
* Empirical Bayes:
mata : CONFA_P_EB("`flist'", "`e(observed)'", "`touse'")
}
if "`mle'`bartlett'" ~= "" {
* MLE/Bartlett scoring:
mata : CONFA_P_MLE("`flist'", "`e(observed)'", "`touse'")
}
nobreak {
forvalues k=1/`nfactors' {
local type : word `k' of `typlist'
local name : word `k' of `varlist'
qui gen `type' `name' = `f`k'' if `touse'
label var `name' `"`e(factor`k')', `ebayes'`empiricalbayes'`regression'`mle'`bartlett' method"'
}
}
}
end
exit
History:
v.1.0 -- June 12, 2008: Empirical Bayes and MLE scoring

View File

@ -0,0 +1,18 @@
*! comfirmdir Version 1.1 dan.blanchette@duke.edu 22Jan2009
*! Center of Entrepreneurship and Innovation Duke University's Fuqua School of Business
* confirmdir Version 1.1 dan_blanchette@unc.edu 17Jan2008
* research computing, unc-ch
* - made it handle long directory names
** confirmdir Version 1.0 dan_blanchette@unc.edu 05Oct2003
** the carolina population center, unc-ch
program define confirmdir, rclass
version 8
local cwd `"`c(pwd)'"'
quietly capture cd `"`1'"'
local confirmdir=_rc
quietly cd `"`cwd'"'
return local confirmdir `"`confirmdir'"'
end

View File

@ -0,0 +1,54 @@
{smcl}
{* 17Jan2008}{...}
{* 28Oct2004}{...}
{* 19Nov2003}{...}
{hline}
help for {hi:confirmdir} {right:manual: {hi:[R] none}}
{right:dialog: {hi: none} }
{hline}
{title:Confirms if directory exists}
{p 8 17 2}
{cmd:confirmdir} {it:full direcotory name}
{p_end}
{title:Description}
{p 4 4 2}{cmd:confirmdir} is designed for programmers who want to know if a directory exists.
This is just like {cmd:confirm} command when used to confirm a file. If Stata allowed their
{cmd:confirm} command to also have the "dir" option, this program would not have been
written. Stata's {cmd:confirm file} will confirm a directory in UNIX/Linux but not in Windows.
{cmd:confirmdir} works in all operating systems.{p_end}
{title:Examples}
{p 4 8 2}{cmd:. confirmdir "c:\My Favorite Directory\Where I Keep Stuff\"}{p_end}
{p 4 8 2}{cmd:. confirmdir /projects/ethiopia/survey2002/data/}{p_end}
{title:Saved Results}
{p 4 8 2}The {cmd:confirmdir} command saves in {cmd:r()}:{p_end}
{synoptset 20 tabbed}{...}
{p2col 5 20 24 2: Macros}{p_end}
{synopt:{cmd:r(confirmdir)}}return code returned by the {help cd:cd} command.{p_end}
{title:Author}
{p 4 4 2}
Dan Blanchette {break}
Center of Entrepreneurship and Innovation {break}
Duke University's Fuqua School of Business {break}
Dan.Blanchette@Duke.edu{p_end}
{title:Also see}
{p 4 13 2}On-line: {help confirm:confirm} {help cd:cd}, {help tmpdir:tmpdir} (if installed){p_end}

View File

@ -0,0 +1,62 @@
program def convlist, rclass
*! NJC 1.0.0 9 April 2001
version 6.0
gettoken lists 0 : 0, parse(",")
if "`lists'" == "" | "`lists'" == "," {
di in r "no lists specified"
exit 198
}
tokenize "`lists'", parse("\")
if "`4'" != "" | "`2'" != "\" {
di in r "incorrect syntax"
exit 198
}
numlist "`1'"
local list1 `r(numlist)'
local n1 : word count `list1'
numlist "`3'"
local list2 `r(numlist)'
local n2 : word count `list2'
syntax [ , Global(str) Noisily ]
if length("`global'") > 8 {
di in r "global name must be <=8 characters"
exit 198
}
local n3 = `n1' + `n2' - 1
local k = 1
while `k' <= `n3' {
tempname c`k'
scalar `c`k'' = 0
local k = `k' + 1
}
tokenize `list1'
local i = 1
while `i' <= `n1' {
local j = 1
while `j' <= `n2' {
local b`j' : word `j' of `list2'
local k = `i' + `j' - 1
scalar `c`k'' = `c`k'' + (``i'' * `b`j'')
local j = `j' + 1
}
local i = `i' + 1
}
local k = 1
while `k' <= `n3' {
local this = `c`k''
local newlist "`newlist'`this' "
local k = `k' + 1
}
if "`noisily'" != "" { di "`newlist'" }
if "`global'" != "" { global `global' "`newlist'" }
return local list `newlist'
end

View File

@ -0,0 +1,2 @@
.h listutil

View File

@ -0,0 +1,877 @@
*! version 0.8.1 - 2009-07-31
* work around S11 bug in estimates table
* To do:
* 1) add weights
* 2) use returns rather than scalars
// compare count models
capture program drop countfit
program define countfit, rclass
version 9.0
* 2009-07-31
local vwidth = 32 // causes error in Stata 11
local vwidth = 30
syntax varlist [if] [in] ///
, [Generate(string) replace ///
INFlate(string) /// inflation variables
MAXcount(integer 9) ///
NOGraph /// suppress graph of differences in predicted probabilities
NODifferences /// suppress table of differences in predicted probabilities
NOEstimates /// suppress table of estimated parameters
NOFit /// suppress table of fit statistics
Prm Nbreg ZIP ZINb ///
nodash ///
NOConstant ///
NOPRTable ///
note(string) /// note for graph
GRAPHexport(string) /// options passed to graph export
NOIsily ///
]
// trap weights
if "`e(wtype)'"!="" {
di in r "-countfix- does not work with weights."
exit
}
// define variable lists
* inflate is same as rhs if not specified
if "`inflate'"=="" {
** better way to do this?
local nvars : word count `varlist'
foreach i of numlist 2(1)`nvars' {
local var : word `i' of `varlist'
local inflate "`inflate' `var'"
}
}
// set up printing
local f83 "%8.3f"
local f93 "%9.3f"
local f93l "%-9.3f"
local f103 "%10.3f"
local dash ""
if "`nodash'"=="" {
local dash ///
"--------------------------------------------------------------------------"
}
// construct information on models being assessed
* defalult name is CF for variables created
if "`generate'"=="" local set "CF"
local set "`generate'" // label for sets of models
local modellist "" // list of types of models estimated
local mdlnmlist "" // like modellist but begin with generate prefix _
local pltsym "" // symbols used in the plot
local plty "" // y variables to plot
local pltx "" // x variables to plot
local mdlnum = 1 // number associated with each model
* set up information for all models specified
if "`prm'"=="prm" {
local mdlnmlist "`mdlnmlist' `set'PRM"
local modellist "PRM "
local pltx "`set'PRMval"
local plty "`set'PRMdif "
local pltsym "Th "
local pltleg1 "`set'PRM"
local mdlnum = `mdlnum' + 1
}
if "`nbreg'"=="nbreg" {
local mdlnmlist "`mdlnmlist' `set'NBRM"
local modellist "`modellist'NBRM "
local pltx "`set'NBRMval"
local plty "`plty' `set'NBRMdif "
local pltsym "`pltsym' Sh "
local pltleg`mdlnum' "`set'NBRM"
local mdlnum = `mdlnum' + 1
}
if "`zip'"=="zip" {
local mdlnmlist "`mdlnmlist' `set'ZIP"
local modellist "`modellist'ZIP "
local pltx "`set'ZIPval"
local plty "`plty' `set'ZIPdif "
local pltsym "`pltsym' T "
local pltleg`mdlnum' "`set'ZIP"
local mdlnum = `mdlnum' + 1
}
if "`zinb'"=="zinb" {
local mdlnmlist "`mdlnmlist' `set'ZINB"
local modellist "`modellist'ZINB "
local pltx "`set'ZINBval"
local plty "`plty' `set'ZINBdif "
local pltsym "`pltsym' S "
local pltleg`mdlnum' "`set'ZINB"
local mdlnum = `mdlnum' + 1
}
* if none, then all
if "`prm'"=="" & "`nbreg'"=="" & "`zip'"=="" & "`zinb'"=="" {
local pltx "`set'PRMval"
local plty "`set'PRMdif `set'NBRMdif `set'ZIPdif `set'ZINBdif"
local pltsym "Th Sh T S"
local pltleg1 "`set'PRM"
local pltleg2 "`set'NBRM"
local pltleg3 "`set'ZIP"
local pltleg4 "`set'ZINB"
local mdlnmlist "`set'PRM `set'NBRM `set'ZIP `set'ZINB"
local modellist "PRM NBRM ZIP ZINB"
local prm "prm"
local nbreg "nbreg"
local zip "zip"
local zinb "zinb"
* drop? local alphaopt "drop(lnalpha:_cons)"
}
// estimate models
* 2009-07-31
_count_estimate `varlist' `if' `in' [`fweight' `pweight' `iweight'], ///
max(`maxcount') gen(`generate') `replace' inflate(`inflate') ///
`prm' `nbreg' `zip' `zinb' `noconstant' `noisily'
// table of estimates
if "`noestimates'"=="" {
estimates table `mdlnmlist', eform ///
b(%9.3f) t(%7.2f) label varwidth(`vwidth') ///
stats(alpha N ll bic aic)
}
// summary table of mean observed - predicted probabilities
local c1 = 10
local c2 = 25
local c3 = 31
local c4 = 47
if "`nodifferences'"=="" {
di in g "Comparison of Mean Observed and Predicted Count"
di
di in g " " _col(`c1') " Maximum" ///
_col(`c2') " At" ///
_col(`c3') " Mean"
di in g "Model" _col(`c1') " Difference" ///
_col(`c2') "Value" ///
_col(`c3') " |Diff|"
di in g "---------------------------------------------"
}
* loop through models and compute differences
foreach m in `modellist' {
local modelnm "`set'`m'"
* stats on difference
qui sum `modelnm'dif
local toplot "`toplot' `modelnm'dif"
local `m'difsd = r(sd) // sd of obs-pred
local `m'difmin = r(min) // min
local `m'difmax = r(max) // max
* stats on mean absolute difference
capture drop `modelnm'difabs
qui gen `modelnm'absdif = abs(`modelnm'dif)
qui sum `modelnm'absdif
local `m'absdifmax = r(max) // max
scalar absdifsd`m' = r(sd) // sd
scalar absdifmn`m' = r(mean) // mean
* find values for largest difference
tempname difval
qui gen `difval' = (`modelnm'absdif>``m'absdifmax'-.00001)*_n ///
if `modelnm'absdif!=.
qui sum `difval'
local `m'absdifmaxval = r(max) - 1
* sign of deviation
tempname maxdif // 0.8.0
if ``m'absdifmax' == abs(``m'difmin') {
*local maxdif = ``m'difmin' // 0.8.0
scalar `maxdif' = ``m'difmin'
}
if ``m'absdifmax' == abs(``m'difmax') {
*local maxdif = ``m'difmax' // 0.8.0
scalar `maxdif' = ``m'difmax'
}
if "`nodifferences'"=="" {
* print summary of differences from predictions
di in y "`modelnm'" _col(`c1') in y `f83' `maxdif' ///
_col(`c2') `f83' " ``m'absdifmaxval'" ///
_col(`c3') `f83' absdifmn`m'
}
} // loop through models
// TABLES OF OBSERVED AND PREDICTED COUNTS
if "`noprtable'" == "" {
foreach t in `mdlnmlist' {
qui {
sum `t'absdif
local max = r(N) - 1 // largest count
tempname sumde sumpe sumob sumpr // 0.8.0
scalar `sumde' = r(sum) // sum of abs dif
sum `t'pearson
scalar `sumpe' = r(sum) // sum of pearson dif
sum `t'obeq
scalar `sumob' = r(sum) // sum of pr observed
sum `t'preq
scalar `sumpr' = r(sum) // sum of pr predicted
} // qui
local c1 = 7
local c2 = 19
local c3 = 30
local c4 = 40
local c5 = 50
di
di in y "`t'" in g ": Predicted and actual probabilities"
di
di in g "Count" _col(`c1') " Actual" ///
_col(`c2') "Predicted" ///
_col(`c3') " |Diff|" ///
_col(`c4') " Pearson"
local dash ""
if "`nodash'"=="" {
local dash ///
"------------------------------------------------"
}
di in g "`dash'"
foreach c of numlist 0(1)`max' {
local i = `c' + 1
tempname ob pr de pe // 0.8.0
scalar `ob' = `t'obeq[`i']
scalar `pr' = `t'preq[`i']
scalar `de' = abs(`t'dif[`i'])
scalar `pe' = `t'pearson[`i']
di in y "`c'" ///
_col(`c1') `f83' `ob' ///
_col(`c2') `f83' `pr' ///
_col(`c3') `f83' `de' ///
_col(`c4') `f83' `pe'
} // loop through counts
di in g "`dash'"
di in y "Sum" ///
_col(`c1') `f83' `sumob' ///
_col(`c2') `f83' `sumpr' ///
_col(`c3') `f83' `sumde' ///
_col(`c4') `f83' `sumpe'
} // loop through modellist
} // print table of predictions
// PLOT DIFFERENCES
if "`nograph'"=="" {
twoway (connected `plty' `pltx', ///
msymbol(`pltsym') ///
clpat(tight_dot tight_dot tight_dot tight_dot ) ///
ytitle("Observed-Predicted") ///
subtitle("Note: positive deviations show underpredictions.", ///
pos(11) size(small)) ///
ylabel(-.10(.05).10, grid gmax gmin) ///
xlabel(0(1)`maxcount') ///
note(`note') ///
ysize(3.5) xsize(4.5) ///
legend(order(1 "`pltleg1'" 2 "`pltleg2'" ///
3 "`pltleg3'" 4 "`pltleg4'")) ///
)
* export graph
if "`graphexport'" != "" {
qui graph export `graphexport'
}
}
// COMPARE FIT STATISTICS
if "`nofit'"=="" {
local dash ""
if "`nodash'"=="" {
local dash ///
"-------------------------------------------------------------------------"
}
local c1 = 16
local c2 = 32
local c3 = 48
local c4 = 56
local c5 = 62
di _n in g "Tests and Fit Statistics"
di
tempname aicd // 0.8.0
* base PRM
if "`prm'"!="" {
local mdl1 "`set'PRM"
local mdl1type "PRM"
di in y "`mdl1'" ///
_col(`c1') in g "BIC=" in y `f103' bic`mdl1' ///
_col(`c2') in g "AIC=" in y `f103' aic`mdl1' ///
_col(`c3') in g "Prefer" ///
_col(`c4') in g "Over" ///
_col(`c5') in g "Evidence"
* prm vs nbreg
if "`nbreg'"!="" {
local mdl2 "`set'NBRM"
local mdl2type "NBRM"
global bic1 = bic`mdl1'
global bic2 = bic`mdl2'
_count_bic_dif
local pref = $bicpref
local nopref = $bicnopref
di "`dash'"
* bic
di in y " " in g " vs " in y "`mdl2'" ///
_col(`c1') in g "BIC=" in y `f103' bic`mdl2' ///
_col(`c2') in g "dif=" in y `f103' $bicdif ///
_col(`c3') in y "`mdl`pref'type'" ///
_col(`c4') in y "`mdl`nopref'type'" ///
_col(`c5') in y "$bicsup"
* aic
scalar `aicd' = aic`mdl1' - aic`mdl2'
local aicfav "`mdl2type'"
local aicnofav "`mdl1type'"
if aic`mdl1' < aic`mdl2' {
local aicfav "`mdl1type'"
local aicnofav "`mdl2type'"
}
di _col(`c1') in g "AIC=" in y `f103' aic`mdl2' ///
_col(`c2') in g "dif=" in y `f103' `aicd' ///
_col(`c3') in y "`aicfav'" ///
_col(`c4') in y "`aicnofav'"
* lr test
local lrfav "`mdl1type'"
local lrnofav "`mdl2type'"
if lrnb_prmp < .05 {
local lrfav "`mdl2type'"
local lrnofav "`mdl1type'"
}
di _col(`c1') in g "LRX2=" in y `f93' lrnb_prm ///
_col(`c2') in g "prob=" in y `f93' lrnb_prmp in g ///
_col(`c3') in y "`lrfav'" ///
_col(`c4') in y "`lrnofav'" ///
_col(`c5') in y "p=" `f93l' lrnb_prmp
} // no nbreg vs prm
* prm vs zip
if "`zip'"!="" {
local mdl2 "`set'ZIP"
local mdl2type "ZIP"
* bic
global bic1 = bic`mdl1'
global bic2 = bic`mdl2'
_count_bic_dif
local pref = $bicpref
local nopref = $bicnopref
di in g "`dash'"
di in y " " in g " vs " in y "`mdl2'" ///
_col(`c1') in g "BIC=" in y `f103' bic`mdl2' ///
_col(`c2') in g "dif=" in y `f103' $bicdif ///
_col(`c3') in y "`mdl`pref'type'" ///
_col(`c4') in y "`mdl`nopref'type'" ///
_col(`c5') in y "$bicsup"
* aic
tempname aicd // 0.8.0
scalar `aicd' = aic`mdl1' - aic`mdl2'
local aicfav "`mdl2type'"
local aicnofav "`mdl1type'"
if aic`mdl1' < aic`mdl2' {
local aicfav "`mdl1type'"
local aicnofav "`mdl2type'"
}
di _col(`c1') in g "AIC=" in y `f103' aic`mdl2' ///
_col(`c2') in g "dif=" in y `f103' `aicd' ///
_col(`c3') in y "`aicfav'" ///
_col(`c4') in y "`aicnofav'"
* vuong test
local vufav "`mdl1type'"
local vunofav "`mdl2type'"
if vu`mdl2'>0 {
local vufav "`mdl2type'"
local vunofav "`mdl1type'"
}
di _col(`c1') in g "Vuong=" in y `f83' vu`mdl2' ///
_col(`c2') in g "prob=" in y `f93' vu`mdl2'p in g ///
_col(`c3') in y "`vufav'" ///
_col(`c4') in y "`vunofav'" ///
_col(`c5') in y "p=" `f93l' vu`mdl2'p
} // no zip vs prm
* prm vs zinb
if "`zinb'"!="" {
local mdl2 "`set'ZINB"
local mdl2type "ZINB"
* bic
global bic1 = bic`mdl1'
global bic2 = bic`mdl2'
_count_bic_dif
local pref = $bicpref
local nopref = $bicnopref
di in g "`dash'"
di in y " " in g " vs " in y "`mdl2'" ///
_col(`c1') in g "BIC=" in y `f103' bic`mdl2' ///
_col(`c2') in g "dif=" in y `f103' $bicdif ///
_col(`c3') in y "`mdl`pref'type'" ///
_col(`c4') in y "`mdl`nopref'type'" ///
_col(`c5') in y "$bicsup"
* aic
scalar `aicd' = aic`mdl1' - aic`mdl2'
local aicfav "`mdl2type'"
local aicnofav "`mdl1type'"
if aic`mdl1' < aic`mdl2' {
local aicfav "`mdl1type'"
local aicnofav "`mdl2type'"
}
di _col(`c1') in g "AIC=" in y `f103' aic`mdl2' ///
_col(`c2') in g "dif=" in y `f103' `aicd' ///
_col(`c3') in y "`aicfav'" ///
_col(`c4') in y "`aicnofav'"
} // no zinb vs prm
} // if no prm
* base nbreg
if "`nbreg'"!="" {
local mdl1 "`set'NBRM"
local mdl1type "NBRM"
di in g "`dash'"
di in y "`mdl1'" ///
_col(`c1') in g "BIC=" in y `f103' bic`mdl1' ///
_col(`c2') in g "AIC=" in y `f103' aic`mdl1' ///
_col(`c3') in g "Prefer" ///
_col(`c4') in g "Over" ///
_col(`c5') in g "Evidence"
* nbreg vs zip
if "`zip'"!="" {
local mdl2 "`set'ZIP"
local mdl2type "ZIP"
* bic
global bic1 = bic`mdl1'
global bic2 = bic`mdl2'
_count_bic_dif
local pref = $bicpref
local nopref = $bicnopref
di in g "`dash'"
di in y " " in g " vs " in y "`mdl2'" ///
_col(`c1') in g "BIC=" in y `f103' bic`mdl2' ///
_col(`c2') in g "dif=" in y `f103' $bicdif ///
_col(`c3') in y "`mdl`pref'type'" ///
_col(`c4') in y "`mdl`nopref'type'" ///
_col(`c5') in y "$bicsup"
* aic
scalar `aicd' = aic`mdl1' - aic`mdl2'
local aicfav "`mdl2type'"
local aicnofav "`mdl1type'"
if aic`mdl1' < aic`mdl2' {
local aicfav "`mdl1type'"
local aicnofav "`mdl2type'"
}
di _col(`c1') in g "AIC=" in y `f103' aic`mdl2' ///
_col(`c2') in g "dif=" in y `f103' `aicd' ///
_col(`c3') in y "`aicfav'" ///
_col(`c4') in y "`aicnofav'"
} // no zip vs nbreg
* nbreg vs zinb
if "`zinb'"!="" {
local mdl2 "`set'ZINB"
local mdl2type "ZINB"
* bic
global bic1 = bic`mdl1'
global bic2 = bic`mdl2'
_count_bic_dif
local pref = $bicpref
local nopref = $bicnopref
di in g "`dash'"
di in y " " in g " vs " in y "`mdl2'" ///
_col(`c1') in g "BIC=" in y `f103' bic`mdl2' ///
_col(`c2') in g "dif=" in y `f103' $bicdif ///
_col(`c3') in y "`mdl`pref'type'" ///
_col(`c4') in y "`mdl`nopref'type'" ///
_col(`c5') in y "$bicsup"
* aic
scalar `aicd' = aic`mdl1' - aic`mdl2'
local aicfav "`mdl2type'"
local aicnofav "`mdl1type'"
if aic`mdl1' < aic`mdl2' {
local aicfav "`mdl1type'"
local aicnofav "`mdl2type'"
}
di _col(`c1') in g "AIC=" in y `f103' aic`mdl2' ///
_col(`c2') in g "dif=" in y `f103' `aicd' ///
_col(`c3') in y "`aicfav'" ///
_col(`c4') in y "`aicnofav'"
* vuong test
local vufav "`mdl1type'"
local vunofav "`mdl2type'"
if vu`mdl2'>0 {
local vufav "`mdl2type'"
local vunofav "`mdl1type'"
}
di _col(`c1') in g "Vuong=" in y `f83' vu`mdl2' ///
_col(`c2') in g "prob=" in y `f93' vu`mdl2'p in g ///
_col(`c3') in y "`vufav'" ///
_col(`c4') in y "`vunofav'" ///
_col(`c5') in y "p=" `f93l' vu`mdl2'p
} // no zinb vs nbreg
} // if no nbreg
if "`zip'"!="" {
local mdl1 "`set'ZIP"
local mdl1type "ZIP"
di in g "`dash'"
di in y "`mdl1'" ///
_col(`c1') in g "BIC=" in y `f103' bic`mdl1' ///
_col(`c2') in g "AIC=" in y `f103' aic`mdl1' ///
_col(`c3') in g "Prefer" ///
_col(`c4') in g "Over" ///
_col(`c5') in g "Evidence"
* zip vs zinb
if "`zinb'"!="" {
local mdl2 "`set'ZINB"
local mdl2type "ZINB"
* bic
global bic1 = bic`mdl1'
global bic2 = bic`mdl2'
_count_bic_dif
local pref = $bicpref
local nopref = $bicnopref
di in g "`dash'"
di in y " " in g " vs " in y "`mdl2'" ///
_col(`c1') in g "BIC=" in y `f103' bic`mdl2' ///
_col(`c2') in g "dif=" in y `f103' $bicdif ///
_col(`c3') in y "`mdl`pref'type'" ///
_col(`c4') in y "`mdl`nopref'type'" ///
_col(`c5') in y "$bicsup"
* aic
scalar `aicd' = aic`mdl1' - aic`mdl2'
local aicfav "`mdl2type'"
local aicnofav "`mdl1type'"
if aic`mdl1' < aic`mdl2' {
local aicfav "`mdl1type'"
local aicnofav "`mdl2type'"
}
di _col(`c1') in g "AIC=" in y `f103' aic`mdl2' ///
_col(`c2') in g "dif=" in y `f103' `aicd' ///
_col(`c3') in y "`aicfav'" ///
_col(`c4') in y "`aicnofav'"
* lr test
local lrfav "`mdl1type'"
local lrnofav "`mdl2type'"
if lrnb_prmp < .05 {
local lrfav "`mdl2type'"
local lrnofav "`mdl1type'"
}
di _col(`c1') in g "LRX2=" in y `f93' lrzip_zinb ///
_col(`c2') in g "prob=" in y `f93' lrzip_zinbp in g ///
_col(`c3') in y "`lrfav'" ///
_col(`c4') in y "`lrnofav'" ///
_col(`c5') in y "p=" `f93l' lrnb_prmp
di in g "`dash'"
} // no zinb vs zip
} // no zip
} // no fit
end
// compute strength of bic difference?
capture program drop _count_bic_dif
program define _count_bic_dif
* compute bin diff from model 1 and model 2
global bicdif = $bic1 - $bic2
tempname bicdabs // 0.8.0
scalar `bicdabs' = abs($bicdif)
* evaluate support based on bic difference
if `bicdabs'~=. {
global bicsup "Very strong"
if `bicdabs'<= .0000000001 {
global bicsup "no"
}
else if `bicdabs' <=2 {
global bicsup "Weak"
}
else if `bicdabs' <=6 {
global bicsup "Positive"
}
else if `bicdabs' <=10 {
global bicsup "Strong"
}
global bicpref = 2
global bicnopref = 1
if $bicdif < 0 {
global bicpref = 1
global bicnopref = 2
}
if `bicdabs'< .0000000001 & `bicdabs'>-.0000000001 {
global bicpref = 0
}
}
end
// ESTIMATE MODELS AND STORE RESULTS
capture program drop _count_estimate
program define _count_estimate, rclass
syntax varlist [if] [in] [, ///
inflate(varlist) ///
MAXcount(integer 9) Generate(string) replace ///
Prm Nbreg ZIP ZINb NOConstant ///
NOIsily ]
// estimate models & create globals with stats
local set "`generate'"
tempname n
local noise ""
if "`noisily'"=="noisily" {
local noise "noisily"
}
// prm
if "`prm'"!="" {
qui {
local modelnm "PRM"
local fullnm "`set'`modelnm'"
`noise' poisson `varlist' `if' `in', `noconstant'
estimates store `fullnm'
scalar ll`fullnm' = e(ll) // log lik
fitstat, bic
scalar bicp`fullnm' = r(bic_p) // bic'
return scalar bicp`fullnm' = r(bic_p) // 0.8.0
scalar aic`fullnm' = r(aic) // aic
scalar x2`fullnm' = r(lrx2) // lrx2 all b=0
scalar x2p`fullnm' = r(lrx2_p)
scalar bic`fullnm' = r(bic) // bic
if "`replace'"=="replace" {
capture drop `fullnm'rate
capture drop `fullnm'prgt
capture drop `fullnm'val
capture drop `fullnm'obeq
capture drop `fullnm'preq
capture drop `fullnm'prle
capture drop `fullnm'oble
capture drop `fullnm'absdif
capture drop `fullnm'dif
capture drop `fullnm'pearson
local i = 0
while `i'<=`maxcount' {
capture drop `fullnm'pr`i'
capture drop `fullnm'cu`i'
local i = `i' + 1
}
}
prcounts `fullnm', plot max(`maxcount') // predicted counts
label var `fullnm'preq "`modelnm' predicted" // predicted Pr(y)
gen `fullnm'dif = `fullnm'obeq - `fullnm'preq // obs - predicted
label var `fullnm'dif "`modelnm' obs - pred"
* 2004-10-29 add CT 5.34
scalar `n' = e(N) // sample size
gen `fullnm'pearson = ///
(`n' * `fullnm'dif * `fullnm'dif) / `fullnm'preq
label var `fullnm'pearson "`modelnm' contribution to Pearson X2"
sum `fullnm'pearson
} // qui
}
// nbreg
if "`nbreg'"!="" {
qui {
local modelnm "NBRM"
local fullnm "`set'`modelnm'"
`noise' nbreg `varlist' `if' `in', `noconstant'
estimates store `fullnm'
scalar ll`fullnm' = e(ll) // log lik
scalar lrnb_prm = e(chi2_c) // lrx2 of nb vs prm
scalar lrnb_prmp = chiprob(1, e(chi2_c))*0.5
fitstat, bic
scalar bicp`fullnm' = r(bic_p) // bic'
scalar aic`fullnm' = r(aic) // aic
scalar x2`fullnm' = r(lrx2) // lrx2 all b=0
scalar x2p`fullnm' = r(lrx2_p)
scalar bic`fullnm' = r(bic) // bic
if "`replace'"=="replace" {
capture drop `fullnm'rate
capture drop `fullnm'prgt
capture drop `fullnm'val
capture drop `fullnm'obeq
capture drop `fullnm'preq
capture drop `fullnm'prle
capture drop `fullnm'oble
capture drop `fullnm'dif
capture drop `fullnm'absdif
capture drop `fullnm'pearson
local i = 0
while `i'<=`maxcount' {
capture drop `fullnm'pr`i'
capture drop `fullnm'cu`i'
local i = `i' + 1
}
}
prcounts `fullnm', plot max(`maxcount') // predicted counts
label var `fullnm'preq "`modelnm' predicted"
gen `fullnm'dif = `fullnm'obeq - `fullnm'preq
label var `fullnm'dif "`modelnm' obs - pred"
* 2004-10-29 add CT 5.34
scalar `n' = e(N) // sample size
gen `fullnm'pearson = ///
(`n' * `fullnm'dif * `fullnm'dif) / `fullnm'preq
label var `fullnm'pearson "`modelnm' contribution to Pearson X2"
sum `fullnm'pearson
} // qui
}
// zip
if "`zip'"!="" {
qui {
local modelnm "ZIP"
local fullnm "`set'`modelnm'"
`noise' zip `varlist' `if' `in', ///
inf(`inflate') vuong `noconstant'
estimates store `fullnm'
scalar vu`fullnm' = e(vuong) // vuong vs prm
scalar vu`fullnm'p = 1-norm(abs(e(vuong)))
scalar ll`fullnm' = e(ll) // log lik
fitstat, bic
scalar bicp`fullnm' = r(bic_p) // bic'
scalar aic`fullnm' = r(aic) // aic
scalar x2`fullnm' = r(lrx2) // lrx2 all b=0
scalar x2p`fullnm' = r(lrx2_p)
scalar bic`fullnm' = r(bic) // bic
if "`replace'"=="replace" {
capture drop `fullnm'rate
capture drop `fullnm'prgt
capture drop `fullnm'val
capture drop `fullnm'obeq
capture drop `fullnm'preq
capture drop `fullnm'prle
capture drop `fullnm'oble
capture drop `fullnm'all0
capture drop `fullnm'dif
capture drop `fullnm'absdif
capture drop `fullnm'pearson
local i = 0
while `i'<=`maxcount' {
capture drop `fullnm'pr`i'
capture drop `fullnm'cu`i'
local i = `i' + 1
}
}
prcounts `fullnm', plot max(`maxcount') // predicted counts
label var `fullnm'preq "`modelnm' predicted"
gen `fullnm'dif = `fullnm'obeq - `fullnm'preq
label var `fullnm'dif "`modelnm' obs - pred"
* 2004-10-29 add CT 5.34
scalar `n' = e(N) // sample size
gen `fullnm'pearson = ///
(`n' * `fullnm'dif * `fullnm'dif) / `fullnm'preq
label var `fullnm'pearson "`modelnm' contribution to Pearson X2"
qui sum `fullnm'pearson
} // qui
}
// zinb
if "`zinb'"!="" {
qui {
local modelnm "ZINB"
local fullnm "`set'`modelnm'"
`noise' zinb `varlist' `if' `in', ///
inf(`inflate') vuong zip `noconstant'
estimates store `fullnm'
scalar vu`fullnm' = e(vuong) // vuong vs nbreg
scalar vu`fullnm'p = 1-norm(abs(e(vuong)))
scalar ll`fullnm' = e(ll) // loglik
scalar lrzip_zinb = e(chi2_cp) // lrx2 zinb vs zip
scalar lrzip_zinbp = chiprob(1, e(chi2_cp))*0.5
fitstat, bic
scalar bicp`fullnm' = r(bic_p) // bic'
scalar aic`fullnm' = r(aic) // aic
scalar x2`fullnm' = r(lrx2) // lrx2 all b=0
scalar x2p`fullnm' = r(lrx2_p)
scalar bic`fullnm' = r(bic) // bic
if "`replace'"=="replace" {
capture drop `fullnm'rate
capture drop `fullnm'prgt
capture drop `fullnm'val
capture drop `fullnm'obeq
capture drop `fullnm'preq
capture drop `fullnm'prle
capture drop `fullnm'oble
capture drop `fullnm'absdif
capture drop `fullnm'dif
capture drop `fullnm'all0
capture drop `fullnm'pearson
local i = 0
while `i'<=`maxcount' {
capture drop `fullnm'pr`i'
capture drop `fullnm'cu`i'
local i = `i' + 1
}
}
prcounts `fullnm', plot max(`maxcount') // predicted counts
label var `fullnm'preq "`modelnm' predicted"
gen `fullnm'dif = `fullnm'obeq - `fullnm'preq
label var `fullnm'dif "`modelnm' obs - pred"
* 2004-10-29 add CT 5.34
scalar `n' = e(N) // sample size
gen `fullnm'pearson = ///
(`n' * `fullnm'dif * `fullnm'dif) / `fullnm'preq
label var `fullnm'pearson "`modelnm' contribution to Pearson X2"
qui sum `fullnm'pearson
} // qui
}
end // _count_estimate
exit
* version 0.8.0 fix pr bug
* version 0.2.1 13Apr2005 add replace; trap weights
* version 0.2.0 27Feb2005 first documented version

View File

@ -0,0 +1,86 @@
.-
help for ^countfit^ - 03Nov2005
.-
Compare fit of alternive count models
-------------------------------------
^countfit^ varlist [if] [in]^,^ [^inf^late^(^varlist2^)^ ^noc^onstant ^p^rm ^n^breg
^zip^ ^zin^b ^g^enerate^(^prefix^)^ ^replace^ ^note(^string^)^
^graph^export^(^filename[^, replace^]^)^ ^nog^raph ^nod^ifferences ^noprt^able
^noe^stimates ^nof^it ^nodash^ ^max^count(#) ^noi^sily]
Description
-----------
^countfit^ compares the fit of the Poissin, negative binomial, zero-inflated
Poisson and zero-inflated negative binomial models, generateing a table of
estimates, a table of differences between observed and average estimated
probabilities for each count, a graph of these differences, and various tests
and measures used to compare the fit of count models.
Specifying the model
--------------------
Immediately afer the command name ^countfit^ you specify the dependent and
independent variables as you would with ^poisson^ or the other models. For
zero-inflated models, the ^inflate^ option is used in the same was as in the
^zip^ and ^zinb^ commands. ^noconstant^ can be used to exclude the constant
term.
Options to select the models to fit
-----------------------------------
By default, ^poisson^, ^nbreg^, ^zip^ and ^zinb^ are estimated. If you
only want some of these models, specify the modesl you want with:
^prm^ - include ^poisson^
^nbreg^ - include ^nbreg^
^zip^ - include ^poisson^
^zinb^ - include ^poisson^
Options to label results
------------------------
^generate()^ is up to five letters to name the variables that are created
and to label the models in the output. This name is placed in front
of the type of model (e.g., namePRM). This is optional but will help
keep track of results from multiple specifications of models.
^replace^ will replace variables created with the ^generate^ option if they
already exist.
^note()^ is a label added to the graph that is saved.
^graphexport()^ contains options to be sent to the ^graph export^ command to
export the graph that is create. For example, ^graph(mdl.emf, replace)^
would save the file mdl.emf and replace it if it exists. If this option
is not included, the graph is not saved.
Options controlling what is printed
-----------------------------------
^noisily^ shows the output from the estimation commands called by ^countfit^.
^nograph^ suppress graph of differences from observed counts.
^nodifferences^ suppress table of differences from observed counts.
^noprtable^ suppresses table of predictions for each model.
^noestimates^ suppress table of estimated coefficients.
^nofit^ suppress table of fit statistics and test of fit
^nodash^ suppress dashed lines between measures of fit
^maxcount()^ number of counts to evaluate.
^noisily^ includes output from Stata estimation commands; without this option
the results are only shown in the ^estimates table^ output.
Notes
-----
^countfit^ is based on the results from the Stata models described above,
the predictions computed by ^prcounts^, and the fit measures computed by
^fitstat^.
Examples
--------
^. use couart2^
^. countfit art fem mar kid5 phd ment, inf(ment fem) nbreg zinb nograph^
.-
Author: J. Scott Long and Jeremy Freese
www.indiana.edu/~jslsoc/spost.htm
spostsup@@indiana.edu

View File

@ -0,0 +1,29 @@
program def cseplist, rclass
*! NJC 1.0.0 31 August 2000
version 6.0
gettoken list 0 : 0, parse(",")
if "`list'" == "" | "`list'" == "," {
di in r "nothing in list"
exit 198
}
syntax , [ Global(str) Noisily ]
if length("`global'") > 8 {
di in r "global name must be <=8 characters"
exit 198
}
tokenize `list'
local n : word count `list'
local i = 1
while `i' < `n' {
local newlist "`newlist'``i'',"
local i = `i' + 1
}
local newlist "`newlist'``n''"
if "`noisily'" != "" { di "`newlist'" }
if "`global'" != "" { global `global' "`newlist'" }
return local list `newlist'
end

View File

@ -0,0 +1,2 @@
.h listutil

View File

@ -0,0 +1,54 @@
program def cvarlist, rclass
*! NJC 1.0.0 18 August 2000
version 6.0
gettoken list 0 : 0, parse(",")
if "`list'" == "" | "`list'" == "," {
di in r "nothing in list"
exit 198
}
syntax , [ new NUmeric String Noisily Global(str)]
local nopts : word count `new' `numeric' `string'
if `nopts' > 1 {
di in r "use just one of options new, numeric, string"
exit 198
}
if "`global'" != "" {
tokenize `global'
args global1 global2 global3
if "`global3'" != "" {
di in r "global( ) must contain at most 2 names"
exit 198
}
if (length("`global1'") > 8) | (length("`global2'") > 8) {
di in r "global name must be <=8 characters"
exit 198
}
}
tokenize `list'
local nwords : word count `list'
local i = 1
while `i' <= `nwords' {
capture confirm `new' `numeric' `string' variable ``i''
if _rc == 0 { local newlist "`newlist'``i'' " }
else local badlist "`badlist'``i'' "
local i = `i' + 1
}
if "`noisily'" != "" {
if "`newlist'" != "" {
di in g "list 1: " in y "`newlist'"
}
if "`badlist'" != "" {
di in g "list 2: " in y "`badlist'"
}
}
if "`global1'" != "" { global `global1' "`newlist'" }
if "`global2'" != "" { global `global2' "`badlist'" }
return local list1 `newlist'
return local list2 `badlist'
end

View File

@ -0,0 +1,2 @@
.h listutil

View File

@ -0,0 +1,119 @@
program def dellist, rclass
*! NJC 1.5.0 14 December 2000
* NJC 1.4.0 6 Sept 2000
* NJC 1.3.0 6 June 2000
* NJC 1.2.0 31 Jan 2000
* NJC 1.1.0 22 Dec 1999
* NJC 1.0.0 22 Sept 1999
version 6.0
gettoken list 0 : 0, parse(",")
if "`list'" == "" | "`list'" == "," {
di in r "nothing in list"
exit 198
}
local nlist : word count `list'
syntax , [ Delete(str) Pos(numlist sort int >=-`nlist' <=`nlist') /*
*/ Exact All Global(str) Noisily ]
local nopts = ("`delete'" != "") + ("`pos'" != "")
if `nopts' != 1 {
di in r "must specify one of delete( ) and pos( )"
exit 198
}
if length("`global'") > 8 {
di in r "global name must be <=8 characters"
exit 198
}
if "`exact'" != "" & "`all'" != "" {
di in r "all option not allowed with exact option"
exit 198
}
tokenize `list'
if "`delete'" != "" {
local i = 1
while `i' <= `nlist' {
local len = length("``i''")
if `len' > 80 {
di in r "cannot handle word length > 80"
exit 498
}
local i = `i' + 1
}
tknz `delete', s(d)
local nd : word count `delete'
if "`exact'" != "" {
while "`1'" != "" {
local i = 1
local OK = 0
while `i' <= `nd' & !`OK' {
local OK = "`1'" == "`d`i''"
local i = `i' + 1
}
if !`OK' { local newlist "`newlist'`1' " }
mac shift
}
}
else {
while "`1'" != "" {
local i = 1
local OK = 0
while `i' <= `nd' {
local thisOK = /*
*/ index("`1'", "`d`i''") > 0
local OK = `OK' + `thisOK'
local i = `i' + 1
}
if "`all'" != "" { local OK = `OK' == `nd' }
if !`OK' { local newlist "`newlist'`1' " }
mac shift
}
}
}
else {
local np1 = `nlist' + 1
tknz `pos' `np1', s(p)
local np : word count `pos'
* negative indexes to positive
local i = 1
while `p`i'' < 0 {
local p`i' = `nlist' + 1 + `p`i''
local i = `i' + 1
}
local i = 1
local j = 1
while `i' <= `nlist' {
if `i' == `p`j'' { local j = `j' + 1 }
else local newlist "`newlist'``i'' "
local i = `i' + 1
}
}
if "`noisily'" != "" { di "`newlist'" }
if "`global'" != "" { global `global' "`newlist'" }
return local list `newlist'
end
program def tknz, rclass
* NJC 1.1.0 2 June 2000
version 6.0
gettoken list 0 : 0, parse(",")
syntax , Stub(str) [ * ]
tokenize `"`list'"' , `options'
local i = 1
while "``i''" != "" {
c_local `stub'`i' `"``i''"'
local i = `i' + 1
}
end

View File

@ -0,0 +1,2 @@
.h listutil

View File

@ -0,0 +1,108 @@
*! Delta version 1.5 - 5 March 2008
*! Jean-Benoit Hardouin
************************************************************************************************************
* DELTA: delta coefficient
* Version 1.5: March 5, 2008
*
* Historic
* Version 1 (2007-05-21): Jean-Benoit Hardouin
* Version 1.1 (2007-05-22): Jean-Benoit Hardouin /* if in and possibility to use the score*/
* Version 1.2 (2007-05-22): Jean-Benoit Hardouin /*bug when a score is missing*/
* Version 1.3 (2007-06-16): Jean-Benoit Hardouin /*change in the options*/
* Version 1.4 (2007-07-03): Jean-Benoit Hardouin /*correct a bug in the options*/
* Version 1.5 (2008-03-05): Jean-Benoit Hardouin /*correct a bug in the ci option*/
*
* Jean-benoit Hardouin, Faculty of Pharmaceutical Sciences - University of Nantes - France
* jean-benoit.hardouin@univ-nantes.fr
*
* News about this program : http://www.anaqol.org
* FreeIRT Project : http://www.freeirt.org
*
* Copyright 2007-2008 Jean-Benoit Hardouin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
************************************************************************************************************
program define delta , rclass
version 7.0
syntax varlist(min=1 numeric) [if] [in] [,ci(integer 0) noDots MINscore(int 0) MAXscore(int 0)]
preserve
tempfile deltafile
qui save `deltafile'
if "`if'"!=""|"`in'"!="" {
qui keep `if' `in'
}
local nbitems:word count `varlist'
tokenize `varlist'
local scoremin=`minscore'
local scoremax=`maxscore'
tempvar score
if `nbitems'==1&`scoremax'==0 {
di in red "If you indicate only the score variable, you must define the {cmd:scoremax} option"
error 198
}
else if `nbitems'==1&`scoremax'!=0 {
qui gen `score'=`varlist'
}
else {
qui genscore `varlist',score(`score')
}
qui drop if `score'==.
qui count
local nbind=r(N)
if `scoremax'==0 {
qui su `score'
local scoremax=r(max)
}
tempname error
gen `error'=`score'<`scoremin'|`score'>`scoremax'
qui count if `error'==1
local err=r(N)
if `err'!=0 {
di in red "`err' individuals has(have) a score inferior to `scoremin' or superior to `scoremax'"
error 198
}
local sumsqscore=0
forvalues i=`scoremin'/`scoremax' {
qui count if `score'==`i'
local score`i'=r(N)
local sumsqscore=`sumsqscore'+`score`i''^2
}
local delta=(1+`scoremax')*(`nbind'^2-`sumsqscore')/(`nbind'^2*`scoremax')
di in green "Range of the scores : " in ye `scoremin' in gr "/" in ye `scoremax'
di in green "Number of used individuals : " in ye `nbind'
if `ci'!=0 {
bootstrap delta=r(delta), reps(`ci') nowarn noheader nolegend `dots': delta `varlist' ,minscore(`scoremin') maxscore(`scoremax')
}
else {
display in green "Delta= " in yellow %8.6f `delta'
}
return scalar delta=`delta'
qui use `deltafile',clear
restore,not
end

View File

@ -0,0 +1,61 @@
{smcl}
{* 16may2007}{...}
{hline}
help for {hi:delta}{right:Jean-Benoit Hardouin}
{hline}
{title:Syntax}
{p 8 14 2}{cmd:delta} {it:varlist} [{cmd:if} {it:exp}] [{cmd:in} {it:range}] [, {cmdab:ci}({it:#}) {cmdab:nod:ots} {cmdab:min:score}({it:#}) {cmdab:max:score}({it:#})]
{title:Description}
{p 4 8 2}{cmd:delta} computes the generalized delta index of scale discrimination developed by Hankins (2007) based on the original work of Ferguson (1949). This index measures the scale's ability to distinguish between individuals.
A value of 1 indicates that the test has maximal discrimination (all possible scores occur with the same frequency) and a value of 0 means that the test has minimal discrimination (all the respondents have the same score).
A value of 0.9 results from a set of scores that is normally distributed. A value of 1 is observed if the scores follow a uniform distribution. Individuals with a missing score are omitted.
{title:Options}
{p 4 8 2}If {it:varlist} is composed of only one variable, the {cmd:delta} module considers that this variable is the score of the individuals.
{p 4 8 2}{cmd:ci}({it:#}) estimates the confidence interval by boostrap. {it:#} is the number of replications to be performed. By default, no confidence interval is calculated.
{p 4 8 2}{cmd:nodots} avoids displaying a dot for each replication (only with {cmd:ci}).
{p 4 8 2}{cmd:minscore}({it:#}) defines the minimal value of the score. By default, this value is fixed to 0.
{p 4 8 2}{cmd:maxscore}({it:#}) defines the maximal value of the score. By default, the maximal observed score is used.
{title:Output}
{p 4 8 2}{cmd:r(delta)}: Observed value of the delta index.
{title:Examples}
{p 4 8 2}{cmd: . delta itemA*}
{p 4 8 2}{cmd: . delta itemA*, ci(500) dots}
{p 4 8 2}{cmd: . delta score, scoremax(8)}
{title: References}
{p 4 8 2}Ferguson G. A. (1949) On the theory of test discrimination. {it:Psychometrika}, 14: 61-68.
{p 4 8 2}Hankins M. (2007) Questionnaire discrimination: (re)- introducting coefficient delta. {it:BMC Medical Research Methodology}, 7: 19.
{title:Author}
{p 4 8 2}Jean-Benoit Hardouin, PhD, assistant professor{p_end}
{p 4 8 2}Team of Biostatistics, Clinical Research and Subjective Measures in Health Sciences{p_end}
{p 4 8 2}University of Nantes - Faculty of Pharmaceutical Sciences{p_end}
{p 4 8 2}1, rue Gaston Veil - BP 53508{p_end}
{p 4 8 2}44035 Nantes Cedex 1 - FRANCE{p_end}
{p 4 8 2}Email:
{browse "mailto:jean-benoit.hardouin@univ-nantes.fr":jean-benoit.hardouin@univ-nantes.fr}{p_end}
{p 4 8 2}Websites {browse "http://www.anaqol.org":AnaQol}
and {browse "http://www.freeirt.org":FreeIRT}
{title:Also see}
{p 4 13 2}Online: help for {help alpha} and {help loevH} if installed.{p_end}

Some files were not shown because too many files have changed in this diff Show More