Announcement

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

  • Line graph of averages

    hi everybody,

    I have difficulties to create a line graph with averages from different observations. On the y axis I have 10 different variables with a each different scale and I on the x axis I have one variable which can have the values 1, 2, 3, 4 and 5
    Now I have many observations y with a given x. I dont wont to have a scatter graph or a lfit line but I would like to generate a graph who shows the average of the different Levels for the x variable and combines These lines.
    I would like to have a graph who shows These Points (mean(yvariable1) if |xvariable=1) next Point (mean(yvariable2)|xvariable=1); (mean(yvariable2|xvariable=2) etc.

    example: graph twoway line mean(yvariable1) xvariable if (...) || line mean (yvariable 2) xvariable if (...)

    unfortunately gen newvar = mean(oldvar) does not work and exist
    egen also does not help me because I cannot replace with mean(var) the variable I created with the fucntion egen


    I would really be pleased if someone could help me.
    Thanks in advance

    Thomas

  • #2
    Thomas:

    I suppose that "Stata" could be your real family name, but somehow I am betting against. As emphasised on the forum home page and in the FAQ Advice you were asked to read before posting, we do prefer full real names here. Please re-register with your real family name.

    On your question, and working backwards, you are wrong: egen can be very useful here. Here's some code:

    Code:
     
    forval j = 1/10 { 
        egen mean_y`j' = mean(y`j'), by(x) 
    }
    line mean_y* x


    Comment


    • #3
      hi Nick,
      thank you for your answer. I will re-Register with my real Family Name.
      However your command creates a mean for each Observation. I simply want to have a graph where the mean of y1 - y10 is on the vertical axis and the x on the horizontal axis
      I thhought I can solve my Problem like this:
      gen mean_y1 = mean(y1) if x=1
      replace mean_y1 = mean(y1) if x=2
      replace mean_y1 = mean(y1) if x=3
      ...
      replace mean_y1 = mean(y1) if x=5
      gen mean_y2 = mean(y2) if x=1
      replace mean_y2 = mean(y2) if x=2
      ...
      replace mean_y2 = mean(y2) if x=5
      gen mean_y3 = mean(y3) if x=1
      ...
      replace mean_y3 = mean(y3) if x=5
      gen mean_y4 = mean(y4) if x=6
      ...

      but as I said gen newvar = mean(oldvar) does not work and "egen" simply does other things than "gen"

      I am sorry for my bad Explanation but you would really do me a favour if you can help me

      Comment


      • #4
        Did you even try the code? It's true that the mean of each variable will be the same for each observation in the same group, but that's just repeating the same point on a graph.

        As you said yourself there is no mean() function in general, so wondering what the solution might be if the syntax were different doesn't help you.

        Here's an example you can repeat

        Code:
         
        webuse grunfeld, clear 
        foreach v in invest mvalue kstock {
            egen mean_`v' = mean(`v') , by(year)
        }
        line mean_* year, c(L ..)

        Comment


        • #5
          when I tried to code I was not Aware that it is repeating the sam Point on the graph, I simply did not recognized it. but my Problem is still that Point 1 and 5 are connected with a line. they are connected with each other but not only from 1 to 2, from 2 to 3, you know what I mean? What do I still wrong?
          Attached Files

          Comment


          • #6
            Already answered within my previous post. You need an option call like c(L ..) If we give you an example you can try, you should try it unless you know that you understand Stata better.

            Comment


            • #7
              Nick, thank you again for your answer. I should have mentioned in my post above that I already considered the Option c(L ..) in the graph I attached since I recognized the difference to the solution you presented. I bother and really strive for a solution on my own but regrettably I cannot find out why c(L ..) does not work in my case. i do not find any Information about this option
              I'd be pleased if you can help me again

              I also found out that c(L ..) only deletes the line connecting the last and the first Point
              Givent this Information I could imagine that the command c(L ..) is the solution to my Problem, but it must be a modification of this command'
              Last edited by Thomas Klein; 05 Mar 2015, 07:39.

              Comment


              • #8
                Please read Section 12 of the FAQ Advice. Please show us the exact command(s) you used.

                Comment


                • #9
                  Hi Nick,
                  it now works but only with the command "sort x" before the line graph
                  However until now, I did not understand why c(L) or even c(L) ist not sufficient as it only connects lines if xj > xj+1. In my view sort x arranges X so that xj > xj+1 holds. So why c(L) does not the same? here is my code

                  Code:
                  forval j = 1/3 {
                  egen mean_y`j' = mean(y`j'), by(x)
                  }
                  *sort x
                  scatter mean_y1 x if z == 0, c(L)
                  hint: c(L ..) or c(L ...) have in my example the same Impact as c(L)

                  I'd prefer to solve my Problem with the c-Option. In case you have any idea, please let me know!


                  And now I also added a variable z:

                  Code:
                  forval j = 1/3 {
                  egen mean_y`j' = mean(y`j'), by(x, z)
                  }
                  sort x
                  line mean_y1 x if z == 0 || mean_y2 x if z == 1, c(L)
                  Stata now says that mean_y1 is not a twoway plot type. Why? Because I added z? Is there any another command who is able to Display my results?
                  Last edited by Thomas Klein; 06 Mar 2015, 01:04.

                  Comment


                  • #10
                    It says what it means and means what it says. mean_y1 is the name of one of your variables and not a type of plot or graph. Syntax that should work here includes

                    Code:
                      
                    line mean_y1 x if z == 0, c(L) || line mean_y2 x if z == 1, c(L)

                    Comment


                    • #11
                      Nick,
                      thank you much for your answer. Now it works. I still have one question conerning the variable Name. In the graph the line is described with the variable Name and not its Label. Is there any possibility to Change this? in Regression tables for instance, "Label" is used; but this command cannot be applied to Graphs.
                      I'd really appreciate it if you can help me one more time.

                      Comment


                      • #12
                        Again, you are not showing exact code so that we can comment. The request is permanent.

                        But here's an example you can replicate:

                        Code:
                         
                        webuse grunfeld, clear 
                        foreach v in invest mvalue kstock {
                            egen mean_`v' = mean(`v') , by(year)
                            label var mean_`v' "mean `v'" 
                        }
                        line mean_* year, c(L ..)
                        So we have a first trick:

                        1. If you show more than one variable on the y axis, any variable label defined shows up in the legend.

                        More tricks that may be what you want:

                        2. If what is shown in the legend is not what you want, you can use the legend() option directly.

                        3. If what is shown on the y axis is not what you want, you can use the ytitle() option directly.

                        Please do fix that "Thomas Stata" as you promised.

                        Comment


                        • #13
                          Hi Nick,

                          is it possible to Change the Name or do I have to re-register?

                          Code:
                          forval j = 1/3 {
                          egen mean_y`j' = mean(y`j'), by(x, z)
                          }
                          lab var mean_`v' "mean `v'"
                          sort x
                          line mean_y1 x if z == 0 || line mean_y2 x if z == 1, c(L)]
                          in this vein, the two lines have the same Label as lab var does not distinguish between z == 0 & z == 1
                          moreover I'd like to Name the variables manually:
                          for instance
                          lab var mean_y1 "net income"
                          lab var mean_y2 "gros income"
                          lab var mean_y3 "tax amount"
                          however what should be done automatically is for z == 0 single should be added in the Label and for z == 1 married should be added
                          to my mind this cannot be done with legend() since there is no command which imputes the variables label

                          It would be conceivable to write legend("[Label Name of variable] single")
                          line mean_y1 x if z == 0, c(L) legend("[Label Name of variable] single") || line mean_y2 x if z == 1, c(L) legend("[Label Name of variable] married")]
                          or
                          line mean_y1 x if z == 0 || line mean_y2 x if z == 1, c(L) legend(0 "[Label Name of variable] single" 1 "[Label Name of variable] married")
                          whereas [Label Name of variable] should Change automatically according to the variable used
                          can you may be help me another time?
                          Last edited by Thomas Klein; 06 Mar 2015, 06:22.

                          Comment


                          • #14
                            Changing your username is accomplished by re-registration: just hit the Contact Us link in the blue line at bottom right of the webpage, and make your request to change to fullname (firstname lastname).

                            Comment


                            • #15
                              On using the forum: You really are asked to read the FAQ Advice before posting. Section 6 tells you about how to fix your name before posting. Section 12 advises you on how to ask questions: show code, show (realistic) data, etc.
                              http://www.statalist.org/forums/help is a direct link.

                              If you don't do this, you are wasting your time as well as ours, asking the wrong questions or because people will give up on you and ignore your questions.

                              On using graphics: You are going to have to work harder at using the documentation, not least because you are not telling us directly about the structure of your data. So, it is very hard for us to know exactly what commands will be right for you.

                              But here is an example showing one way of automating what is shown. Whether it is close to what you need I don't know.

                              Code:
                               
                              sysuse auto, clear 
                              
                              egen mean_mpg = mean(mpg), by(rep78 foreign) 
                              
                              separate mean_mpg, by(foreign) veryshortlabel 
                              
                              twoway connected mean_mpg? rep78, c(L L) sort ytitle(Mean mpg) xla(1/5)



                              Comment

                              Working...
                              X