Announcement

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

  • are there a quicker way to generate a variable?

    This thread continues from the below previous thread:

    https://www.statalist.org/forums/for...pping-interval


    I'd like to create a lower and upper bound for a variable (lambda).

    I first compute lambda in Excel by the formula: lambda = a^(1-r)
    The results are in Table 1.
    And then I create the interval of lambda for each "numbers of accepted options" in Table 2 in Excel, too.

    Table 1:
    Hypothetical options a r=2.91 r=1.96 r=0.66 r=0.31
    1 3 0.12 0.35 1.45 2.13
    2 2 0.27 0.51 1.27 1.61
    3 1.5 0.46 0.68 1.15 1.32
    4 1.2 0.71 0.84 1.06 1.13
    5 1 1.00 1.00 1.00 1.00
    6 0.86 1.34 1.16 0.95 0.90

    Table 2: For each h_safeoption we have one equivalent of r
    h_safeoption
    3 2 1 0
    h_accepted r = 2.91 r = 1.96 r = 0.66 r = 0.31
    0 λ ≤ 0.12 λ ≤ 0.35 λ ≥ 1.45 λ ≥ 2.13
    1 0.12 < λ ≤ 0.27 0.35 < λ ≤ 0.51 1.27 ≤ λ < 1.45 1.61 ≤ λ < 2.13
    2 0.27 < λ ≤ 0.46 0.51 < λ ≤ 0.68 1.15 ≤ λ < 1.27 1.32 ≤ λ < 1.61
    3 0.46 < λ ≤ 0.71 0.68 < λ ≤ 0.84 1.06 ≤ λ < 1.15 1.13 ≤ λ < 1.32
    4 0.71 < λ ≤ 1 0.84 < λ ≤ 1 1 ≤ λ < 1.06 1 ≤ λ < 1.13
    5 1 < λ ≤ 1.34 1 < λ ≤ 1.16 0.95 ≤ λ < 1 0.90 ≤ λ < 1
    6 λ > 1.34 λ > 1.16 λ < 0.95 λ < 0.90

    Then I generate these lambda1h (min) and lambda2h (max) in Stata by the following command and repeat for lambda2h

    gen lambda1h=0.12 if h_safeoption==3&h_accepted==1
    replace lambda1h=0.27 if h_safeoption==3&h_accepted==2
    replace lambda1h=0.46 if h_safeoption==3&h_accepted==3
    replace lambda1h=0.71 if h_safeoption==3&h_accepted==4
    replace lambda1h=1 if h_safeoption==3&h_accepted==5
    replace lambda1h=1.34 if h_safeoption==3&h_accepted==6

    replace lambda1h=0.35 if h_safeoption==2&h_accepted==1
    replace lambda1h=0.51 if h_safeoption==2&h_accepted==2
    replace lambda1h=0.68 if h_safeoption==2&h_accepted==3
    replace lambda1h=0.84 if h_safeoption==2&h_accepted==4
    replace lambda1h=1 if h_safeoption==2&h_accepted==5
    replace lambda1h=1.16 if h_safeoption==2&h_accepted==6

    replace lambda1h=1.45 if h_safeoption==1&h_accepted==0
    replace lambda1h=1.27 if h_safeoption==1&h_accepted==1
    replace lambda1h=1.15 if h_safeoption==1&h_accepted==2
    replace lambda1h=1.06 if h_safeoption==1&h_accepted==3
    replace lambda1h=1 if h_safeoption==1&h_accepted==4
    replace lambda1h=0.95 if h_safeoption==1&h_accepted==5

    replace lambda1h=2.13 if h_safeoption==0&h_accepted==0
    replace lambda1h=1.61 if h_safeoption==0&h_accepted==1
    replace lambda1h=1.32 if h_safeoption==0&h_accepted==2
    replace lambda1h=1.13 if h_safeoption==0&h_accepted==3
    replace lambda1h=1 if h_safeoption==0&h_accepted==4
    replace lambda1h=0.9 if h_safeoption==0&h_accepted==5

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(h_accepted h_safeoption)
    0 3
    0 3
    0 3
    5 1
    6 4
    4 4
    0 1
    4 1
    3 1
    4 0
    6 0
    6 0
    2 2
    2 0
    3 0
    6 0
    3 4
    0 3
    2 0
    0 4
    0 4
    0 3
    0 4
    0 1
    0 4
    0 1
    0 4
    4 0
    0 0
    0 4
    6 0
    3 0
    2 0
    1 0
    0 3
    0 0
    5 4
    6 3
    6 4
    1 3
    1 3
    0 3
    4 0
    0 0
    0 3
    0 1
    6 0
    6 2
    2 0
    0 3
    3 4
    5 0
    1 2
    0 4
    1 3
    2 1
    2 3
    3 0
    4 1
    0 3
    0 0
    2 0
    5 0
    0 4
    0 4
    5 1
    0 4
    2 0
    0 4
    6 0
    6 4
    3 1
    0 4
    4 0
    6 0
    3 4
    1 3
    0 0
    4 0
    0 1
    0 4
    5 2
    1 1
    4 2
    4 0
    1 0
    4 3
    6 0
    2 0
    6 0
    4 1
    6 0
    6 0
    6 0
    3 0
    6 0
    2 0
    6 1
    0 4
    6 4
    end
    ------------------ copy up to and including the previous line ------------------


    Are there any faster way to create lambda1h and lambda2h?

    Thank you,








  • #2
    I don't pretend to understand exactly what you're doing, but the obvious way to make this more efficient would be to do all of your calculations in Stata. Then you would not need to do the bunch of if then statements.

    Comment


    • #3
      I agree with Phil that there doesn't seem to be enough information to understand what you are doing.

      The code below creates a dataset that has variables for the low and high boundaries of lambda. In other words, the interval for lambda is captured by a combination of two variables. I suspect that this isn't not was you want though.
      Code:
      clear
      
      capture mat drop l
      local safe = 3
      foreach r in 2.91 1.96 .66 .31 {
          local accepted = 0
          foreach a in 0 3 2 1.5 1.2 1 .86 {
              mat l = nullmat(l)\ `safe',`accepted',`a',`r',`a'^(1-`r')
              local ++accepted
          }
          local --safe
      }
      
      svmat l
      rename (l*)(safeoption accepted a r l_lo)
      replace l_lo=0 if l_lo==.
      
      bysort safeoption (accepted): gen l_hi = l_lo[_n+1]
      
      gsort -safeoption accepted
      
      replace l_hi = 10 if l_hi==.
      
      list

      Comment

      Working...
      X