Announcement

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

  • Factor variables with collapse

    I am having difficulty with factor variables (i.variable) with a collapsed dataset. The collapse command worked without a problem and I can do other analysis without issues.


    But when I try to do a regression using the i. prefix, it gives the error "factor variables not allowed". If I used the interaction notation ##, I get "interaction variables not allowed"

    For example: reg income i.city


    Is it not possible to use factor notation with a collapsed dataset?

  • #2
    I think that depends on the collapse command and the dataset. Can you give a reproducible example?

    Comment


    • #3
      This gives the same error as I am getting: factor variables not allowed

      Code:
      sysuse auto
      
      collapse mpg, by( rep78)
      
      reg  mpg i.rep78
      [CODE]

      Without the i. prefix, it doesn't give the error.

      Code:
      reg  mpg rep78

      Comment


      • #4
        This works (namely, is legal syntax) for me in Stata 15.1. I suppress detail peculiar to my computer and licence.

        Code:
        . sysuse auto, clear
        (1978 Automobile Data)
        
        . collapse mpg, by(rep78)
        
        . regress mpg i.rep78
        
              Source |       SS           df       MS      Number of obs   =         5
        -------------+----------------------------------   F(4, 0)         =         .
               Model |  44.3347197         4  11.0836799   Prob > F        =         .
            Residual |           0         0           .   R-squared       =    1.0000
        -------------+----------------------------------   Adj R-squared   =         .
               Total |  44.3347197         4  11.0836799   Root MSE        =         0
        
        ------------------------------------------------------------------------------
                 mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
        -------------+----------------------------------------------------------------
               rep78 |
                  2  |     -1.875          .        .       .            .           .
                  3  |  -1.566668          .        .       .            .           .
                  4  |    .666666          .        .       .            .           .
                  5  |   6.363636          .        .       .            .           .
                     |
               _cons |         21          .        .       .            .           .
        ------------------------------------------------------------------------------
        
        . about
        
        Stata/SE 15.1 for Windows (64-bit x86-64)
        Revision 08 May 2018
        Copyright 1985-2017 StataCorp LLC
        In general, a regression like this would not be helpful, as it merely expresses a tautology, and reduces to saying that the mean for each category is the mean for each category.

        But the code is legal and I can't reproduce the error message that you're claiming.

        If you're using a version earlier than 15.1 you're asked to tell us that.

        Comment


        • #5
          Ah my apologies - I was using Stata 12. I just tried it on another computer with Stata 13, and it works.

          Thanks!

          Comment


          • #6
            Thanks for the closure. Factor variable notation was introduced in Stata 11, but I recollect, or imagine, that it wasn't rolled out to all relevant commands until later.

            Comment

            Working...
            X