Announcement

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

  • Factor variable and interaction in syntax command

    Hi,

    I am trying to add interaction terms into a simple new command. Something simple like this:

    Code:
    cap program drop newreg
    program define newreg
    syntax [varlist],  CONTrol(varlist fv)
    
    xi: reg `varlist' `control'
    
    end
    Sine Stata 11 I think factor variable (fv) accept interactions term. Yet when I do:

    Code:
    newreg y x, cont(mu##nu)
    or

    Code:
    newreg y x, cont(mu#nu)
    with mu and nu numerical and categorical variable, I get the following message:

    interactions not allowed. Surprisingly the command

    Code:
    newreg y x, cont(c.mu##c.nu)
    runs but does consider mu and nu as continuous.

    Similarly,

    Code:
    newreg y x, cont(i.mu*i.nu)
    gives me:

    variable mu*i.nu not found
    (error in option control())

    Any idea on how to specify interaction in a syntax command?

    Thanks





  • #2
    The problem is not with your syntax command but with the use of the xi: prefix
    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . xi: reg price weight i.foreign##c.weight
    interactions not allowed
    r(101);
    
    . reg price length i.foreign##c.weight
    
          Source |       SS           df       MS      Number of obs   =        74
    -------------+----------------------------------   F(4, 69)        =     24.25
           Model |   371093867         4  92773466.8   Prob > F        =    0.0000
        Residual |   263971529        69  3825674.33   R-squared       =    0.5843
    -------------+----------------------------------   Adj R-squared   =    0.5602
           Total |   635065396        73  8699525.97   Root MSE        =    1955.9
    
    ----------------------------------------------------------------------------------
               price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -----------------+----------------------------------------------------------------
              length |  -96.49829   31.80898    -3.03   0.003    -159.9555    -33.0411
                     |
             foreign |
            Foreign  |  -2765.136   2684.043    -1.03   0.307    -8119.655    2589.382
              weight |   5.556982   .9319023     5.96   0.000     3.697887    7.416076
                     |
    foreign#c.weight |
            Foreign  |    2.58161   1.063848     2.43   0.018     .4592919    4.703928
                     |
               _cons |   6565.929   3687.215     1.78   0.079    -789.8649    13921.72
    ----------------------------------------------------------------------------------
    Is there a particular reason you are using the xi: prefix in your program? In Stata 16.1 the output of help xi tells us to use factor variable notation if a command allows it, and the reg command does, as the example above demonstrates.

    Comment


    • #3
      To those who come across this topic in the future, the problem was also discussed on the author's previous topic at

      https://www.statalist.org/forums/for...syntax-command

      where the discussants made essentially the same point about the irrelevance of the xi: prefix in most circumstances. Also, the output of help xi tells us that the "#" notation is now how interactions are specified when using the xi: prefix.
      Last edited by William Lisowski; 05 Nov 2020, 19:02.

      Comment


      • #4
        I understand. Thanks. I use xi: cause I wanted to be able to have the possibility to include fixed effects in my control(varlist fv ) option. It does not seem entirely coherent that xi: and ## would not work together.

        Comment


        • #5
          You do not need xi: to have fixed effects. If nu is a categorical variable, i.nu will give the fixed effects for the categories defined by the values of nu. xi: is old syntax that was made obsolete for most commands by the introduction of factor variables notation, which xi: was not designed to understand.

          Comment


          • #6
            Oh yeah you are right! I am just old school I guess. Thanks for the help. Appreciate it.

            Comment

            Working...
            X