Announcement

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

  • foreach using label var needs skip some values

    Hello,

    My sample is 27 countries over the period 1970-2020.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(ln_co2pc_gr ln_gdppc_gr co2pc_index gdppc_index ei_index res_share_index) str28 Country long pais float year
        .04825544    .07338333  88.09743  81.72649 101.61838 103.27965 "Argentina" 1 1991
       .014944077    .06285858  89.42382  87.02858  96.83293 114.00206 "Argentina" 1 1992
       .020612717    .06581497  91.28623  92.94906  90.54688 125.78635 "Argentina" 1 1993
        .02478409    .04406929  93.57697  97.13685  91.04784 126.39912 "Argentina" 1 1994
      -.012306213   -.04115677  92.43246  93.22015  95.21066 127.64198 "Argentina" 1 1995
        .04091835    .04185677  96.29308  97.20493  92.91475 117.01875 "Argentina" 1 1996
      .0079956055   .066394806  97.06602 103.87782   89.3968 123.59282 "Argentina" 1 1997
       .014033318   .026456833  98.43781 106.66273  87.65541 125.32155 "Argentina" 1 1998
       .029364586   -.04557419  101.3713 101.91074  92.75327 108.45278 "Argentina" 1 1999
      -.013619423   -.01892853       100       100       100       100 "Argentina" 1 2000
       -.04242325   -.05600929  95.84637  94.55291  99.79094 121.46896 "Argentina" 1 2001
      -.064777374   -.12618446  89.83456  83.34387 108.35513  126.7974 "Argentina" 1 2002
        .05957603    .07396126  95.34916   89.7418 106.22996  115.7276 "Argentina" 1 2003
        .07559109     .0758953 102.83614  96.81789 106.30276  99.14473 "Argentina" 1 2004
        .02943516    .07445145 105.90813  104.3014  99.97256 105.50493 "Argentina" 1 2005
        .04256248   .067243576 110.51316 111.55614   96.3188 106.78304 "Argentina" 1 2006
        .08056164    .07625961 119.78467  120.3961  95.88024  94.37043 "Argentina" 1 2007
        .02333355   .029844284  122.6126 124.04348  94.14695  99.92487 "Argentina" 1 2008
       -.07792091   -.07100296 113.42123 115.54126  93.25505 121.00477 "Argentina" 1 2009
        .07104397     .0862999 121.77227  125.9554  89.92941   125.727 "Argentina" 1 2010
       .036629677    .04797363  126.3154 132.14523  88.39929 128.00526 "Argentina" 1 2011
       .005539894   -.02078247 127.01711 129.42728  90.18933 121.60122 "Argentina" 1 2012
       .017653465    .01326561 129.27931 131.15573  90.42513 121.68302 "Argentina" 1 2013
       -.02623844   -.03585434 125.93138  126.5364  92.74831 132.65565 "Argentina" 1 2014
        .01380539    .01672554 127.68196 128.67079  91.39137 119.88068 "Argentina" 1 2015
      -.014710426  -.031024933 125.81753    124.74  93.49374 126.12923 "Argentina" 1 2016
      -.024267197   .017990112 122.80096 127.00445  89.81461 139.28287 "Argentina" 1 2017
       -.04940796   -.03612709 116.88105 122.49808   90.7141 137.28702 "Argentina" 1 2018
       -.03939629  -.029878616 112.36594 118.89198  91.20992 136.30003 "Argentina" 1 2019
       -.12809563            .  98.85607         .         .         . "Argentina" 1 2020
     -.0022001266   -.04348564  77.16738  88.52657  98.41671 133.37622 "Barbados"  2 1991
       -.12391949   -.06234932  68.17361  83.17564  82.35715 112.67005 "Barbados"  2 1992
      -.013767242  .0041484833 67.241425  83.52137  84.01865  92.29795 "Barbados"  2 1993
         .1101904   .016054153  75.07444  84.87305  89.65949 121.05844 "Barbados"  2 1994
       .034041405   .015937805  77.67407  86.23666  93.95683  94.09638 "Barbados"  2 1995
         .0448246    .03489113  81.23496  89.29851   94.2771  97.56416 "Barbados"  2 1996
        .12469387    .04205894  92.02315  93.13448  99.78285   88.3758 "Barbados"  2 1997
       -.02123642   .032331467  90.08949  96.19482  93.49627  96.96458 "Barbados"  2 1998
        .05735397 -.0007915497  95.40752  96.11884  96.63953  60.86456 "Barbados"  2 1999
        .04701233    .03958511       100       100       100       100 "Barbados"  2 2000
       -.04536247   -.02755928  95.56507  97.28162  96.52576  84.79669 "Barbados"  2 2001
      -.015072823   .004453659  94.13547  97.71592  94.53738  85.56728 "Barbados"  2 2002
      .0005598068   .018220901  94.18816   99.5127  92.78854  83.98882 "Barbados"  2 2003
       .012296677   .010541916  95.35353 100.56733  92.71589  80.60226 "Barbados"  2 2004
       .017718315    .03502655  97.05807 104.15212   90.5227  91.81223 "Barbados"  2 2005
       .008773804     .0531826  97.91336 109.84119  85.59059  73.91139 "Barbados"  2 2006
       .010412216   .017383575  98.93819 111.76736  85.35068  79.64191 "Barbados"  2 2007
        .15820885  .0025253296 115.89732 112.04993  98.18254  66.22715 "Barbados"  2 2008
      -.035902023   -.05624008 111.81014 105.92211  100.4584   72.3441 "Barbados"  2 2009
       -.07738018   -.02677727  103.4845  103.1235  94.82402  60.56812 "Barbados"  2 2010
         .0507679  -.009784698 108.87388 102.11934 100.22132  55.40932 "Barbados"  2 2011
       -.06547451  -.007087708 101.97378 101.39824  94.48899  54.19486 "Barbados"  2 2012
       .010436058  -.016260147 103.04356  99.76278  96.24812   38.5301 "Barbados"  2 2013
       -.09462738  -.003108978  93.73997  99.45313  86.55653  41.52081 "Barbados"  2 2014
      -.010979652    .02244568  92.71634  101.7106  83.24458 24.311665 "Barbados"  2 2015
       -.06432438   .022830963  86.94016 104.05956  76.90018  31.88192 "Barbados"  2 2016
       .026329994    .00321579  89.25967  104.3947  79.69268  40.48559 "Barbados"  2 2017
        .01234913  -.007247925  90.36886 103.64076  80.12846  51.35692 "Barbados"  2 2018
       .009453773 -.0023002625  91.22721  103.4026  82.26102  39.96509 "Barbados"  2 2019
         -.295187            .  67.90885         .         .         . "Barbados"  2 2020
                .        .0927         .  80.42125         .         . "Belice"    3 1991
                .     .0982647         .  88.72512         .         . "Belice"    3 1992
                .    .04045868         .   92.3884         .         . "Belice"    3 1993
                .  -.018418312         .  90.70238         .         . "Belice"    3 1994
                .  -.018026352         .  89.08195         .         . "Belice"    3 1995
                .  -.019067764         .  87.39949         .         . "Belice"    3 1996
                .  -.001868248         .  87.23634         .         . "Belice"    3 1997
                .   .001320839         .  87.35162         .         . "Belice"    3 1998
                .    .04576206         .  91.44193         .         . "Belice"    3 1999
                .     .0894661       100       100       100         . "Belice"    3 2000
        1.0947976    .01387787  298.8578  101.3975  462.7081         . "Belice"    3 2001
       -.04188967   .019158363  286.5973 103.35874  439.6899         . "Belice"    3 2002
        -.2038312    .04554462  233.7488 108.17513  361.2708         . "Belice"    3 2003
        -.1992817   .014788628  191.5148 109.78668  316.9416         . "Belice"    3 2004
       .032030582  -.012813568 197.74847 108.38895  326.2465         . "Belice"    3 2005
      -.007736206   .015089035  196.2245 110.03683  331.8148         . "Belice"    3 2006
       -.05034304   -.02819538  186.5905 106.97758  331.1753         . "Belice"    3 2007
       -.17690945   .000840187 156.33595  107.0675 302.67722         . "Belice"    3 2008
         .2510848   .024201393 200.95714  109.6903  349.0574         . "Belice"    3 2009
       -.10021162  .0029239655  181.7951  110.0115 339.63785         . "Belice"    3 2010
         .1433444  -.006885529  209.8146 109.25665  355.5048         . "Belice"    3 2011
       -.27655888 -.0019550323  159.1211 109.04323 304.17044         . "Belice"    3 2012
      -.036518097  -.013026237 153.41513  107.6321 280.75897         . "Belice"    3 2013
        .05235434   .013698578 161.66107 109.11655 279.52856         . "Belice"    3 2014
        .29069948  .0001525879  216.1995 109.13322  332.3651         . "Belice"    3 2015
       -.04553223   -.02621746 206.57617 106.30927  332.5261         . "Belice"    3 2016
       -.04472256  -.007909775  197.5411 105.47172  341.4164         . "Belice"    3 2017
       -.04459095  .0027627945   188.926 105.76346  328.1722         . "Belice"    3 2018
         .1819215  -.008408546  226.6206 104.87792  361.6137         . "Belice"    3 2019
       -.17735815            .  189.7901         .         .         . "Belice"    3 2020
       -.00705576   .028149605  95.28852  89.43568 120.88658 159.68777 "Bolivia"   4 1991
         .0192132  -.007154465  97.13704  88.79811 124.01364  149.2438 "Bolivia"   4 1992
       .016575336   .018341064  98.76054  90.44177 126.30735 148.22742 "Bolivia"   4 1993
        .06495714    .02246952 105.38867  92.49696 130.49634 132.36621 "Bolivia"   4 1994
         .0799017   .023166656   114.155  94.66488 137.95741 120.73182 "Bolivia"   4 1995
        -.0983386   .020656586 103.46345  96.64061  163.1596  95.93271 "Bolivia"   4 1996
      -.015071392   .026638985  101.9158  99.24964 170.52783  89.26667 "Bolivia"   4 1997
        .02993822    .02772522  105.0131 102.03988  164.0189  86.36247 "Bolivia"   4 1998
       .007516861   -.01666832 105.80544 100.35309 140.42122 105.39738 "Bolivia"   4 1999
       -.05643177   -.00352478       100       100       100       100 "Bolivia"   4 2000
       -.04245186 -.0021419525  95.84367  99.78609 103.74305  96.54135 "Bolivia"   4 2001
        .05569315   .006061554 101.33295 100.39273 120.36938  81.53298 "Bolivia"   4 2002
         .0761323    .00859642 109.34893 101.25953 119.96055  77.87483 "Bolivia"   4 2003
         .0782442    .02305603 118.24844 103.62125 117.02305  78.53965 "Bolivia"   4 2004
       .064801216     .0257473 126.16484 106.32385 123.75546  70.59754 "Bolivia"   4 2005
        .06742525    .02963829 134.96487 109.52232 121.07775  71.86133 "Bolivia"   4 2006
         .0908618   .027708054  147.8024  112.5994  118.6554  72.93017 "Bolivia"   4 2007
        .06486559    .04302597  157.7075 117.54977 114.35208  73.38753 "Bolivia"   4 2008
       .015411377   .016647339  160.1568  119.5231 113.99065  71.87138 "Bolivia"   4 2009
        .07581043   .024331093  172.7704 122.46687 118.08897  66.66338 "Bolivia"   4 2010
        .06849861   .034879684  185.0197 126.81384 121.11295  63.23761 "Bolivia"   4 2011
        .05281162   .034342766 195.05347 131.24458 124.53957  59.05567 "Bolivia"   4 2012
       .063752174    .05072784  207.8935 138.07413 118.31788  59.75566 "Bolivia"   4 2013
         .0546484    .03823757  219.5707 143.45598 123.19976  53.94275 "Bolivia"   4 2014
       .004561901    .03261566  220.5747 148.21207 117.99851  55.08978 "Bolivia"   4 2015
        .03744268    .02707863  228.9902 152.28027 119.55787  49.86899 "Bolivia"   4 2016
       .003685951    .02657223  229.8358  156.3809 119.43227  50.49334 "Bolivia"   4 2017
    -.00006866455    .02697849 229.81993  160.6573  116.3544  51.64947 "Bolivia"   4 2018
      -.034183502   .007639885 222.09666  161.8894 113.86771   54.8639 "Bolivia"   4 2019
        -.1942377            .  182.8882         .         .         . "Bolivia"   4 2020
       .014796734  -.007326126  87.21692  90.02676  98.42463 119.62138 "Brasil"    5 1991
      -.007015228  -.022527695   86.6072  88.02124  99.37264 117.05435 "Brasil"    5 1992
       .018631935    .03142357  88.23598  90.83114  97.16889  114.8917 "Brasil"    5 1993
       .033761024     .0405426  91.26577  94.58928  96.19737 115.01794 "Brasil"    5 1994
         .0585742    .02527237  96.77129  97.01031  94.99005 110.59998 "Brasil"    5 1995
        .06693506   .005960464 103.47038   97.5902  97.79095 107.00862 "Brasil"    5 1996
        .02712488   .017747879  106.3154  99.33767  95.11569  106.6608 "Brasil"    5 1997
        .01146412  -.011915207  107.5412  98.16114  97.24764 103.91032 "Brasil"    5 1998
       .035334587  -.010141373 111.40904  97.17056  102.8947 103.64593 "Brasil"    5 1999
       -.10803843    .02870178       100       100       100       100 "Brasil"    5 2000
       .004811764  .0001335144 100.48237 100.01337 100.24696  96.78484 "Brasil"    5 2001
        .04259443   .016950607 104.85478 101.72308  94.91445  101.3824 "Brasil"    5 2002
       -.03887796   -.00123024  100.8565 101.59801  95.35709 109.43108 "Brasil"    5 2003
        .04675388    .04398918 105.68388 106.16696  94.96227 108.83916 "Brasil"    5 2004
      -.003501892   .020059586 105.31445 108.31822  94.37063 111.02763 "Brasil"    5 2005
       .003994465     .0279541 105.73593 111.38879  93.58286 112.26595 "Brasil"    5 2006
        .05584764    .04854774 111.80904 116.93005   93.0794 116.09528 "Brasil"    5 2007
        .06244564    .03974533 119.01366 121.67087  93.73104 115.66685 "Brasil"    5 2008
         -.075912  -.010879517  110.3135 120.35445  90.30891 117.78793 "Brasil"    5 2009
        .10742617    .06320381 122.82403 128.20683  92.62157 113.45102 "Brasil"    5 2010
       .017482758   .029815674  124.9902 132.08688  90.20316 109.61284 "Brasil"    5 2011
        .08303165    .01009369 135.81139 133.42696  92.83717 105.56097 "Brasil"    5 2012
        .06584406    .02087021 145.05469 136.24078  94.26726  102.4837 "Brasil"    5 2013
        .04444695  -.003530502  151.6474 135.76056  96.51472  99.58391 "Brasil"    5 2014
       -.03761244   -.04449081  146.0495 129.85297  98.09354 102.82339 "Brasil"    5 2015
       -.08851957   -.04154587 133.67693 124.56873  98.07391 108.73212 "Brasil"    5 2016
       .014027596    .00507164 135.56529 125.20195  98.47678 107.55977 "Brasil"    5 2017
       -.02507496   .009840012  132.2083 126.44014  95.49188 114.08567 "Brasil"    5 2018
      .0022501945   .006498337 132.50607 127.26435  95.54987  114.8834 "Brasil"    5 2019
        -.0548892            . 125.42893         .         .         . "Brasil"    5 2020
       -.06062365    .05624962 65.703125 67.920105  90.64416 131.63052 "Chile"     6 1991
       .012077808    .08646965  66.50148  74.05458  87.65987 141.27846 "Chile"     6 1992
        .04630709    .04456139  69.65339  77.42917   84.4327 131.74744 "Chile"     6 1993
        .08125353   .030635834  75.54926  79.83803  87.03245  125.2851 "Chile"     6 1994
        .06194735    .06847191   80.3773  85.49616  86.55735 124.32755 "Chile"     6 1995
        .11579657    .05014992    90.245  89.89301  88.58909 116.38814 "Chile"     6 1996
         .1049385    .05685616    100.23  95.15215  91.24567 108.26832 "Chile"     6 1997
      .0023117065    .02831459 100.46196  97.88477  90.84552  104.3344 "Chile"     6 1998
         .0386734  -.017549515 104.42326  96.18203 100.05241  94.22979 "Chile"     6 1999
       -.04328251    .03892708       100       100       100       100 "Chile"     6 2000
       -.05485725   .020225525  94.66198 102.04304  92.62241  108.5141 "Chile"     6 2001
       .003077507    .02309036  94.95377 104.42664  93.15086 108.52738 "Chile"     6 2002
       .019994736      .024086   96.8714 106.97245  90.43607 102.79455 "Chile"     6 2003
        .09033298    .05835152 106.02952 113.40009   89.1376   99.6251 "Chile"     6 2004
       .019569397    .05067635 108.12486 119.29506  88.60944 105.38504 "Chile"     6 2005
       -.05156803    .05010223  102.6905 125.42416  81.24078  110.1654 "Chile"     6 2006
        .21250916   .036901474  127.0052 130.13887  84.06816  100.1039 "Chile"     6 2007
       .010652542   .023872375 128.36542 133.28313  83.27036 100.70982 "Chile"     6 2008
       -.05871296   -.02638817 121.04565 129.81206  81.36815  106.3733 "Chile"     6 2009
      .0011100769    .04638863 121.18008 135.97568  78.69421  76.65289 "Chile"     6 2010
        .08642387    .04915428 132.11887 142.82631  78.84789  75.96181 "Chile"     6 2011
        .04074383    .04195213 137.61305 148.94574  82.89036 105.58315 "Chile"     6 2012
      -.015625954    .03014755 135.47934 153.50447  83.35727  111.5339 "Chile"     6 2013
       -.03797722   .008365631 130.43077  154.7941  79.28455 112.20207 "Chile"     6 2014
        .05943203    .01397705  138.4175 156.97284  78.24474 113.15028 "Chile"     6 2015
        .03356266   .008466721 143.14198  158.3073  81.19825  112.8705 "Chile"     6 2016
      .0041360855  .0035419464 143.73521 158.86906  81.45627 115.71986 "Chile"     6 2017
      -.006093979    .02848053 142.86203   163.459   79.7754 118.11314 "Chile"     6 2018
       -.08954144   -.05813026 130.62596 154.22786  79.09851 118.03138 "Chile"     6 2019
       -.04871464            .  124.4151         .         .         . "Chile"     6 2020
       .009307861 .00020217896  99.61648  91.88589 103.21577  109.3119 "Colombia"  27 1991
        .08071852    .02016735  107.9908  93.75782  103.3714 102.27656 "Colombia"  27 1992
       .012161732    .03321457 109.31214  96.92418 104.37955  108.5904 "Colombia"  27 1993
     -.0030760765    .03767014 108.97644 100.64495 102.79118 110.39777 "Colombia"  27 1994
        .02716875   .032360077  111.9778  103.9551  99.53103 108.67682 "Colombia"  27 1995
      -.022383213   .002527237 109.49918 104.21817  99.98817 111.45712 "Colombia"  27 1996
        .04020596    .01641655  113.9914 105.94323  95.72665  93.03889 "Colombia"  27 1997
       .005700111  -.011154175 114.64301 104.76802   94.7146  75.98995 "Colombia"  27 1998
       -.15331173   -.05937004  98.34794    98.729  92.77286  101.8142 "Colombia"  27 1999
       .016658783    .01279068       100       100       100       100 "Colombia"  27 2000
       -.03855753  .0009670258  96.21765 100.09676  99.81773  99.74117 "Colombia"  27 2001
       -.02534342   .009462357  93.80982  101.0484  92.83321 108.68525 "Colombia"  27 2002
       .020062447   .023656845  95.71088 103.46743  93.74853 106.56438 "Colombia"  27 2003
     -.0012931824    .03778648  95.58719 107.45175  89.83081 108.90674 "Colombia"  27 2004
        .03576851    .03366089  99.06808  111.1302    83.566 107.87046 "Colombia"  27 2005
        .11516142    .05212021 111.15977  117.0761   88.3865  93.60452 "Colombia"  27 2006
      -.068422794    .05286407  103.8083  123.4317  86.35629  89.10132 "Colombia"  27 2007
       .067825794    .02054596 111.09341  125.9939  99.90175  78.20176 "Colombia"  27 2008
        .07355642   .000207901  119.5731    126.02  102.6376  71.38212 "Colombia"  27 2009
      -.016270638    .03345871 117.64333 130.30786  84.60494  82.87823 "Colombia"  27 2010
    end
    label values pais pais
    label def pais 1 "Argentina", modify
    label def pais 2 "Barbados", modify
    label def pais 3 "Belice", modify
    label def pais 4 "Bolivia", modify
    label def pais 5 "Brasil", modify
    label def pais 6 "Chile", modify
    label def pais 27 "Colombia", modify
    I have to made some graphs:

    Code:
    forvalues i=1/25 {
        local v : label(pais )`i'
        twoway line co2pc_index gdppc_index year if Country=="`v'", xtitle("Año") title("`v'") note("Números índice Año Base = 2000", size(vsmall)) caption("") subtitle("") legend(label(1 "CO2pc") label(2 "GDPpc") position(6) size(vsmall)) scheme(s1mono) name(index`i')
        }
    I don't have any observation from pais between 7 and 26.

    How I can avoid this cases?

    Then:

    Code:
    forvalues x = 1/27 {
        local v : label(pais )`x'
        *reg ln_co2pc_gr ln_gdppc_gr if pais==`x'
        reg ln_co2pc_gr ln_gdppc_gr if Country=="`v'"
        predict hat
        predict stdf, stdf
        generate lo = hat - 1.96*stdf
        generate hi = hat + 1.96*stdf
        scatter ln_co2pc_gr ln_gdppc_gr if pais==`x' || line hat lo hi ln_gdppc_gr if pais==`x', pstyle(p2 p3 p3) sort legend(label(1 "inc log CO2pc") label(2 "Valores estimados") label(3 "Intervalo de Confianza") position(6) size(vsmall) row(1)) scheme(s1mono) name(inc`x') note(" ") title("`v'")
        drop hat lo hi stdf
    }

    It stop when x reach 7 but I should graph when x equals 27. How I can solve it?

    Regards,

    Sebastián.

  • #2
    Code:
    levelsof pais, local(xvalues)
    foreach x of local xvalues {
        local v : label(pais )`x'
        *reg ln_co2pc_gr ln_gdppc_gr if pais==`x'
        reg ln_co2pc_gr ln_gdppc_gr if Country=="`v'"
        predict hat
        predict stdf, stdf
        generate lo = hat - 1.96*stdf
        generate hi = hat + 1.96*stdf
        scatter ln_co2pc_gr ln_gdppc_gr if pais==`x' || line hat lo hi ln_gdppc_gr if pais==`x', pstyle(p2 p3 p3) sort legend(label(1 "inc log CO2pc") label(2 "Valores estimados") label(3 "Intervalo de Confianza") position(6) size(vsmall) row(1)) scheme(s1mono) name(inc`x') note(" ") title("`v'")
        drop hat lo hi stdf
    }

    Comment


    • #3
      Great, It's work!

      I forgot to show how I generate pais:

      Code:
      encode Country, gen(pais)
      Can be do it without variable pais?

      I try it:

      Code:
      levelsof Country, local(xvalues)
      foreach x of local xvalues {
          reg ln_co2pc_gr ln_gdppc_gr if Country=="`x'"
          predict hat
          predict stdf, stdf
          generate lo = hat - 1.96*stdf
          generate hi = hat + 1.96*stdf
          scatter ln_co2pc_gr ln_gdppc_gr Country=="`x'" || line hat lo hi ln_gdppc_gr if Country=="`x'", pstyle(p2 p3 p3) sort legend(label(1 "inc log CO2pc") label(2 "Valores estimados") label(3 "Intervalo de Confianza") position(6) size(vsmall) row(1)) scheme(s1mono) name(inc`x') note(" ") title("`v'")
          drop hat lo hi stdf
      }
      What can I do when a country is called "Costa Rica"? My script fails because I have an non alowed name "incCosta Rica".

      I tried to include strtoname but I don't how to do it.

      Comment


      • #4
        Well, the rules are the rules: no embedded blanks allowed in variable names. So you can't have a variable named -incCosta Rica-, But you can have a variable named -incCosta_Rica-.

        So, before you run the code shown in #3 run
        Code:
        replace Country = strtoname(Country)
        That will solve almost any problem of this nature. The one thing it won't resolve is if there is a problem with the length of the country name. Stata variable names cannot exceed 32 characters. And you are already using up 3 of those for inc. So you can only have 29 characters in a country name. So check your country names: if any of them are longer than that, I suggest replacing it with some shorter name. So, for example, if Democratic Republic of the Congo is one of your country names, you might replace it with "Dem Rep Congo" or something like that. (strtoname() will then replace the blanks with underscores.)

        Comment


        • #5
          Clyde,

          I cannot change Country because I use it as title in all graphs.

          Can I just change it when I named graph?

          Well, I think is better to use your first solution.

          Or is there a way to use foreach with the country variable created by encode and leave levelsof?

          Greetings,

          Sebastián.

          Comment


          • #6
            I don't see where you use Country in the graph title. The graph command you show has -title(`v')-. There is no local macro v in the code you show, but I assume it is defined somewhere earlier. (If it isn't then you get no title at all.)

            But, be that as it may, instead of using -strtoname()- before starting the loop, you can do this:

            Code:
            levelsof Country, local(xvalues)
            foreach x of local xvalues {
                reg ln_co2pc_gr ln_gdppc_gr if Country=="`x'"
                predict hat
                predict stdf, stdf
                generate lo = hat - 1.96*stdf
                generate hi = hat + 1.96*stdf
                local graphname = strtoname(`"`x'"')
                scatter ln_co2pc_gr ln_gdppc_gr Country=="`x'" || line hat lo hi ln_gdppc_gr if Country=="`x'", ///
                    pstyle(p2 p3 p3) sort legend(label(1 "inc log CO2pc") label(2 "Valores estimados") label(3 "Intervalo de Confianza") position(6) ///
                    size(vsmall) row(1)) scheme(s1mono) name(inc`graphname') note(" ") title("`v'")
                drop hat lo hi stdf
            }

            Comment


            • #7
              I defined

              Code:
              encode(Country), gen(pais)
              to

              Code:
              xtset pais year
              but I cannot use xvalues from #6 to xtset

              so I should use #2

              Comment


              • #8
                I made some changes to #6
                Code:
                drop hat-hi
                levelsof Country, local(xvalues)
                foreach x of local xvalues {
                    reg ln_co2pc_gr ln_gdppc_gr if Country=="`x'"
                    predict hat
                    predict stdf, stdf
                    generate lo = hat - 1.96*stdf
                    generate hi = hat + 1.96*stdf
                    local graphname = strtoname(`"`x'"')
                    scatter ln_co2pc_gr ln_gdppc_gr || line hat lo hi ln_gdppc_gr, pstyle(p2 p3 p3) sort legend(label(1 "inc log CO2pc") label(2 "Valores estimados") label(3 "Intervalo de Confianza") position(6) size(vsmall) row(1)) scheme(s1mono) name(inc`graphname') note(" ") title("`x'") ||  if Country=="`x'"
                    drop hat lo hi stdf
                }
                Are it fine?

                Comment


                • #9
                  Well, the initial -drop hat-hi- is OK only if variables hat through hi already exist. It looks to me like you create them in the loop that follows--which means they wouldn't exist before the loop. Now maybe somewhere earlier in your code you have these variables, and then it's ok. But otherwise, that will break.

                  I have one other suggestion to make. It's not strictly necessary, but you might find yourself in a position where you run this code, and then you have to re-run it (maybe after some small change to the code or data). If the Graph windows are still open, all those graphs named incArgentina, etc. will already be there and Stata will consider it an error when you try to re-create them. You can prevent that by changing -name(inc`graphname')- to -name(inc`graphname', replace)-. That way Stata will know it's OK to overwrite the existing graph.

                  Everything else looks OK to me. And, after eliminating -drop hat-hi-, it runs without error messages and produces some nice-looking graphs when I try it out on the example data in #1. So, I think it's good to go.

                  Comment


                  • #10
                    Sorry, you are right, I forgot to delete drop hat-hi from the code.

                    Is equivalent
                    Code:
                    scatter ln_co2pc_gr ln_gdppc_gr || line hat lo hi ln_gdppc_gr, pstyle(p2 p3 p3) sort legend(label(1 "inc log CO2pc") label(2 "Valores estimados") label(3 "Intervalo de Confianza") position(6) size(vsmall) row(1)) scheme(s1mono) name(inc`graphname') note(" ") title("`x'") ||  if Country=="`x'"
                    to
                    Code:
                    scatter ln_co2pc_gr ln_gdppc_gr if Country=="`x'" || line hat lo hi ln_gdppc_gr, pstyle(p2 p3 p3) sort legend(label(1 "inc log CO2pc") label(2 "Valores estimados") label(3 "Intervalo de Confianza") position(6) size(vsmall) row(1)) scheme(s1mono) name(inc`graphname') note(" ") title("`x'") if Country=="`x'
                    or is redundent the first
                    Code:
                    if Country=="`x'"
                    ?

                    I found an simpler way to do it but CI are different:
                    Code:
                    levelsof Country, local(xvalues)
                    foreach x of local xvalues {
                        local graphname = strtoname(`"`x'"')
                        graph twoway (lfitci ln_co2pc_gr ln_gdppc_gr, stdf pstyle(p2 p3 p3)) (scatter ln_co2pc_gr ln_gdppc_gr, pstyle(p2 p3 p3) sort legend(label(1 "inc log CO2pc") label(2 "Valores estimados") label(3 "Intervalo de Confianza") position(6) size(vsmall) row(1)) scheme(s1mono) name(inc`graphname', replace) note(" ") title("`x'")) if Country=="`x'"
                    }

                    Comment


                    • #11
                      Your second -graph twoway- command is not syntactically valid. You can't have the -if country == "`x'"- dangling at the end there. The -if- clause is not an option, so it cannot follow the comma. In the first version, you got away with it because of the || immediately preceding it, which might it strange but legal. The robust way to code this is:
                      Code:
                      scatter ln_co2pc_gr ln_gdppc_gr if Country=="`x'" || line hat lo hi ln_gdppc_gr if Country == "`x'", ///
                      pstyle(p2 p3 p3) sort legend(label(1 "inc log CO2pc") label(2 "Valores estimados") label(3 "Intervalo de Confianza") ///
                      position(6) size(vsmall) row(1)) scheme(s1mono) name(inc2`graphname') note(" ") title("`x'")
                      I found an simpler way to do it but CI are different:
                      Moral of the story: when using a new command, always read the help file carefully. In the help file it is made very clear that the default confidence interval used by -lfitci- is derived from stdp, which is different from your original code which (explicitly) uses stdf. You can specify the -stdf- option in -lfitci- to get a match to your original code, but because you didn't you got the default stdp instead. Next time, read the help file.

                      Comment


                      • #12
                        Originally posted by Clyde Schechter View Post
                        Moral of the story: when using a new command, always read the help file carefully. In the help file it is made very clear that the default confidence interval used by -lfitci- is derived from stdp, which is different from your original code which (explicitly) uses stdf. You can specify the -stdf- option in -lfitci- to get a match to your original code, but because you didn't you got the default stdp instead. Next time, read the help file.
                        If you read my script carefully you can see that I used the stdf option in fourth line of my last code in #10.

                        The differences do not arise from the confidence interval being defined from stdp or stdf. It was used stdf in both. CI area is white and with black border line in my original script but CI area is colored and without border line in my last script. The markers also have a differente shape.

                        Graph "old" version:
                        Click image for larger version

Name:	inc_old.jpg
Views:	1
Size:	31.0 KB
ID:	1652185

                        Graph "new" version:
                        Click image for larger version

Name:	inc_new.jpg
Views:	1
Size:	26.0 KB
ID:	1652186

                        In both plots it can be seen that the graph is not centered, the framed red area is free. How I con solve it? What options have to be changed in second graph to be identical to the first one?
                        Attached Files

                        Comment


                        • #13
                          You are right, I didn't read your command carefully enough. You did use -stdf- in both cases. My apologies. But then, also, looking at your graphs, it seems to me the confidence intervals are the same in both graphs, so I don't know what the issue raised in your final question in #10.

                          In both plots it can be seen that the graph is not centered, the framed red area is free. How I con solve it? What options have to be changed in second graph to be identical to the first one?
                          I don't follow your question. Both the old and new versions you show are badly centered. They differ instead in terms of the shading and the shape of the markers. To eliminate the shading, I think you would have to specify the -ciplot()- option to -lfitci- since it's default is -area- which results in a shaded area. I guess I would specify it as -ciplot(line)-, though I have never used -lfitci-, so I can't say that with confidence. Similarly, though I have no experience with -lfitci-, I would imagine that using the -msymbol()- option that is part of -scatter- will do that.

                          As for getting the graphs centered, I'm stumped.The problem arises because Stata is trying to put "nice" values on the x-axis, and for some of the countries, the nature of the data is that Stata feels compelled to go farther beyond the range of the plotted data than is visibly nice to achieve that goal. If you specif -xlabel(minmax)- you do get centered graphs, but the value labels that result are ugly, have unrealistically high apparent precision, and, of course, only occupy the ends of the axis. I tried experimenting with various values of -xlabel(#)- but nothing I tried produced consistently centered graphs. You might have to actually go through the data to identify the value labels you want separately for each country and "force feed" that to the graph commands. That is going to be tedious, both to find the values that would produce a good look, and to program its implementation.

                          Maybe somebody else knows a better way to solve this centering problem. If so, I hope they will jump in here.

                          Comment


                          • #14
                            Dear Sebastian and Clyde, how are you? I have a question regarding foreach using label. I'm trying to center, loop and also create a new variable merging previous continuous variables. I'm having a problem with merging. The code seems to work for centering, but give me different variables for each one I added. It looks like this:

                            foreach v of var (neuro5e_8 neuro5e_7 neuro5e_6 neuro5e_5 neuro5e_4 neuro5e_3) {
                            noisily display "This is variable: ""`v'"
                            display `"`: var label `v''"'
                            egen z2`v' = std(`v')
                            summarize z2`v'
                            gen centered_z2`v' = z2`v' - r(min)
                            summarize centered_z2`v'
                            }

                            Click image for larger version

Name:	Screenshot 2022-08-30 at 11.00.34.png
Views:	1
Size:	100.0 KB
ID:	1679858


                            This is not what I wanted, instead I'm trying to make just one big 'neuro' variable. Could you help me?

                            Thank you so much!

                            Comment


                            • #15
                              #14 Maria Cade

                              Please show the results of

                              Code:
                              dataex neuro5e_8 neuro5e_7 neuro5e_6 neuro5e_5 neuro5e_4 neuro5e_3
                              and explain what you want to see in addition in terms of this display.

                              Comment

                              Working...
                              X