Announcement

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

  • under contrast effects for interactions

    I have a pretty basic question about interpreting the results from using the -contrast- command for interaction effects. In the example below, why does the interaction in the results from contrast give the same as in the regression, but the two effects on their own do not match the regression? (Using Stat 15)

    Code:
    sysuse auto, clear
    tab for rep
    drop if rep==1
    drop if rep==2
    reg mpg head trunk weight length i.for##i.rep
    contrast i.for##i.rep, effect nowald

  • #2
    -contrast- will use ANOVA contrasts by default. With factorial ANOVA, the parameterization of the contrasts is such that the main effects will differ from what you get with linear regression using indicator variables, but the interaction term will be the same.

    Comment


    • #3
      A better example might be this:

      Code:
      use http://www.stata-press.com/data/r15/cholesterol, clear
      regress chol i.agegrp##i.race
      contrast i.age##i.race, effects nowald
      Why do the age groups have different coeffs in the regression and the contrast effects?

      Comment


      • #4
        Here's a better example. You'll see why the coefficients differ for the main effects. (Answer: they test different hypotheses. See the -lincom- output.)

        .ÿ
        .ÿversionÿ15.1

        .ÿ
        .ÿclearÿ*

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

        .ÿquietlyÿsetÿobsÿ20

        .ÿ
        .ÿgenerateÿbyteÿAÿ=ÿmod(_n,ÿ2)

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

        .ÿgenerateÿbyteÿoutÿ=ÿAÿ+ÿBÿ+ÿAÿ*ÿBÿ+ÿrnormal()

        .ÿ
        .ÿ*
        .ÿ*ÿBeginÿhere
        .ÿ*
        .ÿregressÿoutÿi.(A##B)

        ÿÿÿÿÿÿSourceÿ|ÿÿÿÿÿÿÿSSÿÿÿÿÿÿÿÿÿÿÿdfÿÿÿÿÿÿÿMSÿÿÿÿÿÿNumberÿofÿobsÿÿÿ=ÿÿÿÿÿÿÿÿ20
        -------------+----------------------------------ÿÿÿF(3,ÿ16)ÿÿÿÿÿÿÿÿ=ÿÿÿÿÿÿ4.49
        ÿÿÿÿÿÿÿModelÿ|ÿÿÿÿÿÿÿÿ6.65ÿÿÿÿÿÿÿÿÿ3ÿÿ2.21666667ÿÿÿProbÿ>ÿFÿÿÿÿÿÿÿÿ=ÿÿÿÿ0.0181
        ÿÿÿÿResidualÿ|ÿÿÿÿÿÿÿÿÿ7.9ÿÿÿÿÿÿÿÿ16ÿÿÿÿÿÿ.49375ÿÿÿR-squaredÿÿÿÿÿÿÿ=ÿÿÿÿ0.4570
        -------------+----------------------------------ÿÿÿAdjÿR-squaredÿÿÿ=ÿÿÿÿ0.3552
        ÿÿÿÿÿÿÿTotalÿ|ÿÿÿÿÿÿÿ14.55ÿÿÿÿÿÿÿÿ19ÿÿ.765789474ÿÿÿRootÿMSEÿÿÿÿÿÿÿÿ=ÿÿÿÿ.70267

        ------------------------------------------------------------------------------
        ÿÿÿÿÿÿÿÿÿoutÿ|ÿÿÿÿÿÿCoef.ÿÿÿStd.ÿErr.ÿÿÿÿÿÿtÿÿÿÿP>|t|ÿÿÿÿÿ[95%ÿConf.ÿInterval]
        -------------+----------------------------------------------------------------
        ÿÿÿÿÿÿÿÿÿ1.Aÿ|ÿÿÿÿÿÿÿÿÿ.4ÿÿÿ.4713677ÿÿÿÿÿ0.85ÿÿÿ0.409ÿÿÿÿ-.5992549ÿÿÿÿ1.399255
        ÿÿÿÿÿÿÿÿÿ1.Bÿ|ÿÿÿÿÿÿÿÿÿ.5ÿÿÿ.4535738ÿÿÿÿÿ1.10ÿÿÿ0.287ÿÿÿÿ-.4615334ÿÿÿÿ1.461533
        ÿÿÿÿÿÿÿÿÿÿÿÿÿ|
        ÿÿÿÿÿÿÿÿÿA#Bÿ|
        ÿÿÿÿÿÿÿÿ1ÿ1ÿÿ|ÿÿÿÿÿÿÿÿÿ.7ÿÿÿ.6350033ÿÿÿÿÿ1.10ÿÿÿ0.287ÿÿÿÿ-.6461468ÿÿÿÿ2.046147
        ÿÿÿÿÿÿÿÿÿÿÿÿÿ|
        ÿÿÿÿÿÿÿ_consÿ|ÿÿ-6.66e-16ÿÿÿ.3513367ÿÿÿÿ-0.00ÿÿÿ1.000ÿÿÿÿ-.7448006ÿÿÿÿ.7448006
        ------------------------------------------------------------------------------

        .ÿcontrastÿAÿBÿA#B

        Contrastsÿofÿmarginalÿlinearÿpredictions

        Marginsÿÿÿÿÿÿ:ÿasbalanced

        ------------------------------------------------
        ÿÿÿÿÿÿÿÿÿÿÿÿÿ|ÿÿÿÿÿÿÿÿÿdfÿÿÿÿÿÿÿÿÿÿÿFÿÿÿÿÿÿÿÿP>F
        -------------+----------------------------------
        ÿÿÿÿÿÿÿÿÿÿÿAÿ|ÿÿÿÿÿÿÿÿÿÿ1ÿÿÿÿÿÿÿÿ5.58ÿÿÿÿÿ0.0312
        ÿÿÿÿÿÿÿÿÿÿÿÿÿ|
        ÿÿÿÿÿÿÿÿÿÿÿBÿ|ÿÿÿÿÿÿÿÿÿÿ1ÿÿÿÿÿÿÿÿ7.17ÿÿÿÿÿ0.0165
        ÿÿÿÿÿÿÿÿÿÿÿÿÿ|
        ÿÿÿÿÿÿÿÿÿA#Bÿ|ÿÿÿÿÿÿÿÿÿÿ1ÿÿÿÿÿÿÿÿ1.22ÿÿÿÿÿ0.2866
        ÿÿÿÿÿÿÿÿÿÿÿÿÿ|
        ÿDenominatorÿ|ÿÿÿÿÿÿÿÿÿ16
        ------------------------------------------------

        .ÿ
        .ÿanovaÿoutÿA##B

        ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿNumberÿofÿobsÿ=ÿÿÿÿÿÿÿÿÿ20ÿÿÿÿR-squaredÿÿÿÿÿ=ÿÿ0.4570
        ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿRootÿMSEÿÿÿÿÿÿ=ÿÿÿÿ.702673ÿÿÿÿAdjÿR-squaredÿ=ÿÿ0.3552

        ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿSourceÿ|ÿPartialÿSSÿÿÿÿÿÿÿÿÿdfÿÿÿÿÿÿÿÿÿMSÿÿÿÿÿÿÿÿFÿÿÿÿProb>F
        ÿÿÿÿÿÿÿÿÿÿÿÿÿÿ-----------+----------------------------------------------------
        ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿModelÿ|ÿÿÿÿÿÿÿ6.65ÿÿÿÿÿÿÿÿÿÿ3ÿÿÿ2.2166667ÿÿÿÿÿÿ4.49ÿÿ0.0181
        ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
        ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿAÿ|ÿÿÿ2.755102ÿÿÿÿÿÿÿÿÿÿ1ÿÿÿÿ2.755102ÿÿÿÿÿÿ5.58ÿÿ0.0312
        ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿBÿ|ÿÿ3.5387755ÿÿÿÿÿÿÿÿÿÿ1ÿÿÿ3.5387755ÿÿÿÿÿÿ7.17ÿÿ0.0165
        ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿA#Bÿ|ÿÿÿÿÿÿÿÿÿ.6ÿÿÿÿÿÿÿÿÿÿ1ÿÿÿÿÿÿÿÿÿÿ.6ÿÿÿÿÿÿ1.22ÿÿ0.2866
        ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
        ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿResidualÿ|ÿÿÿÿÿÿÿÿ7.9ÿÿÿÿÿÿÿÿÿ16ÿÿÿÿÿÿ.49375ÿÿ
        ÿÿÿÿÿÿÿÿÿÿÿÿÿÿ-----------+----------------------------------------------------
        ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿTotalÿ|ÿÿÿÿÿÿ14.55ÿÿÿÿÿÿÿÿÿ19ÿÿÿ.76578947ÿÿ

        .ÿ
        .ÿ*
        .ÿ*ÿExplanation
        .ÿ*
        .ÿquietlyÿregressÿoutÿi.(A##B)

        .ÿ//ÿlinear-regressionÿindicator-variableÿcontrastÿ(matchesÿ-regress-)
        .ÿlincomÿ_b[1.A]

        ÿ(ÿ1)ÿÿ1.Aÿ=ÿ0

        ------------------------------------------------------------------------------
        ÿÿÿÿÿÿÿÿÿoutÿ|ÿÿÿÿÿÿCoef.ÿÿÿStd.ÿErr.ÿÿÿÿÿÿtÿÿÿÿP>|t|ÿÿÿÿÿ[95%ÿConf.ÿInterval]
        -------------+----------------------------------------------------------------
        ÿÿÿÿÿÿÿÿÿ(1)ÿ|ÿÿÿÿÿÿÿÿÿ.4ÿÿÿ.4713677ÿÿÿÿÿ0.85ÿÿÿ0.409ÿÿÿÿ-.5992549ÿÿÿÿ1.399255
        ------------------------------------------------------------------------------

        .ÿ
        .ÿ//ÿANOVAÿcontrastÿforÿmainÿeffectsÿ(matchesÿ-constrast-ÿandÿ-anova-)
        .ÿlincomÿ_b[1.A]ÿ+ÿ_b[1.A#1.B]ÿ/ÿ2

        ÿ(ÿ1)ÿÿ1.Aÿ+ÿ.5*1.A#1.Bÿ=ÿ0

        ------------------------------------------------------------------------------
        ÿÿÿÿÿÿÿÿÿoutÿ|ÿÿÿÿÿÿCoef.ÿÿÿStd.ÿErr.ÿÿÿÿÿÿtÿÿÿÿP>|t|ÿÿÿÿÿ[95%ÿConf.ÿInterval]
        -------------+----------------------------------------------------------------
        ÿÿÿÿÿÿÿÿÿ(1)ÿ|ÿÿÿÿÿÿÿÿ.75ÿÿÿ.3175016ÿÿÿÿÿ2.36ÿÿÿ0.031ÿÿÿÿÿ.0769266ÿÿÿÿ1.423073
        ------------------------------------------------------------------------------

        .ÿ
        .ÿexit

        endÿofÿdo-file


        .ÿ

        Comment


        • #5
          Should have been a floating-point variable for the outcome, but the basic message is the same.

          Here's the code below if you want to run it.
          Code:
          version 15.1
          
          clear *
          
          set seed `=strreverse("1528380")'
          quietly set obs 20
          
          generate byte A = mod(_n, 2)
          generate byte B = _n >= _N / 2
          generate double out = A + B + A * B + rnormal()
          
          *
          * Begin here
          *
          regress out i.(A##B)
          contrast A B A#B
          
          anova out A##B
          
          *
          * Explanation
          *
          quietly regress out i.(A##B)
          // linear-regression indicator-variable contrast (matches -regress-)
          lincom _b[1.A]
          
          // ANOVA contrast for main effects (matches -constrast- and -anova-)
          lincom _b[1.A] + _b[1.A#1.B] / 2
          
          exit

          Comment


          • #6
            Thanks, Joseph. I hope you don't mind, but I made a few more tweaks to make it clearer, to me at least & perhaps students, what was going on.
            -DH

            Code:
            clear *
            
            set seed `=strreverse("1528380")'
            quietly set obs 20
            
            generate byte A = mod(_n, 2)
            generate byte B = _n >= _N / 2
            generate double out = (A + B) + (A * B) + abs(rnormal())
            
            *
            * Begin here
            *
            anova out A##B
            
            regress out i.(A##B)
            
            contrast A##B, effects 
            
            *
            * Explanation
            *
            // linear-regression indicator-variable contrast (matches -regress-)
            lincom _b[1.A]
            
            // ANOVA contrast for main effects (matches -constrast- and -anova-)
            lincom _b[1.A] + _b[1.A#1.B] / 2
            
            * Show that Coeff for regression transforms into ANOVA contrast main effects    
            di .8677731 + (1.522519/2)
            
            * I.e., the coeff for A in the regression is equal to the coeff in 
            **     ANOVA minus one-half the effect of the interaction
            di 1.629033-(1.522519/2)

            Comment

            Working...
            X