Announcement

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

  • Margins at specific values of one variable, expressed/computed as one other variable.

    Dear All,

    In a panel data set of
    50 groups, id_group

    and 3 continuous variables:
    Y, 10 values by group
    X, 10 values by group
    Xspecific : results of previous estimation procedures. One unique value, in the range of X, by group.

    I regress Y on X . Then I want : margins, at( X = Xspecific )
    Ideally I would like to obtain : margins, generate( Xtarget ) at( X = Xspecific )
    I know that “at( X = Xspecific )” is a faulty syntax, but it clearly expresses the purpose.

    A hopeless variant is:
    margins , generate( Xtarget) at( X = gen( X == Xspecific ))

    I want to apply the procedure to thousands of groups.


    My faulty code is :

    forvalues i = 1/50 {
    regress Y X if id_group == `i'
    margins , generate( Xtarget ) at( X = Xspecific )
    }


    Of course the following code works:

    forvalues i = 1/50 {
    regress Y X if id_group == `i'
    margins , generate( Xtarget ) at( X = a numeric value in the range of X )
    }


    Thank you for your help!

  • #2
    As you provide no example data, I illustrate an approach using the built-in auto.dta.

    Code:
    clear*
    sysuse auto
    
    by rep78, sort: egen xtarget = mean(mpg)
    rename price y
    rename mpg x
    rename rep78 group
    
    capture program drop one_group
    program define one_group
        regress y x
        margins, at(x = `=xtarget[1]')
        gen wanted = r(table)["b", 1]
        exit
    end
    
    runby one_group, by(group)
    -runby- is written by Robert Picard and me, and is available from SSC.

    In the future, when asking for help with code, please show example data, and use the -dataex- command to do so. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.


    Comment


    • #3
      Interesting, Clyde. Can you explain this line:

      Code:
       gen wanted = r(table)["b", 1]

      Normally, I'd pull the [1,1]. Is this the same thing? So you could use "se" if you wanted that?
      Last edited by George Ford; 10 Mar 2023, 07:46.

      Comment


      • #4
        Normally, I'd pull the [1,1]. Is this the same thing? So you could use "se" if you wanted that?
        Yes, at least in recent versions of Stata. I don't remember quite when this was introduced--I know it works in versions 16 and 17, not sure about earlier. You can reference any matrix elements by the names of their rows and columns rather than their numbers when that's convenient.

        Comment

        Working...
        X