Announcement

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

  • Storing odds ratio and p-value in logit and ologit from multiple variables

    Hi everyone! I have 10 dependent variables (y1-y10) and 4 independent variables (x1-x4) with x1 as the variable of interest. I am going to compare the results from logit and ologit across those y1-y10. I wonder if there is anyway that I could store the odds ratio and p-value of x1 from both logit and ologit in one table. I greatly appreciate any help with this!

  • #2
    Code:
    clear*
    
    use xy_data
    
    local dichotomies y1 y3 y5 y7 // OR WHATEVER
    local ordinals y2 y4 y6 y8 y9 y10 // OR WHATEVER
    
    frame create results str32 outcome_var str6 model float (b_x1 p)
    
    foreach d of local dichotomies {
        logit `d' x1 x2 x3 x4
        matrix M = r(table)
        frame post results ("`d'") ("logit") (M[1, 1]) (M[4, 1])
    }
    
    foreach o of local ordinals {
        ologit `o' x1 x2 x3 x4
        matrix M = r(table)
        frame post results ("`o'") ("ologit") (M[1, 1]) (M[4, 1])
    }
    
    frame change results
    gen odds_ratio = exp(b_x1)
    isid outcome, sort
    list, noobs clean
    Note: As no example data was shown, this code is untested and may contain typos or other errors.

    This code does what you ask. I urge you to consider something a bit more ambitious. The odds ratio and its p-value is a pretty uninformative presentation of results. You know the estimated odds ratio, but it is anybody's guess what the range of uncertainty in that estimate is. All you can say from the p-value is whether the confidence interval contains 1 or not. But you have no idea how wide it is, and if it contains 1 whether it does so just barely or extends deep into that territory. If it doesn't contain 1 you don't know if it is rather far from there or just barely misses 1. It would be more useful to store the confidence interval than the p-value. If you decide to do that, the lower and upper confidence limits are to be found in rows 5 and 6 of that matrix M. As with the coefficient, they are in the log-odds metric, so to associate them to the odds ratio, you need to exponentiate them.

    One note on the code: you don't say which of your outcome variables you plan to use for -logit- and which for -ologit-. In my code I illustrated how you would do it with y1, y3, y5, and y7 being dichotomous variables (logit) and the rest being polytomous ordinal variables (ologit). Evidently modify the two local macro definitions to the actual situation in your data.
    Last edited by Clyde Schechter; 16 Mar 2021, 19:59.

    Comment


    • #3
      Thanks so very much, Clyde, for your response. I am so sorry if this question is too basic, but am not sure why my stata does not recognize the frame command. Do I need to install that?

      Comment


      • #4
        The -frame- commands were introduced in version 16. Unless your installation is somehow corrupted, I suspect the problem is that you are running an older version of Stata. The Forum FAQ, which everybody has been asked to read before posting, specifically state that if you are not running the current version of Stata you need to say so in your post. That way, I wouldn't have wasted my time writing code that you can't run, and I wouldn't have wasted your time having you attempt to run it!

        Many things that are best done with frames in current Stata can be done in earlier versions using -postfile-s instead.

        Code:
        clear*
        
        use xy_data
        
        local dichotomies y1 y3 y5 y7 // OR WHATEVER
        local ordinals y2 y4 y6 y8 y9 y10 // OR WHATEVER
        
        tempfile results
        capture postutil clear
        postfile handle str32 outcome_var str6 model float (b_x1 p) using `results'
        
        foreach d of local dichotomies {
            logit `d' x1 x2 x3 x4
            matrix M = r(table)
            post handle ("`d'") ("logit") (M[1, 1]) (M[4, 1])
        }
        
        foreach o of local ordinals {
            ologit `o' x1 x2 x3 x4
            matrix M = r(table)
            post handle ("`o'") ("ologit") (M[1, 1]) (M[4, 1])
        }
        
        use `results', clear
        gen odds_ratio = exp(b_x1)
        isid outcome, sort
        list, noobs clean
        Changes to the earlier code are shown in boldface.

        The only functional difference between this code and the original is that the previous code left the original data in memory at the end, whereas this version does not. If you need the original data in memory at the end, you can accomplish that with -preserve- before -use `results', clear- and a -restore- command at the end.

        Comment


        • #5
          Thanks for the clarification and apologies for not mentioning my Stata's version. I added a preserve and restore to the code as well as a save command just before restore to save the results. The loops seem to work perfectly, but after my save command, I get a note that says "dataset contains 0 observations". Any ideas what is happening here?

          Comment


          • #6
            Please show the complete exact code you ran. I need to see all the details of the preserve, restore, and save commands and exactly how they relate to the rest of the code. Copy it exactly from your do-file and paste it here.

            Comment


            • #7
              Sure. Here is my code:

              Code:
              local dichotomies y1 y3 y5 y7 // OR WHATEVER
              local ordinals y2 y4 y6 y8 y9 y10 // OR WHATEVER
              
              tempfile results
              capture postutil clear
              postfile handle str32 outcome_var str6 model float (b_disable p) using `results'
              
              foreach d of local dichotomies {
                       logit `d' i.disable i.SEX_AB age i.urm
                       matrix M = r(table)
                       post handle ("`d'") ("logit") (M[1, 1]) (M[4, 1]) (M[5, 1]) (M[6, 1])
              }
              
              foreach o of local ordinals {
                     ologit `o' i.disable i.SEX_AB age i.urm
                     matrix M = r(table)
                     post handle ("`o'") ("ologit") (M[1, 1]) (M[4, 1]) (M[5, 1]) (M[6, 1])
              }
              
              preserve
              
              use `results', clear
              gen odds_ratio = exp(b_disable)
              isid outcome, sort
              list, noobs clean
              save "C:\Users\znouri\test1.dta", replace
              
              restore

              Comment


              • #8
                Well, I cannot reproduce your problem. That's not because there isn't a problem, but because there is a more important, earlier problem. I don't know how you are getting this message about 0 observations, because the code you show has no chance of ever even getting that far. Apparently you took my advice to expand your statistical horizons a bit and include the confidence interval. You did that in the -post handle- commands, but you forgot to modify the -postfile- statement to provide a place to receive them. So the code will break at the first -post handle- statement and will never even get close to the -save- command. That said, there are other problems in the code. One is my error that you carried forward: I forgot to include a -postclose- command, so the tempfile `results' never actually gets written and closed. And then, because you (appropriately) used factor variable notation for your variable disable, the coefficient, p-value, and CI we need are in the second column of r(table) instead of the first. (I had just assumed that your x variables were continuous.)

                The following code (which includes some extra commands to generate appropriate test data since you did not provide any--don't forget to remove those) runs without error messages and save an appropriate results file.

                Code:
                local dichotomies y1 y3 y5 y7 // OR WHATEVER
                local ordinals y2 y4 y6 y8 y9 y10 // OR WHATEVER
                
                //  CREATE SOME TEST DATA
                clear
                set obs 500
                set seed 1234
                foreach d of local dichotomies {
                    gen `d' = runiformint(0, 1)
                }
                foreach x in disable SEX_AB urm {
                    gen `x' = runiformint(0, 1)
                }
                foreach o of local ordinals {
                    gen `o' = runiformint(1, 4)
                }
                gen age = rnormal(50, 10)
                //  TEST DATA CREATED; RESUME SOLUTION
                
                
                tempfile results
                capture postutil clear
                postfile handle str32 outcome_var str6 model float (b_disable p lb ub) using `results'
                
                foreach d of local dichotomies {
                         logit `d' i.disable i.SEX_AB age i.urm
                         matrix M = r(table)
                         post handle ("`d'") ("logit") (M[1, 2]) (M[4, 2]) (M[5, 2]) (M[6, 2])
                }
                
                foreach o of local ordinals {
                       ologit `o' i.disable i.SEX_AB age i.urm
                       matrix M = r(table)
                       post handle ("`o'") ("ologit") (M[1, 2]) (M[4, 2]) (M[5, 2]) (M[6, 2])
                }
                
                postclose handle
                
                preserve
                
                use `results', clear
                gen odds_ratio = exp(b_disable)
                replace lb = exp(lb)
                replace ub = exp(ub)
                isid outcome, sort
                list, noobs clean
                save "test1.dta", replace
                
                restore
                Note also that I shortened the pathname for the -save- command because, evidently, I don't have a C:\Users\Znouri directory. You can put that back in if you are not running this from that directory in the first place.

                Changes shown in boldface.

                Comment


                • #9
                  OMG! Your code worked so perfectly. So many thanks for your help!

                  Comment

                  Working...
                  X