Announcement

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

  • help in user-defined dimension+level in collect command

    Hi, anyone knows if it is possible to add user-defined dimension+level to play to incorporate in the collect command?

    suppose the following toy example:

    Code:
    . webuse auto, clear
    (1978 automobile data)
    
    . collect: ttest price ,by(foreign )
    
    Two-sample t test with equal variances
    ------------------------------------------------------------------------------
       Group |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
    ---------+--------------------------------------------------------------------
    Domestic |      52    6072.423    429.4911    3097.104    5210.184    6934.662
     Foreign |      22    6384.682    558.9942    2621.915     5222.19    7547.174
    ---------+--------------------------------------------------------------------
    Combined |      74    6165.257    342.8719    2949.496    5481.914      6848.6
    ---------+--------------------------------------------------------------------
        diff |           -312.2587    754.4488               -1816.225    1191.708
    ------------------------------------------------------------------------------
        diff = mean(Domestic) - mean(Foreign)                         t =  -0.4139
    H0: diff = 0                                     Degrees of freedom =       72
    
        Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
     Pr(T < t) = 0.3401         Pr(|T| > |t|) = 0.6802          Pr(T > t) = 0.6599
    
    . collect layout () () (result[mean]), name(default)
    
    Collection: default
        Tables: result[mean]
       Table 1: 1 x 1
    
    --------
    6165.257
    --------
    in the case of ttest command, the Return results that are managed by the collect suite, does not include the foreign variable anywhere.

    I am trying to get this:

    Code:
    foreign: 6165.257

    Any clue? Thanks.

  • #2
    The value 6165.257 is not the foreign mean, it is the mean of price for the combined sample. In fact, your resulting table from collect layout seems impossible given that ttest does not post a result named r(mean). I imagine your default collection has other results collected prior to your call to ttest.

    Collecting results from ttest is problematic, because all the results are scalars, so collect does not have much else to tag the results with other than their result name. You can use collect addtags with option fortags() to add targetted tag elements and also collect recode to rename common but differently named results.

    Here is how you might do this given your example.
    Code:
    sysuse auto
    
    collect: ttest price, by(foreign)
    
    * show -ttest- stored results
    return list
    * show result levels
    collect levels result
    
    * add -foreign- specific tag information
    collect addtags foreign[0], fortags(result[N_1 mu_1 sd_1])
    collect addtags foreign[1], fortags(result[N_2 mu_2 sd_2])
    
    * recode results to remove the subscript, and use better (arguably) names
    collect recode result ///
        N_1 = N mu_1 = Mean sd_1 = SD ///
        N_2 = N mu_2 = Mean sd_2 = SD 
    
    collect style autolevels result N Mean SD
    
    collect layout (foreign) (result)
    Here is the resulting table.
    Code:
    -------------------------------
             |  N     Mean       SD
    ---------+---------------------
    Domestic | 52 6072.423 3097.104
    Foreign  | 22 6384.682 2621.915
    -------------------------------

    Comment

    Working...
    X