I have a set of data like below:
A B C D
1 2 3 4
2 3 4 5
They are aggregated data which ABCD constitutes a 2×2 table, and I need to do Fisher exact test on each row, and add a new column for the p-value of the Fisher exact test for that row.
I can use fisher.exact and loop to do it in R, but I can’t find a command in Stata for Fisher exact test.
You are thinking in R terms, and that is often fruitless in Stata (just as it is impossible for a Stata guy to figure out how to do
by ... : regressin R; every package has its own paradigm and its own strengths).There are no objects to add columns to. May be you could say a little bit more as to what you need to do, eventually, with your p-values, so as to find an appropriate solution that your Stata collaborators would sympathize with.
If you really want to add a new column (
generatea new variable, speaking Stata), then you might want to look attabulateand its returned values:I assume that your
A B C Dstand for two binary variables, and the numbers are frequencies in the data. You have toclearthe memory, as Stata thinks about one data set at a time.Then you could
tabulatethe results andgeneratenew variables containing p-values, although that would be a major waste of memory to create variables that contain a constant value:Here,
[fw=variable]is a way to specify frequency weights; I typedreturn listto find out what kind of information Stata stores as the result of the procedure. THAT’S the object-like thing Stata works with. R would return the test results in thefisher.test()$p.valuecomponent, and Stata creates returned values,r(component)for simple commands ande(component)for estimation commands.If you want a loop solution (if you have many sets), you can do this:
That’s the scripting capacity in which Stata, IMHO, is way stronger than R (although it can be argued that this is an extremely dirty programming trick). The local macro
ktakes values from 1 to 2, and this macro is substituted as “k’` everywhere in the curly bracketed piece of code.Alternatively, you can keep the results in Stata short term memory as scalars:
However, the scalars are not associated with the data set, so you cannot save them with the
data.
The immediate commands like
ccisuggested here would also have returned values that you can similarly retrieve.HTH, Stas