Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • No "SVYSET" option for the new "TABLE" command?

    The new "table" command, as Stata documentation notes, can largely supercede existing tabulate commands. I was excited when this came out as previously, exporting the output from a "tabulate" command into a word or excel spreadsheet was onerous, at least without the user written "tabout" command although that does not afford all of the customizable options of the new collect package.

    However, I mostly work with complex survey data and svyset commands. Am I correct that the "table" command allows weights but does not have a "svyset" option? For the point estimates alone this does not matter as you can simply use the weights, but if you want to export say confidence intervals they will not be correct without specifying the complex survey design. Apologies if I am entirely missing something obvious on this.

  • #2
    It is hard to give any advice without more information, such as an example of the kinds of tables you want to produce.

    You may not need the table command since collect (the engine on which the new table table command is built) works directly with estimation results, including commands like svy: mean, svy: proportion, and svy: regress.

    I'd suggest browsing the [TABLES] manual to get familiar with collect. Notice that [TABLES] Example 7 shows how to use collect with svy: regress to produce a table of regression coefficients estimated using survey data.

    Here is another example, producing a table of estimated continuous mean and categorical percent values with their confidence intervals, using survey data.

    Code:
    webuse nhanes2l
    * data already -svyset-
    
    * estimate population means for some continuous variables
    collect, tags(group[Mean]): svy: mean bpsystol bmi
    * estimate population percentages for categorial variables
    collect, tags(group[_hide]): svy: proportion agegrp sex race, percent
    
    * define your own custom composite for confidence intervals (CIs)
    collect composite define ci = _r_lb _r_ub, notrim delimiter(" ")
    collect style cell result[ci], sformat("[%s]")
    * __LEVEL__ is a keyword that refers to the confidence level used by the
    * estimation command to construct the CI limits
    collect label levels result ci "__LEVEL__% CI"
    
    * numeric format for the mean estimates
    collect style cell group[Mean]#result[_r_b ci], nformat(%7.2f)
    * numeric and string formats for the percentages
    collect style cell group[_hide]#result[_r_b _r_lb _r_ub], ///
        nformat(%7.1f) sformat("%s%%")
    
    * remove the vertical border defined in the default style
    collect style cell border_block[corner row-header], ///
        border(right, pattern(none))
    
    * use shorter labels
    collect label levels result _N "N" _r_b "Estimate", modify
    
    * show the categorical variable's labels; the default style hides them
    collect style header agegrp sex race, title(label)
    
    * stack the categories below the categorical variable labels; the
    * default style uses -binder(=)-
    collect style row stack, nobinder
    
    * arrange the table's layout:
    * _N    - estimate sample sizes; from matrix e(_N)
    * _r_b  - estimated means and proportions
    * ci    - our custom composite containing the CI limits
    collect layout (group#colname) (result[_N _r_b ci])
    Here is the resulting table.
    Code:
    ---------------------------------------------------------
                                  N Estimate         [95% CI]
    ---------------------------------------------------------
    Mean
      Systolic blood pressure 10351   126.95 [125.72  128.18]
      Body mass index (BMI)   10351    25.28  [25.16   25.39]
    Age group
      20–29                   10351    28.0%  [26.6%   29.5%]
      30–39                   10351    20.4%  [19.3%   21.6%]
      40–49                   10351    16.8%  [16.0%   17.7%]
      50–59                   10351    16.7%  [15.7%   17.8%]
      60–69                   10351    13.3%  [12.6%   14.2%]
      70+                     10351     4.6%   [4.1%    5.3%]
    Sex
      Male                    10351    47.9%  [46.8%   49.1%]
      Female                  10351    52.1%  [50.9%   53.2%]
    Race
      White                   10351    87.9%  [84.1%   90.9%]
      Black                   10351     9.6%   [7.2%   12.5%]
      Other                   10351     2.5%   [1.1%    5.8%]
    ---------------------------------------------------------
    Once you style the table to your specifications, you can use collect
    export
    to publish it to a supported document type: MS Work, MS
    Excel, PDF, HTML, Markdown, or plain text.

    Here is a screenshot of my browser showing this table exported to HTML.


    Click image for larger version

Name:	Screenshot 2023-12-16 at 1.21.36 PM.png
Views:	1
Size:	125.4 KB
ID:	1737355

    Comment


    • #3
      Thanks so much Jeff, and sorry I wasn't more clear and specific in my post.

      I was thinking specifically of trying to directly output (for instance) row/column percentages from "svy: tabulate" into a table/collection.

      I'm not sure if that is a possibility? That being said, as you say, one can just use "collect: svy: proportion ..." which will produce identical results. So I think that's probably the best way forward?

      Thank you very much again for your response

      Best,

      Adam

      Comment

      Working...
      X