Announcement

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

  • Exporting results of Ttests with Unequal variances

    I have a bunch of variables and I want to conduct a 2-group mean comparison test with unequal variances for each of the variables. As an example, I used the the quantitative variable Words, which is divided into two groups as follows: Verified = 1 i.e. true, and, Verified = 0 i.e. False.

    I want to export the Means, the diff, the t-value and the p-value (in blue font below). While the t-value and the p-value is in the stored results, the Means of each group are not stored. Neither is the diff. What is the best way to export the Means, the diff, the t-value and the p-value into an excel file all at once for each variable?

    I would be very grateful for help with the code.

    Here is my output:

    ttest Words , by( Verified ) unequal
    Two-sample t test with unequal variances
    ------------------------------------------------------------------------------
    Group | Obs Mean Std. Err. Std. Dev. [95% Conf. Interval]
    ---------+--------------------------------------------------------------------
    0_False | 1,002 23379.45 633.5608 20054.98 22136.19 24622.71
    1_True | 231 37194.2 1526.441 23199.9 34186.6 40201.79
    ---------+--------------------------------------------------------------------
    combined | 1,233 25967.61 608.3704 21362.38 24774.05 27161.17
    ---------+--------------------------------------------------------------------
    diff | -13814.75 1652.701 -17066.52 -10562.98
    ------------------------------------------------------------------------------
    diff = mean(0_False) - mean(1_True) t = -8.3589
    Ho: diff = 0 Satterthwaite's degrees of freedom = 313.931
    Ha: diff < 0 Ha: diff != 0 Ha: diff > 0
    Pr(T < t) = 0.0000 Pr(|T| > |t|) = 0.0000 Pr(T > t) = 1.0000

    . return list
    scalars:
    r(level) = 95
    r(sd) = 21362.37977652895
    r(sd_2) = 23199.89565108091
    r(sd_1) = 20054.97694223148
    r(se) = 1652.701313459743
    r(p_u) = .999999999999999
    r(p_l) = 1.04042546660e-15
    r(p) = 2.08085093319e-15
    r(t) = -8.358892749649407
    r(df_t) = 313.9306465933078
    r(mu_2) = 37194.19913419913
    r(N_2) = 231
    r(mu_1) = 23379.44610778443
    r(N_1) = 1002

    . ereturn list
    scalars:
    e(g_avg) = 6.290816326530612
    e(g_min) = 1
    e(r2_w) = .0473434965397944
    e(r2_o) = .4506131136165867
    e(r2_b) = .5607902730496568
    e(Tcon) = 0
    e(Tbar) = 6.290816326530612
    e(N) = 1233
    e(N_g) = 196
    e(rank) = 21
    e(df_r) = 175
    e(ll_0) = 31.27062242352715
    e(ll) = 111.902890145359
    e(r2_a) = .5105948756839033
    e(rss) = 3.663325593087079
    e(mss) = 4.677394951795689
    e(rmse) = .1446834099105863
    e(r2) = .5607902730496568
    e(F) = 11.17214530574199
    e(df_m) = 20
    e(p) = 4.39187568586e-22
    e(g_max) = 13

    macros:
    e(cmdline) : "xtreg Environmental Trend LnTotalAssets Intangibles MTB leverage Fir.."
    e(title) : "Between regression (regression on group means)"
    e(cmd) : "xtreg"
    e(predict) : "xtrefe_p"
    e(marginsnotok) : "E U UE SCore STDP XBU"
    e(marginstok) : "XB default"
    e(model) : "be"
    e(ivar) : "CompName"
    e(vce) : "conventional"
    e(depvar) : "Environmental"
    e(typ) : "WLS "
    e(properties) : "b V"

    matrices:
    e(b) : 1 x 25
    e(V) : 25 x 25

    functions:
    e(sample)



    I tried asdoc, and, it seems to work only with equal variances. I got the following error message when used this command: asdoc ttest Words , by( Verified ) unequal replace

    file Myfile.doc could not be opened
    fopen(): 603 file could not be opened
    asdoctable(): - function returned error
    <istmt>: - function returned error
    r(603);

  • #2
    The means are returned as r(mu_1) and r(mu_2). You can see that in your own example. The difference is not: you will have to do the subtraction.

    Comment


    • #3
      Thank you so much for pointing that out!

      Do you know if I can use outreg2? I would be grateful for help with the command.

      Comment


      • #4
        I ask because I have seen putexcel, export excel, estout, and esttab examples. I just want to know if you know what works best with ttest stored results and the subtraction. Thank you!

        Comment


        • #5
          I would save the results you are interested in in a tempfile. Then -use- the tempfile, calculate -diff-, and then -export excel-

          Comment


          • #6
            Thank you so much for that! I greatly appreciate your kindness.

            Now, I I want to conduct a 2-group median comparison test with unequal variances for each of the variables. Is there an easy way to obtain the results of that?

            Comment


            • #7
              I tried asdoc, and, it seems to work only with equal variances. I got the following error message when used this command: asdoc ttest Words , by( Verified ) unequal replace
              For what it's worth I can't replicate the error message you're receiving so it might be the case that you need to update asdoc (SSC).


              Code:
              sysuse auto.dta, clear
              
              asdoc ttest price, by(foreign) unequal replace 
              asdoc ttest weight, by(foreign) unequal rowappend
              Click image for larger version

