Announcement

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

  • ttest code multiple variables that define group

    Hello.
    I cant seem to work out the coding that replicates the following function, which when copied from the 'command copied from clipboard', looks like:
    'time_ann_dif PriceSensitive, sort : ttest Volume, by(Pre_or_Post_Colo) unequal'. Does someone know how this can be done?

    Essentially I want to run a ttest but with multiple variable, then save the output using putexcel. I know how to use putecel but cant get the code working for multiple variables.
    As an FYI time_ann_dif has values that range from -30 to 30, while PriceSensitive has values that are TRUE or FALSE.

    Thanks
    Last edited by Imtiaz Bhayat; 12 Aug 2021, 05:07. Reason: ttest

  • #2
    Imtiaz:
    welcome to this forum.
    As far as the first part of your query is concerned, do you mean something along the following lines?
    Code:
    . sysuse auto, clear
    . egen wanted=group(foreign rep78) if rep78==3
    
    . ttest price, unequal by(wanted)
    
    Two-sample t test with unequal variances
    ------------------------------------------------------------------------------
       Group |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
    ---------+--------------------------------------------------------------------
           1 |      27    6607.074    704.6111    3661.267    5158.725    8055.423
           2 |       3    4828.667    742.2489    1285.613    1635.027    8022.306
    ---------+--------------------------------------------------------------------
    combined |      30    6429.233    643.5995     3525.14    5112.924    7745.542
    ---------+--------------------------------------------------------------------
        diff |            1778.407    1023.431               -655.8428    4212.658
    ------------------------------------------------------------------------------
        diff = mean(1) - mean(2)                                      t =   1.7377
    Ho: diff = 0                     Satterthwaite's degrees of freedom =  6.80377
    
        Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
     Pr(T < t) = 0.9365         Pr(|T| > |t|) = 0.1271          Pr(T > t) = 0.0635
    
    .
    In addition, the code you copied and pasted should probably be:
    Code:
    by time_ann_dif PriceSensitive, sort : ttest Volume, by(Pre_or_Post_Colo) unequal'
    Last edited by Carlo Lazzaro; 12 Aug 2021, 07:01.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Hi Carlo,

      Thanks for your reply.
      Essentially I would like to perform multiple ttests and capture the p values to excel. The code below works however I now want to add an element to the test where for each 'i', I would like to further stratify the data by adding an additional criteria which is PriceSensitive (True and False).

      So in essence the test below runs 61 ttest (-30 to 30), I now want to double that with the inclusion of the additional PriceSensitive component (True and False).

      When I use the Statistics > Summaries, Tables, Tests > Classic tests of hypothesis > t test (mean comparison test)....I can do the test I need which represents the code you noted ie
      by time_ann_dif PriceSensitive, sort : ttest Volume, by(Pre_or_Post_Colo) unequal'

      I now don't know how to add the PriceSensitive component to the code below so that it replaces the task performed when using the dropdowns.

      ------------------------------------------------------------------------------------------------------
      **ttest Volume, by (Pre_or_Post_Colo), by min unequal
      putexcel set VolumeTtest
      forvalues i = -30(1)30 {
      ttest Volume if time_ann_dif == `i', by(Pre_or_Post_Colo) unequal
      local counter1 = `i' + 31
      putexcel A`counter1' = `r(t)'
      putexcel B`counter1' = `r(p)'
      }
      encode
      Last edited by Imtiaz Bhayat; 12 Aug 2021, 07:35.

      Comment


      • #4
        Hi again Carlo,

        It took me a while to absorb your suggestion and understand the grouping concept.

        When I try and create a new var I get a type mismatch r109 message. Is it because the time_ann_diff data type is a double and PriceSensitive is a type long?

        Comment


        • #5
          Imtiaz:
          It's difficult to say wthout knowing your data and codes.
          Anyway, the definition of -error109- is the following one (source: -error- entry, Stata .pdf manual)
          109. type mismatch

          In an expression, you attempted to combine a string and numeric subexpression in a logically impossible way. For

          instance, you attempted to subtract a string from a number or you attempted to take the substring of a number.
          Kind regards,
          Carlo
          (Stata 19.0)

          Comment


          • #6
            Thanks Carlo. Your egen suggestion works up to the point of the if statement, in my code.

            When I add the following if PriceSensitive == FALSE or even the code if PriceSensitive == 2 that's when the 109 error appears.

            Data types: the time_ann_diff data type is a double (integer) and PriceSensitive is a type long (visibly written as true or false but I can see that the system reflects this as 1 and 2).

            I apologise in that its probably a really basic question but I am literally 2 weeks into my Stata journey!

            Comment


            • #7
              Imitiaz:
              it would be really useful if you could share what you typed and what Stata gave you back, along with an excerpt of your dataset via -dataex- (as per FAQ). Thanks.
              In addition, I fail to get how yoo coded -FALSE- in your -if- qualifier clause.
              Kind regards,
              Carlo
              (Stata 19.0)

              Comment


              • #8
                Carlo - I really appreciated your advice.
                It all worked fine

                Comment

                Working...
                X