Announcement

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

  • Marginsplot: Interaction effect with logarithmic variable

    Hi,
    I am calculating a linear regression model with an interaction effect between the migration background of the respondents and his or her income, which I log-transformed and centered beforehand.
    Code:
    regress attitude b4.migback##c.ln_income_c sex age hhsize, cf(%9.2f) vce(cluster hid)
    Now I want to generate a margins plot to show the interaction effect. Usually, I would do something like this:
    Code:
    margins, at(ln_income_c=(-2(0.5)1) migback=(1, 4))
    marginsplot, recast(line) recastci(rarea)
    This works, but the X-axis represents the income variable that was log-transformed and centered. This makes it pretty hard to interpret. Is there a way to generate the marginsplot with the original variable (“income”)?
    If I introduce it in the command, I get an error message that the untransformed variable was not found in the list of covariates.
    Code:
    margins, at(income=(0(500)3500) migback=(1, 4))
    variable income not found in list of covariates
    Does someone know how to do generate a marginsplot with the untransformed variable?
    Thank you!
    Last edited by Anna Cusimano; 25 Nov 2024, 04:58.

  • #2
    The only way to calculate marginal effects with respect to income is to include income in the list of regressors, not centered log income. However, the x-axis labels in the marginsplot for log_income_centered = -2, -1.5, ..., 1 can be converted to raw income values by exponentiating the values and reversing the centering (i.e., adding back the subtracted constant). Then that simply becomes a labeling issue.

    Comment


    • #3
      Maybe something like this.

      Code:
      sysuse auto, clear
      
      g lweight = ln(weight)
      summ lweight
      local weight_seq 7.5(0.1)8.5
      
      reg mpg c.lweight##c.foreign turn trunk
      foreach x of numlist `weight_seq' {
          local inc_orig = round(exp(`x'), 1)  // Assuming mean_income is your original mean
          local inc_labs `"`inc_labs' `x' "`inc_orig'""'
      }
      
      di `"`inc_labs'"'
      
      margins, at(lweight=(`weight_seq') foreign=(1 4))
      
      marginsplot, ///
          recast(line) recastci(rarea) ///
          xlabel(`inc_labs', angle(45)) ///
          xtitle("Auto Weight (lb)") ///
          ytitle("MPG") ///
          title("MPG by Auto Weight") ///
          legend(order(0 "Domestic" 2 "Foreign") pos(6)) ///
          name(weight_margins, replace)

      Comment


      • #4

        margins, at(lweight=(`weight_seq') foreign=(0 1))

        Comment

        Working...
        X