Dear Statalist members:
I'm using Stata 12. I'm interested in obtaining the ratio between the observed and expected frequencies in a x by y table obtained by a two-way tabulation. Below you will find a code to generate a 3x3 table similar to my original dataset (my problem is with a 7x7 table, but I believe I can extend a solution to a 3x3 to my original data)
I thought on capturing the matrices of results from my tab command in two different matrices ("o" and "e") and then dividing o/e. I'm using the matcell command to do such task:
However, I have not been able to store my expected frequencies in a similar matrix. Even though the command below displays my expected frequencies on each cell, the matrix it stores contains the observed (and not the expected) frequencies.
I don't take this as being a problem with the command, but as me expecting it to do something it was not set up to (the help tabulate twoway file says the matcell command store frequencies on a matrix, and not the printed results). In any case, I was planing on obtaining both matrices and then dividing them elementwise calling mata and using a command such as:
I believe I might be overly complicating things. I read the tabulate help file but couldn't find a way for it to show the observed/expected ratio, and working with matrices (as I was planing) also brings problems regarding formatting of the output (which I can probably deal with, but it adds another layer of complexity).
Is there something simple that I'm overlooking? Any advices on how to obtain the said o/e ratio?
Thank you in advance for any help/tips.
Best;
I'm using Stata 12. I'm interested in obtaining the ratio between the observed and expected frequencies in a x by y table obtained by a two-way tabulation. Below you will find a code to generate a 3x3 table similar to my original dataset (my problem is with a 7x7 table, but I believe I can extend a solution to a 3x3 to my original data)
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input long cluster_series_np float(first second) 12 3 3 0 3 5 4 3 6 0 5 3 1 5 5 2 5 6 3 6 3 2 6 5 43 6 6 end
Code:
tabulate first second [fweight = cluster_series_np], matcell(o) matrix list o
Code:
tabulate first second [fweight = cluster_series_np], expected nofreq matcell(e) matrix list e
Code:
mata : st_matrix("result", st_matrix("o") :/ st_matrix("e")) mat li result
Is there something simple that I'm overlooking? Any advices on how to obtain the said o/e ratio?
Thank you in advance for any help/tips.
Best;
Comment