st: AW: Graph: Colouring table cells based on conditions or data distribution

classic Classic list List threaded Threaded
103 messages Options
1234 ... 6
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

st: AW: Graph: Colouring table cells based on conditions or data distribution

Stefan.Gawrich
Hi Statalisters,

many of you may know this feature from MS Excel (from Version 2007 on).
It's more or less a "choropleth map" for data tables (cell colour
depends on cell value relative to the table distribution or on
conditions).
(see:
http://msdn.microsoft.com/en-us/library/bb428945%28office.11%29.aspx)


In my work I often have to display both a table (to pick data from) and
a graph (for visual impression) to users and I'm constantly trying to
combine both. Colouring tables is one way to do this if rules/conditions
are user-defineable.

I thought about setting up a coloured twoway table in Stata but that
doesn't seem to be easy:

Stata's results window doesn't allow for fancy tricks so the table must
be in a graph.

Spmap by Maurizio Pisati has all colouring options I need and can be set
up with rectangular polygons and polygon centroids for labeling.
But it's not written for that purpose, so it's difficult to set up:
- a n*m polygon-coordinates dataset for each table must be created
- linkage of table cells to polygons
- handling of missing values and category "missing"
- use of value labels
- display of row and/or column totals (yes/no)
- ...

I already started to produce some coloured tables with spmap but these
are only first tests.
I'm curious if there is any other graph framework available in Stata
which also may be suitable for this purpose.

Best wishes
Stefan Gawrich






*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: st: AW: Graph: Colouring table cells based on conditions or data distribution

Maarten buis
--- On Fri, 16/10/09, [hidden email] wrote:
> In my work I often have to display both a table (to pick
> data from) and a graph (for visual impression) to users
> and I'm constantly trying to combine both. Colouring
> tables is one way to do this if rules/conditions are
> user-defineable.
>
> I thought about setting up a coloured twoway table in Stata
> but that doesn't seem to be easy

This talk by Nick Cox at the last German Stata Users' Meeting may be
what you are looking for.

http://ideas.repec.org/p/boc/dsug09/01.html

Hope this helps,
Maarten

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

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

Send instant messages to your online friends http://uk.messenger.yahoo.com 

*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: st: AW: Graph: Colouring table cells based on conditions or data distribution

Nick Cox
In essence, as Maarten's reference implies, it can be done. I don't have a precise recipe for your particular request, but the code in -corrtable- from SSC shows one strategy:

loop over cells in table {
        draw a graph for each cell with desired text and colour
}

combine the graphs

Another strategy would resemble ways in which people draw heat maps, or indeed maps, by a series of calls to -twoway area-. Sergiy Radyakin has done something similar to that, but I can't vouch for whether he has code to do what you want that is also publicly available.

Nick
[hidden email]

Maarten buis

This talk by Nick Cox at the last German Stata Users' Meeting may be
what you are looking for.

http://ideas.repec.org/p/boc/dsug09/01.html

--- On Fri, 16/10/09, [hidden email] wrote:
> In my work I often have to display both a table (to pick
> data from) and a graph (for visual impression) to users
> and I'm constantly trying to combine both. Colouring
> tables is one way to do this if rules/conditions are
> user-defineable.
>
> I thought about setting up a coloured twoway table in Stata
> but that doesn't seem to be easy


*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: st: AW: Graph: Colouring table cells based on conditions or data distribution

Nick Cox
Colour-coding cells, however, covers at least two kinds of idea:

1. Qualitative coding: Look! This cell contains a very high value, or a very low value, or a negative value, or whatever. Used sparingly, this can be useful and effective.

2. Quantitative coding: Look! In addition to showing you the values, I am also using a series of colour shades. (The example given on the Microsoft web page Stefan referred to shows a scale running from green through yellow to orange, which in my view just complicates the table!)

An alternative to quantitative coding is just to show e.g. bars and numbers, i.e. to hybridise tables and plots. For want of a standard name, I call these table plots. There are programs to do this on SSC, -tabplot- and -tableplot-, and examples in the talk Martin referred to. (The most recent version of this talk was given at the Washington, DC meeting a few months ago.)

Nick
[hidden email]

Nick Cox

In essence, as Maarten's reference implies, it can be done. I don't have a precise recipe for your particular request, but the code in -corrtable- from SSC shows one strategy:

loop over cells in table {
        draw a graph for each cell with desired text and colour
}

combine the graphs

Another strategy would resemble ways in which people draw heat maps, or indeed maps, by a series of calls to -twoway area-. Sergiy Radyakin has done something similar to that, but I can't vouch for whether he has code to do what you want that is also publicly available.

Maarten buis

This talk by Nick Cox at the last German Stata Users' Meeting may be
what you are looking for.

http://ideas.repec.org/p/boc/dsug09/01.html

--- On Fri, 16/10/09, [hidden email] wrote:
> In my work I often have to display both a table (to pick
> data from) and a graph (for visual impression) to users
> and I'm constantly trying to combine both. Colouring
> tables is one way to do this if rules/conditions are
> user-defineable.
>
> I thought about setting up a coloured twoway table in Stata
> but that doesn't seem to be easy

*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

AW: st: AW: Graph: Colouring table cells based on conditions or data distribution

Martin Weiss-5

<>


" in the talk Martin referred to."



I would love to take credit for the reference, but... (avid statalisters can complete this sentence themselves...)



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [hidden email] [mailto:[hidden email]] Im Auftrag von Nick Cox
Gesendet: Freitag, 16. Oktober 2009 11:59
An: [hidden email]
Betreff: RE: st: AW: Graph: Colouring table cells based on conditions or data distribution

Colour-coding cells, however, covers at least two kinds of idea:

1. Qualitative coding: Look! This cell contains a very high value, or a very low value, or a negative value, or whatever. Used sparingly, this can be useful and effective.

2. Quantitative coding: Look! In addition to showing you the values, I am also using a series of colour shades. (The example given on the Microsoft web page Stefan referred to shows a scale running from green through yellow to orange, which in my view just complicates the table!)

An alternative to quantitative coding is just to show e.g. bars and numbers, i.e. to hybridise tables and plots. For want of a standard name, I call these table plots. There are programs to do this on SSC, -tabplot- and -tableplot-, and examples in the talk Martin referred to. (The most recent version of this talk was given at the Washington, DC meeting a few months ago.)

Nick
[hidden email]

Nick Cox

In essence, as Maarten's reference implies, it can be done. I don't have a precise recipe for your particular request, but the code in -corrtable- from SSC shows one strategy:

loop over cells in table {
        draw a graph for each cell with desired text and colour
}

combine the graphs

Another strategy would resemble ways in which people draw heat maps, or indeed maps, by a series of calls to -twoway area-. Sergiy Radyakin has done something similar to that, but I can't vouch for whether he has code to do what you want that is also publicly available.

Maarten buis

This talk by Nick Cox at the last German Stata Users' Meeting may be
what you are looking for.

http://ideas.repec.org/p/boc/dsug09/01.html

--- On Fri, 16/10/09, [hidden email] wrote:
> In my work I often have to display both a table (to pick
> data from) and a graph (for visual impression) to users
> and I'm constantly trying to combine both. Colouring
> tables is one way to do this if rules/conditions are
> user-defineable.
>
> I thought about setting up a coloured twoway table in Stata
> but that doesn't seem to be easy

*
*   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/


*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: st: AW: Graph: Colouring table cells based on conditions or data distribution

Nick Cox
Sorry, wrong Ma*n.

Nick
[hidden email]

Martin Weiss

" in the talk Martin referred to."

I would love to take credit for the reference, but... (avid statalisters can complete this sentence themselves...)

*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

AW: st: AW: Graph: Colouring table cells based on conditions or data distribution

Stefan.Gawrich
Ma*n, thanks for pointing out the talk slides(?) and N*ck for writing them.
Just looked a bit into them and found some very interesting graphs I didn't see before.
"Table plots" seem to be a good alternative for colouring.

Nick Cox wrote:
Colour-coding cells, however, covers at least two kinds of idea:

1. Qualitative coding: Look! This cell contains a very high value, [...]
2. Quantitative coding: Look! In addition to showing you the values, I am also using a series of colour shades. [...]


Indeed, color shading like in the given example may not enhance the readability of a table. I also found that the excel algorithm (which I don't know) leads to strange results sometimes.

I use quantitative coding only with few colour categories (beside: I mainly use greys, yellow to brown, purples or - if applicable to the data - traffic lights: green, yellow and red).

One example of the last: 24 regional units deliver data to one dataset I maintain. Last year we had a major shift in data documentation causing a lot of problems inside the regional agencies. After receiving the data I calculated the proportion of missing values for each var by regional unit (>1000 cells) with Stata. I copied the resulting "missing data matrix" to Excel and applied colours (<5%: green; 5- <20%: yellow, 20%+: red). During the process of data preparation I updated this table on a daily basis and it proved to be a valuable tool.

Thanks

St*n




*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: st: AW: Graph: Colouring table cells based on conditions or data distribution

Austin Nichols
You can probably also get what you want just with -scatter- putting
squares of some color at the right (x,y) spot and then putting numbers
on top of those; see e.g.
http://www.stata.com/statalist/archive/2009-08/msg00191.html
for some ideas to get started, or try out:

sysuse nlsw88, clear
g g=max(grade,8)
la var g "Years of education"
ren married m
collapse wage, by(g m)
g w=round(wage,.01)
su wage, meanonly
g c=round((wage-r(min))/(r(max)-r(min))*255)
qui levelsof c, loc(cs)
loc g
foreach c of loc cs {
 loc g `g'||sc m g if c==`c', ms(S) msize(ehuge) mc("`c' 100 100")
 }
loc g `g'||sc m g, ms(i) mlab(w) mlabp(0)
loc g `g' leg(off) yla(-.75 " " 0 "N" 1 "Y" 1.75 " ", notick)
loc g `g' xla(7 " " 8/18 19 " ", notick)
tw `g' scheme(s1mono) ti(Wage heat map) xsize(8) ysize(5)


On Fri, Oct 16, 2009 at 9:06 AM,  <[hidden email]> wrote:

> Ma*n, thanks for pointing out the talk slides(?) and N*ck for writing them.
> Just looked a bit into them and found some very interesting graphs I didn't see before.
> "Table plots" seem to be a good alternative for colouring.
>
> Nick Cox wrote:
> Colour-coding cells, however, covers at least two kinds of idea:
>
> 1. Qualitative coding: Look! This cell contains a very high value, [...]
> 2. Quantitative coding: Look! In addition to showing you the values, I am also using a series of colour shades. [...]
>
>
> Indeed, color shading like in the given example may not enhance the readability of a table. I also found that the excel algorithm (which I don't know) leads to strange results sometimes.
>
> I use quantitative coding only with few colour categories (beside: I mainly use greys, yellow to brown, purples or - if applicable to the data - traffic lights: green, yellow and red).
>
> One example of the last: 24 regional units deliver data to one dataset I maintain. Last year we had a major shift in data documentation causing a lot of problems inside the regional agencies. After receiving the data I calculated the proportion of missing values for each var by regional unit (>1000 cells) with Stata. I copied the resulting "missing data matrix" to Excel and applied colours (<5%: green; 5- <20%: yellow, 20%+: red). During the process of data preparation I updated this table on a daily basis and it proved to be a valuable tool.
>
> Thanks
>
> St*n

*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: st: AW: Graph: Colouring table cells based on conditions or data distribution

Nick Cox
In reply to this post by Stefan.Gawrich
Sounds interesting. Do remember that many people are red-green colour-blind.

Nick
[hidden email]

[hidden email]

Ma*n, thanks for pointing out the talk slides(?) and N*ck for writing them.
Just looked a bit into them and found some very interesting graphs I didn't see before.
"Table plots" seem to be a good alternative for colouring.

Nick Cox wrote:
Colour-coding cells, however, covers at least two kinds of idea:

1. Qualitative coding: Look! This cell contains a very high value, [...]
2. Quantitative coding: Look! In addition to showing you the values, I am also using a series of colour shades. [...]


Indeed, color shading like in the given example may not enhance the readability of a table. I also found that the excel algorithm (which I don't know) leads to strange results sometimes.

I use quantitative coding only with few colour categories (beside: I mainly use greys, yellow to brown, purples or - if applicable to the data - traffic lights: green, yellow and red).

One example of the last: 24 regional units deliver data to one dataset I maintain. Last year we had a major shift in data documentation causing a lot of problems inside the regional agencies. After receiving the data I calculated the proportion of missing values for each var by regional unit (>1000 cells) with Stata. I copied the resulting "missing data matrix" to Excel and applied colours (<5%: green; 5- <20%: yellow, 20%+: red). During the process of data preparation I updated this table on a daily basis and it proved to be a valuable tool.


*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: st: AW: Graph: Colouring table cells based on conditions or data distribution

Sergiy Radyakin
In reply to this post by Stefan.Gawrich
Dear Stephan,

have a look at this page:

http://www.adeptanalytics.org/radyakin/stata/graphics/radyakin_graphics.htm

If this is something that you are looking for, contact me to get the
program files.

Best regards, Sergiy Radyakin



On Fri, Oct 16, 2009 at 3:33 AM,  <[hidden email]> wrote:

> Hi Statalisters,
>
> many of you may know this feature from MS Excel (from Version 2007 on).
> It's more or less a "choropleth map" for data tables (cell colour
> depends on cell value relative to the table distribution or on
> conditions).
> (see:
> http://msdn.microsoft.com/en-us/library/bb428945%28office.11%29.aspx)
>
>
> In my work I often have to display both a table (to pick data from) and
> a graph (for visual impression) to users and I'm constantly trying to
> combine both. Colouring tables is one way to do this if rules/conditions
> are user-defineable.
>
> I thought about setting up a coloured twoway table in Stata but that
> doesn't seem to be easy:
>
> Stata's results window doesn't allow for fancy tricks so the table must
> be in a graph.
>
> Spmap by Maurizio Pisati has all colouring options I need and can be set
> up with rectangular polygons and polygon centroids for labeling.
> But it's not written for that purpose, so it's difficult to set up:
> - a n*m polygon-coordinates dataset for each table must be created
> - linkage of table cells to polygons
> - handling of missing values and category "missing"
> - use of value labels
> - display of row and/or column totals (yes/no)
> - ...
>
> I already started to produce some coloured tables with spmap but these
> are only first tests.
> I'm curious if there is any other graph framework available in Stata
> which also may be suitable for this purpose.
>
> Best wishes
> Stefan Gawrich
>
>
>
>
>
>
> *
> *   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/
>

*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

AW: st: AW: Graph: Colouring table cells based on conditions or data distribution

Stefan.Gawrich
Austin and Sergiy,

WOW, I'm deeply impressed...
Thanks to all of you

Regards

Stefan


*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: st: AW: Graph: Colouring table cells based on conditions or data distribution

Nick Cox
In reply to this post by Sergiy Radyakin
Another red-green gradation!

http://en.wikipedia.org/wiki/Color_blindness#Red-green_color_blindness

In two books that have crossed my desk recently, (subdued) orange and
blue are used for basic contrasts of two categories to good effect.

Few, Stephen. 2009. Now you see it: Simple visualization techniques for
quantitative analysis. Oakland, CA: Analytics Pres.

Hastie, Trevor, Tibshirani, Robert, Friedman, Jerome. 2009.
The elements of statistical learning: Data mining, inference, and
prediction. New York: Springer.  

Nick
[hidden email]

Sergiy Radyakin

have a look at this page:

http://www.adeptanalytics.org/radyakin/stata/graphics/radyakin_graphics.
htm

If this is something that you are looking for, contact me to get the
program files.

On Fri, Oct 16, 2009 at 3:33 AM,  <[hidden email]>
wrote:
>
> many of you may know this feature from MS Excel (from Version 2007
on).
> It's more or less a "choropleth map" for data tables (cell colour
> depends on cell value relative to the table distribution or on
> conditions).
> (see:
> http://msdn.microsoft.com/en-us/library/bb428945%28office.11%29.aspx)
>
>
> In my work I often have to display both a table (to pick data from)
and
> a graph (for visual impression) to users and I'm constantly trying to
> combine both. Colouring tables is one way to do this if
rules/conditions
> are user-defineable.
>
> I thought about setting up a coloured twoway table in Stata but that
> doesn't seem to be easy:
>
> Stata's results window doesn't allow for fancy tricks so the table
must
> be in a graph.
>
> Spmap by Maurizio Pisati has all colouring options I need and can be
set

> up with rectangular polygons and polygon centroids for labeling.
> But it's not written for that purpose, so it's difficult to set up:
> - a n*m polygon-coordinates dataset for each table must be created
> - linkage of table cells to polygons
> - handling of missing values and category "missing"
> - use of value labels
> - display of row and/or column totals (yes/no)
> - ...
>
> I already started to produce some coloured tables with spmap but these
> are only first tests.
> I'm curious if there is any other graph framework available in Stata
> which also may be suitable for this purpose.
>

*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: st: AW: Graph: Colouring table cells based on conditions or data distribution

Austin Nichols
Nick Cox <[hidden email]> :
So maybe you pick a yellow-purple gradation instead--the color palette
choice I was assuming was a user choice...

sysuse nlsw88, clear
g g=max(grade,8)
la var g "Years of education"
ren married m
collapse wage, by(g m)
g w=round(wage,.01)
su wage, meanonly
g c=round((wage-r(min))/(r(max)-r(min))*255)
qui levelsof c, loc(cs)
loc g
foreach c of loc cs {
 loc c1=120+round(`c'/2)
 loc c2=255-round(`c'/2)
 loc m mc("`c2' `c2' `c1'")
 loc g `g'||sc m g if c==`c', ms(S) msize(ehuge) `m'
 }
loc g `g'||sc m g, ms(i) mlab(w) mlabp(0)
loc g `g' leg(off) yla(-.75 " " 0 "N" 1 "Y" 1.75 " ", notick)
loc g `g' xla(7 " " 8/18 19 " ", notick)
tw `g'  scheme(s1mono) ti(Wage heat map) xsize(8) ysize(5)


On Fri, Oct 16, 2009 at 12:12 PM, Nick Cox <[hidden email]> wrote:

> Another red-green gradation!
>
> http://en.wikipedia.org/wiki/Color_blindness#Red-green_color_blindness
>
> In two books that have crossed my desk recently, (subdued) orange and
> blue are used for basic contrasts of two categories to good effect.
>
> Few, Stephen. 2009. Now you see it: Simple visualization techniques for
> quantitative analysis. Oakland, CA: Analytics Pres.
>
> Hastie, Trevor, Tibshirani, Robert, Friedman, Jerome. 2009.
> The elements of statistical learning: Data mining, inference, and
> prediction. New York: Springer.
>
> Nick
> [hidden email]
>
> Sergiy Radyakin
>
> have a look at this page:
>
> http://www.adeptanalytics.org/radyakin/stata/graphics/radyakin_graphics.
> htm
>
> If this is something that you are looking for, contact me to get the
> program files.
>
> On Fri, Oct 16, 2009 at 3:33 AM,  <[hidden email]>
> wrote:
>>
>> many of you may know this feature from MS Excel (from Version 2007
> on).
>> It's more or less a "choropleth map" for data tables (cell colour
>> depends on cell value relative to the table distribution or on
>> conditions).
>> (see:
>> http://msdn.microsoft.com/en-us/library/bb428945%28office.11%29.aspx)
>>
>>
>> In my work I often have to display both a table (to pick data from)
> and
>> a graph (for visual impression) to users and I'm constantly trying to
>> combine both. Colouring tables is one way to do this if
> rules/conditions
>> are user-defineable.
>>
>> I thought about setting up a coloured twoway table in Stata but that
>> doesn't seem to be easy:
>>
>> Stata's results window doesn't allow for fancy tricks so the table
> must
>> be in a graph.
>>
>> Spmap by Maurizio Pisati has all colouring options I need and can be
> set
>> up with rectangular polygons and polygon centroids for labeling.
>> But it's not written for that purpose, so it's difficult to set up:
>> - a n*m polygon-coordinates dataset for each table must be created
>> - linkage of table cells to polygons
>> - handling of missing values and category "missing"
>> - use of value labels
>> - display of row and/or column totals (yes/no)
>> - ...
>>
>> I already started to produce some coloured tables with spmap but these
>> are only first tests.
>> I'm curious if there is any other graph framework available in Stata
>> which also may be suitable for this purpose.
>>

*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

st: parse last command from ado file?

Stefan.Gawrich
In reply to this post by Sergiy Radyakin
Hi Statalisters,

working in the review window is it possible to access the last command
from an ado file?

My example:
I create a lot of bivariate tables from the review window and e.g. later
copy them to a spreadsheet.
Often tables are too wide but fit with rows and columns interchanged.

sysuse auto
tab rep78 trunk // too wide
tab trunk rep78 // fits

I thought about sparing some typing by writing a small ado that swaps
rows and columns of the last shown table.

For this I need to parse the last command inside the ado. If for
instance I could have it in a global (without typing the next line, of
course)
global tabcmd "tab rep78 trunk, row col"


...I could write a program like this:
program define tt
local commapos = strpos("$tabcmd",",")
local options = substr("$tabcmd",`commapos',.)
local commapos1 = `commapos' - 1
local tabc = substr("$tabcmd'",1, `commapos1')
di "`tabc'"
local tab : word 1 of `tabc'
local var1 : word 3 of `tabc'
local var2 : word 2 of `tabc'
`tab' `var1' `var2' `options'
end

and exchange rows and columns of the last table by simply typing "tt".
(A more sophisticated program could also swap row and column options and
so on)


Regards

Stefan



*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

st: AW: parse last command from ado file?

Martin Weiss-5

<>

If this is all about -tab-, why not


*************
pr tabswap
        version 10.1
        tab `1' `2'
        tab `2' `1'
end
*************

and yes, I do know I have violated all existing rules for good programming,
but still: Stefan can take this further in any direction he wants to.
- tabswap rep tr- does produce both tables, though...

HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [hidden email]
[mailto:[hidden email]] Im Auftrag von
[hidden email]
Gesendet: Dienstag, 3. November 2009 17:31
An: [hidden email]
Betreff: st: parse last command from ado file?

Hi Statalisters,

working in the review window is it possible to access the last command
from an ado file?

My example:
I create a lot of bivariate tables from the review window and e.g. later
copy them to a spreadsheet.
Often tables are too wide but fit with rows and columns interchanged.

sysuse auto
tab rep78 trunk // too wide
tab trunk rep78 // fits

I thought about sparing some typing by writing a small ado that swaps
rows and columns of the last shown table.

For this I need to parse the last command inside the ado. If for
instance I could have it in a global (without typing the next line, of
course)
global tabcmd "tab rep78 trunk, row col"


...I could write a program like this:
program define tt
local commapos = strpos("$tabcmd",",")
local options = substr("$tabcmd",`commapos',.)
local commapos1 = `commapos' - 1
local tabc = substr("$tabcmd'",1, `commapos1')
di "`tabc'"
local tab : word 1 of `tabc'
local var1 : word 3 of `tabc'
local var2 : word 2 of `tabc'
`tab' `var1' `var2' `options'
end

and exchange rows and columns of the last table by simply typing "tt".
(A more sophisticated program could also swap row and column options and
so on)


Regards

Stefan



*
*   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/


*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

st: AW: parse last command from ado file?

Martin Weiss-5
In reply to this post by Stefan.Gawrich

<>

BTW, there is also -#review- which displays the last executed commands quite
bravely. I do not think that it leaves anything behind, though, for your ado
to pick up. Also, some commands do leave the command as typed in
-e(cmdline)-...




HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [hidden email]
[mailto:[hidden email]] Im Auftrag von
[hidden email]
Gesendet: Dienstag, 3. November 2009 17:31
An: [hidden email]
Betreff: st: parse last command from ado file?

Hi Statalisters,

working in the review window is it possible to access the last command
from an ado file?

My example:
I create a lot of bivariate tables from the review window and e.g. later
copy them to a spreadsheet.
Often tables are too wide but fit with rows and columns interchanged.

sysuse auto
tab rep78 trunk // too wide
tab trunk rep78 // fits

I thought about sparing some typing by writing a small ado that swaps
rows and columns of the last shown table.

For this I need to parse the last command inside the ado. If for
instance I could have it in a global (without typing the next line, of
course)
global tabcmd "tab rep78 trunk, row col"


...I could write a program like this:
program define tt
local commapos = strpos("$tabcmd",",")
local options = substr("$tabcmd",`commapos',.)
local commapos1 = `commapos' - 1
local tabc = substr("$tabcmd'",1, `commapos1')
di "`tabc'"
local tab : word 1 of `tabc'
local var1 : word 3 of `tabc'
local var2 : word 2 of `tabc'
`tab' `var1' `var2' `options'
end

and exchange rows and columns of the last table by simply typing "tt".
(A more sophisticated program could also swap row and column options and
so on)


Regards

Stefan



*
*   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/


*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

st: RE: parse last command from ado file?

Nick Cox
In reply to this post by Stefan.Gawrich
You might like to look at -storecmd- from SSC and in turn at what
it uses.

Personally, I think these solutions are usually not worth the trouble
they cause.

A more interesting line of attack, I suggest, is automatically to check
number of values of each variable and then work out which variable is
passed to -tabulate- first. That's probably what you have in mind in
your closing comments.

Nick
[hidden email]

[hidden email]

working in the review window is it possible to access the last command
from an ado file?

My example:
I create a lot of bivariate tables from the review window and e.g. later
copy them to a spreadsheet.
Often tables are too wide but fit with rows and columns interchanged.

sysuse auto
tab rep78 trunk // too wide
tab trunk rep78 // fits

I thought about sparing some typing by writing a small ado that swaps
rows and columns of the last shown table.

For this I need to parse the last command inside the ado. If for
instance I could have it in a global (without typing the next line, of
course)
global tabcmd "tab rep78 trunk, row col"


...I could write a program like this:
program define tt
local commapos = strpos("$tabcmd",",")
local options = substr("$tabcmd",`commapos',.)
local commapos1 = `commapos' - 1
local tabc = substr("$tabcmd'",1, `commapos1')
di "`tabc'"
local tab : word 1 of `tabc'
local var1 : word 3 of `tabc'
local var2 : word 2 of `tabc'
`tab' `var1' `var2' `options'
end

and exchange rows and columns of the last table by simply typing "tt".
(A more sophisticated program could also swap row and column options and
so on)


*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: st: AW: parse last command from ado file?

Nick Winter
In reply to this post by Martin Weiss-5
Another approach: create your own version of the -tab- command, that
puts the command it executes into the review window:

program mytab
        window push tabulate `0'
        tabulate `0'
end

The first line puts the command itself onto the review list.  So in your
do file, use -mytab- instead of -tab-.


Yet another: create your own version of the -tab- command that figures
out which variable has fewer categories:

program mytab2
        capture tab `1'
        local r1 = r(r)
        capture tab `2'
        if r(r)<=`r1' {
                tab `1' `2'
        }
        else {
                tab `2' `1'
        }
end

This isn't perfect because the display width depends on the width of the
columns, not just the number, but it's a start.

- Nick Winter




Martin Weiss wrote:

> <>
>
> If this is all about -tab-, why not
>
>
> *************
> pr tabswap
> version 10.1
> tab `1' `2'
> tab `2' `1'
> end
> *************
>
> and yes, I do know I have violated all existing rules for good programming,
> but still: Stefan can take this further in any direction he wants to.
> - tabswap rep tr- does produce both tables, though...
>
> HTH
> Martin
>
>
> -----Ursprüngliche Nachricht-----
> Von: [hidden email]
> [mailto:[hidden email]] Im Auftrag von
> [hidden email]
> Gesendet: Dienstag, 3. November 2009 17:31
> An: [hidden email]
> Betreff: st: parse last command from ado file?
>
> Hi Statalisters,
>
> working in the review window is it possible to access the last command
> from an ado file?
>
> My example:
> I create a lot of bivariate tables from the review window and e.g. later
> copy them to a spreadsheet.
> Often tables are too wide but fit with rows and columns interchanged.
>
> sysuse auto
> tab rep78 trunk // too wide
> tab trunk rep78 // fits
>
> I thought about sparing some typing by writing a small ado that swaps
> rows and columns of the last shown table.
>
> For this I need to parse the last command inside the ado. If for
> instance I could have it in a global (without typing the next line, of
> course)
> global tabcmd "tab rep78 trunk, row col"
>
>
> ...I could write a program like this:
> program define tt
> local commapos = strpos("$tabcmd",",")
> local options = substr("$tabcmd",`commapos',.)
> local commapos1 = `commapos' - 1
> local tabc = substr("$tabcmd'",1, `commapos1')
> di "`tabc'"
> local tab : word 1 of `tabc'
> local var1 : word 3 of `tabc'
> local var2 : word 2 of `tabc'
> `tab' `var1' `var2' `options'
> end
>
> and exchange rows and columns of the last table by simply typing "tt".
> (A more sophisticated program could also swap row and column options and
> so on)
>
>
> Regards
>
> Stefan
>
>
>
> *
> *   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/
>
>
> *
> *   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/

--
--------------------------------------------------------------
Nicholas Winter                                 434.924.6994 t
Assistant Professor                             434.924.3359 f
Department of Politics                  [hidden email] e
University of Virginia          faculty.virginia.edu/nwinter w
PO Box 400787, 100 Cabell Hall
Charlottesville, VA 22904

*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

st: addendum: parse last command from ado file?

Stefan.Gawrich
In reply to this post by Stefan.Gawrich
Hi Statalisters,

the question was:
While working in the command window: How can I access the last command
from within an ado in order to alter it in a specified way (without
having to retype anything) and run it again.

My example was to interchange row and column vars in a bivariate tab
command.

There is one very simple (halfway) approach to this (still requires some
manual input)

**************
program define tt
di "choose command"
di _request(tabcmd)
...parsing....
...rearrangement...
...run
**************

- Start ado (e.g. set function key F4 to tt;)
- choose recent command from review window or use "page up"-key from the
command line
- (Enter)
- Now the global $tabcmd is altered and executed.

User input is for example: F4 ->  2* PgUp -> Return (quite comfortable)

*****************

Second, a one-click-solution that works for me (Stata 10.1 on Win XP)
but I'm not shure if it will work in all settings.
In my setting the "#review"-command can be executed from the internal
do-editor but not from my external editor. So I created an one-liner
do-file "review.do" containing "#review 2 1"
I also set function key F4 to tt1;

The basic idea is:

**********************
Start ado (over function key)
The ado file:
- opens a log
- executes #review with "do review.do"
- closes the log
- opens log via "file open"
- reads lines (using "file read ... line" in a "while"-loop until a line
starts with "2 " (the #review number)
- copies last line to local
And then:
...parsing....
...rearrangement...
...run
**********************

User input is: F4 (very comfortable)

Stefan

*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

st: "dirty trick": metadata in extended missing value labels

Stefan.Gawrich
In reply to this post by Stefan.Gawrich
Hi Statalisters,

the following is a "dirty" but - at least for me - useful trick:

I produce a lot of graphs in batch mode. Layout usually needs a lot of
tweaking (titles, labels, formats).
So I had to write many "foreach" loops with 6, 7, 8 or more parallel
lists to specify individual layout parameters.

One example: Some metric var (length) (not really metric but integer in
auto.dta) and a 0/1-var (foreign):

clear
sysuse auto
foreach x of var length foreign {
graph bar `x',over(rep78) blabel(bar)
sleep 2000
}

The 0/1-mean is better displayed as a proportion. To make it look good,
I would multiply foreign by 100, label the axis "Percentage", write
"Percentage" into the title and set the label format to %3.1f.

It would be nice to be able to attach such display information to the
variable, so one could take these meta-parameters from the dataset
instead of specifying them by hand each time. There seems to be no
regular way to do so.
As a workaround for this, labels for extended missing values (.a, .b, .c
... .z) came to my mind which can be set for all numerical variables. I
never use any more than ".a" or ".b" so why not store some information
in the value label of some (by me) unused missing value like ".l"?

The following code stores some basic information on type of display and
label format to value label ".l" of variables. Later two graphs are
produced using this information.


*************************************
*** Create example dataset
clear
sysuse auto
*** Create metadata codes
foreach var of varlist _all {
// only for numerical vars
local type : type `var'
if inlist("`type'", "byte", "int", "float", "real", "double") == 0
continue
local form "m21" // default : display as mean, label format 2.1

*** Example 1: m: display as mean, label format 3.0
if inlist("`var'", "length") == 1 local form "m30"

*** Example 2: p: display as percentage, format 3.1
if inlist("`var'", "foreign") == 1 local form "p31"

*** Each var gets a new value label (templbl`var'). Existing value
labels are copied.
local lbl`var' : value label `var'
if "`lbl`var''" == "" {
cap label drop templbl`var'
label define templbl`var' .l "`form'"
label values `var' templbl`var'
}
else {
cap label drop templbl`var'
label copy `lbl`var'' templbl`var'  
label define templbl`var' .l "`form'" , add
label values `var' templbl`var'
}
}

*** Now set up the graphs:
local varover "rep78"
local varlab : variable label `varover'
foreach var of varlist length foreign {
local varlab2 : variable label `var'
local how :  label  templbl`var' .l // ".l" label content into a local
gen xvar = `var'  // in order not to alter the original var, xvar is
used in the graph
if substr("`how'",1,1) == "p" replace xvar = `var' * 100 // multiply
with 100 if var displays percentage
if substr("`how'",1,1) == "m" local value = "Mean" // Label for titles
if substr("`how'",1,1) == "p" local value = "Percentage" // Label for
titles
local form = "%" + substr("`how'",2,1) + "." + substr("`how'",3,1) + "f"
// local for label formatting
graph bar xvar, over(`varover') title("`varlab2' over `varlab'
(`value')") ytitle("`value'") blabel(bar,format(`form'))
sleep 2000
drop xvar
}
************************************


Stefan



*
*   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/
1234 ... 6
Loading...