Announcement

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

  • T-test results Table

    Hello everyone,
    let me premise that I am a beginner in Stata and I don't know if it is possible what I want to do.

    I am writing a paper with ESS (European Social Survey) data on immigration as a benefit to the economy and the correlations and differences between this survey with the level of education and trust in the European parliament in three different countries (Germany, Hungary, Italy).
    I have already done the two sample t-tests for each country between immigration as a benefit and trust in the European parliament (which is my dummy variable).

    Now I would like to make this table with my data, but I have not been able to figure out how to do it nor have I found any information.

    Click image for larger version

Name:	Screenshot (962).png
Views:	1
Size:	50.9 KB
ID:	1741039


    Can anyone help me or give me a link to some explanation on this?

  • #2
    Without example data and the Stata code you used to perform the t-tests,
    it is difficult to give advice on how to use Stata to produce your
    table.

    If you have Stata 17 or newer, you can use the collect suite of
    commands to build your custom table.

    I'll make some guesses on your data and code, and hope the following is
    helpful.

    In the following I use the NHANES dataset to show how to use
    svy: regress to produce t-tests comparing the heights between
    male and female survey participants within 4 regions where the data were
    collected.

    Note I use an if clause to restrict the sample to each region,
    and then use if e(sample) to get the with-in region sample sizes
    of the male and female participants. Normally I would recommend using
    option subpop() when performing subpopuation estimation, but I
    will pretend the regions (e.g. Countries) were sampled independently.

    Code:
    webuse nhanes2l
    
    * use region to define samples for the t-tests
    levelsof region, local(rlist)
    * use sex as the grouping variable for comparing heights
    * 1 -> Male
    * 2 -> Female
    levelsof sex
    
    * clear all the collections, start with a new -default- collection
    collect clear
    foreach r of local rlist {
        * use -regress- to produce a t-test comparing heights between
        * men and women within the given region
    
        * use option -tags()- to put factor levels of variable -region-
        * into dimension -var- for the tags attached to the collected
        * results, -collect- will pick up the variable label for
        * -region- along with its value labels
    
        collect _r_b _r_se _r_ci _r_z _r_p, tags(var[`r'.region]) : ///
            svy: regress height i.sex if region==`r'
    
        * compute the number of men and women in the sample
    
        sum 1.sex if e(sample), meanonly
        collect get N1=(r(sum)) N2=(r(N)-r(sum)), tags(var[`r'.region])
    }
    
    * verify the auto-levels of -result- are the ones we want, and are in the
    * correct order
    collect query autolevels result
    
    * since -region- is a factor variable in dimesion -var- we can use
    * it as a dimension in the layout
    collect layout (region) (result)
    
    * redefine the build-in composite result named _r_ci with some
    * customizations
    collect composite define _r_ci = _r_lb _r_ub, trim delimiter(" - ")
    
    * some numeric formatting
    collect style cell result[_r_b], nformat(%9.3g)
    collect style cell result[_r_se _r_ci], nformat(%9.2f)
    collect style cell result[N1 N2], nformat(%12.0fc)
    
    * use labels from the example table
    collect label levels result ///
        _r_b "Diff." ///
        _r_se "Std. err." ///
        _r_ci "__LEVEL__% Conf. int." ///
        _r_p "p" ///
        N1 "N `:label (sex) 1'" ///
        N2 "N `:label (sex) 2'" ///
        , modify
    
    * change horizontal alignment to match example table
    collect style cell cell_type[column-header item], halign(center)
    
    * change borders to match example table
    collect style cell border_block, border(all, pattern(none))
    collect style cell border_block[column-header row-header item], border(bottom)
    
    * add table title
    collect title ///
    "Table 2: Gender differences in housework. Two sample t-test in the four regions"
    
    * add extra space for plain text output
    collect style column, extraspace(1)
    
    * replay table with these style and label changes
    collect preview
    
    * export table to HTML; see -help collect export- for other supported documents
    collect export table.html, replace
    Here is the resulting table.
    Code:
    Table 2: Gender differences in housework. Two sample t-test in the four regions
        Diff.   Std. err.    95% Conf. int.      t       p     N Male   N Female
       -------------------------------------------------------------------------
    NE   175       0.27     174.27 - 175.53   656.05   0.000    1,018     1,078
    MW   176       0.34     175.25 - 176.83   511.06   0.000    1,310     1,464
    S    176       0.36     174.72 - 176.39   482.42   0.000    1,332     1,521
    W    176       0.34     174.91 - 176.46   523.88   0.000    1,255     1,373
    ----------------------------------------------------------------------------
    Here is a screenshot of the html file from my browser.


    Click image for larger version

Name:	Screenshot 2024-01-24 at 4.03.02 PM.png
Views:	1
Size:	45.3 KB
ID:	1741066

    Comment


    • #3
      Thank you very much for your reply, I more or less solved it that way:

      Code:
      asdoc, row(Country, p, t, Std.Err, CI, Mean1, Mean2, Dif, N1, N2) title(Test) replace
      foreach ccc in "DE" "IT" "HU" {
          ttest imbgeco if cntry == "`ccc'", by(dum1)
      
          * Get the relevant statistics
          loc p_value = `r(p)'
          loc t_test = `r(t)'
          loc stde = `r(se)'
          loc lvl = `r(level)'
          loc mean1 = `r(mu_1)'
          loc mean2 = `r(mu_2)'
          loc dif = `mean1' - `mean2'
          loc n_1 = `r(N_1)'
          loc n_2 = `r(N_2)'
      
          asdoc, row(`ccc', `p_value', `t_test', `stde', `lvl', `mean1', `mean2', `dif', `n_1', `n_2') dec(2)
      }

      Everything is fine, except r(level) gives me the IC level, which is 95, while I want to put the interval. Do you have any idea how I can do that?

      This is the table I got:

      Click image for larger version

Name:	Screenshot (966).png
Views:	1
Size:	12.0 KB
ID:	1741076

      Comment


      • #4
        As Jeff noted in #2, you can obtain the results from ttest using regress. So doing this will be easier than reconstructing the confidence intervals by hand.

        Code:
        webuse nhanes2, clear
        ttest height if region1, by(sex)
        regress height 1.sex if region1
        *GET CIs AND HOLD IN A LOCAL
        local CI= string(r(table)["ll", 1], "%4.3f")+ " - " + string(r(table)["ul", 1], "%4.3f")
        *DISPLAY
        di "`CI'"
        Res.:

        Code:
        . ttest height if region1, by(sex)
        
        Two-sample t test with equal variances
        ------------------------------------------------------------------------------
           Group |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
        ---------+--------------------------------------------------------------------
            Male |   1,018    174.2142    .2240082    7.147231    173.7746    174.6538
          Female |   1,078    160.5811    .2059035    6.760411    160.1771    160.9851
        ---------+--------------------------------------------------------------------
        Combined |   2,096    167.2025    .2126064    9.733565    166.7856    167.6195
        ---------+--------------------------------------------------------------------
            diff |            13.63309    .3037789                13.03735    14.22883
        ------------------------------------------------------------------------------
            diff = mean(Male) - mean(Female)                              t =  44.8783
        H0: diff = 0                                     Degrees of freedom =     2094
        
            Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
         Pr(T < t) = 1.0000         Pr(|T| > |t|) = 0.0000          Pr(T > t) = 0.0000
        
        .
        . regress height 1.sex if region1
        
              Source |       SS           df       MS      Number of obs   =     2,096
        -------------+----------------------------------   F(1, 2094)      =   2014.06
               Model |  97311.4761         1  97311.4761   Prob > F        =    0.0000
            Residual |  101173.619     2,094  48.3159594   R-squared       =    0.4903
        -------------+----------------------------------   Adj R-squared   =    0.4900
               Total |  198485.095     2,095  94.7422888   Root MSE        =     6.951
        
        ------------------------------------------------------------------------------
              height | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
        -------------+----------------------------------------------------------------
                 sex |
               Male  |   13.63309   .3037789    44.88   0.000     13.03735    14.22883
               _cons |   160.5811   .2117073   758.51   0.000     160.1659    160.9963
        ------------------------------------------------------------------------------
        
        .
        . *GET CIs AND HOLD IN A LOCAL
        
        .
        . local CI= string(r(table)["ll", 1], "%4.3f")+ " - " + string(r(table)["ul", 1], "%4.3f")
        
        .
        . *DISPLAY
        
        .
        . di "`CI'"
        13.037 - 14.229
        The modification to your code below is not tested.

        Code:
        asdoc, row(Country, p, t, Std.Err, CI, Mean1, Mean2, Dif, N1, N2) title(Test) replace
        foreach ccc in "DE" "IT" "HU" {
            ttest imbgeco if cntry == "`ccc'", by(dum1)
        
            * Get the relevant statistics
            loc p_value = `r(p)'
            loc t_test = `r(t)'
            loc stde = `r(se)'
            loc mean1 = `r(mu_1)'
            loc mean2 = `r(mu_2)'
            loc dif = `mean1' - `mean2'
            loc n_1 = `r(N_1)'
            loc n_2 = `r(N_2)'
            
            regress imbgeco 1.dum1 if cntry== "`ccc'"
            local lvl= string(r(table)["ll", 1], "%4.3f")+ " - " + string(r(table)["ul", 1], "%4.3f")
        
            asdoc, row(`ccc', `p_value', `t_test', `stde', "`lvl'", `mean1', `mean2', `dif', `n_1', `n_2') dec(2)
        }

        Comment


        • #5
          It works! Thank you very much!

          Comment

          Working...
          X