Bug in Phi-Coefficient calculation or my mistake?

General help and assistance with jamovi. Bug reports can be made at our issues page: https://github.com/jamovi/jamovi/issues . (If you're unsure feel free to discuss it here)
Post Reply
sbeber
Posts: 2
Joined: Thu Dec 04, 2025 9:59 am

Bug in Phi-Coefficient calculation or my mistake?

Post by sbeber »

I am not sure whether I have made a mistake or whether there is a bug in the calculation of the Phi coefficient.

Scenario: two dichotomous items were recorded (vaccinated: yes/no and ill: yes/no), the data is as follows:

vaccinated: yes, ill: yes: n = 9
vaccinated: yes, ill: no: n = 6
vaccinated: no, ill: yes: n = 12
vaccinated: no, ill: no: n = 2

Manual calculation of the Phi coefficient yields Phi = -.287; the Pearson correlation coefficient also yields this result (in the case of two dichotomous items, this should correspond to the Phi coefficient).

If the Phi coefficient is calculated using frequencies and cross-tables of independent samples, it is shown that Phi = .287.

Could someone please validate that there is a bug here? Should I post it in GitHub? Or have I made a mistake in my thinking?

Thank you very much for your help.
User avatar
MAgojam
Posts: 456
Joined: Thu Jun 08, 2017 2:33 pm
Location: Parma (Italy)

Re: Bug in Phi-Coefficient calculation or my mistake?

Post by MAgojam »

Hey @sbeber,

There is no bug, but rather a difference in the common definitions used for the Phi coefficient.
Summary: The Pearson correlation gives a signed result, while the Chi-squared formula used in contingency tables gives the absolute value (unsigned).
1. Your Manual Result (Phi = -.287): Signed Pearson r
This is the signed Pearson correlation (r) for two dichotomous variables, calculated using the standard formula with (ad - bc) in the numerator. The negative sign is correct based on your category ordering.

2. Software Result (Phi = .287): Unsigned Chi-squared Derivation
Many statistical packages calculate Phi from the Chi-squared statistic, using the formula:
> Phi = Square Root(Chi-squared Value / n)
Since the Chi-squared value is always positive, this yields the absolute value of Phi.

References to support this:
* This approach is used because assocstats() from the vcd package is used (check the Jamovi code here): https://github.com/jamovi/jmv/blob/mast ... s.b.R#L328
* The vcd package documentation confirms that this function returns an absolute value (page 10): https://cran.r-project.org/web/packages/vcd/vcd.pdf
Phi.png
Phi.png (140.22 KiB) Viewed 2646 times
Hope this clarifies your issue!

Cheers,
Maurizio
https://www.jamovi.org/about.html
sbeber
Posts: 2
Joined: Thu Dec 04, 2025 9:59 am

Re: Bug in Phi-Coefficient calculation or my mistake?

Post by sbeber »

Thank you very much!
GmA
Posts: 32
Joined: Wed Mar 19, 2025 3:23 pm

Re: Bug in Phi-Coefficient calculation or my mistake?

Post by GmA »

Just to add a related point that may be useful for anyone following this thread:

Beyond the sign issue, it is worth being aware that the Phi coefficient (and many other chi-squared-based association measures) has an upper bound that depends on the table's marginal distributions. In other words, Phi can only reach 1.0 when the row and column marginals are symmetrical — which is often not the case in real data.

This means that when comparing association strength across tables with different structures, raw Phi values can be misleading. A Phi of 0.4 in one table might represent a stronger "relative" association than a Phi of 0.5 in another table, simply because the first table's marginals made it harder for Phi to reach high values.

For this reason, maximum-corrected variants of association measures — which adjust the observed value by the maximum achievable value given the marginals — can be more appropriate for comparisons. These are not widely available in standard software, but the ChiSquaredTools module I recently released includes several of them (Phi corrected, Cramér's V corrected, C corrected, W-hat corrected, Sakoda's D corrected).

I have just posted an announcement about the module here:
viewtopic.php?p=13413#p13413

This is not to say that raw Phi is "wrong" — it remains a perfectly valid measure of association for a single table (when the marginals are symmetric). But if comparability across tables is important, it is worth considering the corrected alternatives.

Best
GmA
User avatar
MAgojam
Posts: 456
Joined: Thu Jun 08, 2017 2:33 pm
Location: Parma (Italy)

Re: Bug in Phi-Coefficient calculation or my mistake?

Post by MAgojam »

Ciao Gianmarco,
thank you for your authoritative contribution here.

Regarding your new ChiSquaredTools module, I have forked your repository and in my release here: https://github.com/MAgojam/ChiSquaredTools/releases
I have put the compiled .jmo file for Windows and for Linux for a sideload.
If you prefer, you can take them and include them in yours along with the one for macOS.
This screenshot is from Windows on the topic.
Phi-update.png
Phi-update.png (360.97 KiB) Viewed 2215 times
I miei più cordiali saluti.
Maurizio
GmA
Posts: 32
Joined: Wed Mar 19, 2025 3:23 pm

Re: Bug in Phi-Coefficient calculation or my mistake?

Post by GmA »

Hello Maurizio,
thanks for that...
I am very rusty when it comes to GitHub, and I got to get familiar with it again...

So far, I have spent I dont know how many hundreds hours working on the module, because I would like it to be ready by early next year 2026 as I want to have my students using it. As you have possibly seen, it has been built with an eye toward teaching, which takes a lot of time in terms of documentation and references.

Grazie
Gm
Post Reply