Announcement

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

  • Formula to standardize a variable without "gen" and "egen"

    Hello,

    I am a French student who is starting to work with Stata 16, and I need to find a formulation to answer the question:
    "Create a new variable z standardizing variable x from a command other than gen and egen. The operation must be performed on a single line of code".

    I transform the real variable into z and x for a better understanding, I don't know if that's how you do it, I'm new.

    I specify that I spent time looking for this everywhere, without concrete results.


    Thank you for your help !

  • #2
    I have no idea how this could be done. I think you should bounce this back to your teachers for help. Pas possible, je crois.

    -- unless you're expected to use Mata.
    Last edited by Nick Cox; 08 Nov 2020, 06:59.

    Comment


    • #3
      What is meant by standardize? There are many ways to standardize a variable. Can you give an example (e.g., using gen or egen)?

      Comment


      • #4
        Perhaps you are supposed to find and use community-contributed commands that do this? Or you are supposed to write such a command and use it?

        Comment


        • #5
          All right, I will share with you the question seen in class and its answer.

          We had to create the variable "farm_area_norm" which normalizes the variable "ha_landused_hh".
          Here is the answer seen in class :

          egen Mean_farm= mean(ha_landused_hh) if ha_landused_hh !=.
          egen Sd_farm=sd(ha_landused_hh) if ha_landused_hh !=.
          gen farm_area_norm = (ha_landused_hh - Mean_farm)/Sd_farm if ha_landused_hh !=.
          sum ha_landused_hh Mean_farm Sd_farm
          sum farm_area_norm
          drop Mean_farm Sd_farm

          Now it would be a question of being able to answer this question via another command that would allow this to be encoded in a single line.

          Thank you for taking the time to answer me.

          Comment


          • #6
            Your instructor might be asking you to write a Stata program and use all the above lines in that program, thereby, reducing all these liens to a single line of code.
            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


            • #7
              Originally posted by Attaullah Shah View Post
              Your instructor might be asking you to write a Stata program and use all the above lines in that program, thereby, reducing all these liens to a single line of code.
              That's the only way I can think of doing it in one line. There's already a user-written command for doing this (type "findit zscore" to download it). There are other similar commands.

              The values you want should be the same as the standardised residuals from a null regression model, but I'm not aware of a way of implementing that in one line of code (other than writing a program).

              Comment


              • #8
                Meanwhile #5 is pretty poor code. It's inefficient and indirect to fire up egen to produce new variables holding constants -- only to drop them again immediately once you've extracted results.


                The code is careful about avoiding missing values, which is better style, but the missing values will be ignored any way.


                This does most of the work directly:

                Code:
                su ha_landused_hh 
                gen farm_area_norm = (ha_landused_hh - r(mean))/ r(sd)
                Code:
                
                



                Comment

                Working...
                X