Stata 15 help for _tab

Title

[P] _tab -- Generating simple tables

Syntax

Declaring a _tab object:

.obj = ._tab.new [, options ]

Setting default parameters:

.obj.reset [, options ]

.obj.width width_1 ... width_k [, noreformat ]

.obj.titlefmt sfmt_1 ... sfmt_k

.obj.strfmt sfmt_1 ... sfmt_k

.obj.numfmt nfmt_1 ... nfmt_k

.obj.pad # ... #

.obj.ignore item_1 ... item_k

.obj.titlecolor color_1 ... color_k

.obj.strcolor color_1 ... color_k

.obj.numcolor color_1 ... color_k

Displaying table elements:

.obj.sep [, top middle bottom ]

.obj.titles title_1 ... title_k

.obj.row element_1 ... element_k

Miscellaneous information:

.obj.width_of_table

Post table information to r():

.obj.post_results

options Description --------------------------------------------------------------------- width(#) default column width; default is 12 columns(#) number of columns; default is 2 lmargin(#) left margin; default is 2 tcolor(color) default color for titles; default is text ncolor(color) default color for numbers; default is input scolor(color) default color for strings; default is text lcolor(color) color of the separator lines; default is text tfmt(sfmt) default title format nfmt(nfmt) default numeric format sfmt(sfmt) default string format nocommas do not use comma in default numeric formats; default behavior commas use comma in default numeric formats ignore(item) ignore item if supplied as an argument to .row separator(#) automatically separate every # rows; default is 0, no separator puttab(name) name for Mata object used by post_results; default is _putTab clear(clear_options) clear the specified parameters clear synonym for clear(all) ---------------------------------------------------------------------

clear_options Description --------------------------------------------------------------------- widths clear the column widths tcolors clear the title colors ncolors clear the number colors scolors clear the string colors tfmts clear the title formats nfmts clear the number formats sfmts clear the string formats paddings clear the column paddings vseparators remove the vertical separators rows reset the row counter to zero columns reset the column starting positions fmts reset the default formats all shortcut for specifying all the above ---------------------------------------------------------------------

Column arguments Description --------------------------------------------------------------------- width_# column width title_# column title nfmt_# numeric format; see [D] format sfmt_# string format; see [D] format color_# text (or txt), error, result, or input item_# string or value, including a missing value element_# quoted string or expression ---------------------------------------------------------------------

Note when the column argument is . or "", the default value is used. For element_# in .row, "" (the empty string) causes nothing to be displayed for the specific column, but . is interpreted as a missing value resulting from an expression.

Description

_tab is a programmer's tool for generating simple tables. Generating a table using an object from class _tab involves several steps:

1. Create an object from class _tab.

_tab.new returns an object from class _tab. To create a _tab object, type the following:

.obj = ._tab.new

where obj is a name of your choosing.

2. Set the default table parameters (this could also have been done with ._tab.new).

.reset changes the table parameters. For example,

.obj.reset, columns(7) lmargin(0)

changes the number of columns in .obj to 7 and removes the left margin. This matches the number of columns produced by ereturn display.

3. Set the individual column widths.

.width sets the widths for each individual column. .width requires an argument for each column. To set column widths that match those produced by ereturn display, type

.obj.width 13 |. . 8 8 . .

The first column is now 13 characters wide, the fourth and fifth columns are now 8 characters wide, and the other columns remain at 12 characters (the default width). Notice the vertical bar (|) specified between the first and second column widths, this indicates that the first and second columns are to be separated by a vertical line.

By default, .width will generate a default title, numeric, and string format for each column; the noreformat option prevents this.

4. Set the title format for each column.

.titlefmt sets the string format for the title of each individual column. .titlefmt requires an argument for each column. To set title formats that match those of ereturn display, type

.obj.titlefmt . . . %6s . %24s .

The fourth column title (for the t or z statistic) has format %6s, leaving two extra spaces to the right of the title, the sixth column title format is stretched to consume the seventh column title, and the other formats remain as they were set.

Note: The .width routine will reset the individual title formats to %ws, where w is one less than the column width; .titlefmt should be used after .width.

5. Set the string format for each column.

.strfmt sets the string format for each individual column. .strfmt requires an argument for each column. To set string formats that match those of ereturn display, type

.obj.strfmt . . . . . . .

Here we did not even have to use .strfmt because the only string ereturn display prints in the table body is the name of the coefficient, and the default string format already matches.

Note: The .width routine will reset the individual string formats to %ws, where w is one less than the column width; .strfmt should be used after .width.

6. Set the number format for each column.

.numfmt sets the numeric format for each individual column. .numfmt requires an argument for each column. To set numeric formats that match those of ereturn display, type

.obj.numfmt . %9.0g %9.0g %7.2f %5.3f %9.0g %9.0g

Note: The .width routine will reset the individual numeric formats to %w.0g, where w is one less than the column width; .numfmt should be used after .width.

7. Set the padding for numeric formats for each column. The default numeric formats use up all but the last space of each column. To keep smaller numeric formats right justified within a column, the column must have padded spaces on the left.

.pad sets the padding spaces associated with the numeric format for each individual column. .pad requires an argument for each column. To set paddings that match those of ereturn display, type

.obj.pad . 2 1 . 2 3 3

8. Set the ignore item for each column.

.ignore sets the item to be ignored for each column. Typically this would be one of the missing value codes, but it could be a string such as "NA". This setting is most useful when the table elements were built up and placed into a matrix where one or more values (usually missing) identify empty cells.

9. Set the title color for each column.

.titlecolor sets the color for the title of each individual column. .titlecolor requires an argument for each column. The default title color is text, which already matches that of ereturn display. To make the title of the first column result, type

.obj.titlecolor result . . . . . .

10. Set the string color for each column.

.strcolor sets the color for strings in each individual column. .strcolor requires an argument for each column. The default string color is text, which already matches that of ereturn display. To make the strings in the second column result, type

.obj.strcolor . result . . . . .

11. Set the number color for each column.

.numcolor sets the color of numbers in each individual column. .numcolor requires an argument for each column. The default number color is text, which already matches that of ereturn display. To make the numbers in the last two columns input, type

.obj.numcolor . . . . . input input

12. Display the table elements, row by row.

.sep will display a horizontal line. It respects the vertical separators specified in .width.

.titles displays the specified titles. .titles requires an argument for each column. To display titles that match those of ereturn display, type

.obj.titles "" "Coef." "Std. Err." "t" "P>|t|" "[95% Conf. Interval]" ""

.row displays a row in the body of the table. .row requires an argument for each column. Strings are expected to be bound in double quotes (or compound double quotes). Expressions with spaces are expected to be bound in parentheses.

To display the row for variable mpg from a regression fit, type

.obj.row "mpg" _b[mpg] _se[mpg] (_b[mpg]/_se[mpg]) 2*ttail(e(df_r),abs(_b[mpg]/_se[mpg])) _b[mpg]-_se[mpg]*invttail(e(df_r),.025) _b[mpg]+_se[mpg]*invttail(e(df_r),.025)

13. Get the width of the table.

local w = `.obj.width_of_table'

or

.obj.width_of_table local w = s(width)

14. Post table information to r().

.obj.post_results [prefix [suffix]]

Options for .new and .reset

width(#) specifies the default column width to be # characters wide. Initially the default is 12 characters.

columns(#) specifies the number of columns in the table. Initially the default is 2 columns.

lmargin(#) specifies the left margin, which is the number of spaces the table is indented. The default left margin is 2 spaces.

tcolor(color) specifies the default title color. Initially the default is text.

ncolor(color) specifies the default color for numbers. Initially the default is result.

scolor(color) specifies the default color for strings. Initially the default is text.

lcolor(color) specifies the default color of the separator lines. Initially the default is text.

tfmt(sfmt) specifies the default title format. Initially the default is determined from the column width.

nfmt(nfmt) specifies the default numeric format. Initially the default is determined from the column width.

sfmt(sfmt) specifies the default string format. Initially the default is determined from the column width.

nocommas indicates that commas are not to be used in the default numeric format. Initially, this is the default behavior.

commas indicates that commas be used in the default numeric format.

ignore(item) specifies that item be ignored if supplied to .row. By default, only empty strings ("") are ignored by .row.

separator(#) specifies that a separator be automatically drawn between every # rows. Initially there are no automatic row separators, which are explicitly set by separator(0).

clear(clear_options) clears the specified table parameters.

clear is a shortcut for clear(all).

Options for .width

noreformat prevents .width from resetting the column formats for titles, strings, and numbers.

Options for .sep

top causes the display of a horizontal line appropriate for the top of the table.

middle causes the display of a horizontal line appropriate for the middle of the table. This is the default for .sep.

bottom causes the display of a horizontal line appropriate for the bottom of the table.

A note about colors

The color identifiers text, result, error, and input have the following respective synonyms: green, yellow, red, and white.

Examples

* _my_tab.ado program _my_tab syntax [, level(int `c(level)') ] tempname mytab z t p ll ul .`mytab' = ._tab.new, col(7) lmargin(0) .`mytab'.width 13 |12 12 8 8 12 12 .`mytab'.titlefmt . . . %6s . %24s . .`mytab'.pad . 2 1 0 2 3 3 .`mytab'.numfmt . %9.0g %9.0g %7.2f %5.3f %9.0g %9.0g if "`e(df_r)'" != "" { local stat t scalar `z' = invttail(e(df_r),(100-`level')/200) } else { local stat z scalar `z' = invnorm((100+`level')/200) } local namelist : colname e(b) local eqlist : coleq e(b) local k : word count `namelist' .`mytab'.sep, top if `:word count `e(depvar)'' == 1 { local depvar "`e(depvar)'" } .`mytab'.titles "`depvar'" /// 1 "Coef." /// 2 "Std. Err." /// 3 "`stat'" /// 4 "P>|`stat'|" /// 5 "[`level'% Conf. Interval]" "" // 6 7 forvalues i = 1/`k' { local name : word `i' of `namelist' local eq : word `i' of `eqlist' if "`eq'" != "_" { if "`eq'" != "`eq0'" { .`mytab'.sep local eq0 `"`eq'"' .`mytab'.strcolor result . . . . . . .`mytab'.strfmt %-12s . . . . . . .`mytab'.row "`eq'" "" "" "" "" "" "" .`mytab'.strcolor text . . . . . . .`mytab'.strfmt %12s . . . . . . } local beq "[`eq']" } else if `i' == 1 { local eq .`mytab'.sep } scalar `t' = `beq'_b[`name']/`beq'_se[`name'] if "`e(df_r)'" != "" { scalar `p' = 2*ttail(e(df_r),abs(`t')) } else scalar `p' = 2*norm(-abs(`t')) scalar `ll' = `beq'_b[`name']-`beq'_se[`name']*`z' scalar `ul' = `beq'_b[`name']+`beq'_se[`name']*`z' .`mytab'.row "`name'" /// `beq'_b[`name'] /// `beq'_se[`name'] /// `t' /// `p' /// `ll' `ul' } .`mytab'.sep, bottom end

. sysuse auto, clear (1978 Automobile Data)

. regress mpg turn trunk displ, noheader ------------------------------------------------------------------------------ mpg | Coef. Std. Err. t P>|t| [95% Conf. Interval] -------------+---------------------------------------------------------------- turn | -.4971904 .165826 -3.00 0.004 -.82792 -.1664608 trunk | -.222583 .1353748 -1.64 0.105 -.4925796 .0474136 displacement | -.0196434 .0080013 -2.46 0.017 -.0356016 -.0036853 _cons | 47.94784 5.290301 9.06 0.000 37.39667 58.49902 ------------------------------------------------------------------------------

. _my_tab ------------------------------------------------------------------------------ mpg | Coef. Std. Err. t P>|t| [95% Conf. Interval] -------------+---------------------------------------------------------------- turn | -.4971904 .165826 -3.00 0.004 -.82792 -.1664608 trunk | -.222583 .1353748 -1.64 0.105 -.4925796 .0474136 displacement | -.0196434 .0080013 -2.46 0.017 -.0356016 -.0036853 _cons | 47.94784 5.290301 9.06 0.000 37.39667 58.49902 ------------------------------------------------------------------------------

. sureg (mpg turn trunk) (displ head gear), noheader

------------------------------------------------------------------------------ | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- mpg | turn | -.7319231 .1276395 -5.73 0.000 -.982092 -.4817543 trunk | -.3189985 .1312914 -2.43 0.015 -.5763249 -.0616721 _cons | 54.70544 4.254873 12.86 0.000 46.36605 63.04484 -------------+---------------------------------------------------------------- displacement | headroom | 20.63565 7.225785 2.86 0.004 6.473372 34.79793 gear_ratio | -150.6982 13.39898 -11.25 0.000 -176.9597 -124.4367 _cons | 589.8645 52.81358 11.17 0.000 486.3518 693.3772 ------------------------------------------------------------------------------

. _my_tab ------------------------------------------------------------------------------ | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- mpg | turn | -.7319231 .1276395 -5.73 0.000 -.982092 -.4817543 trunk | -.3189985 .1312914 -2.43 0.015 -.5763249 -.0616721 _cons | 54.70544 4.254873 12.86 0.000 46.36605 63.04484 -------------+---------------------------------------------------------------- displacement | headroom | 20.63565 7.225785 2.86 0.004 6.473372 34.79793 gear_ratio | -150.6982 13.39898 -11.25 0.000 -176.9597 -124.4367 _cons | 589.8645 52.81358 11.17 0.000 486.3518 693.3772 ------------------------------------------------------------------------------


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