Announcement

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

  • Rename variable names with matrix column names

    Dear Statalist,

    My question is how to rename variable names with matrix column names.

    Currently, I am performing a sequence of regressions iteratively and aim to document the coefficients for each specification. To achieve this, I store the coefficients in a matrix and label the columns of the matrix with details about the corresponding specification. In my actual work, I have more than 400 iteration and have more than 30 independent variables. Here, I will simply to just 1 iteration and 4 independent variables to make my question easier to answer.

    Here is the code I used to store coefficients:

    Code:
    matrix coeff = J(5, 1, .)
    reg y x1 x2 x3 x4 if id == 12 & sample == "0110"
    matrix b = e(b)
    local id_time = string(12,"%02.0f") + "_" + "0110"
    matrix rownames b = `id_time'
    matrix coeff = coeff, b'
    My output matrix looks like :
    Click image for larger version

Name:	Screen Shot 2023-02-17 at 3.38.56 PM.png
Views:	1
Size:	28.4 KB
ID:	1702245


    Than I want to convert the matrix back to a data:

    Code:
    svmat coeff, names(colnames)
    keep colname*
    And the data looks like:
    Click image for larger version

Name:	Screen Shot 2023-02-17 at 3.46.05 PM.png
Views:	1
Size:	42.5 KB
ID:	1702246


    I want to rename 'colnames1' and 'colnames2' in the data with 'c1' and '12_0110' from the matrix. What is the approach I can take in my actual scenario where I need to rename over 400 column names?

    Thank you very much.

  • #2
    I don't think you can have variable names that start with a number, so you may want to change "12_0110" to "x12_0110" or something similar. Then you can use svmat coeff, names(col) instead of names(colnames) to specify the new variable names (see help mkmat).

    Comment

    Working...
    X