Announcement

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

  • Mean comparison

    sing survey data in the following arrangement:

    Group: Firm_investment
    Q1 - each question has 1,2,3, 4 - four options, option 4 is non-investment (below)

    Group: Firm_noninvestment
    Q1 - option 4

    Q1 is a filter question that decides whether a firm invested or not. However, Q1 is broken down into 4 variables (Q1a, Q1b, Q1c, Q1d)

    Then I would like to compare firms that invested vs non-invested, based on their sales, wages, profit (each a variable in the dataset).

    The methodology I've tried does not work (mostly because it a SAS methodology).
    Create a flag (1,0) for investment (Q1a!=4 & Q1b!=4 & Q1c!=4 & Q1d!=4, else flag=0) vs non-investment (Q1==4)

    FYI: SAS equivalent (ˆ=not equal)
    if Q1aˆ=4 and Q1bˆ=4 and Q1cˆ=4 and Q1dˆ=4, then Flag=1,
    else Flag=0

    Then, calculate mean flag==1 and sales, flag==1 and wages, and flag==1 and profits vs flag==2 and sales, flag==2 and wages, and flag==2 and profits

    I hope this made sense

    Could you guide my methodology and help me with the coding

    Thank you in advance

  • #2
    Could this be a possible solution? Not sure how to fit into mine case

    mean lnR&D if flag==0

    or

    gen return_mean = .
    levelsof stock
    foreach stock in `r(levels)' {sum return if stock=="`stock'"
    replace return_mean = r(mean) if stock=="`stock'"
    }

    Ultimately, I would like to bar graph for comparison

    Comment


    • #3
      You'll increase your chances of a useful answer by following the FAQ on asking questions - provide Stata code in code delimiters, readable Stata output, and sample data using dataex.

      Is this what you're looking for?

      gen flag=(Q1a !=4 & Q1b !=4 & Q1c !=4 & Q1d !=4)
      su sales if flag==1
      su wages if flag==1


      [I have not checked this works/] The material on the rhs of the generate is a set of logical conditions - Stata interprets these and enters 1 if true and 0 otherwise.

      The su statements do means etc. for observations where flag==1

      If you want to put these in to a variable, you could do the means with egen.

      Comment


      • #4
        Originally posted by Phil Bromiley View Post
        You'll increase your chances of a useful answer by following the FAQ on asking questions - provide Stata code in code delimiters, readable Stata output, and sample data using dataex.

        Is this what you're looking for?

        gen flag=(Q1a !=4 & Q1b !=4 & Q1c !=4 & Q1d !=4)
        su sales if flag==1
        su wages if flag==1


        [I have not checked this works/] The material on the rhs of the generate is a set of logical conditions - Stata interprets these and enters 1 if true and 0 otherwise.

        The su statements do means etc. for observations where flag==1

        If you want to put these in to a variable, you could do the means with egen.
        Thank you for highlighting the forum rules, I will review them before future posted.

        Yes, that worked. I'm amazed how simple (and intelligent) STAT is - you don't have to identify the case where flag =0.

        Staying on the same topic...If I want to draw bar graphs to compare flag==1 vs flag==0 over (sales) over (wages), for a visual comparison. I tried the below code and received "invalid name" error msg

        Please note I have fweight

        Code:
         graph bar (mean) sales wages,  over(flag==0) over(flag==1)
        Last edited by Dexter Schooner; 18 Jan 2019, 12:47.

        Comment


        • #5
          The contents of the -over()- option cannot be logical expressions, they must be variables. Then you will get separate bars for each value of the variable specified. So what you want is
          Code:
          graph bar (mean) sales wages, over(flag)

          Comment


          • #6
            Just simplify your code from
            Code:
            graph bar (mean) sales wages,  over(flag==0) over(flag==1)
            to
            Code:
            graph bar (mean) sales wages,  over(flag)
            The "over" option takes only a variable name and no further restrictions because it will plot over all categories within the given variable.

            Comment


            • #7
              Thank you Clyde and Sven!

              I've been using STATA for 1 week now...I want to progress (enjoy coding ahah)

              Could you help me with a MACRO options for this? The reason I ask is because I'm working with survey data, which has response to various questions. Instead of creating flags, I could put a global macro for each question, right?

              I'm studying this code

              Code:
              foreach question of varlist q1-q10 {
              graph hbar, over(`question') title($`question')
              }
              All suggestions welcomed

              Comment


              • #8
                First, kill the $ in the -title()- option. $ is used to dereference global macros, not locals. The macro created with a -foreach- loop is a local. (If your intent is to literally have a $ character in the title of the graph, then you need to precede it with a backslash character so that Stata does not misread it as the start of a global macro reference.

                Next, -graph hbar- needs to be followed by a variable to show just what variable you want the bars to represent the value of (with a bar for each possible value of the question variable.)

                Then, once you get the syntax right, the loop will first do the graph for q1, and then on the next pass through the loop it overwrites that graph with one for q2, etc. So you create 10 graphs, but at the end, only the 10th one is still around. So either you need to add another command inside the loop to save or export the graphs so they live on in your file system, or you need to add the -name()- option to -graph hbar- so that it is stored in memory.

                Comment

                Working...
                X