I want to read factor variables into mata and omit the base level from the resulting matrix. The code below:
gives a column of 0s at the start. I thought of removing the base level from the variable list, but this didn't work:
You can see I still get a column of 0s, which I found startling. The only workaround I could come up with was something along the lines of
which seems very clunky. Is there a better way to do this? Incidentally, I find the parsing of factor variables into mata a bit odd. For instance, in my example:
So 1.x and 2.x are grouped together in a different order than the one requested.
Code:
. clear . set obs 10 number of observations (_N) was 0, now 10 . gen x = mod(_n, 3) . mata st_data(., "i.x") 1 2 3 +-------------+ 1 | 0 1 0 | 2 | 0 0 1 | 3 | 0 0 0 | 4 | 0 1 0 5 | 0 0 1 | 6 | 0 0 0 | 7 | 0 1 0 | 8 | 0 0 1 | 9 | 0 0 0 | 10 | 0 1 0 | +-------------+
Code:
. mata st_data(., "1.x 2.x") 1 2 +---------+ 1 | 0 0 | 2 | 0 1 | 3 | 0 0 | 4 | 0 0 | 5 | 0 1 | 6 | 0 0 | 7 | 0 0 | 8 | 0 1 | 9 | 0 0 | 10 | 0 0 | +---------+
Code:
mata X = st_data(., "i.x") mata select(X, !(colsum(X :== 0) :== rows(X)))
Code:
. gen y = _n . mata st_data(., "1.x y 2.x") 1 2 3 +----------------+ 1 | 0 0 1 | 2 | 0 1 2 | 3 | 0 0 3 | 4 | 0 0 4 | 5 | 0 1 5 | 6 | 0 0 6 | 7 | 0 0 7 | 8 | 0 1 8 | 9 | 0 0 9 | 10 | 0 0 10 | +----------------+
Comment