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,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