Announcement

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

  • Creation of Merit Order Diagramm in Stata

    Hello,

    I would like to create a merit order graph in stata (as I will have to automatize the graph generation). On the y-axis the costs per unit generated are displayed. On the x-axis the accumulated capacity is displayed. The bar width is defined by the generation capacity of one country. The color of the bar is defined by the country iso digit.

    The data looks like this:
    INDEX country (defines color) cost (x-axis) capacity (bar width)
    1 EH 18.495 50.00
    2 EH 18.495 100.00
    3 EH 18.495 170.00
    4 EH 18.671 60.00
    5 MA 19.639 20.00
    6 MA 19.764 400.00

    It should look like the figure attached:

    (source: Kranner)


    I have tried to use the graph command but was not able to achieve the result above.


    Thank you very much in advance
    Martin


  • #2
    The example below uses the auto dataset. The key is to expand the observations by the variable indicating capacity, so that the bars representing the respective power source are drawn multiple times. The variable "rep78" is used to indicate the energy source.
    Code:
    sysuse auto, clear
    drop if rep78==.
    
    gen cost = price/displacement
    gen capacity = displacement
    
    gen energy = rep78
    lab def energy 1 "Solar" 2 "Wind" 3 "Uranium" 4 "Hydro" 5 "Biomass"
    lab val energy energy
    
    expand capacity
    sort cost capacity
    gen sort = _n
    
    #delimit ;
    graph twoway
      (bar cost sort if energy==1)
      (bar cost sort if energy==2)
      (bar cost sort if energy==3)
      (bar cost sort if energy==4)
      (bar cost sort if energy==5),
      xlabel(0(2000)14000)
      ylabel(0(20)80, angle(0))
      xtitle("Capacity")
      ytitle("Marginal cost")
      legend(label(1 "Solar") label(2 "Wind") label(3 "Uranium")
        label(4 "Hydro") label(5 "Biomass") row(2))
    ;
    #delimit cr
    Click image for larger version

Name:	Merit order diagram.png
Views:	4
Size:	35.7 KB
ID:	1291953

    Comment


    • #3
      Thank you very much. I have used your example but the problem is that the range of the energy value in my data is so huge that I got this error message after expand:

      sum of expand values exceed 2,147,483,647
      The dataset may not contain more than 2,147,483,647 observations.

      Comment


      • #4
        Working with your values divided by say 1 million and rounded to integers should be more than sufficient for graphical purposes. In fact 100 million might work fine.

        Comment


        • #5
          It is working. I have rounded the costs and capacity. This reduces the amount already quite strongly. There is a test result with fictive data attached.

          Thank you very much.

          Martin

          Comment


          • #6
            I would recommend one modification of the code in my original post. Instead of sort cost capacity I would suggest sort cost capacity energy. This ensures that bars for the same type of energy (or for the same country) are drawn next to each other if the cost and capacity are the same. In the case of my example, there is no difference in sort order and the graph in Figure 1 looks the same as in my earlier post.

            Figure 1: Unrounded cost, sorted by cost, capacity and energy
            Click image for larger version

Name:	Merit order diagram 1 unrounded.png
Views:	4
Size:	35.7 KB
ID:	1292296


            Rounding the cost (replace cost = round(cost)) only has an effect on the memory needed by Stata if it is followed by compress. The dataset in the example I gave has 13,662 observations; rounding the cost and compressing reduces the memory used for data by about 6% from 642,114 bytes to 601,128 bytes.

            Rounding the cost results in less variation in bar height, as you can see by comparing Figures 1 and 2. The disadvantage of rounding the cost is that it can affect the sort order so that the countries or power sources may not be drawn in the same order as with unrounded cost. Figure 2 has the same data as Figure 1, with the cost rounded to integers and the sort order unchanged from Figure 1.

            Figure 2: Rounded cost, sort order same as before rounding
            Click image for larger version

Name:	Merit order diagram 2 rounded.png
Views:	3
Size:	35.8 KB
ID:	1292294


            If the observations are resorted by cost, capacity and energy after rounding the order of the bars is different. Compare for example the rightmost bars, where two biomass bars with the same rounded cost are drawn separately in Figure 2 but next to each other in Figure 3. The correct merit order will only be shown with unrounded cost (Figure 1) or by determining the sort order before rounding (Figure 2).

            Figure 3: Rounded cost, resorted by cost, capacity and energy after rounding
            Click image for larger version

Name:	Merit order diagram 3 rounded resorted.png
Views:	2
Size:	36.4 KB
ID:	1292295

            Comment

            Working...
            X