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
this results in X_submatrix, a 74x2 matrix, containing the 2 columns associated to variables weight and foreign, as intended. However, in reality 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. Hence, the following would do the job
However, 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. So, my question is: is there any way to subscript a matrix in Mata based on the column names (instead of column numbers) of the Stata Matrix? I was thinking that something along the lines of
mata_X=st_matrix("X")[, ("weight", "foreign")]
would do the work but this is wrong. I also tried a way to get st_matrixcolstripe involved in this task, but to no avail.
Thank you so much
JM
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
Code:
mata: mata_X =st_matrix("X")[, (3, 5)] mata_X end
mata_X=st_matrix("X")[, ("weight", "foreign")]
would do the work but this is wrong. I also tried a way to get st_matrixcolstripe involved in this task, but to no avail.
Thank you so much
JM
Comment