image_output_1.png
from wordcloud import WordCloud

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

def visualize_word_counts(counts):
    wc = WordCloud(max_font_size=130, min_font_size=25, colormap='tab20', background_color='white', prefer_horizontal=0.95, width=2100, height=700, random_state=0)
    cloud = wc.generate_from_frequencies(counts)
    plt.axis('off')

data = pd.read_csv('../input/suicide-rates-worldwide-20002019/data.csv')

columns = ['Country', 'Year', 'ProbDyingBoth', 'ProbDyingMale', 'ProbDyingFemale', 'SuicideBoth', 'SuicideMale', 'SuicideFemale']
values = data.iloc[1:, :].values
data = pd.DataFrame(values, columns=columns)
for col in columns[2:]:
    data[col] = data[col].map(lambda x: x.split('[')[0]).astype('float')

data.sample(5)

countries = {val: df for val, df in data.groupby('Country')}

def repeated_measures_effect_size(country, col1, col2):
    col1, col2 = (countries[country][col1], countries[country][col2])
    m1, m2 = (np.mean(col1), np.mean(col2))
    s1, s2 = (np.std(col1), np.std(col2))
    r = col1.corr(col2)
    s_z = np.sqrt(s1 ** 2 + s2 ** 2 - 2 * r * s1 * s2)
    s_rm = s_z / np.sqrt(2 * (1 - r))
    return (m1 - m2) / s_rm

plt.xticks(np.arange(-10, 41, 5))

country_gender_dying_corr = {}
country_gender_suicide_corr = {}
for val, df in data.groupby('Country'):
    corr_gender_dying = df['ProbDyingMale'].corr(df['ProbDyingFemale'])
    corr_gender_suicide = df['SuicideMale'].corr(df['SuicideFemale'])
    country_gender_dying_corr[val] = corr_gender_dying
    country_gender_suicide_corr[val] = corr_gender_suicide

plt.figure(figsize=(15, 5))
plt.hist(country_gender_dying_corr.values(), bins=20, alpha=0.4, label='dying')
plt.hist(country_gender_suicide_corr.values(), bins=20, alpha=0.4, label='suicide')
plt.legend()
plt.grid()
plt.show()