Announcement

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

  • Looping histograms with unique bin widths

    Statalist Forum,

    I would like some help creating a loop to generate histograms. Is it possible to reference variables when specifying "width" and "start" or must these be numeric?

    I have five variables (va1-var5) and there are 10 time periods (identified by var6); I'd like to create histograms by variable and time period (resulting in 50 distinct histograms). Additionally, I'm investigating a distributional discontinuity around zero so I'd like to ensure that no bin straddles 0 (the bin to the left of zero has variables values less than 0 and the bin to the right has variable values greater than or equal to 0) and I have only 20 bins total so that my x axis scale is tight around 0. Finally, each histogram will have it's own unique bin width based on descriptive statistics for that variable and time period. I have calculated desired bin widths for each desired histogram and they are called var1_binwidth, var2_binwidth, etc.

    In order to 1) loop the histogram command, 2) center each histogram on 0 with 20 bins, and 3) allow for unique bin widths by histogram, I am running (unsuccessfully) the following program.

    set more off
    foreach x of varlist var1 var2 var3 var4 var5 {
    forvalues i=1/10{
    histogram `x' if var6==`i' & `x'>=(`x'_binwidth*-10) & `x'<=(`x'_binwidth*10), ///
    width(`x'_binwidth) start(`x'_binwidth*-10) frequency addlabel fcolor(black) ///
    lcolor(white) ytitle(# of Observations) xtitle(Scaled Distance to Threshold) ///
    title("`x' Histogram") subtitle("Quarter=`i'")
    graph export `x'_`i'.png
    }
    }

    For example, the histogram for var1 in period 1 would be:
    histogram var1 if var6==1 & var1>=(-3.198306358) & var1<=(3.198306358), ///
    width(0.3198306358) start(-3.198306358) frequency addlabel fcolor(black) ///
    lcolor(white) ytitle(# of Observations) xtitle(Scaled Distance to Threshold) ///
    title("var1 Histogram") subtitle("Period=1")
    graph export var1_1.png


    Below is the output from using the dataex command. Thanks






    dataex var1 var2 var3 var4 var5 var6 var1_binwidth var2_binwidth var3_binwidth var4_binwidth var5_binwidth

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(var1 var2 var3 var4 var5 var6 var1_binwidth var2_binwidth var3_binwidth var4_binwidth var5_binwidth)
      1.909684986595174                  . .    .2863459174605927 .  1 .31983063576795845 1.2612228507616505  .4359744872042019 .13485509726744171 .27758881773335553
     1.8685012836970474                  . .   .22409344949688526 .  2  .2966752741043353 1.3704188874108245  .4496248388326724  .1345159581602156  .2810090244671412
     2.4249063365368952                  . .    .2890558510241195 .  3 .27057367532398935  1.227539935344309  .4119085084578788 .14234493903421291 .30787437152326874
     2.7614290551981044                  . .    .2233753900420567 .  4 .27069416170478483 1.1120183130338988  .4187668904608991  .1429269373755315  .3160051327280103
     3.0030901287553657                  . .   .20201086588967293 .  5 .27058002254868113 1.1078446876030397  .4297015286826252 .15310682064090125  .3270739589258046
      2.973058501539514                  . .    .2345032738391996 .  6  .2810002691179573  1.139895708131415  .4679812827171502 .15577704931311906  .3330216618063794
     2.6070845576232453                  . .    .3976118366680745 .  7 .28799122624183837 1.3118069441183313 .46136986995961843   .159405475987049 .39335858365697124
     2.5133540372670806                  . .   .22542669962167108 .  8  .2893366683100606  1.294292837470743  .5100104684541729 .16259758830446958   .440582623227503
     2.5825107958050584                  . .   .19757686576960184 .  9  .2915314214134848 1.2295291384347233  .5063997962335158 .15509023915001038  .4493919845257831
      2.756689342403628                  . .    .3115577528003682 . 10  .2953432196592178 1.2994009723933633  .5229288363219519 .16560341024267622 .46434285549165427
                      .                  . .   .42450709531360004 .  1 .31983063576795845 1.2612228507616505  .4359744872042019 .13485509726744171 .27758881773335553
                      .                  . .    .5562325146059417 .  2  .2966752741043353 1.3704188874108245  .4496248388326724  .1345159581602156  .2810090244671412
                      .                  . .   .34971650685041483 .  3 .27057367532398935  1.227539935344309  .4119085084578788 .14234493903421291 .30787437152326874
                      .                  . .    .4318024050616739 .  4 .27069416170478483 1.1120183130338988  .4187668904608991  .1429269373755315  .3160051327280103
                      .                  . .     .751301627326002 .  5 .27058002254868113 1.1078446876030397  .4297015286826252 .15310682064090125  .3270739589258046
                      .                  . .  -.06913121891905934 .  6  .2810002691179573  1.139895708131415  .4679812827171502 .15577704931311906  .3330216618063794
                      .                  . .  -.05935693511182436 .  7 .28799122624183837 1.3118069441183313 .46136986995961843   .159405475987049 .39335858365697124
                      .                  . . -.030996913904872515 .  8  .2893366683100606  1.294292837470743  .5100104684541729 .16259758830446958   .440582623227503
                      .                  . .   .01702006665453017 .  9  .2915314214134848 1.2295291384347233  .5063997962335158 .15509023915001038  .4493919845257831
                      .                  . .   .24525787024797102 . 10  .2953432196592178 1.2994009723933633  .5229288363219519 .16560341024267622 .46434285549165427
                      .                  . .                    . .  1 .31983063576795845 1.2612228507616505  .4359744872042019 .13485509726744171 .27758881773335553
                      .                  . .                    . .  2  .2966752741043353 1.3704188874108245  .4496248388326724  .1345159581602156  .2810090244671412
                      .                  . .                    . .  3 .27057367532398935  1.227539935344309  .4119085084578788 .14234493903421291 .30787437152326874
                      .                  . .                    . .  4 .27069416170478483 1.1120183130338988  .4187668904608991  .1429269373755315  .3160051327280103
                      .                  . .                    . .  5 .27058002254868113 1.1078446876030397  .4297015286826252 .15310682064090125  .3270739589258046
                      .                  . .                    . .  6  .2810002691179573  1.139895708131415  .4679812827171502 .15577704931311906  .3330216618063794
                      .                  . .                    . .  7 .28799122624183837 1.3118069441183313 .46136986995961843   .159405475987049 .39335858365697124
                      .                  . .                    . .  8  .2893366683100606  1.294292837470743  .5100104684541729 .16259758830446958   .440582623227503
                      .                  . .                    . .  9  .2915314214134848 1.2295291384347233  .5063997962335158 .15509023915001038  .4493919845257831
                      .                  . .                    . . 10  .2953432196592178 1.2994009723933633  .5229288363219519 .16560341024267622 .46434285549165427
      .7200102092904542                  . .    .9140853302162479 .  1 .31983063576795845 1.2612228507616505  .4359744872042019 .13485509726744171 .27758881773335553
      .8836672738312087                  . .    .8926924441291235 .  2  .2966752741043353 1.3704188874108245  .4496248388326724  .1345159581602156  .2810090244671412
     1.2227006338954671                  . .    .9509232866526212 .  3 .27057367532398935  1.227539935344309  .4119085084578788 .14234493903421291 .30787437152326874
      2.112093308778391                  . .    .7675036075036075 .  4 .27069416170478483 1.1120183130338988  .4187668904608991  .1429269373755315  .3160051327280103
     2.3386504620838084                  . .    .7937422807739809 .  5 .27058002254868113 1.1078446876030397  .4297015286826252 .15310682064090125  .3270739589258046
      2.848446147296722                  . .    .7877118421743008 .  6  .2810002691179573  1.139895708131415  .4679812827171502 .15577704931311906  .3330216618063794
     3.7945794083622544                  . .    .9292974672881451 .  7 .28799122624183837 1.3118069441183313 .46136986995961843   .159405475987049 .39335858365697124
      4.936895083236546                  . .   1.2915705529559087 .  8  .2893366683100606  1.294292837470743  .5100104684541729 .16259758830446958   .440582623227503
      6.101706484641638                  . .   1.6888185654008439 .  9  .2915314214134848 1.2295291384347233  .5063997962335158 .15509023915001038  .4493919845257831
      5.862398822663724                  . .   2.1637630662020904 . 10  .2953432196592178 1.2994009723933633  .5229288363219519 .16560341024267622 .46434285549165427
                      .                  . .                    . .  1 .31983063576795845 1.2612228507616505  .4359744872042019 .13485509726744171 .27758881773335553
                      .                  . .                    . .  2  .2966752741043353 1.3704188874108245  .4496248388326724  .1345159581602156  .2810090244671412
                      .                  . .                    . .  3 .27057367532398935  1.227539935344309  .4119085084578788 .14234493903421291 .30787437152326874
                      .                  . .                    . .  4 .27069416170478483 1.1120183130338988  .4187668904608991  .1429269373755315  .3160051327280103
                      .                  . .                    . .  5 .27058002254868113 1.1078446876030397  .4297015286826252 .15310682064090125  .3270739589258046
                      .                  . .                    . .  6  .2810002691179573  1.139895708131415  .4679812827171502 .15577704931311906  .3330216618063794
                      .                  . .                    . .  7 .28799122624183837 1.3118069441183313 .46136986995961843   .159405475987049 .39335858365697124
                      .                  . .                    . .  8  .2893366683100606  1.294292837470743  .5100104684541729 .16259758830446958   .440582623227503
                      .                  . .                    . .  9  .2915314214134848 1.2295291384347233  .5063997962335158 .15509023915001038  .4493919845257831
                      .                  . .                    . . 10  .2953432196592178 1.2994009723933633  .5229288363219519 .16560341024267622 .46434285549165427
     -.0346871484541681                  . .    .5255263562317504 .  1 .31983063576795845 1.2612228507616505  .4359744872042019 .13485509726744171 .27758881773335553
     .06964410939691446                  . .   .14699409674205585 .  2  .2966752741043353 1.3704188874108245  .4496248388326724  .1345159581602156  .2810090244671412
      .2469601061878432                  . .   .14961562566482098 .  3 .27057367532398935  1.227539935344309  .4119085084578788 .14234493903421291 .30787437152326874
      .7512985990870456                  . .  .045743005025879434 .  4 .27069416170478483 1.1120183130338988  .4187668904608991  .1429269373755315  .3160051327280103
     1.9373890000845715                  . .  -.00466676991378856 .  5 .27058002254868113 1.1078446876030397  .4297015286826252 .15310682064090125  .3270739589258046
     3.8575475874280656                  . .   .07066062570742397 .  6  .2810002691179573  1.139895708131415  .4679812827171502 .15577704931311906  .3330216618063794
      4.230498425050954                  . .   .36109608797548215 .  7 .28799122624183837 1.3118069441183313 .46136986995961843   .159405475987049 .39335858365697124
      4.376136923697299                  . .   .23029069347192976 .  8  .2893366683100606  1.294292837470743  .5100104684541729 .16259758830446958   .440582623227503
     3.7914577311073083                  . .   .12601803374054685 .  9  .2915314214134848 1.2295291384347233  .5063997962335158 .15509023915001038  .4493919845257831
      2.686216153127918                  . .    .2232321718912139 . 10  .2953432196592178 1.2994009723933633  .5229288363219519 .16560341024267622 .46434285549165427
                      . -81.39058629232048 .                    . .  1 .31983063576795845 1.2612228507616505  .4359744872042019 .13485509726744171 .27758881773335553
                      .  -33.8713550600343 .                    . .  2  .2966752741043353 1.3704188874108245  .4496248388326724  .1345159581602156  .2810090244671412
                      .  207.3934533551555 .                    . .  3 .27057367532398935  1.227539935344309  .4119085084578788 .14234493903421291 .30787437152326874
                      . 1.5939569122812212 .                    . .  4 .27069416170478483 1.1120183130338988  .4187668904608991  .1429269373755315  .3160051327280103
                      . -369.9003370245565 .                    . .  5 .27058002254868113 1.1078446876030397  .4297015286826252 .15310682064090125  .3270739589258046
                      .  252.8139018197203 .                    . .  6  .2810002691179573  1.139895708131415  .4679812827171502 .15577704931311906  .3330216618063794
                      .  62.33231985405895 .                    . .  7 .28799122624183837 1.3118069441183313 .46136986995961843   .159405475987049 .39335858365697124
                      .   .758606005037338 .                    . .  8  .2893366683100606  1.294292837470743  .5100104684541729 .16259758830446958   .440582623227503
                      .   52.4697303137004 .                    . .  9  .2915314214134848 1.2295291384347233  .5063997962335158 .15509023915001038  .4493919845257831
                      .  49.53904958677682 .                    . . 10  .2953432196592178 1.2994009723933633  .5229288363219519 .16560341024267622 .46434285549165427
                      .                  . .   3.7337102052960427 .  1 .31983063576795845 1.2612228507616505  .4359744872042019 .13485509726744171 .27758881773335553
                      .                  . .    3.231887691150664 .  2  .2966752741043353 1.3704188874108245  .4496248388326724  .1345159581602156  .2810090244671412
                      .                  . .    2.793748624257099 .  3 .27057367532398935  1.227539935344309  .4119085084578788 .14234493903421291 .30787437152326874
                      .                  . .     2.91233413095497 .  4 .27069416170478483 1.1120183130338988  .4187668904608991  .1429269373755315  .3160051327280103
                      .                  . .    3.680727874276262 .  5 .27058002254868113 1.1078446876030397  .4297015286826252 .15310682064090125  .3270739589258046
      .7576966036045879                  . .    .2696484047536859 .  1 .31983063576795845 1.2612228507616505  .4359744872042019 .13485509726744171 .27758881773335553
      .8055626489615252                  . .    .5447924310683211 .  2  .2966752741043353 1.3704188874108245  .4496248388326724  .1345159581602156  .2810090244671412
      .7056218433448235                  . .    .3091278009189257 .  3 .27057367532398935  1.227539935344309  .4119085084578788 .14234493903421291 .30787437152326874
      .7001447644239833                  . .    .4131477130804636 .  4 .27069416170478483 1.1120183130338988  .4187668904608991  .1429269373755315  .3160051327280103
      .7255837359617093                  . .    .2972160881792273 .  5 .27058002254868113 1.1078446876030397  .4297015286826252 .15310682064090125  .3270739589258046
       .706296650717703                  . .    .2886737883203982 .  6  .2810002691179573  1.139895708131415  .4679812827171502 .15577704931311906  .3330216618063794
      .7731989579048403                  . .  .036914446215169656 .  7 .28799122624183837 1.3118069441183313 .46136986995961843   .159405475987049 .39335858365697124
      .8129459547775837                  . .   .11512160838585712 .  8  .2893366683100606  1.294292837470743  .5100104684541729 .16259758830446958   .440582623227503
       .729118458137683                  . .   .12089147711771707 .  9  .2915314214134848 1.2295291384347233  .5063997962335158 .15509023915001038  .4493919845257831
       .617522063198099                  . .  -.03943454755463982 . 10  .2953432196592178 1.2994009723933633  .5229288363219519 .16560341024267622 .46434285549165427
    -.13892036066657232                  . .    -.041036274971717 .  1 .31983063576795845 1.2612228507616505  .4359744872042019 .13485509726744171 .27758881773335553
    -.33917072662464437                  . .   .08680710239928739 .  2  .2966752741043353 1.3704188874108245  .4496248388326724  .1345159581602156  .2810090244671412
     -.3061677763760963                  . .   .08090433239594154 .  3 .27057367532398935  1.227539935344309  .4119085084578788 .14234493903421291 .30787437152326874
    -.24376206523387609                  . .   .08455459571845196 .  4 .27069416170478483 1.1120183130338988  .4187668904608991  .1429269373755315  .3160051327280103
    -.15276680724347808                  . .   .10564663999996105 .  5 .27058002254868113 1.1078446876030397  .4297015286826252 .15310682064090125  .3270739589258046
    -.09788213277229345                  . .   .12160140993540851 .  6  .2810002691179573  1.139895708131415  .4679812827171502 .15577704931311906  .3330216618063794
    -.07516957146448419                  . .   .07521011257941661 .  7 .28799122624183837 1.3118069441183313 .46136986995961843   .159405475987049 .39335858365697124
    -.11618825568989227                  . .   .05625439044991534 .  8  .2893366683100606  1.294292837470743  .5100104684541729 .16259758830446958   .440582623227503
    -.20388652800810753                  . .   .06601909214412167 .  9  .2915314214134848 1.2295291384347233  .5063997962335158 .15509023915001038  .4493919845257831
    -.31554383601076086                  . .   .07433046357588026 . 10  .2953432196592178 1.2994009723933633  .5229288363219519 .16560341024267622 .46434285549165427
      .4141190409509704                  . .                    . .  1 .31983063576795845 1.2612228507616505  .4359744872042019 .13485509726744171 .27758881773335553
      .4143482301169736                  . .                    . .  2  .2966752741043353 1.3704188874108245  .4496248388326724  .1345159581602156  .2810090244671412
     .44142814955201565                  . .                    . .  3 .27057367532398935  1.227539935344309  .4119085084578788 .14234493903421291 .30787437152326874
     .47219797071813174                  . .                    . .  4 .27069416170478483 1.1120183130338988  .4187668904608991  .1429269373755315  .3160051327280103
      .5881195527091141                  . .                    . .  5 .27058002254868113 1.1078446876030397  .4297015286826252 .15310682064090125  .3270739589258046
    end
    ------------------ copy up to and including the previous line ------------------


  • #2
    No, you can't put a variable name in the start() or width() options. How would Stata even know which value of those variables to refer to? So what you need to do is select the correct value of the binwidth before you reach the -histogram- command, save it in a local macro, and then use that to calculate numbers inside the options:

    Code:
    foreach x of varlist var1 var2 var3 var4 var5 {
        forvalues i=1/10{
            summ `x'_binwidth if var6 == `i', meanonly
            local binwidth `r(mean)'
            histogram `x' if var6==`i' & `x'>=(`binwidth'*-10) & `x'<=(`=`binwidth'*10'), ///
            width(`binwidth') start(`=`binwidth'*-10') frequency addlabel fcolor(black) ///
            lcolor(white) ytitle(# of Observations) xtitle(Scaled Distance to Threshold) ///
            title("`x' Histogram") subtitle("Quarter=`i'") name(`x'_`i', replace)
            graph export `x'_`i'.png
        }
    }
    Note: This runs fine for the var1 histograms in your data example, but it breaks when we get to var2 because there are apparently not enough observations with non-missing values to create them. I assume your real data is larger and this won't be a problem. Similarly, it would clearly break on var3 and var5 because there are no non-missing observations of those at all in your example. Again, I assume this is not true of your real data.

    Finally, the code you show in #1 appears in a rather disjointed form, lacking indentation, and with things like "<=" replaced by "&gt;=". It required heavy editing to work with it. In the future, please be sure to surround code with code delimiters (as I have done here) to assure readability and integrity. If you are not familiar with code delimiters, you can find instructions in Forum FAQ #12, or in this video by David Benson : https://youtu.be/bXfaRCAOPbI

    Comment

    Working...
    X