Announcement

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

  • Forval and In not working together.

    I'm using Stata 14

    I'm trying to put the ORs from a regression into successive rows of the variable test1 and test2. I have 1500 observations in my data set. I will eventually want to change the number of iterations from 5 to a large number.
    The correct values end up in the test1 and test2 from the matrix log_tbl but they do not end up in the 1st 5 rows of test1 and test2. They do show up on the same row but the rows seems to be randomly selected. If I run the for loop again, the OR end up on different rows.
    The disp shows up as 1,2,3,4,5 as expected.

    In the second for loop below, the values in the 1st 5 rows are 1 to 5 just as I'd expect.
    And if I enter replace test1 = log_tbl[1,1] in 1 on the command line, it shows up in the first line.

    Is this a known bug, am I missing something obvious?
    Thanks

    Code:
    gen test1 = .
    gen test2 = .
    
    forval w = 1/5 {    
        bsample 100 if strats == 2 & sex == 0, strata(strats) weight(Qwts)
        quiet replace Qwts = 1 if strats == 0 
        quiet logit outcome i.exposure c.age28 c.age29 i.urban_rural ///
            if sex == 0 & (Q_posneg == 0 | Q_posneg == .) ///
            [pweight=Qwts], iterate(50) vce(robust) or 
        matrix logist_tbl = r(table)' 
        * grabs 2nd and 3rd OR, won't need 3rd row for diab. 
        matselrc logist_tbl log_tbl, r(2,3) c(1)  
        quiet replace test1 = log_tbl[1,1] in `w'
        quiet replace test2 = log_tbl[2,1] in `w'    
        disp `w'    
    }
    
    forval w = 1/5 {    
        replace test1 = `w' in `w'
        replace test2 = `w' in `w'
    }

  • #2
    It seems like the bsample may resort the data. Adding a sort right before the replace, fixed the problem.

    Comment


    • #3
      I'm not certain that sorting is the solution to your problem, but then, I'm no expert on bsample.

      My concern arises reading in the output of help bsample

      bsample replaces the data in memory with a bootstrap sample (random sample with replacement) drawn from the current dataset.
      That suggests to me that after your first pass through the loop, the data in memory will then consist of the 100 observations chosen by the first bsample, which are then used as input to the second bsample, and so forth.
      Code:
      . clear
      
      . set obs 100
      number of observations (_N) was 0, now 100
      
      . generate id = _n
      
      . set seed 666
      
      . forvalues w = 1/9 {
        2.     bsample 25
        3.         display "sample `w': " _continue
        4.         levelsof id
        5.         }
      sample 1: 1 8 10 18 19 21 24 28 41 43 45 50 54 55 56 58 65 69 76 84 85 96 97
      sample 2: 1 10 18 21 24 28 45 55 58 65 69 76 85 97
      sample 3: 1 10 21 24 28 45 55 58 76 85
      sample 4: 1 10 24 28 45 76 85
      sample 5: 1 10 24 28 45 76 85
      sample 6: 1 10 24 28 45 76 85
      sample 7: 1 10 24 28 45 76 85
      sample 8: 1 28 45 76
      sample 9: 1 28 45 76
      Added in edit: The postfile command was designed for storing bootstrap results; you may find it solves the problem described in post #1. With that in hand, a preserve at the top of your loop and a restore at the bottom will cause each sample to be drawn from the full dataset.
      Last edited by William Lisowski; 15 Mar 2019, 18:50.

      Comment


      • #4
        If the option weight is used in bsample the variable designated, in my case Qwts, is overwritten with the number of times an observation is randomly selected for sampling (0 to maybe 4 or 5). Using bsample without the option of weight does drop the other samples. By using the option weight removes the need to preserve and restore the data. And bsample does seem to resort the data so by using the sort right before writing to a cell, it works great.

        Comment


        • #5
          Thank you for the further information.

          Comment

          Working...
          X