Dear All,
I have quick question. I have a matrix X and I want to extract, say, 2 columns based in the column names associated to X. In Stata, this would be achieved using the command
matselrc
Code:
sysuse auto, clear
mkmat price rep78 weight length foreign trunk, matrix(X) /*this sets X as a 74x6 matrix, with varnames as column names*/
matselrc X X_submatrix, c(`=colnumb(matrix(X), "weight")' `=colnumb(matrix(X), "foreign")') /*this extracts X's columns associated with colnames "weight" (column 3) and "foreign" (column 5) */
matrix list X_submatrix
this results in X_submatrix, a 74x2 matrix, containing columns 3 and 5, as intended. However, in my case I have a way much larger dataset and so I want to do the task in Mata rather than setting a large matsize number. However, the Mata documentation I have seen shows that this extraction can be done
only by indicating numbers corresponding to the variables' relative position
Code:
mata:
mata_X =st_matrix("X")[, (3, 5)]
mata_X
end
So, my question is: is there any way to subscript a matrix in Mata based on the column names of the Stata Matrix? I want to stick to specifying variable names instead of their relative position because this latter might change in every application and so updating this time and time again will be error-prone. I was thinking (wrongly) about something about the lines of
mata_X=st_matrix("X")[, ("weight", "foreign")], but this does not work. I also tried a way to get
st_matrixcolstripe involved in this task, but to no avail.
Thank you so much
JM
0 Response to Matrix subscripting based on matrix colnames
Post a Comment