Announcement

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

  • How can we get the names of the independent variables in the regression like e(depvar) for later use?

    Dear Stata users:

    Hello!
    I'd like to ask you the way to get the names of the independent variables in the "regress" command for the post-use.
    Regarding the dependent variable, I know there is e(depvar).
    However, are there any useful command for independent variables?

    Thanks in advance:
    Kazuto

  • #2
    There is probably a simpler way, but maybe something like

    Code:
    webuse nhanes2f, clear
    reg weight height i.female
    local depvars regress `=e(depvar)'
    local indvars: list cmdline - depvars
    macro list _indvars
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    StataNow Version: 19.5 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://www3.nd.edu/~rwilliam

    Comment


    • #3
      I think Richard forgot to include a line in his code:

      Code:
      webuse nhanes2f, clear
      reg weight height i.female
      local depvars regress `e(depvar)'
      local cmdline `e(cmdline)'
      local indvars: list cmdline - depvars
      macro list _indvars

      Comment


      • #4
        Clyde is right. The local macro cmdline had been previously defined by me so I didn't get an error when I ran my earlier code.

        Incidentally, all sorts of things will cause even the corrected code to fail, e.g. specifying options as well as variables. But presumably you could come up with an error-prone approach.
        -------------------------------------------
        Richard Williams, Notre Dame Dept of Sociology
        StataNow Version: 19.5 MP (2 processor)

        EMAIL: [email protected]
        WWW: https://www3.nd.edu/~rwilliam

        Comment


        • #5
          Thanks Richard and Clyde!
          Your comments really help me a lot!

          Best Regards

          Kazuto

          Comment


          • #6
            Turns out Maarten Buis wrote a program for this 10 years ago. Type -findit indeplist-.
            -------------------------------------------
            Richard Williams, Notre Dame Dept of Sociology
            StataNow Version: 19.5 MP (2 processor)

            EMAIL: [email protected]
            WWW: https://www3.nd.edu/~rwilliam

            Comment


            • #7
              Thanks a lot!
              This command is useful in the case that we have conditions and group dummies.

              Code:
              xi : reg y x  i.area if year==2015
              indeplist
              local x = r(X)
              Hear area is the variable indicating the group.

              Kazuto

              Comment


              • #8
                Unless you have a horribly antiquated version of Stata, I wouldn't use xi:. Factor variables are usually better. See -help fvvarlist-.
                -------------------------------------------
                Richard Williams, Notre Dame Dept of Sociology
                StataNow Version: 19.5 MP (2 processor)

                EMAIL: [email protected]
                WWW: https://www3.nd.edu/~rwilliam

                Comment


                • #9
                  Thanks for your comment!
                  However, "area" is a string variable,
                  such as "area1, area2,..."
                  And without "xi:", I obtained the following error message:

                  Code:
                  reg y x  i.area if year==2015
                  
                  area:  string variables may not be used as factor variables
                  r(109);
                  How can I cope with dummies created from string variables?

                  Comment


                  • #10
                    Using -c(cmdline)- can get messy if the command is not -regress-; for instance

                    mixed y x weight || id:, vce(robust)


                    means having to parse out things that aren't variable names.

                    The best solution is to store the list in advance, since you are typing them anyway:

                    local ivars x weight
                    mixed y `ivars' || id:



                    but if some reason that's not practical, I typically use the column names from e(b); there will still be some things that aren't variable names, but it's easy to loop over them and -confirm variable- or such.

                    hth,
                    Jeph


                    Comment


                    • #11
                      Dear Jeph

                      Thank you!

                      Best,
                      Kazuto

                      Comment

                      Working...
                      X