diff --git a/docs/_static/fig/statlab_kappa_fig1.svg b/docs/_static/fig/statlab_kappa_fig1.svg index 4de7d5fc..92c3f50c 100644 --- a/docs/_static/fig/statlab_kappa_fig1.svg +++ b/docs/_static/fig/statlab_kappa_fig1.svg @@ -6,7 +6,7 @@ - 2024-07-03T10:34:09.628157 + 2024-07-07T14:34:23.813054 image/svg+xml @@ -32,122 +32,122 @@ z - - - - - - - - - + + + + + + + + + - - + @@ -240,7 +240,7 @@ z - + @@ -384,7 +384,7 @@ z - + @@ -503,17 +503,17 @@ z - - + - + @@ -525,12 +525,12 @@ L -3.5 0 - + - + @@ -543,12 +543,12 @@ L -3.5 0 - + - + @@ -558,7 +558,7 @@ L -3.5 0 - + @@ -611,68 +611,69 @@ L 450 303.64 " style="fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square"/> - - - + + - - - + - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - + + - - - - - - - - + + - - - - - + + + + - - - - - + + + + + - - - + + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + - - + + diff --git a/docs/_static/fig/statlab_kappa_fig2.svg b/docs/_static/fig/statlab_kappa_fig2.svg index 77262c12..78d74c43 100644 --- a/docs/_static/fig/statlab_kappa_fig2.svg +++ b/docs/_static/fig/statlab_kappa_fig2.svg @@ -6,7 +6,7 @@ - 2024-07-03T15:17:35.069338 + 2024-07-07T14:34:23.919856 image/svg+xml @@ -38,116 +38,116 @@ z " style="fill: #ffffff"/> - - - - - - - - - + + + + + + + + + - - + @@ -240,7 +240,7 @@ z - + @@ -384,7 +384,7 @@ z - + @@ -503,12 +503,12 @@ z - - + @@ -525,7 +525,7 @@ L -3.5 0 - + @@ -543,7 +543,7 @@ L -3.5 0 - + @@ -606,63 +606,64 @@ L 275.5425 318.04 " style="fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square"/> - - + + - - - + - + - + + + + - - - - - - - - - - - - - - - - - - - - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + - - - + + + + - + - - + + - + - - + + @@ -721,70 +770,70 @@ z +" clip-path="url(#p53cf2da82c)" style="fill: #ff7f0e; fill-opacity: 0.75; stroke: #000000; stroke-linejoin: miter"/> +" clip-path="url(#p53cf2da82c)" style="fill: #ff7f0e; fill-opacity: 0.75; stroke: #000000; stroke-linejoin: miter"/> +" clip-path="url(#p53cf2da82c)" style="fill: #ff7f0e; fill-opacity: 0.75; stroke: #000000; stroke-linejoin: miter"/> - +" clip-path="url(#p53cf2da82c)" style="fill: #1f77b4; fill-opacity: 0.75; stroke: #000000; stroke-linejoin: miter"/> - +" clip-path="url(#p53cf2da82c)" style="fill: #1f77b4; fill-opacity: 0.75; stroke: #000000; stroke-linejoin: miter"/> - +" clip-path="url(#p53cf2da82c)" style="fill: #1f77b4; fill-opacity: 0.75; stroke: #000000; stroke-linejoin: miter"/> - + - + - + @@ -793,12 +842,35 @@ z - + + + + @@ -806,31 +878,17 @@ z - + - + - - - - - - - - - - - - - - +" clip-path="url(#p63044dce3f)" style="fill: #ff7f0e; fill-opacity: 0.75; stroke: #000000; stroke-linejoin: miter"/> +" clip-path="url(#p63044dce3f)" style="fill: #ff7f0e; fill-opacity: 0.75; stroke: #000000; stroke-linejoin: miter"/> +" clip-path="url(#p63044dce3f)" style="fill: #ff7f0e; fill-opacity: 0.75; stroke: #000000; stroke-linejoin: miter"/> - +" clip-path="url(#p63044dce3f)" style="fill: #1f77b4; fill-opacity: 0.75; stroke: #000000; stroke-linejoin: miter"/> - +" clip-path="url(#p63044dce3f)" style="fill: #1f77b4; fill-opacity: 0.75; stroke: #000000; stroke-linejoin: miter"/> - +" clip-path="url(#p63044dce3f)" style="fill: #1f77b4; fill-opacity: 0.75; stroke: #000000; stroke-linejoin: miter"/> - + - + - + @@ -915,66 +973,39 @@ z - + - + - - - - - - - - + + + + + - - + + - + - - + + - + - - + + - + @@ -992,13 +1023,13 @@ L 349.2 318.04 - + - + - + diff --git a/docs/_static/fig/statlab_kappa_fig3.svg b/docs/_static/fig/statlab_kappa_fig3.svg new file mode 100644 index 00000000..299a0da8 --- /dev/null +++ b/docs/_static/fig/statlab_kappa_fig3.svg @@ -0,0 +1,1036 @@ + + + + + + + + 2024-07-07T14:34:24.045860 + image/svg+xml + + + Matplotlib v3.8.3, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/statlab_kappa.rst b/docs/statlab_kappa.rst index 9c107201..544f13f3 100644 --- a/docs/statlab_kappa.rst +++ b/docs/statlab_kappa.rst @@ -393,13 +393,12 @@ To create a bubble plot using ``mtbp3.statlab``: .. code:: python - from mtbp3.statlab import kappa + from mtbp3.statlab import kappa - r1 = ['Apple'] * 10 + ['Orange'] * 50 + ['Pear'] * 40 - r2 = ['Apple'] * 20 + ['Pear'] * 60 + ['Orange'] * 20 + r1 = random.choices(fruits, k=100) + r2 = random.choices(fruits, k=100) kappa = KappaCalculator([r1,r2], stringna='NA') - print("Cohen's kappa (mtbp3.statlab): "+str(kappa.cohen_kappa)) print("Number of raters per sample: "+str(kappa.n_rater)) print("Number of rating categories: "+str(kappa.n_category)) @@ -411,7 +410,7 @@ Output: .. testoutput:: - Cohen's kappa (mtbp3.statlab): -0.09375 + Cohen's kappa (mtbp3.statlab): -0.07283167119975827 Number of raters per sample: 2.0 Number of rating categories: 3 Number of sample: 100 @@ -433,6 +432,32 @@ To create a bubble plot with individual raters summary using ``mtbp3.statlab``: :align: center :alt: bubble plot with hist +Note that the agreed counts are on the 45 degree line. +To put agreed counts on the -45 degree line: + +.. code:: python + + kappa.create_bubble_plot(hist=True, reverse_y=False) + +.. figure:: /_static/fig/statlab_kappa_fig3.svg + :scale: 70% + :align: center + :alt: bubble plot with hist - reverse + +************* +Lab Exercise +************* + +Assume that there are two raters responsible to rate 2 studies with 100 sample size for each. + +For the first study, the rater-1 marginal rates follow :math:`multi(100, (1/3,1/3,1/3))`. +However, the second rater's cat hacked into the computer and filled first :math:`0= 2, "y must be a list with at least 2 elements" diff --git a/mtbp3/statlab/kappa.py b/mtbp3/statlab/kappa.py index 518ca907..371b7b61 100644 --- a/mtbp3/statlab/kappa.py +++ b/mtbp3/statlab/kappa.py @@ -19,6 +19,7 @@ from sklearn.utils import resample import os import seaborn as sns +import random class KappaCalculator: @@ -245,7 +246,7 @@ def bootstrap_cohen_ci(self, n_iterations=1000, confidence_level=0.95, outfmt='s else: return [self.cohen_kappa, n_iterations, confidence_level, lower_bound, upper_bound] - def create_bubble_plot(self, out_path="", axis_label=[], max_size_ratio=0, hist=False): + def create_bubble_plot(self, out_path="", axis_label=[], max_size_ratio=0, hist=False, reverse_y=True): """ Creates a bubble plot based on the y_count_sq matrix. @@ -289,8 +290,9 @@ def create_bubble_plot(self, out_path="", axis_label=[], max_size_ratio=0, hist= marginal_kws={"hue": df0['agree'], "multiple": "stack", "weights": sizes, "shrink":.5, "legend": False}, joint_kws={"hue": df0['agree'], "size": sizes, "legend": False, "sizes":(min(sizes), max(sizes)*max_size_ratio)} ) - tmp1 = plt.ylim() - plt.ylim(tmp1[1], tmp1[0]) + if reverse_y: + tmp1 = plt.ylim() + plt.ylim(tmp1[1], tmp1[0]) else: sns.scatterplot(data=df0, x="r1", y="r2", size="sizes", sizes=(min(sizes), max(sizes)*max_size_ratio), legend=False) tmp1 = plt.xlim() @@ -322,15 +324,17 @@ def create_bubble_plot(self, out_path="", axis_label=[], max_size_ratio=0, hist= if __name__ == "__main__": - r1 = ['Apple'] * 10 + ['Orange'] * 50 + ['Pear'] * 40 - r2 = ['Apple'] * 20 + ['Pear'] * 60 + ['Orange'] * 20 + fruits = ['Apple', 'Orange', 'Pear'] + r1 = random.choices(fruits, k=100) + r2 = random.choices(fruits, k=100) kappa = KappaCalculator([r1,r2], stringna='NA') - print("Cohen's kappa (mtbp3.statlab): "+str(kappa.cohen_kappa)) print("Number of raters per sample: "+str(kappa.n_rater)) print("Number of rating categories: "+str(kappa.n_category)) print("Number of sample: "+str(kappa.y_count.shape[0])) - - kappa.create_bubble_plot(hist=True) + p1 = '/Users/yh2020/dt2/proj/mtbp3/docs/_static/fig/' + kappa.create_bubble_plot(out_path=p1+'statlab_kappa_fig1.svg') + kappa.create_bubble_plot(hist=True, out_path=p1+'statlab_kappa_fig2.svg') + kappa.create_bubble_plot(hist=True, reverse_y=False, out_path=p1+'statlab_kappa_fig3.svg')