Announcement

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

  • Putting various numeric variables into one: "parentheses do not balance"

    Hello,

    I need to put several political parties together and observe the dissimilarity between certain groups. GNPP stands for Governing Non Premiership Party and PP for Premiership Party. Using the commands below I receive "parentheses do not balance". The partys with the party variables 41111, 41112, 41113, 41521 are GNPP parties and 41320 and 41420 are PP parties. I use MPDataset_MPDS2017b_stata14.dta.

    egen DI = rowmean(mdiffper*)
    gen eyear = trunc(date/100) // election year
    twoway (connected DI eyear if party == 41111 | party == 41112 | ///
    party == 41113) | party == 41521 ///
    (connected DI eyear if party == 41320 | party == 41420 | ///
    title("Party Dissimilarity Index") xtitle("election year") ///
    legend(label(1 "GermanGNPP") label(2 "GermanPP")

    this results in "parentheses do not balance" .

  • #2
    Stata's right. There are numerous small syntax errors there. With some corrections and some extra simplifications I suggest

    Code:
    twoway /// 
    connected DI eyear if inlist(party, 41111, 41112, 41113, 41521) ///
    || connected DI eyear if inlist(party, 41320, 41420),           ///
    title("Party Dissimilarity Index") xtitle("election year")      ///
    legend(label(1 "GermanGNPP") label(2 "GermanPP"))
    I recommend the || syntax over () for different graphs; there are usually quite enough parentheses to track.

    Comment


    • #3
      Thank you very much Nick.
      That problem was solved.

      However, I just wanted to create a Herfindahl Hirschmann Index for the GNPP Parties, yet I get the same error message, the parentheses which do not balance.

      foreach VAR of varlist peruncod per??? {
      gen `VAR'P2 = (`VAR'/100)^2
      }
      egen herfin = rowtotal (peruncodP2 per???P2)
      label variable herfin "Herfindahl Index for 44 categories"
      gen herfinN = (herfin - 1/44)/(1-1/44)
      label variable herfinN "Herfindahl Index (normalized)"
      label define herfinN 0 "min. concentration" 1 "max. concentration"
      sort country date party

      drop per???P2 peruncodP2
      foreach VAR of varlist peruncod per??? {
      gen `VAR'P2 = (`VAR'/100)^2
      }
      egen herfin44 = rowtotal(peruncodP2 per???P2)
      label variable herfin44 "Herfindahl Index for 44 categories"
      gen herfinN44 = (herfin44 - 1/44)/(1-1/44)
      label variable herfinN44 "Herfindahl Index 44 (normalized)"
      label values herfinN44 herfinN





      twoway ///
      (connected herfinN44 eyear if inlist(party, 41111, 41112, 41113, 41521)) || ///
      (connected herfinN eyear if inlist(party, 41111, 41112, 41113, 41521)) ///
      title("Concentration") subtitle("GermanGNPP") xtitle("election year") ///
      xlabel(1980 (5) 2020) legend(label("44 cat.") ///
      graph export GermanGNPP.pdf, replace

      -> no graph comes up. I also need to make two herfindahl indices with both party types so I would do this if the former parentheses balanced (in this example only using 44 categories):

      twoway ///
      (connected herfinN44 eyear if inlist(party, 41111, 41112, 41113, 41521)) || ///
      (connected herfinN44 eyear if inlist(party 41320, 41420)) || ///
      title("Concentration") subtitle(1 "GermanGNPP") subtitle (2 "GermanPP") xtitle("election year") ///
      xlabel(1980 (5) 2020) legend(label("44 cat.") ///
      graph export German2groups.pdf, replace

      but also here, the parentheses do not balance.

      Tom

      Comment


      • #4
        For Hirschmann read Hirschman.

        The || notation is an alternative to () notation. graph export is a new command.

        Code:
        twoway ///
        connected herfinN44 eyear if inlist(party, 41111, 41112, 41113, 41521) || ///
        connected herfinN eyear if inlist(party, 41111, 41112, 41113, 41521),  ///
        title("Concentration") subtitle("GermanGNPP") xtitle("election year") ///
        xlabel(1980 (5) 2020) legend(label("44 cat.")
        
        graph export GermanGNPP.pdf, replace
        has, I hope, fewer errors but I can't test it

        The problems with the last example look similar.





        Comment


        • #5
          That did not solve my problem unfortunately.

          I tried something else. This is very weird. The following functions perfectly as you can see in the attachment.

          twoway (connected herfinN eyear if party == 41111 | party == 41112 | ///
          party == 41113) ///
          (connected herfinN44 eyear if party == 41111 | party == 41112 | ///
          party == 41113) ///
          (connected herfinN8 eyear if party == 41111 | party == 41112 | ///
          party == 41113), ///
          title("Concentration") subtitle("German Greens") xtitle("election year") ///
          xlabel(1980 (5) 2020) legend(label(1 "57 cat.") label(2 "44 cat.") ///
          label(3 "8 cat."))
          graph export w11_greens_herfin.pdf, replace


          However, if I add one more party, "| is not a twoway plot type" . All I did was to add " | party == 41521" to the three herfindahl indices.

          twoway (connected herfinN eyear if party == 41111 | party == 41112 | ///
          party == 41113) | party == 41521 ///
          (connected herfinN44 eyear if party == 41111 | party == 41112 | ///
          party == 41113) | party == 41521 ///
          (connected herfinN8 eyear if party == 41111 | party == 41112 | ///
          party == 41113 | party == 41521), ///
          title("Concentration") subtitle("GermanGNPP") xtitle("election year") ///
          xlabel(1980 (5) 2020) legend(label(1 "57 cat.") label(2 "44 cat.") ///
          label(3 "8 cat."))
          graph export w11_germanGNPP_herfin.pdf, replace

          edit_: I made a jpg of the graph so one can open it.
          Attached Files
          Last edited by Tom Stein; 05 Mar 2018, 06:39.

          Comment


          • #6
            It really is a good idea to learn inlist()

            The extra syntax

            Code:
            | part == 41521
            belongs inside the () notation on every occasion. You have one right and two wrong.

            So,


            Code:
            twoway (connected herfinN eyear if party == 41111 | party == 41112 | ///
            party == 41113) | party == 41521 ///
            (connected herfinN44 eyear if party == 41111 | party == 41112 | ///
            party == 41113) | party == 41521 ///
            (connected herfinN8 eyear if party == 41111 | party == 41112 | ///
            party == 41113 | party == 41521), ///
            title("Concentration") subtitle("GermanGNPP") xtitle("election year") ///
            xlabel(1980 (5) 2020) legend(label(1 "57 cat.") label(2 "44 cat.") ///
            label(3 "8 cat."))
            should be (minimally)

            Code:
            twoway (connected herfinN eyear if party == 41111 | party == 41112 | ///
            party == 41113 | party == 41521) ///
            (connected herfinN44 eyear if party == 41111 | party == 41112 | ///
            party == 41113 | party == 41521) ///
            (connected herfinN8 eyear if party == 41111 | party == 41112 | ///
            party == 41113 | party == 41521), ///
            title("Concentration") subtitle("GermanGNPP") xtitle("election year") ///
            xlabel(1980 (5) 2020) legend(label(1 "57 cat.") label(2 "44 cat.") ///
            label(3 "8 cat."))
            and I think that reduces in turn to

            Code:
            twoway connected herfinN herfinN44 herfin8 eyear if inlist(party, 41111, 41112, 41113, 41521) , ///
            title("Concentration") subtitle("GermanGNPP") xtitle("election year") ///
            xlabel(1980 (5) 2020) legend(label(1 "57 cat.") label(2 "44 cat.") ///
            label(3 "8 cat."))

            Comment


            • #7
              Thank you very much, it worked.


              So one last thing. I need to differentiate between the party types. I want to see both partytypes (GNPP and PP) shown on a graph, but I always put them together. I only need herfinN44 so I excluded herfinN and herfin8.

              gen partytype = 0
              label define partytype 1 "GermanGNPP" 2 "GermanPP"
              replace partytype = 1 if inlist(party, 41111, 41112, 41113, 41521)
              replace partytype = 2 if inlist(party, 41320, 41420)
              keep if partytype >0

              twoway connected herfinN44 eyear if inlist(partytype, 1,2) , ///
              title("Concentration") subtitle("Germanboth") xtitle("election year") ///
              xlabel(1980 (5) 2020) legend(label(1 "44 cat."))


              Instead of showing partytype 1 and 2 separate, they are put together so I can not distinguish between the two which is my final goal.

              The below example works, it distinguishes between partytype 1 and 2 so i have to boxplots but that's not the same as the twoway connected command. But also here, instead of showing 1 as "GermanGNPP" and 2 as "GermanPP" as I labeled them above, it shows them as "1" and "2".

              graph box herfinN44, over(partytype, label(angle(45)))



              Comment


              • #8
                Code:
                help label
                
                label val partytype partytype

                Comment

                Working...
                X