Announcement

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

  • How to draw a vertical graph with a list of coefficients and confident interval

    Today I found a very nice way to present our result by using the vertical line with the coefficients and their confident interval following Tanaka 2021 as below

    What I want to replicate is his Figure 3

    Click image for larger version

Name:	1.PNG
Views:	1
Size:	101.6 KB
ID:	1626416


    Simplistically speaking, I want to draw a figure like that with the list of coefficients, associated variables, confident intervals by using STATA. Can you please give me a hint or guidance?
    Many thanks and warmest regards.

  • #2
    Take a look at Ben Jann's coefplot, which you can install using -ssc install coefplot-.

    Comment


    • #3
      You can refer to the following thread: https://www.statalist.org/forums/for...-on-right-axis

      Comment


      • #4
        Originally posted by Andrew Musau View Post
        You can refer to the following thread: https://www.statalist.org/forums/for...-on-right-axis
        Hi Andrew Musau , it is a really nice guidance, I am looking for the code that I can draw the vertical line like that with the import coefficients and intervals rather than running the regression then plotting tho. Hope I can get a response.

        coefplot seems to draw the graph based on regressions as mentioned in the coefplot help file

        coefplot plots results from estimation commands or Stata matrices
        Last edited by Phuc Nguyen; 06 Sep 2021, 00:27.

        Comment


        • #5
          Where are the coefficients and CIs? In a dataset? If so, present a data example using dataex.

          Comment


          • #6
            Dear Phuc,

            Another very useful option of coefplot to do what you need is the ability to use a matrix, as is explained here by Ben Jann under the heading: Plotting results from matrices

            Best,
            Eric
            http://publicationslist.org/eric.melse

            Comment


            • #7
              Originally posted by Andrew Musau View Post
              Where are the coefficients and CIs? In a dataset? If so, present a data example using dataex.
              Hi Andrew Musau I tried to use the dataex but I did not fully understand how to use it, I will learn about it gradually, it is the data for drawing the first four lines. Thanks a heap
              Coefficients low CI high CI
              Baseline 0.99 0.95 1.03
              State of emergency, male adult 0.79 0.73 0.85
              Female adult 0.73 0.65 0.83
              Male elderly 0.85 0.73 1

              By following the code from here, I try to replicate but I got lost here

              Code:
              matrix median = J(1,3,.)
              
              matrix colnames median = baseline state_male female_adult 
              
              matrix CI = J(2,3,.)
              
              matrix colnames CI = baseline state_male female_adult
              
              matrix rownames CI = ll95 ul95
              
              matrix list median
              
              
              
              
              matrix list CI
              Code:
              . matrix list median
              
              median[1,3]
                      baseline    state_male  female_adult
              r1             .             .             .
              Last edited by Phuc Nguyen; 06 Sep 2021, 05:49.

              Comment


              • #8
                Read FAQ Advice #12 on using dataex.

                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input str30 var1 float(coefficients lowci highci)
                "Baseline"                       .99 .95 1.03
                "State of emergency, male adult" .79 .73  .85
                "Female adult"                   .73 .65  .83
                "Male elderly"                   .85 .73    1
                end
                
                sort coefficients var1
                mkmat coefficients lowci highci, mat(X)
                mat X= X'
                set scheme s1color
                coefplot mat(X), ci((2 3)) xsc(r(. 1.2)) mlabcolor(none) ///
                ylab(1 "Female adult" 2 "Male adult" 3 "Male elderly" 4 "Baseline") ///
                mlabel(string(@b,"%9.3f")+ " ("+string(@ll,"%9.3f") +" "+ string(@ul,"%9.3f")+")" )  ///
                addplot(scatter @at @ul if @plot==1, ms(i) mlabel(@mlbl) pstyle(p1) || ///
                scatter @at @ul if @plot==2, ms(i) mlabel(@mlbl) pstyle(p2))
                Click image for larger version

