Announcement

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

  • Matrix operations

    Hi all,

    I am trying to perform a simple simulation on STATA. In particular I am constructing a vector called beta which is 1x4 and a matrix, X which is 4x11000. I would like to perform the following operation: beta*X but it is actually not allowed.
    The code is the following:

    Code:
    clear all
    
    set obs 11000
    
    set seed 45684123
    
    *CASO CON DELTA_T_min:
    gen alpha = 0.5
    gen epsilon = (0.2)^(-0.5) 
    
    gen x_1 = rnormal(0.4,2)
    gen x_2 = rnormal(1,4)
    gen x_3 = rnormal(0.7,1)
    gen x_4 = rnormal(0.9,3)
    
    set matsize 11000
    
    mkmat x_1 x_2 x_3 x_4, matrix(X) //training set matrix
    
    matrix beta = (0.8\0.8\0.8\0.8)
    matrix X_transpose = X'
    
    gen y_tilde = beta*X_transpose+epsilon
    The error displayed states:
    Code:
    matrix operators that return matrices not allowed in this context
    Thank you in advance

  • #2
    -generate- does not work with matrices.

    Comment


    • #3
      Also there is a comformability error in the expression

      Code:
        
       gen y_tilde = beta*X_transpose+epsilon
      beta(4X1) cannot be mutliplied by X_transpose(4X11000).

      Comment


      • #4
        #2 is incorrect as a blanket statement.

        Code:
        . clear
        
        . mat foo = [1,2,3,4,5]
        
        . set obs `=colsof(foo)'
        number of observations (_N) was 0, now 5
        
        . gen foo = foo[1, _n]
        
        . l
             +-----+
             | foo |
             |-----|
          1. |   1 |
          2. |   2 |
          3. |   3 |
          4. |   4 |
          5. |   5 |
             +-----+
        I often use this method, especially in other places where people post data using R code.

        However, for this specific problem matrices seem quite unnecessary. I don't understand the goal (for example, alpha is never used), but store constants in scalars or locals.


        Code:
        clear all  
        set obs 11000  
        set seed 45684123  
        scalar alpha = 0.5
        scalar epsilon = 1 / sqrt(0.2)
        gen x_1 = rnormal(0.4,2)
        gen x_2 = rnormal(1,4)
        gen x_3 = rnormal(0.7,1)
        gen x_4 = rnormal(0.9,3)  
        gen y_tilde = 0.8 * (x_1 + x_2 + x_3 + x_4) + epsilon
        Naturally if the coefficients differ, the short-cut above won't work.

        Comment


        • #5
          I see...so it is not possible to make it as a matrix operation. I mean, I need to split the different variables and perform the multiplication. What if beta varies then? (e.g. I have beta = [0.1, 0.9, 0.3, 0.5].
          Prof Cox, the names are given for future reference (this is just the first part of the code. The second is unnecessary)

          Thanks again,

          Federico

          Comment


          • #6
            Nick Cox , thank you for teaching us something new (like on any other day ) .

            However you are cheating a bit I think. The example you gave is not a matrix expression, it is more like you have a vector and you index the vector, then you go through the index and you assign the elements of the vector indexed by the index to a variable. As much as I am surprised and fascinated that this works and Stata takes your syntax, this is not a matrix expression to me.

            In the current context this is a matrix expression (correcting the comformability issue):

            Code:
            . matrix beta_transpose = beta'
            
            . mat dir
                beta_transpose[1,4]
                X_transpose[4,11000]
                     beta[4,1]
                        X[11000,4]
            
            . set obs 11000
            obs was 11000, now 11000
            
            . gen x = beta_transpose*X_transpose
            matrix operators that return matrices not allowed in this context
            r(509);
            And Stata does not take it, just as Stata has not taken any other matrix expression I have ever tried to apply in -generate- context.

            Last edited by Joro Kolev; 07 Jan 2019, 04:16.

            Comment


            • #7
              Joro: We don't disagree. #2 remains wrong. That's my point. It's not cheating to give counter-examples.

              What remains true is that * to mean matrix multiplication is not allowed in expressions fed to generate, which is central to the original question. .

              Comment


              • #8
                If you desperately wanted to stick with matrices and Stata, you could

                Code:
                [...]
                matrix Result = beta_transpose*X_transpose
                gen x = Result[1, _n]
                When working with matrices, I strongly prefer Mata over Stata.

                Best
                Daniel

                Comment

                Working...
                X