Announcement

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

  • creating a scale with round numbers

    Hey everyone,

    I'm creating an attitude scale concerning support of Lesbians' and Gays' rights with three variable a b c, coded from 1 to 4, all in the same direction.
    I'm using the command:
    alpha a b c, item gen(LGtotal)
    when i tab LGtotal, i obtain values like 1.66667 or 1.33333
    I'd like to have only round number.
    In another scale, I have coded 1=1 1.5=2 2=3 etc., so I've tried to do the same here, but it doesn't work and the values with more than one figure after the comma stays even though I mentioned them in the command:
    recode LGtotal (1=1) (1.333333 1.5 1.666667=2) (2=3) (2.333333 2.5 2.666667=4) (3=5) (3.333333 3.666667 3.5=6) (4=7) (4.333333 4.5 4.666667=8) (5=9), gen (LGrights)


    Do you have any idea, why this doesn't work / what I could do to obtain round number by creating my attitude scale?

    Thanks a lot,
    Juliette

  • #2
    if you want to round LGtotal to the nearest integer try,
    Code:
    replace LGtotal = round(LGtotal)

    Comment


    • #3
      The problem is likely to be precision. Stata works in binary and decimal values like 1.333333 and 1.666667 have no exact binary equivalent.

      I dislike recode although it is also a favourite command of many. I gather that it was written to emulate a procedure outside Stata that is widely familiar. One reason I dislike recode is that it can just lead to long-winded code for all sorts of separate cases that is hard to write and hard to read.

      Here your rule is systematic and be expressed as

      if input code is an integer. double it and subtract 1

      otherwise, round down and double it


      Hence that leads to

      Code:
      clear 
      input foo 
      1 
      1.333333 
      1.5 
      1.666667 
      2
      2.333333
      2.5 
      2.666667 
      3
      3.333333
      3.5
      3.666667 
      4 
      4.333333
      4.5 
      4.666667 
      5
      end
      
      gen bar = cond(floor(foo) == foo, 2 * foo - 1, 2 * floor(foo)) 
      
      list, sepby(bar)
      
      
           +----------------+
           |      foo   bar |
           |----------------|
        1. |        1     1 |
           |----------------|
        2. | 1.333333     2 |
        3. |      1.5     2 |
        4. | 1.666667     2 |
           |----------------|
        5. |        2     3 |
           |----------------|
        6. | 2.333333     4 |
        7. |      2.5     4 |
        8. | 2.666667     4 |
           |----------------|
        9. |        3     5 |
           |----------------|
       10. | 3.333333     6 |
       11. |      3.5     6 |
       12. | 3.666667     6 |
           |----------------|
       13. |        4     7 |
           |----------------|
       14. | 4.333333     8 |
       15. |      4.5     8 |
       16. | 4.666667     8 |
           |----------------|
       17. |        5     9 |
           +----------------+
      Here floor() rounds down to the nearest integer -- look down at the floor -- so that floor(foo) == foo selects integers.


      More discussion at

      https://www.stata-journal.com/articl...article=dm0002

      https://www.stata-journal.com/articl...article=dm0058

      https://www.stata-journal.com/articl...article=dm0095

      Comment


      • #4
        A better formulation is

        Code:
        ceil(round(2 * foo)) - 1

        Comment

        Working...
        X