Announcement

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

  • Iteratively adding rows in a matrix

    These are the average treatment effects computed from different causal inference approaches, namely (in order) Synthetic Controls, Synthetic Difference in Differences, SCUL/Synthetic Controls Using LASSO (my estimator), Generalized SCM, Matrix Completion, and Principal Components Regression (also my command).
    Code:
    mat ATTs = (-0.690, -0.770, -0.673, -0.498, -0.533, -0.773)
    I'm interested in making another row in this matrix which represents the difference of each of these approaches from the benchmark, or Synthetic Controls. Practically, this would involve subtracting -0.690 from each coefficient we see here.
    How might I automate this in ado or Mata, beyond just doing -0.690--0.690, -0.770--0.690....

    I ask, because what if I had 12 approaches? What if I had 20? I know there's an easy way to do this, I just don't work with matrices very often.

  • #2
    Something like this?

    Code:
    clear matrix
    mat ATTs = (-0.690, -0.770, -0.673, -0.498, -0.533, -0.773)
    mat holding= ATTs
    forval i= 1/`=colsof(holding)'{
        mat s`i'= J(1,`=colsof(holding)',`=holding[1, `i']')  
        mat ATTs = ATTs\(holding-s`i')
    }
    mat l ATTs
    Res.:

    Code:
    . mat l ATTs
    
    ATTs[7,6]
           c1     c2     c3     c4     c5     c6
    r1   -.69   -.77  -.673  -.498  -.533  -.773
    r1      0   -.08   .017   .192   .157  -.083
    r1    .08      0   .097   .272   .237  -.003
    r1  -.017  -.097      0   .175    .14    -.1
    r1  -.192  -.272  -.175      0  -.035  -.275
    r1  -.157  -.237   -.14   .035      0   -.24
    r1   .083   .003     .1   .275    .24      0

    It is easier to do element-by-element operations in Mata.

    Comment


    • #3
      I interpreted #1 as a simpler version of what Andrew shows in #2. Namely, I assume Jared only wants 1 difference row, whose reference is in the first column. Here's a Mata implementation that only needs to know the position of this reference.

      Code:
      mata:
      X = (-0.690, -0.770, -0.673, -0.498, -0.533, -0.773)
      X = X \ (X :- X[1,1])
      X
      end
      Result

      Code:
      : X
                 1       2       3       4       5       6
          +-------------------------------------------------+
        1 |   -.69    -.77   -.673   -.498   -.533   -.773  |
        2 |      0    -.08    .017    .192    .157   -.083  |
          +-------------------------------------------------+

      Comment


      • #4
        Yes something like this, but actually I only want the first and second rows. How might we do this in Mata? Andrew Musau



        EDIT: Yes, 3 does what I want. Now, just to finish it off, how would I now represent this new matrix as a coefficient plot? This is my result so far.

        Code:
        qui coefplot mat(ATTs), ///
            xlab(-1(.1)-.4) ///
            ti("Causal Impact of Terrorism in Basque Country") ///
            note("ADH uses the nested quadratic solver. ADH lags GDP from 1960 to 1960, population in 1969, as well as lags of the non/retail, energy, construction, industry, and agricultural" "sectors from 1961 to 1969. Synth. Difference-in-Differences uses no covariates and placebo standard errors." "SCUL uses the adaptive LASSO and the optimal lambda with 1 forecasting period. Gen. SCM uses no covariates." "Matrix Completion uses 10 folds of Cross-Validation. Principal Components Regression uses 2 singular values.")
        I have the coefficients, but now all I'd need to do is represent the second row as another graph to the right of the first one.
        Last edited by Jared Greathouse; 10 Apr 2022, 09:12.

        Comment


        • #5
          Originally posted by Jared Greathouse View Post
          Yes something like this, but actually I only want the first and second rows. How might we do this in Mata?
          See my post in #3.

          Comment


          • #6
            Originally posted by Jared Greathouse View Post
            I have the coefficients, but now all I'd need to do is represent the second row as another graph to the right of the first one.
            coefplot is from SSC. By subtracting a constant, you are just shifting the coefficients to the right by the value of the constant, but here is how you do it.

            Code:
            mat ATTs = (-0.690, -0.770, -0.673, -0.498, -0.533, -0.773)
            mat ATTs= ATTs\ (ATTs-J(1, `=colsof(ATTs)', `=ATTs[1,1]'))
            set scheme s1mono
            coefplot mat(ATTs[1]), bylabel("ATEs") || ///
                mat(ATTs[2]), bylabel("Differences") xlab(-1(.2)0.2) ||
                ti("Causal Impact of Terrorism in Basque Country") ///
                note("ADH uses the nested quadratic solver. ADH lags GDP from 1960 to 1960, population in 1969, as well as lags of the non/retail, energy, construction, industry, and agricultural" "sectors from 1961 to 1969. Synth. Difference-in-Differences uses no covariates and placebo standard errors." "SCUL uses the adaptive LASSO and the optimal lambda with 1 forecasting period. Gen. SCM uses no covariates." "Matrix Completion uses 10 folds of Cross-Validation. Principal Components Regression uses 2 singular values.")
            Res.:

            Click image for larger version

Name:	Graph.png
Views:	1
Size:	24.0 KB
ID:	1658926

            Comment


            • #7
              Okay thank you all so much, this worked!

              Comment

              Working...
              X