Announcement

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

  • Creating a reproducible example for Statalist

    I'm using Stata 17 on a Mac. I have a more substantive question on mapping, but first I have a quick question on how to best create a reproducible example for Statalist. I'm trying to create a binary variable "pnc" (received postnatal care, yes/no) with 0s and 1s that varies randomly across individuals. I want the county-level average percentages of mothers who received postnatal care to vary for my example because this is the variable I am mapping. Below is the code I used so far. I'm sure there is a more concise way to create the random binary variable pnc and this code gets even more clumsy if I need more than 10 observations. For a previous example, I used something like gen pnc = cond(mod(_n, 2), 0,1), but I don't want the pattern to be regular for this. Would be grateful if you could point me in the right direction to help with creating reproducible examples for Statalist posts!

    Code:
     clear
    set obs 10
    gen id = _n
    gen pnc = 1 if id==1
    replace pnc = 1 if id==2
    replace pnc = 1 if id==3
    replace pnc = 0 if id==4
    replace pnc = 1 if id==5
    replace pnc = 0 if id==6
    replace pnc = 0 if id==7
    replace pnc = 0 if id==8
    replace pnc = 1 if id==9
    replace pnc = 0 if id==10
    gen county = ""
    replace county = "Kilifi" in 1/2  
    replace county = "Meru" in 3/5  
    replace county = "Nyeri" in 5/10  
    
    lab define pnc 0 "No" 1 "Yes"
    lab values pnc pnc
    Last edited by Valerie Scott; 01 Apr 2023, 16:00.

  • #2
    Code:
    set seed 1234 // OR WHATEVER RANDOM NUMBER SEED YOU LIKE
    gen pnc = runiformint(0, 1)
    This will give you 0's and 1's randomly with 50% probability. If you want a different probability, say, .75 1's, then you can do it as
    Code:
    gen byte pnc = (runiform() < 0.75)
    Last edited by Clyde Schechter; 01 Apr 2023, 17:20.

    Comment


    • #3
      Clyde Schechter Thank you so much, this is exactly what I was looking for!

      Comment

      Working...
      X