Announcement

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

  • How to make sure all site have both treatments?

    Hello

    I'm simulating for a multicentre trial, and I got problems for generating site and trt to make sure all my sites have both trt==1 and trt==0.
    Here's my original code (which is nested within loop), but it failed to achieve that.

    May I ask how to fix it?

    set obs `n'
    gen index = _n
    gen site = 1 + int(runiform(0,`cluster'))
    egen trt = seq(), from(0) to(1)
    sort site
    gen u0 = rnormal(0,`u0')


    by site: replace u0 = u0[1]
    gen y = u0 + 0.4 * trt + rnormal()

  • #2
    The local macros n, u0, and cluster referenced in your code are not defined in what you show. You also do not set your random number seed, so your results will not be reproducible. It also appears that you are giving the sites random identifying numbers. While not illegal, it is strange to do, and can result in multiple sites having the same number. Let's say you intend to have 100 sites, each receiving both trt = 0 and trt = 1. I would do this as:
    Code:
    clear*
    local n_sites 100
    local u0 1.6 // OR WHATEVER IT IS SUPPOSED TO BE
    set seed 12345 // OR WHATEVER YOU LIKE
    set obs `n_sites'
    gen site = _n
    expand 2
    by site, sort: gen trt = _n-1
    gen u0 = rnormal(0, `u0')
    by site, sort: replace u0 = u0[1]
    gen y = u0 + 0.4*trt + rnormal()
    I've tried in the above to interpret your code and infer what you are trying to get. I may have misunderstood. So if this doesn't produce something like what you want, please post back with a clearer explanation.

    Comment


    • #3
      If you're trying to do DID, then you need some sites that are not treated.

      Code:
      clear
      set obs 100
      set seed 123456
      g site = _n
      g u0 = rnormal(0,1)
      g trtx = runiform()
      expand 2
      sort site
      bys site: g period = _n
      g treated = trtx>0.5
      g post = period==2
      g y = u0 + 1.0*treated*post + rnormal()
      
      reghdfe y c.treated#c.post , absorb(site period)
      reg y c.treated#c.post post treated

      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        The local macros n, u0, and cluster referenced in your code are not defined in what you show. You also do not set your random number seed, so your results will not be reproducible. It also appears that you are giving the sites random identifying numbers. While not illegal, it is strange to do, and can result in multiple sites having the same number. Let's say you intend to have 100 sites, each receiving both trt = 0 and trt = 1. I would do this as:
        Code:
        clear*
        local n_sites 100
        local u0 1.6 // OR WHATEVER IT IS SUPPOSED TO BE
        set seed 12345 // OR WHATEVER YOU LIKE
        set obs `n_sites'
        gen site = _n
        expand 2
        by site, sort: gen trt = _n-1
        gen u0 = rnormal(0, `u0')
        by site, sort: replace u0 = u0[1]
        gen y = u0 + 0.4*trt + rnormal()
        I've tried in the above to interpret your code and infer what you are trying to get. I may have misunderstood. So if this doesn't produce something like what you want, please post back with a clearer explanation.
        Thank you for your post and sorry that I didn't explain clearly .

        Here's some additional information about my original code :

        local nSims 2
        local n 500

        *factors to vary
        local u0list 0 0.1 // for icc
        local clusterlist 10 50 // number of clusters

        frame create data n u0 cluster
        foreach u0 of local u0list{
        foreach cluster of local clusterlist{
        forvalues i = 1/`nSims'{

        set obs `n'
        gen index = _n
        egen trt = seq(), from(0) to(1)
        gen site = 1 + int(runiform(0,`cluster'))

        gen u0 = rnormal(0,`u0')
        sort site
        by site: replace u0 = u0[1]
        gen y = u0 + 0.4 * trt + rnormal()

        frame post data (`n`) (u0) (`cluster`)
        }
        }
        }

        I have a clusterlist and I would like to generate all sites with equal size or with vary size but more important thing is each cluster (centre) should have both trt ==1 and trt==0.

        I don't know how to fix my code to achieve this. When my code generating for the clusters, it is usually that some cluster only have trt==1 or trt==0, not include both.

        I hope it's clear now. Thank you very much!

        Comment


        • #5
          OK, I think this does what you want:
          Code:
          clear*
          local n_sites 500
          local u0_list 0 1
          local cluster_list 10 50
          
          set seed 12345 // OR WHATEVER YOU LIKE
          
          foreach c of local cluster_list {
              foreach u of local u0_list {
                  clear
                  local u0 = `u'/10
                  set obs `n_sites'
                  gen site = _n
                  expand `c'
                  by site, sort: gen unit = _n
                  gen byte trt = mod(unit, 2)
                  gen u0 = rnormal(0, `u0')
                  by site, sort: replace u0 = u0[1]
                  gen y = u0 + 0.4*trt + rnormal()
                  frame put _all, into(data_`c'_`u')
              }
          }
          At the end of this, in addition to the default frame, you will have four other frames: data_c_u, where c = 10 or 50 denoting the cluster size, and u = 0 or 1 standing for u0 = 0, or 0.1. You can't have a decimal point in the name of a frame. This fact also accounts for why I set up u0_list to contain 0 and 1 rather than 0 and .1. I then calculate u0 inside the loop by dividing by 10.

          I have resolved the problem of assuring that there are both trt = 0 and trt = 1 in each cluster by mandating that exactly half of them be 0 and half 1. If you are designing a randomized controlled trial and trying to simulate potential results, this is a reasonable starting point--many RCTs randomize with 1:1 balance. While this usually doesn't result in an exact 1:1 distribution, this is close enough to reality for planning purposes. If you want some other allocation balance, you can do that by replacing -gen byte trt = mod(unit, 2)- with some other similarly deterministic way of doing it. For example if you want 3 trt = 1 for each 1 trt = 0 you could do -gen byte trt = (mod(unit, 2) > 0).

          If what you are trying to simulate is not a randomized trial but observational data, then, in the real world, there is no way you can be sure that you wouldn't end up with clusters that are all trt = 0 or all trt = 1, so I wouldn't try to simulate it that way. I would use random numbers to assign trt status and accept the fact that sometimes a cluster is all one way.

          Comment


          • #6
            Originally posted by Clyde Schechter View Post
            OK, I think this does what you want:
            Code:
            clear*
            local n_sites 500
            local u0_list 0 1
            local cluster_list 10 50
            
            set seed 12345 // OR WHATEVER YOU LIKE
            
            foreach c of local cluster_list {
            foreach u of local u0_list {
            clear
            local u0 = `u'/10
            set obs `n_sites'
            gen site = _n
            expand `c'
            by site, sort: gen unit = _n
            gen byte trt = mod(unit, 2)
            gen u0 = rnormal(0, `u0')
            by site, sort: replace u0 = u0[1]
            gen y = u0 + 0.4*trt + rnormal()
            frame put _all, into(data_`c'_`u')
            }
            }
            At the end of this, in addition to the default frame, you will have four other frames: data_c_u, where c = 10 or 50 denoting the cluster size, and u = 0 or 1 standing for u0 = 0, or 0.1. You can't have a decimal point in the name of a frame. This fact also accounts for why I set up u0_list to contain 0 and 1 rather than 0 and .1. I then calculate u0 inside the loop by dividing by 10.

            I have resolved the problem of assuring that there are both trt = 0 and trt = 1 in each cluster by mandating that exactly half of them be 0 and half 1. If you are designing a randomized controlled trial and trying to simulate potential results, this is a reasonable starting point--many RCTs randomize with 1:1 balance. While this usually doesn't result in an exact 1:1 distribution, this is close enough to reality for planning purposes. If you want some other allocation balance, you can do that by replacing -gen byte trt = mod(unit, 2)- with some other similarly deterministic way of doing it. For example if you want 3 trt = 1 for each 1 trt = 0 you could do -gen byte trt = (mod(unit, 2) > 0).

            If what you are trying to simulate is not a randomized trial but observational data, then, in the real world, there is no way you can be sure that you wouldn't end up with clusters that are all trt = 0 or all trt = 1, so I wouldn't try to simulate it that way. I would use random numbers to assign trt status and accept the fact that sometimes a cluster is all one way.
            That inspires me, thank you!

            Comment

            Working...
            X