Announcement

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

  • Trouble plotting real and predicted data in Stata: issues with code or data?

    Hey! I'm having trouble with the following code in Stata, which is supposed to plot lines for real data and predicted data for each Bundesland (i from 1 to 9), with a legend indicating which line corresponds to which data:
    forvalues i = 1/9 { twoway (line rel_Energiekonsum Jahr if Bundesland == `i', lcolor(black) lpattern(solid)) (line yhat Jahr if Bundesland == `i', lcolor(blue) lpattern(dash)) , legend(order(1 "Real data" 2 "Predicted data") lcolor(black blue)) xtitle("Year") ytitle("rel_Energiekonsum") title("Predicted vs. real data for Bundesland `i'") }



    However, the plot doesn't appear as intended, and I'm not sure what the issue is (only the real data is plottet). I've already double-checked that the closing brace } is present at the end of the code, but I'm not sure if there are other issues with the code itself or with my data. The data used "Jahr" and "Bundesland" are used to create the panel. Bundesland is cathegorical from 1 to 9.

    Here the error messege:

    forvalues i = 1/9 {
    2.
    . twoway (line rel_Energiekonsum Jahr if Bundesland == `i', lcolor(black) lpattern(solid))
    3.
    . (line yhat Jahr if Bundesland == `i', lcolor(blue) lpattern(dash))
    4.
    . , legend(order(1 "Real data" 2 "Predicted data") lcolor(black blue))
    5.
    . xtitle("Year") ytitle("rel_Energiekonsum") title("Predicted vs. real data for Bundesland `i'")
    6.
    . }
    ( is not a valid command name
    r(199);



    Could someone help me identify the issue and suggest a possible solution? Any help would be greatly appreciated!

  • #2
    This looks like something better run from a do-file editor window with code more like


    Code:
    forvalues i = 1/9 {
    
    twoway (line rel_Energiekonsum Jahr if Bundesland == `i', lcolor(black) lpattern(solid)) ///
    (line yhat Jahr if Bundesland == `i', lcolor(blue) lpattern(dash)) ///
    legend(order(1 "Real data" 2 "Predicted data") lcolor(black blue)) ///
    xtitle("Year") ytitle("rel_Energiekonsum") title("Predicted vs. real data for Bundesland `i'")
    
    }
    That said, the first 8 graphs will just flash by, so you should add a name() or saving() option to put each graph somewhere.
    Last edited by Nick Cox; 19 Apr 2023, 06:43.

    Comment


    • #3
      I dont get why this works for you, i get the error :
      ) required
      r(100);

      But I don“t see where ther is a missing or unnecessary )

      Comment


      • #4
        I did an automated count of the code in #2 and find 12 ( and 12 ). That doesn't rule out other problems.

        I can't test the code on your data because you don't give a data example.

        I don't use the ( ) syntax around twoway calls for precisely this reason: there are already enough parentheses to keep track of.

        Looking at it again, I suspect that you would be better off with something more like this, as I can't work out why lcolor() is given the third time.

        Code:
         
         forvalues i = 1/9 {  twoway line rel_Energiekonsum Jahr if Bundesland == `i', lcolor(black) lpattern(solid) /// || line yhat Jahr if Bundesland == `i', lcolor(blue) lpattern(dash) /// legend(order(1 "Real data" 2 "Predicted data"))  /// xtitle("Year") ytitle("rel_Energiekonsum") title("Predicted vs. real data for Bundesland `i'")  }
        That said, the first 8 graphs will just flash by, so you should add a name() or saving() option to put each graph somewhere.
        Still true.

        Comment

        Working...
        X