Announcement

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

  • looping over all pairwise combinations of a variable in an inlist() for use in a command

    Hi All,

    I am getting stuck on my thinking of how to loop over all pairwise combinations of levels of a variable to be used in an inlist(). More specifically, assume we have 7 doses of a drug and we'd like to run pairwise comparisons of the dosages. The code below is what I started with, but the obvious problem is that this is not limited to only pairwise comparisons, and it will rerun pairs twice (e.g. 0/10, and then again 10/0).

    Any help will be appreciated!

    Ariel


    Code:
    forvalues i = 0 10 20 60 150 300 400 {
        forvalues j = 0 10 20 60 150 300 400 {
            regress y x if inlist(dose, `i', `j')
        }
    }

  • #2
    Code:
    local values 0 10 20 60 150 300 400
    local n_values: word count `values'
    
    forvalues i = 1/`n_values' {
        forvalues j = `=`i'+1'/`n_values' {
            regress y x if inlist(dose, `:word `i' of `values'', `:word `j' of `values'')
        }
    }

    Comment


    • #3
      Brilliant and simple! Thank you, Clyde!

      Comment


      • #4
        I think this technique will also produce the desired results.
        Code:
        local values 0 10 20 60 150 300 400
        foreach i of local values {
            foreach j of local values {
                if `j'>`i' regress y x if inlist(dose, `i', `j')
            }
        }
        which runs the following regressions.
        Code:
        regress y x if inlist(dose, 0, 10)
        regress y x if inlist(dose, 0, 20)
        regress y x if inlist(dose, 0, 60)
        regress y x if inlist(dose, 0, 150)
        regress y x if inlist(dose, 0, 300)
        regress y x if inlist(dose, 0, 400)
        regress y x if inlist(dose, 10, 20)
        regress y x if inlist(dose, 10, 60)
        regress y x if inlist(dose, 10, 150)
        regress y x if inlist(dose, 10, 300)
        regress y x if inlist(dose, 10, 400)
        regress y x if inlist(dose, 20, 60)
        regress y x if inlist(dose, 20, 150)
        regress y x if inlist(dose, 20, 300)
        regress y x if inlist(dose, 20, 400)
        regress y x if inlist(dose, 60, 150)
        regress y x if inlist(dose, 60, 300)
        regress y x if inlist(dose, 60, 400)
        regress y x if inlist(dose, 150, 300)
        regress y x if inlist(dose, 150, 400)
        regress y x if inlist(dose, 300, 400)

        Comment


        • #5
          ahhh, this is what I was imagining when I wrote my code!

          Thank you, William!

          Ariel

          Comment

          Working...
          X