04.06.2013 Views

Introduction to Stata 8 - (GRIPS

Introduction to Stata 8 - (GRIPS

Introduction to Stata 8 - (GRIPS

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

10.7. Reshaping data<br />

collapse [R] collapse<br />

You want <strong>to</strong> create an aggregated data set, not with the characteristics of each individual, but<br />

of groups of individuals. One situation might be <strong>to</strong> characterize physicians by number of<br />

patient contacts, another <strong>to</strong> make a reduced data set for Poisson regression (see section 13):<br />

. // gen.stcollaps.cancer2.do<br />

. use c:\dokumenter\proj1\stsplit.cancer2.dta , clear<br />

. collapse (sum) risktime died , by(agegr drug)<br />

. save c:\dokumenter\proj1\stcollaps.cancer2.dta<br />

. summarize<br />

Variable | Obs Mean Std. Dev. Min Max<br />

-------------+-----------------------------------------------------<br />

drug | 15 2 .8451543 1 3<br />

agegr | 15 55 7.319251 45 65<br />

risktime | 15 4.133334 3.01067 .3581161 10.88906<br />

died | 15 2.066667 2.374467 0 8<br />

reshape [R] reshape<br />

E.g. with repeated measurements some analyses require a 'wide', some a 'long' data structure:<br />

Long structure Wide structure<br />

id time sbp<br />

1 1 140<br />

1 2 120<br />

1 3 130<br />

1 4 135<br />

2 1 155<br />

etc.<br />

id sbp1 sbp2 sbp3 sbp4<br />

1 140 120 130 135<br />

2 155 etc.<br />

Switch between structures by:<br />

reshape wide sbp , i(id) j(time) // Restructures long <strong>to</strong> wide<br />

reshape long sbp , i(id) j(time) // Restructures wide <strong>to</strong> long<br />

xpose [R] xpose<br />

You may transpose observations and variables, i.e. let observations become variables and<br />

variables become observations. This may be useful e.g. for restructuring data for a graph:<br />

xpose , clear // clear is not optional<br />

expand [R] expand, contract<br />

You may duplicate observations according <strong>to</strong> a weighting variable. Look at the example in<br />

section 7 about weighting observations. You can obtain the same result by expanding the four<br />

observations <strong>to</strong> 130:<br />

expand pop<br />

tabulate expos case , chi<br />

contract does the opposite of expand.<br />

26

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!