Announcement

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

  • parentheses do not balance-tw scatter

    Hello everyone, I want to use the tw command to draw a scatter plot, but Stata keeps getting the error "parents do not balance". I checked the parentheses and there seems to be no problem.
    DATA:
    Code:
    n1    coef    se    low    up    sample
    17                    
    16                    
    15    -.677258    .5220768    -1.703554    .3490382    First-child:Girl
    14    -1.343865    .311451    -1.954803    -.7329267    First-child:Boy
    13                    
    12                    
    11    -.7073836    .4829693    -1.656105    .2413375    Private sectors
    10    -.7405966    .619513    -1.959962    .4787686    Public sectors
    9                    
    8                    
    7    -.8515229    .2903653    -1.421153    -.2818931    urban
    6    -.8001119    .3184165    -1.424995    -.1752291    Rural
    5                    
    4                    
    3    -1.05584    .2592954    -1.564424    -.5472564    High-educated
    2    -.2954414    .3623798    -1.007028    .4161452    Low-educated
    1
    I want to plot graph like this image:
    Click image for larger version

Name:	Snipaste_2025-04-23_16-07-02.png
Views:	1
Size:	112.7 KB
ID:	1776386

    So i have typed code as follow, but something wrong in my code because stata said that: "parentheses do not balance":
    Code:
    Code:
    loc s = -0.1
    
    tw(scatter n1 coef ,
        ylabel( "" , noticks nogrid)
        mlabel(sample)
        mlabp(17)
        xlabel(-0.12(0.02)0.06 0 "",noticks nogrid)
        xline(0, lp(dash) lcolor(brown)))
        (rcap up low n1 ,
        lp(dash) lcolor(gsd1)  ytit("")
        legend( label(1 "Coef.") label(2 "95% CI")
        col(1) ring(0) pos(5) size(*0.95))
        hori graphregion(fcolor(white)) 
        text(4  `s' "Panel D: ",place(e) size(*0.95))
        text(8  `s' "Panel C: ",place(e) size(*0.95))
        text(12 `s' "Panel B: ",place(e) size(*0.95))
        text(16 `s' "Panel A: ",place(e) size(*0.95)))
    I would appreciate any suggestions how can plot this graph. Thank you all.

  • #2
    My personal take here is that || syntax is preferable for indicating different twoway commands, the whole point being that there are quite enough parentheses to confuse.

    A different point is that any good text editor, including Stata's will find matching parentheses, or fail to find one. Try

    Code:
    tw scatter n1 coef ,
        ylabel( "" , noticks nogrid)
        mlabel(sample)
        mlabp(17)
        xlabel(-0.12(0.02)0.06 0 "",noticks nogrid)
        xline(0, lp(dash) lcolor(brown))
    ||  rcap up low n1 ,
        lp(dash) lcolor(gsd1)  ytit("")
        legend( label(1 "Coef.") label(2 "95% CI")
        col(1) ring(0) pos(5) size(*0.95))
        hori graphregion(fcolor(white)) 
        text(4  `s' "Panel D: ",place(e) size(*0.95))
        text(8  `s' "Panel C: ",place(e) size(*0.95))
        text(12 `s' "Panel B: ",place(e) size(*0.95))
        text(16 `s' "Panel A: ",place(e) size(*0.95))

    Comment


    • #3
      As a matter of fact, parentheses do balance in #1. The problem lies in that you didn't use comment indicator, ///. Thus, Stata only execute the first line tw (scatter n1 coef , and quit.

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(n1 coef se low up) str30 sample
      17         .        .         .         . "."            
      16         .        .         .         . "."            
      15  -.677258 .5220768 -1.703554  .3490382 "First-child"  
      14 -1.343865  .311451 -1.954803 -.7329267 "First-child"  
      13         .        .         .         . "."            
      12         .        .         .         . "."            
      11 -.7073836 .4829693 -1.656105  .2413375 "Private"      
      10 -.7405966  .619513 -1.959962  .4787686 "Public"       
       9         .        .         .         . "."            
       8         .        .         .         . "."            
       7 -.8515229 .2903653 -1.421153 -.2818931 "urban"        
       6 -.8001119 .3184165 -1.424995 -.1752291 "Rural"        
       5         .        .         .         . "."            
       4         .        .         .         . "."            
       3  -1.05584 .2592954 -1.564424 -.5472564 "High-educated"
       2 -.2954414 .3623798 -1.007028  .4161452 "Low-educated" 
       1         .        .         .         . "."            
      end
      
      tw (scatter n1 coef , ///
          ylabel( "" , noticks nogrid) ///
          mlabel(sample) ///
          mlabp(17) ///
          xlabel(-0.12(0.02)0.06 0 "",noticks nogrid) ///
          xline(0, lp(dash) lcolor(brown))) ///
          (rcap up low n1 , ///
          lp(dash) lcolor(gsd1)  ytit("") ///
          legend( label(1 "Coef.") label(2 "95% CI") ///
          col(1) ring(0) pos(5) size(*0.95)) ///
          hori graphregion(fcolor(white)) ///
          text(4  `s' "Panel D: ",place(e) size(*0.95)) ///
          text(8  `s' "Panel C: ",place(e) size(*0.95)) ///
          text(12 `s' "Panel B: ",place(e) size(*0.95)) ///
          text(16 `s' "Panel A: ",place(e) size(*0.95)))
      Or you can also change delimite. Please note the ; in the end of long tw sentence.
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(n1 coef se low up) str30 sample
      17         .        .         .         . "."            
      16         .        .         .         . "."            
      15  -.677258 .5220768 -1.703554  .3490382 "First-child"  
      14 -1.343865  .311451 -1.954803 -.7329267 "First-child"  
      13         .        .         .         . "."            
      12         .        .         .         . "."            
      11 -.7073836 .4829693 -1.656105  .2413375 "Private"      
      10 -.7405966  .619513 -1.959962  .4787686 "Public"       
       9         .        .         .         . "."            
       8         .        .         .         . "."            
       7 -.8515229 .2903653 -1.421153 -.2818931 "urban"        
       6 -.8001119 .3184165 -1.424995 -.1752291 "Rural"        
       5         .        .         .         . "."            
       4         .        .         .         . "."            
       3  -1.05584 .2592954 -1.564424 -.5472564 "High-educated"
       2 -.2954414 .3623798 -1.007028  .4161452 "Low-educated" 
       1         .        .         .         . "."            
      end
      
      loc s = -0.1
      
      #delimit ; 
      tw(scatter n1 coef ,
          ylabel( "" , noticks nogrid)
          mlabel(sample)
          mlabp(17)
          xlabel(-0.12(0.02)0.06 0 "",noticks nogrid)
          xline(0, lp(dash) lcolor(brown)))
          (rcap up low n1 ,
          lp(dash) lcolor(gsd1)  ytit("")
          legend( label(1 "Coef.") label(2 "95% CI")
          col(1) ring(0) pos(5) size(*0.95))
          hori graphregion(fcolor(white)) 
          text(4  `s' "Panel D: ",place(e) size(*0.95))
          text(8  `s' "Panel C: ",place(e) size(*0.95))
          text(12 `s' "Panel B: ",place(e) size(*0.95))
          text(16 `s' "Panel A: ",place(e) size(*0.95))) ;
          
      #delimit cr
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	136.1 KB
ID:	1776391

      Comment


      • #4
        And for your real data in 1#, you'd better change your xlabel to get a good looking graph. And local s should be set to -1.8, i.e. local s = -1.8.
        Code:
        xlabel(-2(0.2)0.6 0 "",noticks nogrid)
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	135.6 KB
ID:	1776395

        Comment

        Working...
        X