Announcement

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

  • A Problem of Combining Two Columns in Panel Data

    Consider the following example describing a panel data set:
    Code:
    clear all
    version 18
    mata:
    N = 100
    T = 3
    pid  = (1::N) # J(T, 1, 1)
    tid  = J(N, 1, 1::T)
    V = rnormal(N * T, 1, 0, 1)
    
    nvs  = st_addvar("double", ("pid", "tid", "V"))
    st_addobs(length(V) - st_nobs())
    st_store(., nvs, (pid, tid, V))
    end
    xtset pid tid
    Using this data, I have generated a matrix containing the first-and second-difference of V.
    Code:
    mata: st_view(dV = ., ., "S" :+ strofreal((1, 2)) :+ "." :+ "V")
    mata: dV[1::9, ]
                      1              2
        +-------------------------------+
      1 |             .              .  |
      2 |   1.743666979              .  |
      3 |   1.124789274    2.868456253  |
      4 |             .              .  |
      5 |   -1.05772296              .  |
      6 |   .4311303958   -.6265925644  |
      7 |             .              .  |
      8 |  -.2559339912              .  |
      9 |   1.213522593    .9575886021  |
        +-------------------------------+
    Here, I want to combine the two columns without missing values like below:
    Code:
                      1
        +----------------+
      1 |   1.743666979  |
      2 |   1.124789274  |
      3 |   2.868456253  |
      4 |   -1.05772296  |
      5 |   .4311303958  |
      6 |  -.6265925644  |
      7 |  -.2559339912  |
      8 |   1.213522593  |
      9 |   .9575886021  |
        +----------------+
    A solution I found is
    Code:
    mata: Vt = colshape(dV[, ], cols(dV) * 3)
    mata: colshape((Vt[, 3], Vt[, 5], Vt[, 6]), 1)
    However, I think, there could be a more tidy way to combine the column vectors.
    Do you have some good ideas?
Working...
X