Name:	Graph.png
Views:	1
Size:	21.1 KB
ID:	1626449

                Comment


                • #9
                  Originally posted by Andrew Musau View Post
                  Read FAQ Advice #12 on using dataex.

                  Code:
                  * Example generated by -dataex-. For more info, type help dataex
                  clear
                  input str30 var1 float(coefficients lowci highci)
                  "Baseline" .99 .95 1.03
                  "State of emergency, male adult" .79 .73 .85
                  "Female adult" .73 .65 .83
                  "Male elderly" .85 .73 1
                  end
                  
                  sort coefficients var1
                  mkmat coefficients lowci highci, mat(X)
                  mat X= X'
                  set scheme s1color
                  coefplot mat(X), ci((2 3)) xsc(r(. 1.2)) mlabcolor(none) ///
                  ylab(1 "Female adult" 2 "Male adult" 3 "Male elderly" 4 "Baseline") ///
                  mlabel(string(@b,"%9.3f")+ " ("+string(@ll,"%9.3f") +" "+ string(@ul,"%9.3f")+")" ) ///
                  addplot(scatter @at @ul if @plot==1, ms(i) mlabel(@mlbl) pstyle(p1) || ///
                  scatter @at @ul if @plot==2, ms(i) mlabel(@mlbl) pstyle(p2))
                  [ATTACH=CONFIG]n1626449[/ATTACH]
                  Hi Andrew Musau , it is an amazing help, can you please help me to adjust the code to pull all the number to one column at the right hand side as in the figure on the original post, please?
                  Many thanks and warmest regards.

                  Comment


                  • #10
                    Originally posted by Andrew Musau View Post
                    Read FAQ Advice #12 on using dataex.

                    Code:
                    * Example generated by -dataex-. For more info, type help dataex
                    clear
                    input str30 var1 float(coefficients lowci highci)
                    "Baseline" .99 .95 1.03
                    "State of emergency, male adult" .79 .73 .85
                    "Female adult" .73 .65 .83
                    "Male elderly" .85 .73 1
                    end
                    
                    sort coefficients var1
                    mkmat coefficients lowci highci, mat(X)
                    mat X= X'
                    set scheme s1color
                    coefplot mat(X), ci((2 3)) xsc(r(. 1.2)) mlabcolor(none) ///
                    ylab(1 "Female adult" 2 "Male adult" 3 "Male elderly" 4 "Baseline") ///
                    mlabel(string(@b,"%9.3f")+ " ("+string(@ll,"%9.3f") +" "+ string(@ul,"%9.3f")+")" ) ///
                    addplot(scatter @at @ul if @plot==1, ms(i) mlabel(@mlbl) pstyle(p1) || ///
                    scatter @at @ul if @plot==2, ms(i) mlabel(@mlbl) pstyle(p2))
                    [ATTACH=CONFIG]n1626449[/ATTACH]
                    Dear Andrew Musau

                    I try to mimick your code with another result but it seems that there is something wrong, could you please help me to sort it out?


                    Code:
                    clear
                    input str30 var1 float(coefficients lowci highci)
                    "Baseline"                       .99 .95 1.03
                    "State of emergency, male adult" .79 .73  .85
                    "Female adult"                   .73 .65  .83
                    "Male elderly"                   .85 .73    1
                    "hahu hahu"                      .90 .22  0.5
                    end
                    
                    sort coefficients var1
                    mkmat coefficients lowci highci, mat(X)
                    mat X= X'
                    set scheme s1color
                    coefplot mat(X), ci((2 3)) xsc(r(. 1.2)) mlabcolor(none) ///
                    ylab(1 "Female adult" 2 "Male adult" 3 "Male elderly" 4 "Baseline" 5 "hahu hahu") ///
                    mlabel(string(@b,"%9.3f")+ " ("+string(@ll,"%9.3f") +" "+ string(@ul,"%9.3f")+")" )  ///
                    addplot(scatter @at @ul if @plot==1, ms(i) mlabel(@mlbl) pstyle(p1) || ///
                    scatter @at @ul if @plot==2, ms(i) mlabel(@mlbl) pstyle(p2))
                    (I add the input hahu hahu and add 5 "hahu hahu" to ylab)

                    Click image for larger version

Name:	1.PNG
Views:	1
Size:	42.6 KB
ID:	1626454

                    Comment


                    • #11
                      Either the coefficient or confidence interval of "hahu hahu" is wrong. The interval is from 0.220 to 0.5 and the coefficient is 0.9, which makes no sense. You are sorting in terms of the coefficient estimates, so be careful not to mess up the labeling of the axis. 0.9 is the second largest coefficient value, so it is second to bottom. Increase the xscale range if you need to, so that the labels are not truncated.

                      Code:
                      clear
                      input str30 var1 float(coefficients lowci highci)
                      "Baseline"                       .99 .95 1.03
                      "State of emergency, male adult" .79 .73  .85
                      "Female adult"                   .73 .65  .83
                      "Male elderly"                   .85 .73    1
                      "hahu hahu"                      .90 .22  0.5
                      end
                      
                      sort coefficients var1
                      mkmat coefficients lowci highci, mat(X)
                      mat X= X'
                      set scheme s1color
                      coefplot mat(X), ci((2 3)) xsc(r(. 1.4)) mlabcolor(none) ///
                      ylab(1 "Female adult" 2 "Male adult" 3 "Male elderly"  4 "hahu hahu" 5 "Baseline") ///
                      mlabel(string(@b,"%9.3f")+ " ("+string(@ll,"%9.3f") +" "+ string(@ul,"%9.3f")+")" )  ///
                      addplot(scatter @at @ul if @plot==1, ms(i) mlabel(@mlbl) pstyle(p1) || ///
                      scatter @at @ul if @plot==2, ms(i) mlabel(@mlbl) pstyle(p2))

                      Comment

                      Working...
                      X