Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: How to save cell values in the tabulate twoway output?


From   Maarten buis <[email protected]>
To   [email protected]
Subject   Re: st: How to save cell values in the tabulate twoway output?
Date   Thu, 17 Mar 2011 09:07:26 +0000 (GMT)

--- On Wed, 16/3/11, Bo Rud wrote:
> I have a problem with the handling of data resulting
> from a dichotomous test performed by 53 observers in 102
> patients. My data set consists of the 53 observers' 
> assessments as well as the true diagnosis in the
> coloumns (variables). The data set has 102 rows
> corresponding to the number of patients assessed by each
> of the observers.
> 
> patient  obs1  obs2 ... obs53  diagnose
> 1
> 2
> ..
> 102
> 
> I use the following commands to generate 2x2 tables with
> numbers of true-positive(tp), false-positive(fp),
> false-negative(fn) and
> true-negative(tn) test outcomes for each observer:
> 
> forvalues i = 1/53 {
> tab obs`i' diagnose
> }
> 
> I would like to save the cell values in the tab output for
> further analysis, but I can't find a way to do it.
> My ambition is to generate a new data set with the
> following structure:
> 
> Obs tp fp fn tn
> 1
> 2
> ..
> 53

Below is an example on how to do that. The trick is to add
4 new rows at the bottom, store the number of true positives
in the first extra row, the number of false positives in the
second extra row, etc. Than remove the rows with the original
data and the variables id and diagnose. Than flip the 
dataset.

*-------------------- begin example -------------------
// create data
drop _all
set obs 102
gen int id = _n
forvalues i = 1/53 {
	gen byte obs`i' = runiform() < .25
}
gen byte diagnose = runiform() < .25

// preparations
local N = _N        // store number of patients
set obs `= _N + 4'  // add 4 extra rows

forvalues i = 1/53 {
	//====== true positive
	// count number of true positives
	count if obs`i' == 1 & diagnose == 1 
	// store this count in first extra row
	replace obs`i' = r(N) in `= `N' + 1' 

	//====== false postive
	count if obs`i' == 1 & diagnose == 0
	replace obs`i' = r(N) in `= `N' + 2'

	//====== false negative
	count if obs`i' == 0 & diagnose == 1
	replace obs`i' = r(N) in `= `N' + 3'

	//====== true negative
	count if obs`i' == 0 & diagnose == 0
	replace obs`i' = r(N) in `= `N' + 4'
}

// last 4 observations contain the counts of interest
drop in f/`N'

// id and diagnose are no longer necesary
drop id diagnose 

// flip the data around
xpose, clear promote

// add the observer id
gen int obs_id = _n

// give meaningful variable names
rename v1 tp
rename v2 fp
rename v3 fn
rename v4 tn
*----------------------- end example --------------------
(For more on examples I sent to the Statalist see: 
http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany

http://www.maartenbuis.nl
--------------------------


      

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index