Announcement

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

  • Transpose in Mata

    Dear Listers,

    I have a problem in transposing a variable in mata.


    My code is

    p1 = moptimize_util_xb(M,b,1)
    p2 = moptimize_util_xb(M,b,2)
    rho = moptimize_util_xb(M,b,3)
    y1 = moptimize_util_depvar(M,1)
    y2 = moptimize_util_depvar(M,2)
    y3 = moptimize_util_depvar(M,3)


    K = (y1: - p1):+ rho:*(y2: - p2)

    ...


    when I run maximum likelihood with K':*K above it returns conformability. Anyone knows how to make a transposed variable with K'?

    Many thanks in advance!

    Kind regards,

    Kim
    Last edited by sungwook kim; 03 Oct 2018, 07:37.

  • #2
    The :* operator works on an element by element basis, and doesn't indicate regular matrix multiplication. Consequently, I don't think K' :* K can be conformable unless K is a square matrix. What are the dimensions of your matrix K? (I'm thinking it's N X 1.), and what do you want the result of the multiplication to be? Perhaps you just want regular matrix multiplication of K' * K? Or perhaps you want a matrix of the squared elements of K, i.e., K :* K?

    (Some relevant documentation is available at -help m2_op_colon-, but I think the real issue is what you want the result to be.)

    Comment


    • #3
      Hi Mike,

      Thank you very much for your reply. My data has 100 observations. As a result, I think K will be 100 x 1 matrix. The result should be presented like 100 x 1.

      Kind regards,

      Kim

      Comment


      • #4
        OK, you indicated what you wanted the dimensions of the result to be, but what do you want the *contents* the result to be? I'm guessing you want the i_th element of your result to be (K[i])^2, but since you don't say what you want, all we can do is guess.

        Comment


        • #5
          Dear Mike,

          Thank you very much for your reply. I will give you another but similar example.



          I am estimating spatial autocorrelation parameter- rho. but encountered conformability error.

          I run two regressions and want to find a autocorrelation parameter.

          test = beta*wealth + e1

          lag_test = beta*wealth + e2

          rho = (test - beta*wealth)/lag_test




          mata


          n = st_nobs() //or n = 100
          st_view(y=.,.,"test")
          st_view(y2=.,.,"lag_test")

          st_view(X=.,.,"wealth")
          st_view(X1=.,.,"lag_test wealth")
          beta = luinv(X'X)*X'y
          beta

          *.2888583219


          beta1 = beta[1,1]

          e=y:-beta1:*X

          e1 = e[1,1]
          e2 = e[1,2]

          lag = luinv(X1'X1)*X1'y
          lag

          *
          1
          +---------------+
          1 | .9583429811 |
          2 | .032516881 |
          +---------------+

          *

          lag1 = lag[1,1]
          lag2 = lag[2,1]
          rho = (y :- lag2:*X1):/y2

          z =rho[.,1]
          z


          *
          1
          +---------------+
          1 | 1.168927096 |
          2 | -.032516881 |
          3 | 1.168927096 |
          4 | 1.168927096 |
          5 | 1.174080288 |
          6 | -.032516881 |
          7 | 1.174080288 |
          8 | 1.174080288 |
          9 | -.032516881 |
          10 | 1.174080288 |
          11 | -.032516881 |
          12 | 1.177037502 |
          13 | 1.177037502 |
          14 | 1.177037502 |
          15 | 1.177037502 |
          16 | 1.177037502 |
          17 | 1.178621609 |
          18 | 1.178621609 |
          19 | 1.178621609 |
          20 | -.032516881 |
          21 | 1.178621609 |
          22 | 1.178621609 |
          23 | 1.178621609 |
          24 | -.032516881 |
          25 | 1.172390333 |
          26 | 1.172390333 |
          27 | 1.172390333 |
          28 | -.032516881 |
          29 | 1.172390333 |
          30 | 1.172390333 |
          31 | 1.172390333 |
          32 | 1.180890063 |
          33 | 1.180890063 |
          34 | 1.180890063 |
          35 | -.032516881 |
          36 | 1.180890063 |
          37 | 1.180890063 |
          38 | 1.180890063 |
          39 | 1.180890063 |
          40 | 1.180890063 |
          41 | 1.180890063 |
          42 | 1.180890063 |
          43 | 1.180890063 |
          44 | 1.180890063 |
          45 | 1.166249143 |
          46 | -.032516881 |
          47 | 1.166249143 |
          48 | -.032516881 |
          49 | 1.166249143 |
          50 | 1.166249143 |
          51 | 1.166249143 |
          52 | 1.166249143 |
          53 | 1.166249143 |
          54 | 1.166249143 |
          55 | 1.166249143 |
          56 | -.032516881 |
          57 | 1.166249143 |
          58 | 1.18291365 |
          59 | 1.18291365 |
          60 | 1.183830878 |
          61 | 1.183830878 |
          62 | 1.183830878 |
          63 | 1.183830878 |
          64 | 1.183830878 |
          65 | 1.183830878 |
          66 | 1.183830878 |
          67 | 1.174405008 |
          68 | 1.174405008 |
          69 | 1.174405008 |
          70 | 1.174405008 |
          71 | 1.174405008 |
          72 | 1.174405008 |
          73 | 1.174405008 |
          74 | 1.174405008 |
          75 | 1.174405008 |
          76 | 1.174405008 |
          77 | 1.174405008 |
          78 | 1.174405008 |
          79 | 1.174405008 |
          80 | -.032516881 |
          81 | 1.174405008 |
          82 | 1.174405008 |
          83 | 1.174405008 |
          84 | 1.174405008 |
          85 | 1.174405008 |
          86 | 1.174405008 |
          87 | 1.174405008 |
          88 | 1.174405008 |
          89 | 1.174405008 |
          90 | -.032516881 |
          91 | 1.174405008 |
          92 | 1.174405008 |
          93 | 1.174405008 |
          94 | 1.174405008 |
          95 | 1.182513761 |
          96 | 1.182513761 |
          97 | 1.182513761 |
          98 | 1.182513761 |
          99 | 1.182513761 |
          100 | 1.182513761 |
          +---------------+

          *


          K = (e1- z:*e2)'(e1- z:*e2)
          K
          * <istmt>: 3200 conformability error

          end



          All is fine but K is not conformable. how to deal with z here? if I replace z with number (e.g. 0.5), I can successfully calculate K.


          Many thanks in advance.

          Kind regards,
          Last edited by sungwook kim; 09 Oct 2018, 15:43.

          Comment


          • #6
            I'm going to have to give up on your question, since I'm not able to understand you are asking, as it seems much more complicated and somewhat different than what you originally asked. I'd suggest you find some colleague who can help you rewrite your question.

            Comment


            • #7
              I did not check whether the formulas are econometrically correct. In any case, you have constructed e1 and e2 as scalars, while z is a column vector. Thus, e1- z:*e2 yields a conformability error because you are attempting to subtract a column vector from a scalar. You might want to redefine e1 and e2 as follows:
              Code:
              e1 = e[., 1]
              e2 = e[., 2]
              In any case, you could easily identify the problem yourself by checking the dimensions of the variables in the problematic expression:
              Code:
              rows(e1), cols(e1)
              rows(e2), cols(e2)
              rows(z), cols(z)
              https://twitter.com/Kripfganz

              Comment


              • #8
                Dear Sebastian,

                Thank you very much for your picky & detailed eye on this code!. You are absolutely right. I just realised that too.
                Also thank you so much for your kind advice on the dimension checking using rows function.

                Kind regards,

                Kim

                Comment

                Working...
                X