Announcement

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

  • margins calculation

    Hi,

    I try to figure out what Stata (15.0) exactly does when calculating margins for a regression model with factor variables. Below you can find my calculations: model A and B are easy to understand, margins and display command show the same results. But model C has a factor variable and I don't know how to calculate the correct value via the display command. How does Stata handles (adjusts) the factor variable within my margins command?

    ---

    sysuse auto, clear

    * A) 1 continous independent variable
    reg price weight
    margins, at(weight=(2000))
    display _b[_cons] + _b[weight] * 2000

    * B) 2 continous independent variable
    reg price weight length
    margins, at(weight=(2000))
    su length if e(sample)
    display _b[_cons] + _b[weight] * 2000 + _b[length] * `r(mean)'

    * C) 2 independent variables: 1 continous & 1 factor
    generate length2 = recode(length,180,200,233)
    replace length2 = 1 if length2 == 180
    replace length2 = 2 if length2 == 200
    replace length2 = 3 if length2 == 233
    reg price weight i.length2
    margins, at(weight=(2000))
    display _b[_cons] + _b[weight] * 2000 + ???
    Last edited by Nick Bornschein; 21 Sep 2017, 07:32.

  • #2
    This should get you there:

    Code:
    generate length2 = recode(length,180,200,233)
    replace length2 = 1 if length2 == 180
    replace length2 = 2 if length2 == 200
    replace length2 = 3 if length2 == 233
    prop length2
    mat props = r(table)'
    mat prop2 = props[2,1]
    mat prop3 = props[3,1]
    reg price weight i.length2
    margins, at(weight=(2000))
    display _b[_cons] + _b[weight] * 2000 + _b[2.length2]*(prop2[1,1]) + _b[3.length2]*(prop3[1,1])



    Comment


    • #3
      Thank you!!
      Last edited by Nick Bornschein; 21 Sep 2017, 08:38.

      Comment

      Working...
      X