Name:	Screen Shot 2020-12-26 at 11.42.40 AM.png
Views:	1
Size:	116.9 KB
ID:	1587415



              Comment


              • #8
                Hi Justin,

                I downloaded into my copy of Stata 16 just yesterday evening (using ssc install asdoc), after which I tried using it for my analysis. I tried again now, and, it says that its up to date:

                ssc install asdoc
                checking asdoc consistency and verifying not already installed...
                all files already exist and are up to date.

                I also tried the command again just now, and, still getting the error message:

                asdoc ttest Words , by( Verified ) unequal replace

                Two-sample t test with equal variances
                ------------------------------------------------------------------------------
                Group | Obs Mean Std. Err. Std. Dev. [95% Conf. Interval]
                ---------+--------------------------------------------------------------------
                0_False | 1,002 23379.45 633.5608 20054.98 22136.19 24622.71
                1_True | 231 37194.2 1526.441 23199.9 34186.6 40201.79
                ---------+--------------------------------------------------------------------
                combined | 1,233 25967.61 608.3704 21362.38 24774.05 27161.17
                ---------+--------------------------------------------------------------------
                diff | -13814.75 1509.28 -16775.8 -10853.71
                ------------------------------------------------------------------------------
                diff = mean(0_False) - mean(1_True) t = -9.1532
                Ho: diff = 0 degrees of freedom = 1231

                Ha: diff < 0 Ha: diff != 0 Ha: diff > 0
                Pr(T < t) = 0.0000 Pr(|T| > |t|) = 0.0000 Pr(T > t) = 1.0000

                Two-sample t test with unequal variances

                ------------------------------------------------------------------------------------------
                | obs1 obs2 Mean1 Mean2 dif St_Err
                -----------------------+------------------------------------------------------------------
                Words by Verified |
                0 1 | 1002 231 23379.45 37194.2 -13814.75 1509.281
                ------------------------------------------------------------------------------------------

                ----------------------------------------------
                | t_value p_value
                -----------------------+----------------------
                Words by Verified |
                0 1 | -9.15 0
                ----------------------------------------------
                (note: file Myfile.doc not found)
                file Myfile.doc could not be opened
                fopen(): 603 file could not be opened
                asdoctable(): - function returned error
                <istmt>: - function returned error
                r(603);

                Comment


                • #9
                  Regarding #6, I don't know what median test you are referring to. Stata's -median- command is a non-parametric test of equality of medians in two groups and it makes no use of variance at all and does not care whether the variances in the two groups are equal. Are you using some other command? If so, what is it, and where does it come from?

                  Bottom line, in any case, is that you can pluck whatever results are given in r(). Anything else that you might see in the Stata output would be something that you must calculate from what is shown in r().

                  I cannot help you with your -asdoc- difficulties as I do not have or use that command. But -asdoc- is written by Attaullah Shah , who is an active Forum participant. If you message him and call his attention to this thread, I anticipate he will respond promptly and helpfully; he always does.
                  Last edited by Clyde Schechter; 26 Dec 2020, 11:35.

                  Comment


                  • #10
                    It might be worth trying

                    Code:
                     
                      net install asdoc, from(http://fintechprofessor.com) replace  

                    Comment


                    • #11
                      Thank you, Dr. Schechter! I considered both the -median- command and the -ranksum- command. Do you think the -median- command is more appropriate? Is one superior to the other?

                      Comment


                      • #12
                        Hi again Justin,

                        I tried the command that you suggested. Still getting the same error:

                        net install asdoc, from(http://fintechprofessor.com) replace
                        checking asdoc consistency and verifying not already installed...

                        the following files will be replaced:
                        c:\ado\plus\a\asdoc.ado
                        c:\ado\plus\l\lasdoc.mlib
                        c:\ado\plus\a\asdoc.sthlp

                        installing into c:\ado\plus\...
                        installation complete.

                        . asdoc ttest Words , by( Verified ) unequal replace

                        Two-sample t test with equal variances
                        ------------------------------------------------------------------------------
                        Group | Obs Mean Std. Err. Std. Dev. [95% Conf. Interval]
                        ---------+--------------------------------------------------------------------
                        0_False | 1,002 23379.45 633.5608 20054.98 22136.19 24622.71
                        1_True | 231 37194.2 1526.441 23199.9 34186.6 40201.79
                        ---------+--------------------------------------------------------------------
                        combined | 1,233 25967.61 608.3704 21362.38 24774.05 27161.17
                        ---------+--------------------------------------------------------------------
                        diff | -13814.75 1509.28 -16775.8 -10853.71
                        ------------------------------------------------------------------------------
                        diff = mean(0_False) - mean(1_True) t = -9.1532
                        Ho: diff = 0 degrees of freedom = 1231

                        Ha: diff < 0 Ha: diff != 0 Ha: diff > 0
                        Pr(T < t) = 0.0000 Pr(|T| > |t|) = 0.0000 Pr(T > t) = 1.0000

                        Two-sample t test with unequal variances

                        ------------------------------------------------------------------------------------------
                        | obs1 obs2 Mean1 Mean2 dif St_Err
                        -----------------------+------------------------------------------------------------------
                        Words by Verified |
                        0 1 | 1002 231 23379.45 37194.2 -13814.75 1509.281
                        ------------------------------------------------------------------------------------------

                        ----------------------------------------------
                        | t_value p_value
                        -----------------------+----------------------
                        Words by Verified |
                        0 1 | -9.15 0
                        ----------------------------------------------
                        (note: file Myfile.doc not found)
                        file Myfile.doc could not be opened
                        fopen(): 603 file could not be opened
                        asdoctable(): - function returned error
                        <istmt>: - function returned error
                        r(603);

                        Comment


                        • #13
                          Regarding #11. The median test is a test of equality of medians in two groups. The ranksum test is an overall test of the equality of the distributions in the two groups. As such, -ranksum- tests a more restrictive hypothesis: you can easily have two groups with equal medians but otherwise differ greatly in their distributions. Which of these two tests is preferred depends on your specific research question and goals.

                          Comment


                          • #14
                            Thank you so much!

                            Comment


                            • #15
                              Dear Prof. Schechter,

                              Thank you again!

                              The -ranksum- command works for me. I greatly appreciate your efforts!

                              I sent a message to Dr. Shah. He might respond after the holidays.

                              Comment

                              Working...
                              X