
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# Load the Excel file
file_path = 'Final FORRT R&R data .xlsx'  # Update this to your file path if needed
data = pd.read_excel(file_path)

# Dropping rows where DOI is NaN to ensure uniqueness and dropping duplicates based on DOI
valid_data = data.dropna(subset=["DOI"]).drop_duplicates(subset="DOI")

# Defining effect size columns
effect_size_columns = ["d (calculated)", "d (reported)", "Hedges' g", "Other Effect Size (Specify)"]

# For each unique paper, check if it has a reported effect size in any of the specified columns
has_effect_size = valid_data[effect_size_columns].notnull().any(axis=1)
papers_with_effect_size = valid_data[has_effect_size]

# Count how many unique papers of each type have reported an effect size
effect_size_counts_by_type = papers_with_effect_size["OP, RP, MA, CP"].value_counts()

# Count total number of unique papers
total_unique_papers = valid_data.shape[0]

# Count how many papers there are for each type
papers_count_by_type = valid_data["OP, RP, MA, CP"].value_counts()

# Calculate the percentage of each paper type that reported an effect size
percentages = {
    "OP": (effect_size_counts_by_type.get("OP", 0) / papers_count_by_type.get("OP", 1)) * 100,
    "RP": (effect_size_counts_by_type.get("RP", 0) / papers_count_by_type.get("RP", 1)) * 100,
    "MA": (effect_size_counts_by_type.get("MA", 0) / papers_count_by_type.get("MA", 1)) * 100,
}

# Plotting percentages
plt.figure(figsize=(8, 5))
plt.bar(list(percentages.keys()), list(percentages.values()), color=['blue', 'orange', 'green'])
plt.xlabel('Paper Type')
plt.ylabel('Percentage (%)')
plt.title('Percentage of Each Paper Type Reporting an Effect Size')
plt.ylim(0, 100)
plt.grid(axis='y')
for i, value in enumerate(percentages.values()):
    plt.text(i, value + 1, f"{value:.2f}%", ha='center')
plt.show()

# Cleaning data for plotting d (reported) vs d (calculated)
df = pd.read_excel(file_path, sheet_name=0)  # Reload the data if needed

# Function to clean and plot data
def clean_and_plot(df, paper_type, title, xlim=None, ylim=None):
    df_filtered = df[df['OP, RP, MA, CP'] == paper_type]
    df_filtered['d (reported)'] = pd.to_numeric(df_filtered['d (reported)'], errors='coerce')
    df_filtered['d (calculated)'] = pd.to_numeric(df_filtered['d (calculated)'], errors='coerce')
    df_clean = df_filtered.dropna(subset=['d (reported)', 'd (calculated)'])
    
    plt.figure(figsize=(10, 6))
    sns.scatterplot(x='d (reported)', y='d (calculated)', data=df_clean, alpha=0.6)
    plt.title(title)
    plt.xlabel('d (reported)')
    plt.ylabel('d (calculated)')
    if xlim:
        plt.xlim(xlim)
    if ylim:
        plt.ylim(ylim)
    plt.grid(True)
    plt.show()

# Plotting for OP, MA, and RP
clean_and_plot(df, 'OP', 'Original Papers: d (reported) vs d (calculated)')
clean_and_plot(df, 'MA', 'Meta-Analyses: d (reported) vs d (calculated)', xlim=(-2, 2), ylim=(-2, 2))
clean_and_plot(df, 'RP', 'Replication Papers: d (reported) vs d (calculated)', xlim=(-2, 2), ylim=(-2, 2))

# Combining and plotting for all types
df_combined = pd.concat([
    df[df['OP, RP, MA, CP'] == 'OP'].assign(Type='OP'), 
    df[df['OP, RP, MA, CP'] == 'MA'].assign(Type='MA'), 
    df[df['OP, RP, MA, CP'] == 'RP'].assign(Type='RP')
])
df_combined['d (reported)'] = pd.to_numeric(df_combined['d (reported)'], errors='coerce')
df_combined['d (calculated)'] = pd.to_numeric(df_combined['d (calculated)'], errors='coerce')
df_combined_clean = df_combined.dropna(subset=['d (reported)', 'd (calculated)'])

plt.figure(figsize=(12, 8))
sns.scatterplot(x='d (reported)', y='d (calculated)', hue='Type', data=df_combined_clean, alpha=0.6, palette={'OP': 'red', 'MA': 'green', 'RP': 'blue'})
plt.title('Comparison: d (reported) vs d (calculated) for OP, MA, and RP')
plt.xlabel('d (reported)')
plt.ylabel('d (calculated)')
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.grid(True)
plt.legend(title='Paper Type')
plt.show()
