Announcement

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

  • Poisson regression with global macro

    I am using poisson regression model with global macro. It works fine, but the final outcome is a coefficient not irr. Can anyone please help?

    Please ignore what the following model is trying to calculate. This is just for reference:

    Code:
    sysuse auto, clear
    rename price population
    generate logpop = ln(population)
    encode make, generate(make1)
    poisson mpg i.foreign c.length rep78, offset(logpop) irr // here the result is in irr
    
    global model1 = "`mpg i.foreign c.length rep78, offset(logpop) irr'"
    poisson $model1 // here the result is in Coef


    Also, I just realised the code with macro does not work if the regression model is not run separately without a macro:

    The following code does not work, as I won't be running the poison model without macro
    Code:
    sysuse auto, clear
    rename price population  
    generate logpop = ln(population)
    encode make, generate(make1)  
    //poisson mpg i.foreign c.length rep78, offset(logpop) irr // here the result is in irr 
    global model1 = "`mpg i.foreign c.length rep78, offset(logpop) irr'"
    poisson $model1 // here the result is in Coef
    Last edited by bibha dhungel; 12 Feb 2022, 04:45.

  • #2
    remove the single quotes,
    Code:
    global model "mpg i.foreign c.length rep78, offset(logpop) irr"

    Comment


    • #3
      Thank you Oyvind Snilsberg for your prompt response. But it did not work after removing the single quotes.


      Additionally, I noticed that once:
      1) I run a poison model
      2) Store a global macro
      3) Run poison model with the global macro, same model would appear even when I give any random macro name

      Code:
      sysuse auto, clear rename price population  
      generate logpop = ln(population)
      encode make, generate(make1)    
      
      * Step 1
      poisson mpg i.foreign c.length rep78, offset(logpop) irr // here the result is in irr  
      
      * Step 2
      global model1 = "`mpg i.foreign c.length rep78, offset(logpop) irr'"  
      
      * Step3
      poisson $model1 // here the result is in Coef  
      
      * Step 3 with different model name
      poisson $hello // this will give the same stored model  ???? WHY????
      Last edited by bibha dhungel; 12 Feb 2022, 04:57.

      Comment


      • #4
        As implied by #2, your global

        Code:
        global model1 = "`mpg i.foreign c.length rep78, offset(logpop) irr'"
        is returned as an empty string, as inside the " " you are referring to a local macro that doesn't exist. Incidentally, the local macro name is illegal, but Stata just ignores the text after the first space. You can check this by displaying the global after you define it. Thus

        Code:
        poisson $model1
        is just the same as
        Code:
        poisson 
        which shows you the last model estimated, if one is remembered, with default choices,, The solution is simple. The single quotes ` ' are not needed and indeed harmful to your purpose.
        Code:
        global model1  "mpg i.foreign c.length rep78, offset(logpop) irr"
        is what you seek. I would use a local macro for that purpose, but that is style not syntax.

        I can't follow #3 because you say you removed the single quotes, but they are still there in your code.
        Last edited by Nick Cox; 12 Feb 2022, 05:09.

        Comment


        • #5
          Thank you Nick Cox for your explanation. I understood now where I went wrong. My actual codes were slightly different

          Code:
          global model1 "mpg c.length rep78 if foreign == `x', offset(logpop) irr"
          foreach x of numlist 1/2 {
          poisson $model1
          }
          As the value of the macro is stored as string 'irr' did not work and also "if foreign == `x' " did not work.

          I now changed the codes as follows:

          Code:
          global model1 "mpg c.length rep78"
          foreach x of numlist 1/2 {
          poisson $model1 if foreign == `x', offset(logpop) irr
          }
          This seems to give me satisfactory result. I will update again if I come up with any other issue.

          I am using global macro and not local macro as my poisson model is complex, and I have codes to store result from different lincom commands as putexcel. For this reason, I am running the poisson command each time followed by lincom command, which is in total about 7-8 chunks.
          Last edited by bibha dhungel; 12 Feb 2022, 07:21.

          Comment


          • #6
            Good to learn that you have made progress, but I don’t follow this either. Your global definition used earlier would have led to an illegal poisson command unless the local macro x was defined previously.

            Also, what you describe does not seem to imply that you need to use globals. The advice not to use globals routinely arises from bitter experience, that sooner or later that habit will bite you badly. The problem will be that your definition clashes with another, and finding that other definition can be hard.
            Last edited by Nick Cox; 12 Feb 2022, 08:03.

            Comment


            • #7
              I assumed this would work as `x' would take the value of 1 when running the poison model
              Code:
              global model1 "mpg c.length rep78 if foreign == `x', offset(logpop) irr"
              foreach x of numlist 1/2 {
              poisson $model1
              }
              but that would not be the case. So it did not work. However, if the global macro command is kep inside the loop it would work


              Code:
              foreach x of numlist 1/2 {
              global model1 "mpg c.length rep78 if foreign == `x', offset(logpop) irr"
              poisson $model1
              }

              But this will not serve me any good as I have 7-8 chunks of foreach loops and keeping the global macro inside each loop means I need to change all 7 macros each time I change the variables in my model.

              So I will stick to:

              Code:
              global model1 "mpg c.length rep78"
              foreach x of numlist 1/2 {
              poisson $model1 if foreign == `x', offset(logpop) irr
              }
              I use local macros and have started using global macros very recently as I make different tables such as 1) RxC table 2) Crude odds ratio 3) Adjusted odds ratio all of which would require the same set of variables. But using a local macro would mean I need to change the list of variables in 3 or more places everytime I decide to drop or add a new variable. I appreciate your advice given your expertise and long experience in this field. I will try to avoid using global macros to the extent possible.



              Just for your reference, my current codes using actual data look something like this
              Code:
              global model1 = "disease i.race##c.year##i.method"
              global race = "White Black Green Red Blue"
              global method = "Apple Ball Cat Dog Elephant Frog"
              global age = "10-19 20-39 40-59 60-79 80+"
              
              
              * Chunk 1
              foreach y of numlist 1/2 {
                  local row = 1
                  poisson $model1 if sex == `y' , offset(popln) irr
                  putexcel set "data_by_me/table/table2.xlsx", modify sheet("sex`y'", replace)
                  putexcel   B`row'  = ("RR")   C`row'  = ("Lower CI")     D`row'  = ("Upper CI")
                  
                  local row = 2
                  foreach x of global race {
                      putexcel   A`row'  = ("`x'")
                      local      row = `row' + 1
                  }
                  
                  local row = 2
                  foreach x of numlist 1/5 {
                      lincom c.year + c.year#`x'.race, irr
                      putexcel B`row' = (r(estimate)) C`row'  = (r(lb)) D`row' = (r(ub))
                      local row = `row' + 1    
              }
              }
              
              * Chunk 2
              foreach y of numlist 1/2 {
                  local row = 1
                  putexcel set "data_by_me/table/table4_1.xlsx", modify sheet("sex`y'", replace)
                  putexcel   B`row'  = ("RR")   C`row'  = ("Lower CI")     D`row'  = ("Upper CI")
              
                  foreach x of global race {
                      putexcel   A`row'  = ("`x'")
                      local      row = `row' + 1
                      
                      foreach z of global method {
                          putexcel   A`row'  = ("`z'")
                          local      row = `row' + 1
                      }
                  }
                  
                  local row = 2
                  poisson $model1 if sex == `y' , offset(popln) irr
                  foreach r of numlist 1/5 {
                      foreach m of numlist 1/6 {
                      lincom c.year + c.year#`r'.race + c.year#`m'.method + c.year#`r'.race#`m'.method, irr // white-gun
              
                      putexcel B`row' = (r(estimate)) C`row'  = (r(lb)) D`row' = (r(ub))
                      local row = `row' + 1    
              }
                      local row = `row' + 1    
              }
              }
              I might be able to get everything under one loop, but that would mean a never ending loop inside loop inside loop.
              Last edited by bibha dhungel; 12 Feb 2022, 23:31.

              Comment

              Working...
              X