Announcement

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

  • Taking the ratio of a Y variable in a linear regression with a dummy X

    I am trying to figure out how to divide the values of the variable when my X=1 versus when my X=0 rather than subtracting, as a linear regression does.
    For example, I have a Y variable that takes on values between -30 and +30. I have a dummy X variable taking on values of 0 and 1. When I run a simple linear regression, I get the following:
    reg Y X
    where the coefficient on X is equal to the value of Y when X=1 MINUS the value of Y when X=0.
    What I need instead is for the coefficient on X to isolate the value of Y when X=1 DIVIDED BY the value of Y when X=0, and for the standard error to be calculated properly. Unfortunately I cannot simply transform the Y variable to be equal to a ratio because this is a between-subjects design (some subjects have X=0, some have X=1).
    My first thought was to transform the Y variable via logs or something else, but the negative values of the variable make logs a suboptimal choice.
    I'm looking for other transformations or alternative regressions that can accomplish this! Thanks in advance for any thoughts. It seems like there should be an obvious solution but it's not coming to me.

  • #2
    You can try a general linear model with log-link, and then exponentiate the coefficient.
    Code:
    glm Y i.X, link(log)
    display "Ratio = " =exp(_b[1.X])
    But it may fail to converge, in which case I don't know what else to suggest.

    To be honest, it's hard for me to imagine a situation where it is meaningful to compare the ratios of things whose range crosses over 0. Maybe if you explain your context, some other approach might become more apparent.
    Last edited by Clyde Schechter; 22 Apr 2021, 20:32.

    Comment


    • #3
      Originally posted by Emm Ra View Post
      and for the standard error to be calculated properly.
      With Clyde's suggestion, you can use the -eform- option of -glm- to get at the standard error. Or you can use -nlcom- afterward. If you have trouble with convergence (a real possibility, given that you have negative values), then you can use -regress- for a noniterative fit, again, followed by -nlcom- to get the ratio version of the coefficient and its standard error. See below.

      .ÿ
      .ÿversionÿ17.0

      .ÿ
      .ÿclearÿ*

      .ÿ
      .ÿsetÿseedÿ`=strreverse("1605135")'

      .ÿ
      .ÿquietlyÿsetÿobsÿ50

      .ÿgenerateÿbyteÿxÿ=ÿ_nÿ>ÿ_Nÿ/ÿ2

      .ÿgenerateÿdoubleÿyÿ=ÿrnormal(25ÿ+ÿ25ÿ*ÿx,ÿ0.5)

      .ÿ
      .ÿversionÿ16.1:ÿtableÿx,ÿcontents(meanÿy)

      ----------------------
      ÿÿÿÿÿÿÿÿxÿ|ÿÿÿÿmean(y)
      ----------+-----------
      ÿÿÿÿÿÿÿÿ0ÿ|ÿÿ25.108971
      ÿÿÿÿÿÿÿÿ1ÿ|ÿÿ49.896089
      ----------------------

      .ÿ
      .ÿ*
      .ÿ*ÿBeginÿhere
      .ÿ*
      .ÿglmÿyÿi.x,ÿfamily(gaussian)ÿlink(log)ÿeformÿnolog

      GeneralizedÿlinearÿmodelsÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿNumberÿofÿobsÿÿÿ=ÿÿÿÿÿÿÿÿÿ50
      Optimizationÿÿÿÿÿ:ÿMLÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿResidualÿdfÿÿÿÿÿ=ÿÿÿÿÿÿÿÿÿ48
      ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿScaleÿparameterÿ=ÿÿÿ.2324261
      Devianceÿÿÿÿÿÿÿÿÿ=ÿÿ11.15645483ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ(1/df)ÿDevianceÿ=ÿÿÿ.2324261
      Pearsonÿÿÿÿÿÿÿÿÿÿ=ÿÿ11.15645483ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ(1/df)ÿPearsonÿÿ=ÿÿÿ.2324261

      Varianceÿfunction:ÿV(u)ÿ=ÿ1ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ[Gaussian]
      Linkÿfunctionÿÿÿÿ:ÿg(u)ÿ=ÿln(u)ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ[Log]

      ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿAICÿÿÿÿÿÿÿÿÿÿÿÿÿ=ÿÿÿ1.417872
      Logÿlikelihoodÿÿÿ=ÿ-33.44680749ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿBICÿÿÿÿÿÿÿÿÿÿÿÿÿ=ÿÿ-176.6206

      ------------------------------------------------------------------------------
      ÿÿÿÿÿÿÿÿÿÿÿÿÿ|ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿOIM
      ÿÿÿÿÿÿÿÿÿÿÿyÿ|ÿÿÿÿÿexp(b)ÿÿÿstd.ÿerr.ÿÿÿÿÿÿzÿÿÿÿP>|z|ÿÿÿÿÿ[95%ÿconf.ÿinterval]
      -------------+----------------------------------------------------------------
      ÿÿÿÿÿÿÿÿÿ1.xÿ|ÿÿÿ1.987182ÿÿÿ.0085427ÿÿÿ159.74ÿÿÿ0.000ÿÿÿÿÿ1.970509ÿÿÿÿ2.003996
      ÿÿÿÿÿÿÿ_consÿ|ÿÿÿ25.10897ÿÿÿ.0964212ÿÿÿ839.36ÿÿÿ0.000ÿÿÿÿÿÿ24.9207ÿÿÿÿ25.29867
      ------------------------------------------------------------------------------

      .ÿnlcomÿRatio:ÿexp(_b[1.x])

      ÿÿÿÿÿÿÿRatio:ÿexp(_b[1.x])

      ------------------------------------------------------------------------------
      ÿÿÿÿÿÿÿÿÿÿÿyÿ|ÿCoefficientÿÿStd.ÿerr.ÿÿÿÿÿÿzÿÿÿÿP>|z|ÿÿÿÿÿ[95%ÿconf.ÿinterval]
      -------------+----------------------------------------------------------------
      ÿÿÿÿÿÿÿRatioÿ|ÿÿÿ1.987182ÿÿÿ.0085427ÿÿÿ232.62ÿÿÿ0.000ÿÿÿÿÿ1.970438ÿÿÿÿ2.003925
      ------------------------------------------------------------------------------

      .ÿ
      .ÿ//ÿIfÿyouÿhaveÿtroubleÿwithÿconvergence,ÿthen
      .ÿregressÿyÿibn.x,ÿnoconstant

      ÿÿÿÿÿÿSourceÿ|ÿÿÿÿÿÿÿSSÿÿÿÿÿÿÿÿÿÿÿdfÿÿÿÿÿÿÿMSÿÿÿÿÿÿNumberÿofÿobsÿÿÿ=ÿÿÿÿÿÿÿÿ50
      -------------+----------------------------------ÿÿÿF(2,ÿ48)ÿÿÿÿÿÿÿÿ>ÿÿ99999.00
      ÿÿÿÿÿÿÿModelÿ|ÿÿ78002.0032ÿÿÿÿÿÿÿÿÿ2ÿÿ39001.0016ÿÿÿProbÿ>ÿFÿÿÿÿÿÿÿÿ=ÿÿÿÿ0.0000
      ÿÿÿÿResidualÿ|ÿÿ11.1564548ÿÿÿÿÿÿÿÿ48ÿÿ.232426142ÿÿÿR-squaredÿÿÿÿÿÿÿ=ÿÿÿÿ0.9999
      -------------+----------------------------------ÿÿÿAdjÿR-squaredÿÿÿ=ÿÿÿÿ0.9999
      ÿÿÿÿÿÿÿTotalÿ|ÿÿ78013.1597ÿÿÿÿÿÿÿÿ50ÿÿ1560.26319ÿÿÿRootÿMSEÿÿÿÿÿÿÿÿ=ÿÿÿÿ.48211

      ------------------------------------------------------------------------------
      ÿÿÿÿÿÿÿÿÿÿÿyÿ|ÿCoefficientÿÿStd.ÿerr.ÿÿÿÿÿÿtÿÿÿÿP>|t|ÿÿÿÿÿ[95%ÿconf.ÿinterval]
      -------------+----------------------------------------------------------------
      ÿÿÿÿÿÿÿÿÿÿÿxÿ|
      ÿÿÿÿÿÿÿÿÿÿ0ÿÿ|ÿÿÿ25.10897ÿÿÿ.0964212ÿÿÿ260.41ÿÿÿ0.000ÿÿÿÿÿÿ24.9151ÿÿÿÿ25.30284
      ÿÿÿÿÿÿÿÿÿÿ1ÿÿ|ÿÿÿ49.89609ÿÿÿ.0964212ÿÿÿ517.48ÿÿÿ0.000ÿÿÿÿÿ49.70222ÿÿÿÿ50.08996
      ------------------------------------------------------------------------------

      .ÿnlcomÿRatio:ÿ_b[1.x]ÿ/ÿ_b[0.x]

      ÿÿÿÿÿÿÿRatio:ÿ_b[1.x]ÿ/ÿ_b[0.x]

      ------------------------------------------------------------------------------
      ÿÿÿÿÿÿÿÿÿÿÿyÿ|ÿCoefficientÿÿStd.ÿerr.ÿÿÿÿÿÿzÿÿÿÿP>|z|ÿÿÿÿÿ[95%ÿconf.ÿinterval]
      -------------+----------------------------------------------------------------
      ÿÿÿÿÿÿÿRatioÿ|ÿÿÿ1.987182ÿÿÿ.0085427ÿÿÿ232.62ÿÿÿ0.000ÿÿÿÿÿ1.970438ÿÿÿÿ2.003925
      ------------------------------------------------------------------------------

      .ÿ
      .ÿexit

      endÿofÿdo-file


      .


      By the way, I wholeheartedly agree with Clyde. Are your outcome variable's values always one sign when the value of the predictor is zero and always the other sign when the predictor is one? Even then, I'm not sure I'd know what to make of the ratio.

      Comment

      Working...
      X