I am using tabulate Stata 15.1
I have generated a twoway table using the variables irsd_decile and australian_born
tab irsd_decile australian_born
/*
SEIFA IRSD | born in Australia
decile | no yes | Total
-----------+----------------------+----------
1 | 32,574 16,802 | 49,376
2 | 7,163 8,471 | 15,634
3 | 18,445 19,662 | 38,107
4 | 3,163 7,888 | 11,051
5 | 27,803 17,212 | 45,015
6 | 21,791 18,719 | 40,510
7 | 24,494 24,310 | 48,804
8 | 14,295 17,095 | 31,390
9 | 13,830 19,886 | 33,716
10 | 3,701 8,648 | 12,349
-----------+----------------------+----------
Total | 167,259 158,693 | 325,952
*/
*I can manually see, when looking at column frequency, that the top 5 "no" responses correspond to deciles 1,5,7,6
*the following code, using a matrix, will do the sort and list deciles in order of descending frequency of "no" responses
tab irsd_decile australian_born, matrow(R) matcell(C)
matrix A=R,C,C[1...,1]+C[1...,2]
mata: st_matrix("A", sort(st_matrix("A"),-2))
matrix list A
/*
A[10,4]
c1 c2 c3 c4
r1 1 32574 16802 49376
r2 5 27803 17212 45015
r3 7 24494 24310 48804
r4 6 21791 18719 40510
r5 3 18445 19662 38107
r6 8 14295 17095 31390
r7 9 13830 19886 33716
r8 2 7163 8471 15634
r9 10 3701 8648 12349
r10 4 3163 7888 11051
*/
*However, I am actually wanting to sort by row percent of "no" responses, not by column frequency
tabulate irsd_decile australian_born, row nofreq
/*
SEIFA IRSD | born in Australia
decile | no yes | Total
-----------+----------------------+----------
1 | 65.97 34.03 | 100.00
2 | 45.82 54.18 | 100.00
3 | 48.40 51.60 | 100.00
4 | 28.62 71.38 | 100.00
5 | 61.76 38.24 | 100.00
6 | 53.79 46.21 | 100.00
7 | 50.19 49.81 | 100.00
8 | 45.54 54.46 | 100.00
9 | 41.02 58.98 | 100.00
10 | 29.97 70.03 | 100.00
-----------+----------------------+----------
Total | 51.31 48.69 | 100.00
*/
*I can manually see, when looking at row percent, that the top 5 "no" responses correspond to deciles 1,5,6,7
*I have tried using the following code, but get a 'conformability error' message
*While I can continue to the end of the code, the sort is by column frequency
tab irsd_decile australian_born, row nofreq matcol(C) matrow(R)
matrix A=R,C,C[1...,1]+C[1...,2]
mata: st_matrix("A", sort(st_matrix("A"),-2))
matrix list A
Is there a way to sort by row percent without using matrices?
If not, I would appreciate any advice on where I am going wrong with my matrix code.
If anyone is able to help, thanks in advance,
Angela Joe
A small sample of data from my large data set is provided:
I have generated a twoway table using the variables irsd_decile and australian_born
tab irsd_decile australian_born
/*
SEIFA IRSD | born in Australia
decile | no yes | Total
-----------+----------------------+----------
1 | 32,574 16,802 | 49,376
2 | 7,163 8,471 | 15,634
3 | 18,445 19,662 | 38,107
4 | 3,163 7,888 | 11,051
5 | 27,803 17,212 | 45,015
6 | 21,791 18,719 | 40,510
7 | 24,494 24,310 | 48,804
8 | 14,295 17,095 | 31,390
9 | 13,830 19,886 | 33,716
10 | 3,701 8,648 | 12,349
-----------+----------------------+----------
Total | 167,259 158,693 | 325,952
*/
*I can manually see, when looking at column frequency, that the top 5 "no" responses correspond to deciles 1,5,7,6
*the following code, using a matrix, will do the sort and list deciles in order of descending frequency of "no" responses
tab irsd_decile australian_born, matrow(R) matcell(C)
matrix A=R,C,C[1...,1]+C[1...,2]
mata: st_matrix("A", sort(st_matrix("A"),-2))
matrix list A
/*
A[10,4]
c1 c2 c3 c4
r1 1 32574 16802 49376
r2 5 27803 17212 45015
r3 7 24494 24310 48804
r4 6 21791 18719 40510
r5 3 18445 19662 38107
r6 8 14295 17095 31390
r7 9 13830 19886 33716
r8 2 7163 8471 15634
r9 10 3701 8648 12349
r10 4 3163 7888 11051
*/
*However, I am actually wanting to sort by row percent of "no" responses, not by column frequency
tabulate irsd_decile australian_born, row nofreq
/*
SEIFA IRSD | born in Australia
decile | no yes | Total
-----------+----------------------+----------
1 | 65.97 34.03 | 100.00
2 | 45.82 54.18 | 100.00
3 | 48.40 51.60 | 100.00
4 | 28.62 71.38 | 100.00
5 | 61.76 38.24 | 100.00
6 | 53.79 46.21 | 100.00
7 | 50.19 49.81 | 100.00
8 | 45.54 54.46 | 100.00
9 | 41.02 58.98 | 100.00
10 | 29.97 70.03 | 100.00
-----------+----------------------+----------
Total | 51.31 48.69 | 100.00
*/
*I can manually see, when looking at row percent, that the top 5 "no" responses correspond to deciles 1,5,6,7
*I have tried using the following code, but get a 'conformability error' message
*While I can continue to the end of the code, the sort is by column frequency
tab irsd_decile australian_born, row nofreq matcol(C) matrow(R)
matrix A=R,C,C[1...,1]+C[1...,2]
mata: st_matrix("A", sort(st_matrix("A"),-2))
matrix list A
Is there a way to sort by row percent without using matrices?
If not, I would appreciate any advice on where I am going wrong with my matrix code.
If anyone is able to help, thanks in advance,
Angela Joe
A small sample of data from my large data set is provided:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input byte(irsd_decile australian_born) 7 0 7 0 7 0 7 0 7 0 6 0 7 1 10 0 10 0 9 1 7 1 3 1 3 1 6 1 6 1 6 1 9 1 9 1 6 0 10 0 10 0 2 1 7 0 7 0 7 0 6 1 6 1 6 1 7 0 6 1 1 0 1 0 8 1 5 0 8 0 8 0 3 1 2 1 7 1 2 1 7 1 1 1 1 1 6 0 6 0 6 0 6 0 6 0 6 0 1 0 end label values australian_born aussie label def aussie 0 "no", modify label def aussie 1 "yes", modify
Comment