Announcement

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

  • T-Test between two varibles with dummy condition

    Hey, I woul like to know how I could do the t-test between 2 variables if I calculated the mean with an if condition (if the dummy has the value 1)
    So I did: egen meanvar1=mean(var) if dummy1==1
    egen meanvar2=mean(var) if dummy2==1
    Now I want to do know wether the difference between meanvar1 and meanvar2 is significantly different to zero.
    Hope you can help me!

  • #2
    There's something confusing here. Are dummy1 and dummy2 just two copies of the same variable? Or are they complements? Or what. It isn't clear that you are defining two mutually exclusive and exhaustive groups (which is what you need to do for a t-test). So until we clear that up, it doesn't even look like you're heading in the right direction yet.

    If you do have a grouping variable, let's call it group, that divides the observations into two groups based on different values of the variable group, then you don't have to go to the trouble of calculating your own means and standard deviations, etc. You can just run
    Code:
    ttest var, by(group)

    Comment


    • #3
      Thanks Clyde, so dummy1 gives an observation the value of 1 if it is a buy and dummy2 if it is a sell (I actually have more than 2 dummys so I think I can not do that with group because I do not only differntiate between 2 groups with 1 and 0). So I first calculate the mean of the variable for buys and sells and then I want to do a ttest to check if the difference of these two means are different to zero. But I am still not sure what to do.

      Comment


      • #4
        So, I would create a new variable, let's call it buy_sell that takes on the values 0 for buys and 1 for sells, and missing values for anything that belongs to neither of those categories. Then I would do
        Code:
        gen var buy_sell = 0 if dummy1 == 1
        replace buy_sell = 1 if dummy2 == 1
        ttest var, by(buy_sell)

        Comment


        • #5
          Thank you so far. Would it be possible to do that for 4 groups? because I split the buys into 2 different buys and the sells also in 2 different sells if a trade matches certain conditions. lets call it buy1 buy2 sell1 sell2. I want to do the ttest for the difference of buy1 and sell1, buy2 and sell 2, buy1 and buy 2, sell1 and sell2

          Comment


          • #6
            Most people will tell you that it is not appropriate to handle a four-way pairwise comparison with ttests and would recommend instead recommend doing a direct test of the four way variable, accompanied with post-hoc pairwise comparisons if you like, selecting some method of adjusting for the multiple comparisons. For example, if your plan is to do all possible pairwise contrasts, the Scheffe adjustment wold be appropriate here.

            So if you have four mutually exclusive and exhaustive categories:
            Code:
            gen group = 1 if buy1 == 1
            replace group = 2 if buy2 == 1
            replace group = 3 if sell1 == 1
            replace group = 4 if sell2 == 1
            regress var i.group
            pwcompare group, effects mcompare(scheffe)

            Comment


            • #7
              Thanks, but unfortunately I need to do the ttests here... so even if it does not make sense to you, could you please help me with the code?

              Comment


              • #8
                I would like to do sth. like: ttest var if buy1==1 == var if sell1==1 .....just to give you a hint what I want to do but this code is not correct. But this is what I want to do for the observations that match one of these 4 dummyvariables with value of 1
                Last edited by John Lei; 10 Dec 2018, 14:48.

                Comment


                • #9
                  I need to do the ttests here
                  Why? Is this a homework assignment?

                  Comment


                  • #10
                    Not quite. But this is given in what I want to do.

                    Comment


                    • #11
                      This should do it:

                      Code:
                      local dummies buy1 buy2 sell1 sell2
                      
                      forvalues i = 1/3 {
                          forvalues j = `=`i'+1'/4 {
                              local g1: word `i' of `dummies'
                              local g2: word `j' of `dummies'
                              gen byte group = 1 if `g1' == 1
                              replace group = 2 if `g2' == 1
                              display `"`g1' vs `g2'"'
                              ttest var, by(group)
                              drop group
                          }
                      }
                      Note: as no example data was provided, this code is untested. It may contain typographical errors.

                      The logic is exactly the same as before: create a two-level variable, group, that distinguishes the two groups of observations being compared in an individual t-test, and then do the t-test. That logic gets wrapped in side some loops that cycle through the various combinations of the two groups.

                      Comment


                      • #12
                        thank you, it works!

                        Comment

                        Working...
                        X