Announcement

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

  • Finding values that are two standard deviations above the mean

    I need to find values of a variable that are two standard devotions above the mean. I want to create a signal for this variable when it crosses this threshold.

    I have been doing something like below but this doesn't work at all.

    egen memp= mean(emp)
    egen stdemp= std(emp)
    gen tstd=2*stdemp+memp
    gen sig=0
    replace sig=1 if tsdt>mean


    and using this, I never have a signal. I thing I am mising something in here.
































  • #2
    I don't think this is actually the code you were running. The final line, refers to a variable that does not exist (or if it does, it has nothing to do with what you are doing here.) Assuming it does not exist, not only will you not get a signal, you will get no results--only an error message that you tried to refer to a non-existing variable.

    Code:
    sum emp
    gen byte signal = (emp > r(mean) + 2*r(sd))

    Comment


    • #3
      Dear Arbnor, Please see whether the following code is what you want?
      Code:
      sysuse auto, clear
      egen mprice= mean(price)
      egen stdprice= std(price)
      gen tstd=2*stdprice+mprice
      gen sig = price > tstd if price < .
      PS: You don't have the `mean' variable in the last row, do you?

      Ho-Chuan (River) Huang
      Stata 17.0, MP(4)

      Comment


      • #4
        Thanks Clyde. That works perfectly but it has a small problem it creates signals when there are missing values for emp. Should I use something like; gen byte signal=(emp>r(mean)+2*(sd)) if emp!=. ?

        Comment


        • #5
          Code:
          if emp != ,
          should work fine. BTW the problem with the code in #1 -- once careless typos have been fixed -- is that the mean plus 2 SD is always greater than the mean whenever the SD is positive, which is usually -- and more fundamentally that the original values do not feature in the recipe for the new variable.
          Last edited by Nick Cox; 16 Nov 2019, 05:14.

          Comment


          • #6
            Previous code should be

            Code:
            if emp != .

            Comment

            Working...
            X