Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: sts graph legend, reshape and value labels


From   "Austin Nichols" <[email protected]>
To   [email protected]
Subject   Re: st: sts graph legend, reshape and value labels
Date   Thu, 31 May 2007 12:39:15 -0400

Emma--
This is a beautifully written email, and I am inspired to write a
general program and help file to do what you need the program to do,
but it's not clear to me that you need -reshape- at all--can't you
just

sysuse cancer,clear
stset studytime,fail(died)
label define drug 1 "useless" 2 "ok" 3 "good"
label values drug drug
sts graph,by(drug) name(sts,replace)
sts generate res=s,by(drug)
qui levelsof drug, local(d)
set obs `=_N+1'
foreach v of local d {
 g res`v'=res if drug==`v'
 la var res`v' `: label (drug) `v''
 replace res`v'=1 if mi(stu)
 }
replace stu=0 if mi(stu)
line res? stu,c(J...) sort name(an, replace)

to get your desired graph?  You could wrap this code in a program
called stsgr2 or somesuch, but it may be pedagogically useful not
to...

That said, I am frequently annoyed by the labeling (or destruction of
labels) done by -reshape- and -collapse-, so I think a program to
preserve variable and value labels is a good notion.  I would probably
use -label save- to write out value labels, though.

On 5/31/07, Emma Slaymaker <[email protected]> wrote:
Dear Statalist,
I am often mildly irritated with the legend on sts graph because legend
entries are automatically generated as varname=value label which
produces something unsiutable for presentation with long variable names
or value labels.   I figure the problem with sts graph stems from
reshape and the fact that you lose value labels when you reshape wide.

I'd like to know if anyone else finds this behaviour a nuisance.

Admittedly it's simple enough to override sts graph and specify my own
legend but I'm trying to write some extensible code that will be used
for teaching.  The number of categories in the by() variable will vary
and I don't want to waste time in the class writing legends.  For
example:

sysuse cancer
stset studytime,fail(died)
label define drug 1 "useless" 2 "ok" 3 "good"
label values drug drug
sts graph,by(drug)

I looked and couldn't find anything that does exactly what I want
(label variables with the old j var value labels when you reshape wide
and generate a value label from the old j var variables labels when you
reshape long) so I wrote something that solves the reshape problem but
that will be too complicated to explain to my workshop group.

Could reshape have this option added to it, and sts graph pass this
option onto reshape so that value labels are retained for the by()
variables?  If so, am I the only person who would like this?!  I'm not
teaching until September so a new option would save me having to explain
it at all!

Below is my solution, which does what I want in a clunky way.

Cheers,

Emma

sysuse cancer,clear
stset studytime,fail(died)
label define drug 1 "useless" 2 "ok" 3 "good"
label values drug drug
sts graph,by(drug) name(sts,replace)
sts generate res=s,by(drug)
label var res ""
gen id=_n
<prog omitted>
reshapelabelwide res,i( id) j( drug) keeplabel
line res* studytime,connect(J...) sort
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index