Announcement

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

  • conditional mean for a given category

    Hi everyone,
    I have a dataset which contains for each observation: firm's specific variables (i.e. balance sheet data), two main categories "default" and "not default" and the rating class of the firm.
    I need to obtain a variable Y with the mean of a variable X (firm specific) by rating class conditioning to the category "default".
    The issue is that variable Y must be "global" in the sense that also the "not default" firms must contain these values but what I get at the moent is just missing values for the "not default" type.
    Any suggestion?
    Thank you!
    Michael

  • #2
    I don't understand what your data set looks like. In the future, please use the -dataex- command to post example Stata data when you want help with code. Writing code for imaginary data sets often proves a waste of time when your data set turns out to not match the assumptions underlying the code.

    I'll assume you have a variable X, another variable firm, and another variable called category. The last variable, category, is a string variable and takes on two values: "default" and "not default." It sounds like you want to calculate for each firm, the mean value of X for that firm among those observations where category == "default."

    Code:
    by firm, sort: egen conditional_mean_x = mean(cond(category == "default", X, .))
    will do that.

    If that's not what you want, or if your data set doesn't meet my assumptions (up to renaming variables), then post back using -dataex- to show an actual example of your data, and also explain more clearly what you want, preferably also showing what the results should look like as applied to your example.

    If you are running Stata version 15.1, -dataex- is part of your official installation. If running an earlier version, run -ssc install dataex- to get the -dataex- command. Then run -help dataex- to read the simple instructions for using it.

    Comment


    • #3
      I will follow your recommendation. It worked perfectly. Thank you very much.

      Comment


      • #4
        See also http://www.stata-journal.com/sjpdf.h...iclenum=dm0055 (especially Sections 9 and 10) for a fairly systematic discussion.

        If not sated, the otherwise hard-to-guess dm0055 is a search term to find related threads on the forum.

        Comment


        • #5
          I have one data set of DHS. I want to get conditional mean of a variable, say, Y, condition upon some variable X=1 and the conditional mean of Y when X=0. And finally want to get the difference between (Y|X=1) - (Y|X=0). But I am not getting any code for that and also I am new to stata. Can anyone please help me in this regard.

          Comment


          • #6
            An example dataset makes it convenient for us to make a code for what you want. Anyway, see this dummy data and the codes for conditional means
            Code:
            clear
            input float(Y X)
            .20470947 1
            .8927587 0
            .5844658 1
            .3697791 0
            .8506309 1
            .3913819 0
            .11966132 1
            .7542434 0
            .6950234 1
            .6866152 0
            end
            
            * Conditional mean of Y when X is 1
            egen meanY_X1 = mean(Y / (X == 1))
            
            * Conditonal mean of Y when X is zero
            egen meanY_X0 = mean(Y / (X == 0))
            
            * The difference of the two conditional means
            gen dif = meanY_X1- meanY_X0
            
            * List the results
            
            . list
            
                 +------------------------------------------------+
                 |        Y   X   meanY_X1   meanY_X0         dif |
                 |------------------------------------------------|
              1. | .2047095   1   .4908982   .6189557   -.1280575 |
              2. | .8927587   0   .4908982   .6189557   -.1280575 |
              3. | .5844658   1   .4908982   .6189557   -.1280575 |
              4. | .3697791   0   .4908982   .6189557   -.1280575 |
              5. | .8506309   1   .4908982   .6189557   -.1280575 |
                 |------------------------------------------------|
              6. | .3913819   0   .4908982   .6189557   -.1280575 |
              7. | .1196613   1   .4908982   .6189557   -.1280575 |
              8. | .7542434   0   .4908982   .6189557   -.1280575 |
              9. | .6950234   1   .4908982   .6189557   -.1280575 |
             10. | .6866152   0   .4908982   .6189557   -.1280575 |
                 +------------------------------------------------+
            Last edited by Attaullah Shah; 03 Mar 2019, 10:33.
            Regards
            --------------------------------------------------
            Attaullah Shah, PhD.
            Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
            FinTechProfessor.com
            https://asdocx.com
            Check out my asdoc program, which sends outputs to MS Word.
            For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

            Comment

            Working...
            X