Announcement

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

  • How to multiply -1 with _n+1 value of a variable?

    I am facing a problem that seems simple, but I cannot figure it out to solve. Let us say I have the following data:
    input id date Ri
    1 1 .3
    1 2 .4
    1 3 .6
    1 4 .7
    end

    what I want is to multiply -1 with secon Ri , fourth Ri, sixth Ri and so. So, for the above data, I should get the data in following result
    input id date Ri
    1 1 .3
    1 2 -.4
    1 3 .6
    1 4 -.7
    end
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

  • #2
    replace Ri = -Ri if mod(_n, 2) == 0 will do the trick.
    Best,

    Aljar

    Comment


    • #3

      Multiplication by -1 is otherwise known as negation.

      I wouldn't overwrite the original, not least because you would mess it up if you make a mistake. Some possibilities are

      Code:
      gen new_Ri = cond(mod(_n,2), Ri, -Ri)
      or

      Code:
      gen new_Ri = cond(!mod(_n,2), -Ri, Ri)
      or

      Code:
      gen new_Ri = cond(mod(_n,2) == 0, -Ri, Ri)
      or

      Code:
      gen new_Ri = Ri
      replace new_Ri = -Ri if !mod(_n, 2)
      or

      Code:
      gen new_Ri = Ri * (mod(_n,2) - mod(_n-1,2))
      although the last is a kind of feeble joke.

      The mod() function (systematically misnamed since FORTRAN in the 1950s) yields the remainder on division. If you divide an integer by 2, there are two possible remainders, 1 if an integer is odd and 0 if it is even.

      Thought or experiment shows that with observation numbers _n (manifestly positive integers) (mod(_n,2) == 1) and mod(_n,2) both yield 1 or 0; they can also be negated logically.

      See http://www.stata-journal.com/sjpdf.h...iclenum=pr0031 To my chagrin, I forgot to mention applications to rotation problems. Thus mod(270 + 180, 360) yields a new orientation in degrees after a rotation of 180 degrees clockwise.

      Comment


      • #4
        Thanks Aljar and Nick for your replies. Nick! your comments and solutions are always like poetry.
        Regards
        --------------------------------------------------
        Attaullah Shah, PhD.
        Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
        FinTechProfessor.com
        https://asdocx.com
        Check out my asdoc program, which sends outputs to MS Word.
        For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

        Comment


        • #5
          Or

          gen new_Ri=Ri*((-1)^_n)

          Comment


          • #6
            Jeph's solution is neat. I was queasy on whether there might be a problem on e.g. powering with powers like 1e6, but every seems to work out as mathematics implies.

            Comment


            • #7
              Jeph's solution is neat.
              ...except that it changes the odd numbers, not the evens. Should have been

              gen new_RI=Ri*((-1)^(_n+1))

              Comment

              Working...
              X