Announcement

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

  • Means of two sub-groups of a variable for each year

    Hello,

    I'm a new user of STATA, i'm trying to create two new variables wich calcutes the averages of two subgroups of the variable UNEPFI for each year of the variable YEAR.

    Then I would like to create a graphic that compare over the years the trend of the averages.

    Anyone can help me?

    Thank you.


  • #2
    To get a helpful reponse, you need to show an example of the data you have to work with. Please do this by using the -dataex- command. Run -ssc install dataex- to install it, and then run -help dataex- to read the simple instructions for using it. Please also read the entire FAQ for good advice on how to ask clear, specific questions and other tips for writing questions that are likely to get useful, timely responses.

    Comment


    • #3
      This is the example of the variables I have to work with. I have to create two subgroups of the variable UNEPFI: one group contains all the values of UNEPFI if the variable 'emerging' is 1, and the other group contains the values of UNEPFI if the variable 'emerging' is 0. Then, I have to calculate the averages of the two groups for each year of the variable YEAR listed below.

      I hope that this will be more clear and correct

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input byte UNEPFI
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      1
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0
      end
      The variable emerging below:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input byte emerging
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      1
      end
      The variable YEAR below:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input int YEAR
      1990
      1991
      1992
      1993
      1994
      1995
      1996
      1997
      1998
      1999
      2000
      2001
      2002
      2003
      2004
      2005
      2006
      2007
      2008
      2009
      2010
      2011
      2012
      2013
      2014
      1990
      1991
      1992
      1993
      1994
      1995
      1996
      1997
      1998
      1999
      2000
      2001
      2002
      2003
      2004
      2005
      2006
      2007
      2008
      2009
      2010
      2011
      2012
      2013
      2014
      1990
      1991
      1992
      1993
      1994
      1995
      1996
      1997
      1998
      1999
      2000
      2001
      2002
      2003
      2004
      2005
      2006
      2007
      2008
      2009
      2010
      2011
      2012
      2013
      2014
      1990
      1991
      1992
      1993
      1994
      1995
      1996
      1997
      1998
      1999
      2000
      2001
      2002
      2003
      2004
      2005
      2006
      2007
      2008
      2009
      2010
      2011
      2012
      2013
      2014
      end

      Comment


      • #4
        The data display is not clear, at least to me.

        But you may try something like:

        Code:
        . separate var1, by(var2)
        
        
        
        */ The command above would provide the separation you wish, but I gather you just need something like:
         
        . by year var1, sort: egen mymean = mean(yvar)
        Best regards,

        Marcos

        Comment


        • #5
          Neither your data post nor your question as stated makes much sense to me. First, there is no reason to post each variable separately? More important, you say you need to group according to whether emerging = 0 or 1. But you have only emerging = 1 in your data, so there is only one such group. If you did have both 0 and 1 values for the variable emerging, there would be no need to create any groups: the variable emerging itself already defines the two groups.

          Then you say you want to average the value of the variable year for each group within each year. But within any given year, the average value of year is just the value of year itself.

          The desired endpoint you have in mind is also unclear. When you want to calculate the mean of (something) for two groups, do you want to just calculate them and show the results, or do you want to create a new variable that contains the mean of that (something) corresponding to the value of emerging in each observation.

          So I'm going to disregard what you've posted here and guess that what you want is something like this (though it won't do anything useful on your example data):

          Code:
          // TO CALCULATE MEANS FOR EACH GROUP DEFINED BY emerging
          // AND DISPLAY THEM IN THE RESULTS WINDOW:
          
          tabstat YEAR, by(emerging) statistics(mean)
          
          // TO CREATE A NEW VARIABLE CONTAINING THE MEAN VALUE
          // OF YEAR FOR EACH LEVEL OF emerging IN THE DATA SET
          egen mean_year = mean(YEAR), by(emerging)
          Gabriele, your experience as a new Stata users will be greatly enhanced if you prepare yourself before you jump into doing analyses. I strongly recommend you read the Getting Started [GS] and User's Guide [U] sections of the PDF manual that comes with your Stata installation. This will give you a survey of the commands most commonly used in data management and basic data analysis. It's a large amount of reading, and you won't remember everything. But it will leave you familiar enough with the commands, and with a feel for how things get done in Stata, so that when confronting problems you will generally have a good sense of which commands will be useful. Then, if you don't remember the details of those commands, you can look them up in the help files or in their chapters in the PDF manuals. The time you invest reading [U] and [GS] will be amply repaid by faster progress in your work and less time spent posting questions on the internet and waiting for someone to respond.

          Added: Crossed with Marcos' reply which offers a similar solution to the mean calculations.

          Comment


          • #6
            I solved my problem with the second code structure.

            Thanks for your time and patience.

            Regards.

            Comment

            Working...
            X