Announcement

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

  • Using xi to create design matrix of dummies / alternatives to xi3 and desmat?

    Dear community,

    I'm looking for a way to create a designmatrix for a model with categorical variables using Stata's factor notation as input. Basically I want to do what the no longer available xi3 prefix command did: Create dummy variables for every category of each variable and each interaction.

    There is the ado desmat which basically does exactly what I want only I can't use it because I'm working on code for my own ado and desmat has to be installed seperately before it can be used.

    Here's an example of what I want to do that uses desmat:
    Code:
    clear
    
    input A B C f
    1 1 1 23
    1 1 2 34
    1 2 1 31
    1 2 2 49
    1 3 1 71
    1 3 2 19
    2 1 1 78
    2 1 2 93
    2 2 1 16
    2 2 2 48
    2 3 1 12
    2 3 2 93
    end
    
    desmat A*B*C
    
    list, noobs clean
    
    mkmat _x_1-_x_11, mat(design)
    
    mat list design
    The result looks like this:
    Code:
          _x_1   _x_2   _x_3   _x_4   _x_5   _x_6   _x_7   _x_8   _x_9  _x_10  _x_11
     r1      0      0      0      0      0      0      0      0      0      0      0
     r2      0      0      0      0      0      1      0      0      0      0      0
     r3      0      1      0      0      0      0      0      0      0      0      0
     r4      0      1      0      0      0      1      0      1      0      0      0
     r5      0      0      1      0      0      0      0      0      0      0      0
     r6      0      0      1      0      0      1      0      0      1      0      0
     r7      1      0      0      0      0      0      0      0      0      0      0
     r8      1      0      0      0      0      1      1      0      0      0      0
     r9      1      1      0      1      0      0      0      0      0      0      0
    r10      1      1      0      1      0      1      1      1      0      1      0
    r11      1      0      1      0      1      0      0      0      0      0      0
    r12      1      0      1      0      1      1      1      0      1      0      1
    I also know that there is the prefix xi but it doesn't allow for interactions of more than two variables.
    Does anyone have a suggestion? How can I use xi to create results like that (without inputting the variable names by hand)?

    Best regards,
    Max Hörl

  • #2
    Max: please explain why you want this matrix (what you're going to do with it once you have it). It might be that there are alternative routes to achieving your goals. Also tell us which version of Stata you are using, and clarify why use of factor variable notation doesn't accomplish what you want to do (help fvvarlist).

    Comment


    • #3
      I want to use the matrix as input for a calculation with external software. Therefore there is not much wiggle room concerning the form of the result.

      But I found a solution for my problem using the command fvrevar. For those who might be interested:

      Code:
      clear
      
      input A B C f
      1 1 1 23
      1 1 2 34
      1 2 1 31
      1 2 2 49
      1 3 1 71
      1 3 2 19
      2 1 1 78
      2 1 2 93
      2 2 1 16
      2 2 2 48
      2 3 1 12
      2 3 2 93
      end
      
      local model i.A##i.B##i.C
      
      * create dummy variables
      fvrevar `model', stub(_x_)
      
      * drop reference category and create labels
      fvexpand `model'
      
      local colnames = ""
      local i = 1
      foreach wrd in `r(varlist)' {
          if strpos("`wrd'", "b.") > 0 {
              drop _x_`i'
          }
          else {
              local colnames `colnames' `wrd'
          }
          local i = `i' + 1
      }
      
      mkmat _x_*, mat(X)
      mat colnames X = `colnames'
      
      * show results
      mat list X
      Best regards,
      Max

      Comment


      • #4
        Nice closure. Thanks

        Comment

        Working...
        X